【问题标题】:I want to show accuracy under each picture output shown in Image Classification with Transfer Learning in PyTorch我想在 PyTorch 中使用迁移学习的图像分类中显示的每个图片输出下显示准确性
【发布时间】:2020-05-01 01:34:54
【问题描述】:

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

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

【问题讨论】:

    标签: python image classification pytorch data-visualization


    【解决方案1】:

    该示例中使用的模型返回一个形状的对数张量(批量大小、类别)。假设您所说的“准确度值”是具有最大概率的类别的预测概率,您需要做的是首先通过获取模型输出的 SoftMax 来计算您的概率,这给出了每个图像的预测概率在你的批次中。尽管我还没有测试过,但他们的形象化模型函数看起来像下面这样。

    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)
    

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

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2021-01-19
      • 2021-04-08
      • 1970-01-01
      • 2020-10-13
      • 2020-12-18
      • 2022-09-24
      • 1970-01-01
      • 2018-11-30
      相关资源
      最近更新 更多