MCPcopy
hub / github.com/facebookresearch/MetaCLIP / accuracy

Function accuracy

clipeval/eval_zeroshot.py:130–144  ·  view source on GitHub ↗

Computes the accuracy over the k top predictions for the specified values of k

(output, target, topk=(1,))

Source from the content-addressed store, hash-verified

128
129
130def accuracy(output, target, topk=(1,)):
131 """Computes the accuracy over the k top predictions for the specified values of k"""
132 with torch.no_grad():
133 maxk = max(topk)
134 batch_size = target.size(0)
135
136 _, pred = output.topk(maxk, 1, True, True)
137 pred = pred.t()
138 correct = pred.eq(target.reshape(1, -1).expand_as(pred))
139
140 res = []
141 for k in topk:
142 correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
143 res.append(correct_k.mul_(100.0 / batch_size))
144 return res
145
146
147def mean_per_class(outputs, targets):

Callers 3

evaluate_logitsFunction · 0.90
evaluate_datasetFunction · 0.90
evaluateFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected