Prediction not matching with the same fastai model, loading in pytorch

Hi all,
I have built a resnet18 model (two classes- classification model) in fastai1(both .pkl file and .pth file). Now I load this model using pytorch and use it for predictions (pls see below code). For a single image, when I am trying to get predictions, values out of fastai and pytorch are coming out to be different. See the below code for reference.

Pytorch prediction for one image: [0.9592, 0.0408]
Fastai prediction for same image: [0.0089, 0.9911]

##Loading the model in pytorch
import torch
import torchvision.models as models
import torch.nn as nn
from PIL import Image
import torchvision.transforms.functional as TF
import torchvision.transforms as transforms

class AdaptiveConcatPool2d(nn.Module):
“Layer that concats AdaptiveAvgPool2d and AdaptiveMaxPool2d.”
def init(self):
“Output will be 2*sz or 2 if sz is None”
super().init()
sz = 1
self.ap,self.mp = nn.AdaptiveAvgPool2d(sz), nn.AdaptiveMaxPool2d(sz)
def forward(self, x):
return torch.cat([self.mp(x), self.ap(x)], 1)

loc_pth = torch.load(‘path/model.pth’,map_location=‘cpu’)

mod = models.resnet18()
mod_test = torch.nn.Sequential(*list(mod.children())[:-2])

model1 = nn.Sequential(AdaptiveConcatPool2d(),
nn.Flatten(),
nn.BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.Dropout(p=0.25),
nn.Linear(in_features=1024, out_features=512, bias=True),
nn.ReLU(inplace=True),
nn.BatchNorm1d(512, eps=1e-05,momentum=0.1, affine=True, track_running_stats=True),
nn.Dropout(0.5),
nn.Linear(in_features=512, out_features=2, bias=True))

model2 = nn.Sequential(mod_test, model1)
model2.eval()
model2.load_state_dict(loc_pth, strict=False)

trans = transforms.Compose([transforms.ToPILImage(),
transforms.Resize((150,150)),
transforms.ToTensor()])
image = Image.open(image_path+‘img1.PNG’)
x = TF.to_tensor(image)
x2 = trans(x)
x2 = x2.unsqueeze(0)

m = nn.Softmax(dim=1)
prediction_pytorch = m(model2(x2))

code for prediction in fastai1

from fastai.vision import *
model_path = Path(‘path’)
learn = load_learner(model_path,‘model.pkl’)
img = open_image(path + “img1.PNG”)
prediction_fastai = learn.predict(img)

##MODEL BUILDING CODE
data = ImageDataBunch.from_folder(path,train = ‘Train’,test= ‘Test’, valid = ‘Validation’,ds_tfms=get_transforms(do_flip=True,flip_vert=True), size=150, bs=bs ).normalize(imagenet_stats)

Please suggest me how can i get same predication using this model?

Thanks in advance.

You forgot to normalize your image via imagenet here