JeVoisBase  1.18
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
base_metric.py
Go to the documentation of this file.
1 import cv2 as cv
2 
3 from ..timer import Timer
4 
5 class BaseMetric:
6  def __init__(self, **kwargs):
7  self._warmup = kwargs.pop('warmup', 3)
8  self._repeat = kwargs.pop('repeat', 10)
9  self._reduction = kwargs.pop('reduction', 'median')
10 
11  self._timer = Timer()
12 
13  def _calcMedian(self, records):
14  ''' Return the median of records
15  '''
16  l = len(records)
17  mid = int(l / 2)
18  if l % 2 == 0:
19  return (records[mid] + records[mid - 1]) / 2
20  else:
21  return records[mid]
22 
23  def _calcGMean(self, records, drop_largest=3):
24  ''' Return the geometric mean of records after drop the first drop_largest
25  '''
26  l = len(records)
27  if l <= drop_largest:
28  print('len(records)({}) <= drop_largest({}), stop dropping.'.format(l, drop_largest))
29  records_sorted = sorted(records, reverse=True)
30  return sum(records_sorted[drop_largest:]) / (l - drop_largest)
31 
32  def _getResult(self):
33  records = self._timer.getRecords()
34  if self._reduction == 'median':
35  return self._calcMedian(records)
36  elif self._reduction == 'gmean':
37  return self._calcGMean(records)
38  else:
39  raise NotImplementedError('Reduction {} is not supported'.format(self._reduction))
40 
41  def getReduction(self):
42  return self._reduction
43 
44  def forward(self, model, *args, **kwargs):
45  raise NotImplementedError('Not implemented')
demo.int
int
Definition: demo.py:37
utils.metrics.base_metric.BaseMetric._timer
_timer
Definition: base_metric.py:11
utils.metrics.base_metric.BaseMetric
Definition: base_metric.py:5
utils.metrics.base_metric.BaseMetric._reduction
_reduction
Definition: base_metric.py:9
utils.metrics.base_metric.BaseMetric.getReduction
def getReduction(self)
Definition: base_metric.py:41
utils.metrics.base_metric.BaseMetric._repeat
_repeat
Definition: base_metric.py:8
utils.metrics.base_metric.BaseMetric._warmup
_warmup
Definition: base_metric.py:7
utils.metrics.base_metric.BaseMetric._calcGMean
def _calcGMean(self, records, drop_largest=3)
Definition: base_metric.py:23
utils.metrics.base_metric.BaseMetric._calcMedian
def _calcMedian(self, records)
Definition: base_metric.py:13
utils.timer.Timer
Definition: timer.py:3
utils.metrics.base_metric.BaseMetric.forward
def forward(self, model, *args, **kwargs)
Definition: base_metric.py:44
utils.metrics.base_metric.BaseMetric.__init__
def __init__(self, **kwargs)
Definition: base_metric.py:6