我想在 PyTorch 中使用迁移学习进行图像分类中显示的每个图片输出下显示准确性



我点击这个链接:https://stackabuse.com/image-classification-with-transfer-learning-and-pytorch/#settingupapretrainedmodel

但我是编码技能的新手。 请告诉我如何在图像下显示精度值。

该示例中使用的模型返回形状(批量大小、类(的对数张量。假设您所说的"准确度值"是具有最大概率的类的预测概率,您需要做的是首先通过从模型中获取输出的 SoftMax 来计算概率,该模型给出了批处理中每个图像的预测概率。他们的visualize_model功能如下所示,尽管我还没有对其进行测试。

def visualize_model(model, num_images=6):
was_training = model.training
model.eval()
images_handeled = 0
fig = plt.figure()
with torch.no_grad():
for i, (inputs, labels) in enumerate(dataloaders['val']):
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
probabilities = nn.functional.softmax(outputs, dim=-1) # compute probabilities
_, preds = torch.max(outputs, 1)
for j in range(inputs.size()[0]):
images_handeled += 1
ax = plt.subplot(num_images//2, 2, images_handeled)
ax.axis('off')
ax.set_title('predicted: {}, probability: {}'.format(class_names[preds[j]], probabilities[preds[j]])) # add predicted class probability
imshow(inputs.cpu().data[j])
if images_handeled == num_images:
model.train(mode=was_training)
return
model.train(mode=was_training)

或者你的意思是整体分类准确性?

最新更新