【问题标题】:Pytorch: How to find accuracy for Multi Label Classification?Pytorch:如何找到多标签分类的准确性?
【发布时间】:2020-08-14 21:27:45
【问题描述】:

我正在使用 vgg16,其中类数为 3,我可以为一个数据点预测多个标签。

vgg16 = models.vgg16(pretrained=True) vgg16.classifier[6]= nn.Linear(4096, 3)

使用损失函数:nn.BCEWithLogitsLoss()

如果出现单个标签问题,我能够找到准确性,因为

 `images, labels = data
 images, labels = images.to(device), labels.to(device)
 labels = Encode(labels)
 outputs = vgg16(images)
 _, predicted = torch.max(outputs.data, 1)
 total += labels.size(0)
 correct += (predicted == labels).sum().item()
 acc = (100 * correct / total)`

如何找到多标签分类的准确性?

【问题讨论】:

    标签: deep-learning pytorch


    【解决方案1】:

    根据您的问题,vgg16 正在返回原始 logits。所以你可以这样做:

    labels = Encode(labels)  # torch.Size([N, C]) e.g. tensor([[1., 1., 1.]])
    outputs = vgg16(images)  # torch.Size([N, C])
    outputs = torch.sigmoid(outputs)  # torch.Size([N, C]) e.g. tensor([[0., 0.5, 0.]])
    outputs[outputs >= 0.5] = 1
    accuracy = (outputs == labels).sum()/(N*C)*100
    

    【讨论】:

      【解决方案2】:

      如果您正在考虑总校正标签的准确性,那么您还应该将 0 分配给与接受的答案相比小于阈值的输出。

      见以下代码:

      labels = Encode(labels) ## for example, labels = [[1,0,1],[0,1,1]]
      N,C = labels.shape
      
      outputs = vgg16(images)
      outputs = torch.sigmoid(outputs) ## for example, outputs = [[0.75,0.4,0.9],[0.2,0.6,0.8]]
      
      ## for threshold of 0.5 accuracy should be 100% while from accepted answer you will get 66.67% so
      
      outputs[outputs >= 0.5] = 1
      outputs[outputs < 0.5] = 0 ## assign 0 label to those with less than 0.5
      
      accuracy = (outputs == labels).sum() / (N*C) * 100
      

      【讨论】:

        猜你喜欢
        • 2012-02-14
        • 2017-12-03
        • 1970-01-01
        • 2019-11-18
        • 2019-03-22
        • 1970-01-01
        • 2018-11-14
        • 2014-10-21
        • 2013-11-06
        相关资源
        最近更新 更多