ONNX
ytfeng commited on
Commit
2b4a4e2
·
1 Parent(s): 5f7090a

Limit combinations of backends and targets in demos and benchmark (#145)

Browse files

* limit backend and target combination in demos and benchmark

* simpler version checking

Files changed (2) hide show
  1. demo.py +26 -30
  2. mobilenet.py +3 -5
demo.py CHANGED
@@ -5,43 +5,39 @@ import cv2 as cv
5
 
6
  from mobilenet import MobileNet
7
 
8
- def str2bool(v):
9
- if v.lower() in ['on', 'yes', 'true', 'y', 't']:
10
- return True
11
- elif v.lower() in ['off', 'no', 'false', 'n', 'f']:
12
- return False
13
- else:
14
- raise NotImplementedError
15
-
16
- backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
17
- targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
18
- help_msg_backends = "Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
19
- help_msg_targets = "Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
20
- try:
21
- backends += [cv.dnn.DNN_BACKEND_TIMVX]
22
- targets += [cv.dnn.DNN_TARGET_NPU]
23
- help_msg_backends += "; {:d}: TIMVX"
24
- help_msg_targets += "; {:d}: NPU"
25
- except:
26
- print('This version of OpenCV does not support TIM-VX and NPU. Visit https://github.com/opencv/opencv/wiki/TIM-VX-Backend-For-Running-OpenCV-On-NPU for more information.')
27
-
28
- all_mobilenets = [
29
- 'image_classification_mobilenetv1_2022apr.onnx',
30
- 'image_classification_mobilenetv2_2022apr.onnx',
31
- 'image_classification_mobilenetv1_2022apr-int8-quantized.onnx',
32
- 'image_classification_mobilenetv2_2022apr-int8-quantized.onnx'
33
  ]
34
 
35
  parser = argparse.ArgumentParser(description='Demo for MobileNet V1 & V2.')
36
- parser.add_argument('--input', '-i', type=str, help='Usage: Set input path to a certain image, omit if using camera.')
37
- parser.add_argument('--model', '-m', type=str, choices=all_mobilenets, default=all_mobilenets[0], help='Usage: Set model type, defaults to image_classification_mobilenetv1_2022apr.onnx (v1).')
38
- parser.add_argument('--backend', '-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
39
- parser.add_argument('--target', '-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
 
 
 
 
 
 
 
 
40
  args = parser.parse_args()
41
 
42
  if __name__ == '__main__':
 
 
43
  # Instantiate MobileNet
44
- model = MobileNet(modelPath=args.model, backendId=args.backend, targetId=args.target)
45
 
46
  # Read image and get a 224x224 crop from a 256x256 resized
47
  image = cv.imread(args.input)
 
5
 
6
  from mobilenet import MobileNet
7
 
8
+ # Check OpenCV version
9
+ assert cv.__version__ >= "4.7.0", \
10
+ "Please install latest opencv-python to try this demo: python3 -m pip install --upgrade opencv-python"
11
+
12
+ # Valid combinations of backends and targets
13
+ backend_target_pairs = [
14
+ [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_TARGET_CPU],
15
+ [cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA],
16
+ [cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16],
17
+ [cv.dnn.DNN_BACKEND_TIMVX, cv.dnn.DNN_TARGET_NPU],
18
+ [cv.dnn.DNN_BACKEND_CANN, cv.dnn.DNN_TARGET_NPU]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  ]
20
 
21
  parser = argparse.ArgumentParser(description='Demo for MobileNet V1 & V2.')
22
+ parser.add_argument('--input', '-i', type=str,
23
+ help='Usage: Set input path to a certain image, omit if using camera.')
24
+ parser.add_argument('--model', '-m', type=str, default='image_classification_mobilenetv1_2022apr.onnx',
25
+ help='Usage: Set model type, defaults to image_classification_mobilenetv1_2022apr.onnx (v1).')
26
+ parser.add_argument('--backend_target', '-bt', type=int, default=0,
27
+ help='''Choose one of the backend-target pair to run this demo:
28
+ {:d}: (default) OpenCV implementation + CPU,
29
+ {:d}: CUDA + GPU (CUDA),
30
+ {:d}: CUDA + GPU (CUDA FP16),
31
+ {:d}: TIM-VX + NPU,
32
+ {:d}: CANN + NPU
33
+ '''.format(*[x for x in range(len(backend_target_pairs))]))
34
  args = parser.parse_args()
35
 
36
  if __name__ == '__main__':
37
+ backend_id = backend_target_pairs[args.backend_target][0]
38
+ target_id = backend_target_pairs[args.backend_target][1]
39
  # Instantiate MobileNet
40
+ model = MobileNet(modelPath=args.model, backendId=backend_id, targetId=target_id)
41
 
42
  # Read image and get a 224x224 crop from a 256x256 resized
43
  image = cv.imread(args.input)
mobilenet.py CHANGED
@@ -33,12 +33,10 @@ class MobileNet:
33
  def name(self):
34
  return self.__class__.__name__
35
 
36
- def setBackend(self, backendId):
37
- self.backend_id = backendId
 
38
  self.model.setPreferableBackend(self.backend_id)
39
-
40
- def setTarget(self, targetId):
41
- self.target_id = targetId
42
  self.model.setPreferableTarget(self.target_id)
43
 
44
  def _preprocess(self, image):
 
33
  def name(self):
34
  return self.__class__.__name__
35
 
36
+ def setBackendAndTarget(self, backendId, targetId):
37
+ self._backendId = backendId
38
+ self._targetId = targetId
39
  self.model.setPreferableBackend(self.backend_id)
 
 
 
40
  self.model.setPreferableTarget(self.target_id)
41
 
42
  def _preprocess(self, image):