I load a pretrained Resnet152 from torchvision. I evaluate the model multiple times with the same input image, but each time the output is different. It's very strange. Anyone knows what is the reason? My code is
from torchvision import transforms
import torch
from torchvision import models
from PIL import Image
# load a pretrained model
model = models.resnet152(pretrained=True)
# load a image and preprocess it
preprocessor = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)])
input_image = Image.open('lion2.jpg')
input_tensor = preprocessor(input_image)
input_batch = torch.unsqueeze(input_tensor, 0)
# run multiple times and print output
for k in range(5):
model.train()
out = model(input_batch)
model.eval()
out2 = model(input_batch)
print(out2[0][:10].cpu().detach())
The output are
tensor([ 0.4722, -2.1463, -0.5993, -0.3880, -2.6292, 1.9123, -1.7939, -0.3289,
-0.3189, 0.5306])
tensor([ 0.4407, -2.0370, -0.7397, -0.4447, -2.6059, 1.9052, -1.9715, -0.6495,
-0.5361, 0.2618])
tensor([ 0.3874, -1.9249, -0.8254, -0.5408, -2.5266, 1.8302, -2.1151, -0.8739,
-0.7206, 0.0478])
tensor([ 0.3150, -1.8490, -0.9004, -0.6544, -2.4615, 1.7409, -2.2083, -1.0194,
-0.8352, -0.1017])
tensor([ 0.2310, -1.7754, -0.8858, -0.7081, -2.3238, 1.5943, -2.2625, -1.1185,
-0.9551, -0.2954])
(If I remove either model.train() or model.eval(), the output keep constant. )