samedi 26 décembre 2020

What does does if self.transforms mean?

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.

Aucun commentaire:

Enregistrer un commentaire