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

时间:2020-04-30 13:39:16

标签: deep-learning pytorch

我正在使用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)`

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

2 个答案:

答案 0 :(得分:1)

根据您的问题,vgg16返回原始logit。因此,您可以执行以下操作:

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

答案 1 :(得分:0)

如果您考虑的是总校正标签的准确性,那么与接受的答案相比,您还应将小于阈值的输出分配为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