question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multi class accuracy metric

See original GitHub issue

I really looked at issues and googled the question but couldn’t find anything I could use so here goes.

I’m porting some code from vanila pytorch to Ignite, and have a CIFAR10 classifier. Every end of Evaluation epoch, I want to report the per class accuracy. In Ignite, I only found total accuracy (which I use) but not a per class one. I wrote this CustomMetric, but I have a few problems with it:

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

    def __init__(self, *args, **kwargs):
        self._num_correct = [0] * len(classes)
        self._num_examples = [0] * len(classes)
        super().__init__(*args, **kwargs)
        self.i = 0

    @reinit__is_reduced
    def reset(self):
        self._num_correct = [0] * len(classes)
        self._num_examples = [0] * len(classes)
        self.i = 0
        super(CustomAccuracy, self).reset()


    @reinit__is_reduced
    def update(self, output):
        y_pred, y = output

        for predtensor,real in zip(y_pred,y):
            pred = torch.argmax(F.softmax(predtensor,0), 0)
            self._num_examples[real] += 1
            if real == pred:
                self._num_correct[real] += 1

    @sync_all_reduce("_num_examples", "_num_correct")
    def compute(self):
        class_accuracy = [0] * len(classes)
        for i, (correct, total) in enumerate(zip(self._num_correct,self._num_examples)):
            class_accuracy[i] = correct / total
        #if self._num_examples == 0:
        #    raise NotComputableError('CustomAccuracy must have at least one example before it can be computed.')
        return class_accuracy

First, It depends on the Loss I chose. I use nn.CrossEntropyLoss() which has softmax, so in my metric I also had to add it. But I assume if I use another Loss without softmax I won’t need it (and my network will handle that). Second, this feels like something that should be out of the box, so I’m wondering if I’m missing something.

Any advice?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
erezalgcommented, Nov 2, 2020

I see, OK makes sense! I’ll open the feature request and let’s see where it leads!

Thanks again!

1reaction
vfdev-5commented, Nov 2, 2020

@erezalg thanks for the feedback !

One last question that I couldn’t figure out myself is, when I have multiclass recall, the name of the class in the TB graph is the label enumeration.

Unfortunately, it is not possible out-of-the-box to add labes. This is something I was also thinking about to add as a feature request (if you’d like to send one, it could be helpful). The limitation is due to a tensor nature of metrics output: we have for example Recall metric that outputs torch.tensor([0.1, 0.2, 0.3, ..., 0.8]) and this is directly used within OutputHandler for the Tensorboard: https://github.com/pytorch/ignite/blob/75e20420a2391ad2e11ee17df65f781a659ae6ec/ignite/contrib/handlers/tensorboard_logger.py#L290-L291

However, there is a workaround to that. Idea is to create N metrics that output scalars instead of a single metric that gives a tensor. Thus we can label the metric as we’d like. We can use metrics arithmetics for that. Something like that should work:

num_classes = 10
cls_name_mapping = ["car", ...]
val_metrics = {}

for i in range(num_classes):
    cls_name = cls_name_mapping[i]
    val_metrics["Recall/{}".format(cls_name)] = Recall(average=False)[i].item()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Comprehensive Guide on Multiclass Classification Metrics
So, this post will be about the 7 most commonly used MC metrics: precision, recall, F1 score, ROC AUC score, Cohen Kappa score,...
Read more >
Evaluation Metrics For Multi-class Classification - Kaggle
Accuracy : It is one of the most straightforward metrics used in machine learning. It defines how accurate your model is. For example,...
Read more >
Metrics for Multi-Class Classification: an Overview - arXiv Vanity
Accuracy is one of the most popular metrics in multi-class classification and it is directly computed from the confusion matrix.
Read more >
Which metrics are used to evaluate a multiclass classification ...
Multiclass classification models classify each observation in a dataset into one of many categories. Evaluating these multiclass classification models for their ...
Read more >
What is the best validation metric for multi-class classification?
Machine Learning FAQ. What is the best validation metric for multi-class classification? It really depends on our “goal” and our dataset.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found