Why does the output changes every forward pass?

I am using a pretrained VGG16 network (the code is given below). Why does each forward pass of the same image produces different outputs? (see below) I thought it is the result of the “transforms”, but the variable “img” remains unchanged between the forward passes. In addition, the weights and biases of the network remain unchanged. What am I getting wrong here?


from torchvision import datasets, models, transforms

transform = transforms.Compose([transforms.RandomResizedCrop(224),
 transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) #does this occur every time we call an image?

trainset = torchvision.datasets.ImageFolder(root="C:/...", transform=transform  )

model = models.vgg16(pretrained=True)
num_features = model.classifier[6].in_features
features = list(model.classifier.children())[:-1]  # Remove last layer
features.extend([nn.Linear(num_features, 2)])  # Add our layer with 2 outputs
model.classifier = nn.Sequential(*features)  # Replace the model classifier
model = model.cuda()

train_loader = DataLoader(trainset, batch_size=1, shuffle=True, drop_last=True)
train_iterator = iter(train_loader)
img, label= next(train_iterator)

img = Variable(img.cuda(), requires_grad=False)
print( model(img) ) #iterating over this line (i.e., forward passing the same image again and again) produces different outputs... 

Try to set your model to model.eval().
Since you have Dropout layers in your model, the output will change.

1 Like

Thanks a lot ptrblck!!!