From 35c9740826d2b7636647e45afab4ec87075647a6 Mon Sep 17 00:00:00 2001 From: Yohann Lereclus Date: Thu, 19 Aug 2021 11:58:59 +0200 Subject: [PATCH 1/2] Fix accuracy when topk > num_classes --- timm/utils/metrics.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/timm/utils/metrics.py b/timm/utils/metrics.py index 8e0b1f99..4f5d95a1 100644 --- a/timm/utils/metrics.py +++ b/timm/utils/metrics.py @@ -2,6 +2,7 @@ Hacked together by / Copyright 2020 Ross Wightman """ +import torch class AverageMeter: @@ -24,9 +25,12 @@ class AverageMeter: def accuracy(output, target, topk=(1,)): """Computes the accuracy over the k top predictions for the specified values of k""" - maxk = max(topk) + maxk = min(max(topk), output.size()[1]) batch_size = target.size(0) _, pred = output.topk(maxk, 1, True, True) pred = pred.t() correct = pred.eq(target.reshape(1, -1).expand_as(pred)) - return [correct[:k].reshape(-1).float().sum(0) * 100. / batch_size for k in topk] + return [ + correct[:k].reshape(-1).float().sum(0) * 100. / batch_size + if k <= maxk else torch.tensor(100.) for k in topk + ] From d667351eac57da2b07a50c07482652103a7839ee Mon Sep 17 00:00:00 2001 From: Ross Wightman Date: Thu, 19 Aug 2021 14:18:53 -0700 Subject: [PATCH 2/2] Tweak accuracy topk safety. Fix #807 --- timm/utils/metrics.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/timm/utils/metrics.py b/timm/utils/metrics.py index 4f5d95a1..9fdbe13e 100644 --- a/timm/utils/metrics.py +++ b/timm/utils/metrics.py @@ -2,7 +2,6 @@ Hacked together by / Copyright 2020 Ross Wightman """ -import torch class AverageMeter: @@ -30,7 +29,4 @@ def accuracy(output, target, topk=(1,)): _, pred = output.topk(maxk, 1, True, True) pred = pred.t() correct = pred.eq(target.reshape(1, -1).expand_as(pred)) - return [ - correct[:k].reshape(-1).float().sum(0) * 100. / batch_size - if k <= maxk else torch.tensor(100.) for k in topk - ] + return [correct[:min(k, maxk)].reshape(-1).float().sum(0) * 100. / batch_size for k in topk]