mirror of
https://github.com/huggingface/pytorch-image-models.git
synced 2025-06-03 15:01:08 +08:00
Fix accuracy when topk > num_classes
This commit is contained in:
parent
a16a753852
commit
35c9740826
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
Hacked together by / Copyright 2020 Ross Wightman
|
Hacked together by / Copyright 2020 Ross Wightman
|
||||||
"""
|
"""
|
||||||
|
import torch
|
||||||
|
|
||||||
|
|
||||||
class AverageMeter:
|
class AverageMeter:
|
||||||
@ -24,9 +25,12 @@ class AverageMeter:
|
|||||||
|
|
||||||
def accuracy(output, target, topk=(1,)):
|
def accuracy(output, target, topk=(1,)):
|
||||||
"""Computes the accuracy over the k top predictions for the specified values of k"""
|
"""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)
|
batch_size = target.size(0)
|
||||||
_, pred = output.topk(maxk, 1, True, True)
|
_, pred = output.topk(maxk, 1, True, True)
|
||||||
pred = pred.t()
|
pred = pred.t()
|
||||||
correct = pred.eq(target.reshape(1, -1).expand_as(pred))
|
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
|
||||||
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user