Hi there,
I am running a very simple piece of code that loads an image with OpenCV, does some preprocessing and then applies a pre-trained alexnet. The problem is, that whenever I run that program, the results are significantly different. In the worst case, the wrong class is returned.
Here is the code:
import os
import numpy as np
import cv2

import torch
from torchvision.models.alexnet import alexnet
import torch.nn as nn

MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
STD  = np.array([0.229, 0.224, 0.225], dtype=np.float32)

########################################
# Load pytorch model
########################################
torch_model = alexnet(pretrained=True)


########################################
# Load and preprocess image
########################################
img = cv2.imread("PATH_TO_IMAGE.jpg", cv2.IMREAD_COLOR)
img = cv2.resize(img, (227, 227))
#cv2.imshow("image", img)


img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype(np.float32) / 255.0
img = (img - MEAN) / STD


########################################
# Make image pytorch compatible
########################################
torch_img = np.transpose(img, [2, 0, 1])[np.newaxis, :, :, :]
torch_tensor = torch.from_numpy(torch_img)

print(torch_tensor.shape)


########################################
# Predict (pytorch)
########################################
torch_prediction = torch_model(torch_tensor)
torch_prediction = torch_prediction.detach().numpy()
print("Result pytorch:")
print("Top 10:", np.argsort(np.squeeze(torch_prediction))[-10:][::-1])
print(torch_prediction.shape)
print(np.squeeze(torch_prediction)[:20])

I can even put a loop around the prediction and get different results every iteration.
What am I missing?
PS: I know that I can do the preprocessing more easily using the torchvision functions , but I need to load the image in that way for a different purpose.