8 from models
import MODELS
9 from utils
import METRICS, DATALOADERS
11 parser = argparse.ArgumentParser(
"Benchmarks for OpenCV Zoo.")
12 parser.add_argument(
'--cfg',
'-c', type=str,
13 help=
'Benchmarking on the given config.')
14 args = parser.parse_args()
18 obj_name = cfg.pop(key)
19 obj = registery.get(obj_name)
21 elif name
is not None:
22 obj = registery.get(name)
25 raise NotImplementedError()
28 for k, v
in cfg.items():
29 if isinstance(v, dict):
32 if 'path' in k.lower():
33 cfg[k] = os.path.join(os.environ[
'PYTHONPATH'], v)
37 self.
_type = kwargs.pop(
'type',
None)
38 if self.
_type is None:
40 print(
'Benchmark[\'type\'] is omitted, set to \'Base\' by default.')
43 assert self.
_data_dict,
'Benchmark[\'data\'] cannot be empty and must have path and files.'
50 assert self.
_metric_dict,
'Benchmark[\'metric\'] cannot be empty.'
56 backend_id = kwargs.pop(
'backend',
'default')
57 available_backends = dict(
58 default=cv.dnn.DNN_BACKEND_DEFAULT,
61 opencv=cv.dnn.DNN_BACKEND_OPENCV,
63 cuda=cv.dnn.DNN_BACKEND_CUDA,
66 target_id = kwargs.pop(
'target',
'cpu')
67 available_targets = dict(
68 cpu=cv.dnn.DNN_TARGET_CPU,
74 cuda=cv.dnn.DNN_TARGET_CUDA,
75 cuda_fp16=cv.dnn.DNN_TARGET_CUDA_FP16,
81 available_backends[
'timvx'] = cv.dnn.DNN_BACKEND_TIMVX
82 available_targets[
'npu'] = cv.dnn.DNN_TARGET_NPU
84 print(
'OpenCV is not compiled with TIM-VX backend enbaled. See https://github.com/opencv/opencv/wiki/TIM-VX-Backend-For-Running-OpenCV-On-NPU for more details on how to enable TIM-VX backend.')
86 self.
_backend = available_backends[backend_id]
87 self.
_target = available_targets[target_id]
96 filename, input_data = data[:2]
99 if isinstance(input_data, np.ndarray):
100 size = [input_data.shape[1], input_data.shape[0]]
102 size = input_data.getFrameSize()
107 print(
' image: {}'.format(imgName))
109 for key, latency
in results.items():
110 total_latency += latency
111 print(
' {}, latency ({}): {:.4f} ms'.format(key, self.
_metric.getReduction(), latency))
113 if __name__ ==
'__main__':
114 assert args.cfg.endswith(
'yaml'),
'Currently support configs of yaml format only.'
115 with open(args.cfg,
'r')
as f:
116 cfg = yaml.safe_load(f)
128 print(
'Benchmarking {}:'.format(model.name))
130 benchmark.printResults()