-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmetric.py
47 lines (35 loc) · 1.64 KB
/
metric.py
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
import os
# MXNET_CPU_WORKER_NTHREADS must be greater than 1 for custom op to work on CPU
os.environ['MXNET_CPU_WORKER_NTHREADS'] = '2'
import mxnet as mx
# define multi task accuracy
class Multi_Accuracy(mx.metric.EvalMetric):
def __init__(self, num=None, output_names = None):
self.num = num
super(Multi_Accuracy, self).__init__('multi_accuracy', num)
self.output_names = output_names
def reset(self):
''' Resets the internal evaluation result to initial state.'''
self.num_inst = 0 if self.num is None else [0] * self.num
self.sum_metric = 0.0 if self.num is None else [0.0] * self.num
def update(self, labels, preds):
mx.metric.check_label_shapes(labels,preds)
if self.num != None:
assert len(labels) == self.num
for i in range(len(labels)):
pred_label = mx.nd.argmax_channel(preds[i]).asnumpy().astype('int32')
label = labels[i].asnumpy().astype('int32')
mx.metric.check_label_shapes(label,pred_label)
if self.num is None:
self.sum_metric += (pred_label.flat == label.flat).sum()
self.num_inst += len(pred_label.flat)
else:
self.sum_metric[i] += (pred_label.flat == label.flat).sum()
self.num_inst[i] += len(pred_label.flat)
def get(self):
if self.num is None:
return super(Multi_Accuracy, self).get()
else:
return zip(*(('%s-task%d' % (self.name, i), float('nan') if self.num_inst[i] == 0
else self.sum_metric[i] / self.num_inst[i])
for i in range(self.num)))