pytorch高精度模型预测错误

时间:2018-09-20 10:39:14

标签: prediction pytorch correctness

Pytorch模型的精度超过92%。但是,当我使用PIL加载图像并使用模型对其评分时,总是会给我错误的预测。 我要发布Image_loader的代码以及用于预测图片的代码。

loader = transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
def image_loader(image_name):
    """load image, returns cuda tensor"""
    image = Image.open(image_name)
    image = loader(image).float()
    image = Variable(image, requires_grad=True)
    image = image.unsqueeze(0) 
    return image 


image = image_loader('flowers/test/3/image_06634.jpg')
image = image.cuda()
model.to('cuda')
x = model(image)
top5 = torch.topk(x, 5)
classes = torch.topk(x, 5)
probs = torch.topk(x, 5)
probs = probs[0]
probs = torch.exp(probs)
probs = (Variable(probs).data).cpu().numpy()
classes = classes[1]
classes = np.array(classes)
class_to_idx = model.class_to_idx
cat_to_name = dict((class_to_idx.get(k, k), v) for (k, v) in cat_to_name.items())

我从预测中获得的类别以及概率在测试阶段从未与正确的类别相对应。

0 个答案:

没有答案