/usr/local/lib64/python3.6/site-packages/caffe2/contrib/playground
NameSizeModeActions
resnetdemo/-0755rm
__pycache__/-0755rm
AnyExp.py179680644editdlrm
AnyExpOnTerm.py33460644editdlrm
checkpoint.py68810644editdlrm
compute_loss.py9650644editdlrm
compute_topk_accuracy.py21890644editdlrm
meter.py2920644editdlrm
ModuleRegister.py47710644editdlrm
module_map.py14270644editdlrm
output_generator.py4780644editdlrm
__init__.py00644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/caffe2/contrib/playground/compute_topk_accuracy.py (2189B)
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