What makes a pre-trained model in pytorch misclassify an image
I successfully trained Data Efficient Image Transformer (deit) on cifar-10 dataset with an accuracy of about 95%. However and saved it for later use. I created a separate class to load the model and make inference on just one image. I keep getting different value for prediction every time I run it.
import torch
from models.deit import deit_small_patch16_224

import matplotlib.pyplot as plt
import torch.nn.functional as F
import numpy as np
from PIL import Image
from torchvision.transforms import transforms as transforms
class_names = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

model = deit_small_patch16_224(pretrained=True, use_top_n_heads=8, use_patch_outputs=False)

checkpoint = torch.load(&quot;./checkpoint/deit224.t7&quot;)
model.load_state_dict(checkpoint, strict=False)
model.head = torch.nn.Linear(in_features=model.head.in_features, out_features=10)
model.eval()


img = Image.open(&quot;cats.jpeg&quot;)
img_tensor = torch.tensor(np.array(img))/255.0
img_tensor = img_tensor.unsqueeze(0).permute(0, 3, 1, 2)
# print(img_tensor.shape)
with torch.no_grad():
    output = model(img_tensor)
    predicted_class = np.argmax(output)
    print(predicted_class)


Yes,figured out the error. updated code below
import torch
from models.deit import deit_small_patch16_224
from torch.utils.data import dataset
import torchvision.datasets
import matplotlib.pyplot as plt
import torch.nn.functional as F
import numpy as np
from PIL import Image
from torchvision.transforms import transforms as transforms
class_names = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

model = deit_small_patch16_224(pretrained=True, use_top_n_heads=8, use_patch_outputs=False)

checkpoint = torch.load(&quot;./checkpoint/deit224.t7&quot;)
state_dict = checkpoint[&quot;model&quot;]
new_state_dict = {}
for key in state_dict:
    new_key = '.'.join(key.split('.')[1:])
    new_state_dict[new_key] = state_dict[key]

model.head = torch.nn.Linear(in_features=model.head.in_features, out_features=10)
model.load_state_dict(new_state_dict)
model.eval()


img = Image.open(&quot;cats.jpeg&quot;)
trans = transforms.ToTensor()
# img_tensor = torch.tensor(np.array(img, dtype=np.float64))/255.0
img_tensor = torch.tensor(np.array(img))/255.0
# img_tensor = torch.tensor(np.array(img))

img_tensor = img_tensor.unsqueeze(0).permute(0, 3, 1, 2)
# print(img_tensor.shape)
with torch.no_grad():
    output = model(img_tensor)
    predicted_class = np.argmax(output)
    print(predicted_class)

