PyTorch model prediction fail for single item

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 08:52:31

问题


I use PyTorch and transfer learning to train mobilenet_v2 based classifier. I use a batch of 20 images during training and my test accuracy is ~80%.

I try to use the model with single image for individual prediction and output is a wrong class.

At the same time if I will take a batch from my test dataset and insert my single image in it instead of element 0 it will have a correct prediction. Prediction 0 will be a correct class. So model works for a batch but not for an individual item.

If I just repeat my single image and pass 10 copies of it the result prediction still will be wrong. So I have situation when my prediction accuracy somehow depends on other items in the batch.

My code for individual item test:

from PIL import Image

# load one batch from test set (20)
dataiter = iter(test_loader)
images, labels = dataiter.next()

# load image we want get prediction for
img_path = "dataset/Barber50/Barber50-25r.jpg"
image = Image.open(img_path)
image_cropped = transforms.CenterCrop(img_size)(transforms.Resize(img_size)(image))
image_tensor = transforms.Normalize(mean=mean, std=std)(transforms.ToTensor()(image_cropped))

# insert the image into batch from test set
images[0] = image_tensor

# show image
fig = plt.figure(figsize=(5, 5))
inp = images[0].numpy().transpose((1, 2, 0))
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.imshow(inp)

# mode data to gpu, switch to eval
if train_on_gpu: images = images.cuda()
model.eval()

# get prediction. PROBLEM HERE
output = model(images[:1]) # predicts wrong class if :1 and correct class if :10

# turn prediction into class label
output = F.softmax(output, dim=1)
_, pred = torch.max(output, 1)
print(classes[pred[0]])

来源:https://stackoverflow.com/questions/62708433/pytorch-model-prediction-fail-for-single-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!