How to find individual class Accuracy

I am using Transfer Learning for Classification of my Dataset.

How to calculate Classification accuracy of each class?

1 Like

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].

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))
11 Likes

how to calculate overall precision and recall here

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

Thank you so much Mona for your reply.

This doesn’t calculate accuracy. The true negatives are missing from the numerator of your fraction:

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

You are either calculating precision or recall. I don’t know which of the two, because I can’t tell which of the axis is prediction and which is ground truth.

Really nice! Thanks…