I was trying to do Inference from my saved model i got this error can anyone help me with it.
import torch
import torch.nn as nn
from torchvision.transforms import transforms
import numpy as np
from torch.autograd import Variable
import torch.functional as F
from io import open
import os
from PIL import Image
import pathlib
import glob
import cv2
train_path='D:/Anaconda Environment/Dataset/final/train'
pred_path='D:/Anaconda Environment/Dataset/Infence Images'
root=pathlib.Path(train_path)
classes=sorted([j.name.split('/')[-1] for j in root.iterdir()])
model=torch.load('Inference model/mobilenet_v2_pre(8).pth')
model.eval()
#Transforms
transformer=transforms.Compose([
transforms.Resize((224,224)),
transforms.ToTensor(), #0-255 to 0-1, numpy to tensors
transforms.Normalize([0.5,0.5,0.5], # 0-1 to [-1,1] , formula (x-mean)/std
[0.5,0.5,0.5])
])
#prediction function
def prediction(img_path,transformer):
image=Image.open(img_path)
image_tensor=transformer(image).float()
image_tensor=image_tensor.unsqueeze_(0)
if torch.cuda.is_available():
image_tensor.cuda()
input=Variable(image_tensor)
output=model(input)
index=output.data.numpy().argmax()
pred=classes[index]
return pred
images_path=glob.glob(pred_path+'/*.jpg')
pred_dict={}
for i in images_path:
pred_dict[i[i.rfind('/')+1:]]=prediction(i,transformer)
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor
Can anyone suggest me a solution