0

What does

if self.transforms:
            data = self.transforms(data)

do? I don't understand the logic behind this line - what is the condition the line is using?

I'm reading an article on creating a custom dataset with pytorch based on the below implementation:

#custom dataset
class MNISTDataset(Dataset):
    def __init__(self, images, labels=None, transforms=None):
        self.X = images
        self.y = labels
        self.transforms = transforms
         
    def __len__(self):
        return (len(self.X))
    
    def __getitem__(self, i):
        data = self.X.iloc[i, :]
        data = np.asarray(data).astype(np.uint8).reshape(28, 28, 1)
        
        if self.transforms:
            data = self.transforms(data)
            
        if self.y is not None:
            return (data, self.y[i])
        else:
            return data
train_data = MNISTDataset(train_images, train_labels, transform)
test_data = MNISTDataset(test_images, test_labels, transform)
# dataloaders
trainloader = DataLoader(train_data, batch_size=128, shuffle=True)
testloader = DataLoader(test_data, batch_size=128, shuffle=True)

thank you! i'm basically trying to understand why it works & how it applies transforms to the data.

1 Answers1

0

The dataset MNISTDataset can optionnaly be initialized with a transform function. If such transform function is given it be saved in self.transforms else it will keep its default values None. When calling a new item with __getitem__, it first checks if the transform is a truthy value, in this case it checks if self.transforms can be coerced to True which is the case for a callable object. Otherwise it means self.transforms hasn't been provided in the first place and no transform function is applied on data.


Here's a general example, out of a torch/torchvision context:

def do(x, callback=None):
    if callback: # will be True if callback is a function/lambda
        return callback(x)
    return x

do(2) # returns 2
do(2, callback=lambda x: 2*x) # returns 4
Ivan
  • 34,531
  • 8
  • 55
  • 100
  • that makes a lot more sense, thank you! could i clarify what you mean by coerced to be True? how does that relate to a callable function? and also, i've seen this a ton but haven't been able to understand what lamda refers to. thanks! – codinggirl123 Dec 26 '20 at 15:40
  • also, would you mind explaining what the not None in ```` if self.y is not None: return (data, self.y[i]) else: return data ```` refers to? – codinggirl123 Dec 26 '20 at 15:51
  • When using an `if` statement, the condition is coerced (converted) to a boolean by calling the object's `__bool__` function. Most objects in Python translate to `True`, only a handful translate to `False` (for example `False`, `None`, `[]` empty list...). Here's more about [truthy and falsy values](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false). [Lambdas](https://docs.python.org/3/reference/expressions.html?highlight=lambda#lambda) are **anonymous functions** that behave like function objects. In my example I used a... – Ivan Dec 26 '20 at 16:40
  • ...lambda to define the expression `f(x)=2*x` to pass it to `do`. Because there's no real point in creating a standalone function outside as `def callback(x): return 2*x` just to pass it as `do(2, callback=callback)`. Lastly, about the `if self.y is not None`. Same thing here, you are checking that `self.y` is not `None`, if it is you only return `data` else you return a *tuple* made up of `data` and `self.y[i]`. – Ivan Dec 26 '20 at 16:40
  • sorry, forgot to reply! thanks for such a detailed response (-: – codinggirl123 Dec 28 '20 at 03:08