Randomness in VGG16 prediction
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


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.
