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

username: jnx
cooked: <p>Hi Guys,</p>
<p>I encountered some weird behaviors in the torchvision library. Whenever I use the CIFAR-10 dataset and a model (e.g., resnets) <strong>both</strong> from torchvision, they seem to work fine.</p>
<p>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.</p>
<p>All of the settings are the same including the transforms in the dataloader.</p>
<p>Here are pictures to illustrate my point.</p>
<p>Torchvision Resnet and CIFAR-10 working normally<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/8/c/8cbe0b7f9e788c9df132710ca70f2b1a8f0de131.png" alt="Capture1" data-base62-sha1="k540WuNCMAEYF80sCIkfDzcf0oV" width="259" height="296"></p>
<p>Torchvision Resnets and CIFAR-10 via ImageFolder<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/9/a952cde6c1a59b68a9ed13cbab26a9e6a91d9fc5.png" data-download-href="https://discuss.pytorch.org/uploads/default/a952cde6c1a59b68a9ed13cbab26a9e6a91d9fc5" title="Capture2"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/9/a952cde6c1a59b68a9ed13cbab26a9e6a91d9fc5.png" alt="Capture2" data-base62-sha1="o9U6c2fNL1qUdcY1GbxW57fAf1r" width="261" height="500" data-dominant-color="2C2B2A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture2</span><span class="informations">309×590 5.24 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
(The last result (8.35…) is actually the training set, which is different from the previous training loss)</p>
<p>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?</p>
<p>Thank you in advance!</p>

username: omarfoq
cooked: <p>Hello,</p>
<p>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.</p>
<p>Note also that your model is almost giving random guesses, if its accuracy score is 10%</p>

username: jnx
cooked: <p>Hi,</p>
<p>I do not think the problem is with the normalization. Here is the transform for both set-ups.</p>
<pre><code class="lang-auto">transforms.RandomResizedCrop(input_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
</code></pre>
<p>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.</p>

username: jnx
cooked: <p>The simple solution is actually shuffling the CIFAR-10 dataset in the dataloader.</p>

username: omarfoq
cooked: <p>Hi,</p>
<p>What do you mean by shuffling CIFAR-10 in the data loader, that shouldn’t have any effect on your test score</p>