Testing on one image not working

lakshyaM: <p>Hi,<br>
So I have been working on fruits classification problem whose dataset is available on kaggle. It contains 120 classes of different fruits type and has image size of 100x100. I am doing transfer learning by <a href="https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html" rel="noopener nofollow ugc"><em>fine-tuning</em></a> the <strong>ResNet-50</strong> model. I split the dataset into train, validation and test sets and after 10 epochs, it has 99.7% validation accuracy and is doing an amazing job of classifying test images as you can see here:<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/6/c6d78caa66538761da01c567b76cf0e09c252cf2.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c6d78caa66538761da01c567b76cf0e09c252cf2" title="Screen Shot 2020-02-24 at 7.47.49 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/6/c6d78caa66538761da01c567b76cf0e09c252cf2_2_690x169.jpeg" alt="Screen Shot 2020-02-24 at 7.47.49 AM" data-base62-sha1="sn2lOcvtS8aOYy3qEFfdhqNhjaO" width="690" height="169" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/6/c6d78caa66538761da01c567b76cf0e09c252cf2_2_690x169.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/6/c6d78caa66538761da01c567b76cf0e09c252cf2_2_1035x253.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/6/c6d78caa66538761da01c567b76cf0e09c252cf2_2_1380x338.jpeg 2x" data-dominant-color="B3A796"><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">Screen Shot 2020-02-24 at 7.47.49 AM</span><span class="informations">2578×634 374 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></p>
<p>But when I try testing it on a few images from google, it is just predicting one class. Even when I try to use a random image from test set, it just outputs the same class. My code is here:</p>
<pre><code class="lang-auto">loader = transforms.Compose([transforms.Resize(224),
                            transforms.ToTensor(),
                            transforms.Normalize((0.5, 0.5, 0.5), 
                            (0.5, 0.5, 0.5))])
# Since the model inputs images of size 100x100
# need to resize them before feeding them to `loader`
h = 100
w = 100
img_path = test_dir + '/Dates/80_100.jpg'
# img_path = 'avocado.jpg'
img = Image.open(img_path)

img = img.resize((h, w))
img = loader(img)
img = torch.unsqueeze(img, 0)
print(img.shape)
model.eval()
result = model(img)
print(result.shape)
 _, preds_tensor = torch.max(result, 1)
preds = np.squeeze(preds_tensor.numpy()) 
print(class_names[preds])
</code></pre>
<p>I even tried the solution discussed <a href="https://discuss.pytorch.org/t/how-to-read-just-one-pic/17434">here</a> but still getting the same result as above. Any help will be appreciated. Thank you in advance.</p>
<p>Lakshya</p>

lakshyaM: <p>Nevermind, I fixed it. It was missing <code>model.eval()</code>. It is correctly recognizing standalone test images and training images. But still making errors on the images downloaded from google. Can this be possible since the images from google belong to different distribution?</p>

albanD: <p>Hi,</p>
<p>Yes, you will need the model.eval() if you have batchnorm-like models.<br>
This is very possible that the image from google are very different?</p>