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

问题描述 投票:0回答:1

我使用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个回答
1
投票

从你的问题来看。vgg16 正在返回原始对数。所以你可以这样做。

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
© www.soinside.com 2019 - 2024. All rights reserved.