Testing on one image not working

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 fine-tuning the ResNet-50 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:

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:

loader = transforms.Compose([transforms.Resize(224),
                            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)
result = model(img)
 _, preds_tensor = torch.max(result, 1)
preds = np.squeeze(preds_tensor.numpy()) 

I even tried the solution discussed here but still getting the same result as above. Any help will be appreciated. Thank you in advance.


Nevermind, I fixed it. It was missing model.eval(). 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?

1 Like


Yes, you will need the model.eval() if you have batchnorm-like models.
This is very possible that the image from google are very different?