pytorch框架分类器各个子类准确率计算代码

In the Cifar10 image classifier example, what are the classes that performed well, and the classes did not perform well.

firstly, set the constant:

N_CLASSES

BATCH_SIZE

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

class_correct = list(0. for i in range(N_CLASSES))
class_total = list(0. for i in range(N_CLASSES))
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        for i in range(BATCH_SIZE):
            label = labels[i]
            class_correct[label] += c[i].item()
            class_total[label] += 1


for i in range(N_CLASSES):
    print('Accuracy of %5s : %2d %%' % (
        classes[i], 100 * class_correct[i] / class_total[i]))

Very clever codes from the official cite.

猜你喜欢

转载自blog.csdn.net/Arctic_Beacon/article/details/85068188