How to find individual class Accuracy

(RAJAT KUMAR SINHA) #1

I am using Transfer Learning for Classification of my Dataset.

How to calculate Classification accuracy of each class?

1 Like
#2

I would just have an array correct filled with zeros and size of number of total classes. Then I would classify data point j, if it matches the target label[j] then just increment the array with that class index, correct[j] += 1. If num_class is an array that contains the number of points for each class c, then the accuarcy is correct[c] / num_class[c].

(Mona Jalal) #4

Answer given by @ptrblck Thanks a lot!

nb_classes = 9

confusion_matrix = torch.zeros(nb_classes, nb_classes)
with torch.no_grad():
    for i, (inputs, classes) in enumerate(dataloaders['val']):
        inputs = inputs.to(device)
        classes = classes.to(device)
        outputs = model_ft(inputs)
        _, preds = torch.max(outputs, 1)
        for t, p in zip(classes.view(-1), preds.view(-1)):
                confusion_matrix[t.long(), p.long()] += 1

print(confusion_matrix)

To get the per-class accuracy:

print(confusion_matrix.diag()/confusion_matrix.sum(1))
2 Likes
#5

how to calculate overall precision and recall here

(Mona Jalal) #7

Yes, you should calculate the accuracy on your test images. I also suggest you create stratified 5 or 10 fold experiment.

(KanZa ) #8

Thank you so much Mona for your reply.

(KanZa ) #10

Kindly help me to make confusion matrix for the following code, where I am calculating accuracy per class for 101 class labels

video_pred = [np.argmax(x[0]) for x in output]

video_labels = [x[1] for x in output]

print('Accuracy {:.02f}% ({})'.format(
    float(np.sum(np.array(video_pred) == np.array(video_labels))) / len(video_pred) * 100.0,
    len(video_pred)))       

#Accuracy per Class
for i in range(num_class):

    indicies_correct = np.where(np.array(video_pred) == np.array(video_labels))
    
    class_total = np.sum(np.array(video_labels) == i)

    class_correct = np.sum(np.array(video_pred)[indicies_correct ] == i)
    
    print('Class {}: Accuracy {:.02f}%'.format(i, class_correct / class_total * 100))

          

confusion_matrix = torch.zeros(num_class, num_class)

data_gen = enumerate(data_loader)
#with torch.no_grad():

for i, (data, label) in data_gen:
    data = data.to(device)
    label = label.to(device)
    outputs = model_ft(data)
    _, preds = torch.max(outputs, 1)
    for t, p in zip(label.view(-1), preds.view(-1)):
        print(t, p)
        confusion_matrix[t.long(), p.long()] += 1

print(confusion_matrix)
print(confusion_matrix.diag()/confusion_matrix.sum(1))