2

I am using the VGG-16 network available in pytorch out of the box to predict some image index. I found out that for same input file, if i predict multiple time, I get different outcome. This seems counter-intuitive to me. Once the weights are predicted ( since I am using the pretrained model) there should not be any randomness at any step, and hence multiple run with same input file shall return same prediction.

Here is my code:

import torch
import torchvision.models as models
VGG16 = models.vgg16(pretrained=True)
def VGG16_predict(img_path):
  transformer = transforms.Compose([transforms.CenterCrop(224),transforms.ToTensor()])
  data = transformer(Image.open(img_path))
  output = softmax(VGG16(data.unsqueeze(0)), dim=1).argmax().item()
  return output # predicted class index
VGG16_predict(image) 

Here is the image

Neel
  • 360
  • 1
  • 10

1 Answers1

5

Recall that many modules have two states for training vs evaluation: "Some models use modules which have different training and evaluation behavior, such as batch normalization. To switch between these modes, use model.train() or model.eval() as appropriate. See train() or eval() for details." (https://pytorch.org/docs/stable/torchvision/models.html)

In this case, the classifier layers include dropout, which is stochastic during training. Run VGG16.eval() if you want the evaluations to be non-random.

Joseph
  • 116
  • 3
  • surprisingly, model in eval mode is much less sure about its prediction than in train model, 90%+ drop to 80%-. – liang Nov 02 '21 at 14:46