Torchvision CIFAR-10 and ImageFolder CIFAR-10 has different behaviors in Torchvision preset networks

Hi Guys,

I encountered some weird behaviors in the torchvision library. Whenever I use the CIFAR-10 dataset and a model (e.g., resnets) both from torchvision, they seem to work fine.

However, if I swap the CIFAR-10 dataset with a jpeg version and loaded it using the torchvision ImageFolder, somehow the network cannot seem to train it properly. Furthermore, the weights after training cannot output the same accuracy found during training.

All of the settings are the same including the transforms in the dataloader.

Here are pictures to illustrate my point.

Torchvision Resnet and CIFAR-10 working normally
Capture1

Torchvision Resnets and CIFAR-10 via ImageFolder


(The last result (8.35…) is actually the training set, which is different from the previous training loss)

I am at a loss. If I use the ImageFolder function with my own network, the training problem does not appear. Is this a problem with ImageFolder and preset torchvision networks or am I missing something?

Thank you in advance!

Hello,

I think the problem here, is that you don’t apply the same preprocessing (mostly normalization) to your images as the one done by torchvision, which is what the network expects.

Note also that your model is almost giving random guesses, if its accuracy score is 10%

Hi,

I do not think the problem is with the normalization. Here is the transform for both set-ups.

transforms.RandomResizedCrop(input_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

I also want to note that the CIFAR-10 using ImageFolder works with a network that is manually created but not by torchvision preset network.

The simple solution is actually shuffling the CIFAR-10 dataset in the dataloader.

Hi,

What do you mean by shuffling CIFAR-10 in the data loader, that shouldn’t have any effect on your test score