PyTorch: inconsistent pretrained VGG output
When loading a pretrained VGG network with the torchvision.models module and using it to classify an arbitrary RGB image, the network's output differs noticeably from invocation to invocation. Why does this happen? From my understanding no part of the VGG forward pass should be non-deterministic.

Here's an MCVE:

import torch
from torchvision.models import vgg16

vgg = vgg16(pretrained=True)

img = torch.randn(1, 3, 256, 256)

torch.all(torch.eq(vgg(img), vgg(img))) # result is 0, but why?


vgg16 has a nn.Dropout layer that, during training, randomly drops 50% of its inputs. During test time you should "switch off" this behavior by setting the mode of the net to "eval" mode:

vgg.eval()
torch.all(torch.eq(vgg(img), vgg(img)))



Out[73]: tensor(1, dtype=torch.uint8)



Note that there are other layers with random behavior and different behavior for training and evaluation (e.g., BatchNorm). Therefore, it is important to switch to eval() mode before evaluating a trained model.
