File: compute_topk_accuracy.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (59 lines) | stat: -rw-r--r-- 2,189 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59





import caffe2.contrib.playground.meter as Meter
from caffe2.python import workspace
import numpy as np


class ComputeTopKAccuracy(Meter.Meter):
    # Python default arguments are evaluated once when the function is
    # defined, not each time the function is called
    # This means that if you use a mutable default argument and mutate it,
    # you will and have mutated that object for
    # all future calls to the function as well.
    # def __init__(self, blob_name=['softmax', 'label'], opts=None, topk=1):
    def __init__(self, blob_name=None, opts=None, topk=1):
        if blob_name is None:
            blob_name = ['softmax', 'label']
        self.blob_name = blob_name
        self.opts = opts
        self.topk = topk
        self.iter = 0
        self.value = 0

    def Reset(self):
        self.iter = 0
        self.value = 0

    def Add(self):
        for idx in range(self.opts['distributed']['first_xpu_id'],
                         self.opts['distributed']['first_xpu_id'] +
                         self.opts['distributed']['num_xpus']):
            prefix = '{}_{}/'.format(self.opts['distributed']['device'], idx)
            softmax = workspace.FetchBlob(prefix + self.blob_name[0])
            labels = workspace.FetchBlob(prefix + self.blob_name[1])
            output = np.squeeze(softmax)
            target = np.squeeze(labels)
            if len(output.shape) == 1:
                output = output.reshape((1, output.shape[0]))
            else:
                assert len(output.shape) == 2, \
                    'wrong output size (1D or 2D expected)'
            assert len(target.shape) == 1, 'wrong target size (1D expected)'
            assert output.shape[0] == target.shape[0], \
                'target and output do not match'

            N = output.shape[0]
            pred = np.argsort(-output, axis=1)[:, :self.topk]
            correct = pred.astype(target.dtype) == np.repeat(
                target.reshape((N, 1)), [self.topk], axis=1)
            self.value += np.sum(correct[:, :self.topk])
            self.iter += N

    def Compute(self):
        result = self.value / self.iter
        self.Reset()
        return result