Did I misunderstand data.DataSet.__getitem__()?

This is very weird. I have loaded CIFAR10 dataset:

import torchvision.datasets as datasets
import torchvision.transforms as transforms
dataloader = datasets.CIFAR10
transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
trainset = dataloader(root='./data', train=True, download=True, transform=transform_train)

Then I find the image returned by trainset.getitem and the one accessed directly are different.

The getitem in class CIFAR10(data.Dataset) says

    def __getitem__(self, index):
        img, target = self.data[index], self.targets[index]

        # doing this so that it is consistent with all other datasets
        # to return a PIL Image
        img = Image.fromarray(img)

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target

But if I implement getitem() directly and run this

by_get_item = trainset[0][0]
directly = trainset.transform(Image.fromarray(trainset.data[0]))
    by_get_item - directly

it prints a non-zero and the value keeps changing. Why does this happen?

Since you are using random transformations, you’ll get randomly transformed samples every time.
Remove RandomHorizontalFlip and RandomCrop and you should get the same outputs.


Thanks a lot. I’m not familiar with torchvision transforms