ytfeng commited on
Commit
c6644c1
·
1 Parent(s): fe1df9a

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 +39 -31
  2. yunet.py +1 -13
demo.py CHANGED
@@ -11,36 +11,42 @@ import cv2 as cv
11
 
12
  from yunet import YuNet
13
 
14
- def str2bool(v):
15
- if v.lower() in ['on', 'yes', 'true', 'y', 't']:
16
- return True
17
- elif v.lower() in ['off', 'no', 'false', 'n', 'f']:
18
- return False
19
- else:
20
- raise NotImplementedError
21
-
22
- backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
23
- targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
24
- help_msg_backends = "Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
25
- help_msg_targets = "Choose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
26
- try:
27
- backends += [cv.dnn.DNN_BACKEND_TIMVX]
28
- targets += [cv.dnn.DNN_TARGET_NPU]
29
- help_msg_backends += "; {:d}: TIMVX"
30
- help_msg_targets += "; {:d}: NPU"
31
- except:
32
- 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.')
33
 
34
  parser = argparse.ArgumentParser(description='YuNet: A Fast and Accurate CNN-based Face Detector (https://github.com/ShiqiYu/libfacedetection).')
35
- parser.add_argument('--input', '-i', type=str, help='Usage: Set input to a certain image, omit if using camera.')
36
- parser.add_argument('--model', '-m', type=str, default='face_detection_yunet_2022mar.onnx', help="Usage: Set model type, defaults to 'face_detection_yunet_2022mar.onnx'.")
37
- parser.add_argument('--backend', '-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
38
- parser.add_argument('--target', '-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
39
- parser.add_argument('--conf_threshold', type=float, default=0.9, help='Usage: Set the minimum needed confidence for the model to identify a face, defauts to 0.9. Smaller values may result in faster detection, but will limit accuracy. Filter out faces of confidence < conf_threshold.')
40
- parser.add_argument('--nms_threshold', type=float, default=0.3, help='Usage: Suppress bounding boxes of iou >= nms_threshold. Default = 0.3.')
41
- parser.add_argument('--top_k', type=int, default=5000, help='Usage: Keep top_k bounding boxes before NMS.')
42
- parser.add_argument('--save', '-s', type=str, default=False, help='Usage: Set “True” to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input. Default will be set to “False”.')
43
- parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Usage: Default will be set to “True” and will open a new window to show results. Set to “False” to stop visualizations from being shown. Invalid in case of camera input.')
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  args = parser.parse_args()
45
 
46
  def visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None):
@@ -70,14 +76,17 @@ def visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps
70
  return output
71
 
72
  if __name__ == '__main__':
 
 
 
73
  # Instantiate YuNet
74
  model = YuNet(modelPath=args.model,
75
  inputSize=[320, 320],
76
  confThreshold=args.conf_threshold,
77
  nmsThreshold=args.nms_threshold,
78
  topK=args.top_k,
79
- backendId=args.backend,
80
- targetId=args.target)
81
 
82
  # If input is an image
83
  if args.input is not None:
@@ -134,4 +143,3 @@ if __name__ == '__main__':
134
  cv.imshow('YuNet Demo', frame)
135
 
136
  tm.reset()
137
-
 
11
 
12
  from yunet import YuNet
13
 
14
+ # Check OpenCV version
15
+ assert cv.__version__ >= "4.7.0", \
16
+ "Please install latest opencv-python to try this demo: python3 -m pip install --upgrade opencv-python"
17
+
18
+ # Valid combinations of backends and targets
19
+ backend_target_pairs = [
20
+ [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_TARGET_CPU],
21
+ [cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA],
22
+ [cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16],
23
+ [cv.dnn.DNN_BACKEND_TIMVX, cv.dnn.DNN_TARGET_NPU],
24
+ [cv.dnn.DNN_BACKEND_CANN, cv.dnn.DNN_TARGET_NPU]
25
+ ]
 
 
 
 
 
 
 
26
 
27
  parser = argparse.ArgumentParser(description='YuNet: A Fast and Accurate CNN-based Face Detector (https://github.com/ShiqiYu/libfacedetection).')
28
+ parser.add_argument('--input', '-i', type=str,
29
+ help='Usage: Set input to a certain image, omit if using camera.')
30
+ parser.add_argument('--model', '-m', type=str, default='face_detection_yunet_2022mar.onnx',
31
+ help="Usage: Set model type, defaults to 'face_detection_yunet_2022mar.onnx'.")
32
+ parser.add_argument('--backend_target', '-bt', type=int, default=0,
33
+ help='''Choose one of the backend-target pair to run this demo:
34
+ {:d}: (default) OpenCV implementation + CPU,
35
+ {:d}: CUDA + GPU (CUDA),
36
+ {:d}: CUDA + GPU (CUDA FP16),
37
+ {:d}: TIM-VX + NPU,
38
+ {:d}: CANN + NPU
39
+ '''.format(*[x for x in range(len(backend_target_pairs))]))
40
+ parser.add_argument('--conf_threshold', type=float, default=0.9,
41
+ help='Usage: Set the minimum needed confidence for the model to identify a face, defauts to 0.9. Smaller values may result in faster detection, but will limit accuracy. Filter out faces of confidence < conf_threshold.')
42
+ parser.add_argument('--nms_threshold', type=float, default=0.3,
43
+ help='Usage: Suppress bounding boxes of iou >= nms_threshold. Default = 0.3.')
44
+ parser.add_argument('--top_k', type=int, default=5000,
45
+ help='Usage: Keep top_k bounding boxes before NMS.')
46
+ parser.add_argument('--save', '-s', action='store_true',
47
+ help='Usage: Specify to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input.')
48
+ parser.add_argument('--vis', '-v', action='store_true',
49
+ help='Usage: Specify to open a new window to show results. Invalid in case of camera input.')
50
  args = parser.parse_args()
51
 
52
  def visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None):
 
76
  return output
77
 
78
  if __name__ == '__main__':
79
+ backend_id = backend_target_pairs[args.backend_target][0]
80
+ target_id = backend_target_pairs[args.backend_target][1]
81
+
82
  # Instantiate YuNet
83
  model = YuNet(modelPath=args.model,
84
  inputSize=[320, 320],
85
  confThreshold=args.conf_threshold,
86
  nmsThreshold=args.nms_threshold,
87
  topK=args.top_k,
88
+ backendId=backend_id,
89
+ targetId=target_id)
90
 
91
  # If input is an image
92
  if args.input is not None:
 
143
  cv.imshow('YuNet Demo', frame)
144
 
145
  tm.reset()
 
yunet.py CHANGED
@@ -33,19 +33,8 @@ class YuNet:
33
  def name(self):
34
  return self.__class__.__name__
35
 
36
- def setBackend(self, backendId):
37
  self._backendId = backendId
38
- self._model = cv.FaceDetectorYN.create(
39
- model=self._modelPath,
40
- config="",
41
- input_size=self._inputSize,
42
- score_threshold=self._confThreshold,
43
- nms_threshold=self._nmsThreshold,
44
- top_k=self._topK,
45
- backend_id=self._backendId,
46
- target_id=self._targetId)
47
-
48
- def setTarget(self, targetId):
49
  self._targetId = targetId
50
  self._model = cv.FaceDetectorYN.create(
51
  model=self._modelPath,
@@ -64,4 +53,3 @@ class YuNet:
64
  # Forward
65
  faces = self._model.detect(image)
66
  return faces[1]
67
-
 
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 = cv.FaceDetectorYN.create(
40
  model=self._modelPath,
 
53
  # Forward
54
  faces = self._model.detect(image)
55
  return faces[1]