Different outputs even calling model.eval()

Hello, when doing prediction with a saved model the output is different when i run the program again.

Here is my code:

		image_transforms = {
			'predict': transforms.Compose([
				transforms.RandomResizedCrop(size=256, scale=(0.8, 1.0)),
				transforms.CenterCrop(size=224), 
				transforms.ToTensor(),
				transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) 
				]),
		}

		dataset = {'predict' : ImageFolderWithPaths('./det-results', image_transforms['predict'])}
		dataloader = {'predict': DataLoader(dataset['predict'], batch_size = 16, shuffle=False)}

		model.load_state_dict(torch.load(opt.save_file_name))

		with torch.no_grad():
			model.cuda()
			model.eval()

			for inputs, _, paths in dataloader['predict']:
				inputs = inputs.cuda()
				output = model(inputs)
				output = output.cuda()

				_, index = torch.max(output, dim=1)
				print(index)

Here is the output from the first time i run:

tensor([1, 2, 2, 2, 2, 0, 3, 3, 3, 3, 1, 2, 2, 2, 2, 2], device='cuda:0')
tensor([2, 2, 1, 1, 1, 1, 0, 1, 0, 3, 2, 3, 1, 2, 2, 2], device='cuda:0')
tensor([1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 1, 3, **2**, 1, 2, 3], device='cuda:0')
tensor([0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2], device='cuda:0')

and here for second:

tensor([1, 2, 2, 2, 2, 0, 3, 3, 3, 3, 1, 2, 2, 2, 2, 2], device='cuda:0')
tensor([2, 2, 1, 1, 1, 1, 0, 1, 0, 3, 2, 3, 1, 2, 2, 2], device='cuda:0')
tensor([1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 1, 3, **1**, 1, 2, 3], device='cuda:0')
tensor([0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2], device='cuda:0')

I put double ** in the output that is different (third line), each line corresponds a one batch (size=16)

Anyone can guide me and show what iā€™m doing wrong?

Thanks :smiley:

Hi,

You are cropping image randomly so for every image in each batch, you get different image rather than original image. Although it should not affect model too much as we can see only one difference is happening.

Let me know about the output by removing this line.

Bests

1 Like

I let this detail pass by totally. After remove, the outputs are the same.

Thank you!