ONNX
ytfeng commited on
Commit
5f7090a
·
1 Parent(s): 17cd08b

Add model file collector, fix some bugs and add some features (#123)

Browse files

add model path searcher and remove hardwritten file paths from benchmark configs; pack ppresnet, mobilenet & crnn with labels; fix palm det data; add flags to enable models of different precision separately (#123)

Files changed (4) hide show
  1. demo.py +11 -14
  2. imagenet_labels.txt → mobilenet.py +81 -2
  3. mobilenet_v1.py +0 -81
  4. mobilenet_v2.py +0 -81
demo.py CHANGED
@@ -3,8 +3,7 @@ import argparse
3
  import numpy as np
4
  import cv2 as cv
5
 
6
- from mobilenet_v1 import MobileNetV1
7
- from mobilenet_v2 import MobileNetV2
8
 
9
  def str2bool(v):
10
  if v.lower() in ['on', 'yes', 'true', 'y', 't']:
@@ -26,24 +25,23 @@ try:
26
  except:
27
  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.')
28
 
 
 
 
 
 
 
 
29
  parser = argparse.ArgumentParser(description='Demo for MobileNet V1 & V2.')
30
  parser.add_argument('--input', '-i', type=str, help='Usage: Set input path to a certain image, omit if using camera.')
31
- parser.add_argument('--model', '-m', type=str, choices=['v1', 'v2', 'v1-q', 'v2-q'], default='v1', help='Usage: Set model type, defaults to image_classification_mobilenetv1_2022apr.onnx (v1).')
32
  parser.add_argument('--backend', '-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
33
  parser.add_argument('--target', '-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
34
- parser.add_argument('--label', '-l', type=str, default='./imagenet_labels.txt', help='Usage: Set path to the different labels that will be used during the detection. Default list found in imagenet_labels.txt')
35
  args = parser.parse_args()
36
 
37
  if __name__ == '__main__':
38
- # Instantiate ResNet
39
- models = {
40
- 'v1': MobileNetV1(modelPath='./image_classification_mobilenetv1_2022apr.onnx', labelPath=args.label, backendId=args.backend, targetId=args.target),
41
- 'v2': MobileNetV2(modelPath='./image_classification_mobilenetv2_2022apr.onnx', labelPath=args.label, backendId=args.backend, targetId=args.target),
42
- 'v1-q': MobileNetV1(modelPath='./image_classification_mobilenetv1_2022apr-int8-quantized.onnx', labelPath=args.label, backendId=args.backend, targetId=args.target),
43
- 'v2-q': MobileNetV2(modelPath='./image_classification_mobilenetv2_2022apr-int8-quantized.onnx', labelPath=args.label, backendId=args.backend, targetId=args.target)
44
-
45
- }
46
- model = models[args.model]
47
 
48
  # Read image and get a 224x224 crop from a 256x256 resized
49
  image = cv.imread(args.input)
@@ -56,4 +54,3 @@ if __name__ == '__main__':
56
 
57
  # Print result
58
  print('label: {}'.format(result))
59
-
 
3
  import numpy as np
4
  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']:
 
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)
 
54
 
55
  # Print result
56
  print('label: {}'.format(result))
 
imagenet_labels.txt → mobilenet.py RENAMED
@@ -1,4 +1,83 @@
1
- tench
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  goldfish
3
  great white shark
4
  tiger shark
@@ -997,4 +1076,4 @@ earthstar
997
  hen-of-the-woods
998
  bolete
999
  ear
1000
- toilet tissue
 
1
+ import numpy as np
2
+ import cv2 as cv
3
+
4
+ class MobileNet:
5
+ '''
6
+ Works with MobileNet V1 & V2.
7
+ '''
8
+
9
+ def __init__(self, modelPath, topK=1, backendId=0, targetId=0):
10
+ self.model_path = modelPath
11
+ assert topK >= 1
12
+ self.top_k = topK
13
+ self.backend_id = backendId
14
+ self.target_id = targetId
15
+
16
+ self.model = cv.dnn.readNet(self.model_path)
17
+ self.model.setPreferableBackend(self.backend_id)
18
+ self.model.setPreferableTarget(self.target_id)
19
+
20
+ self.input_names = ''
21
+ self.output_names = ''
22
+ self.input_size = [224, 224]
23
+ self.mean=[0.485, 0.456, 0.406]
24
+ self.std=[0.229, 0.224, 0.225]
25
+
26
+ # load labels
27
+ self._labels = self._load_labels()
28
+
29
+ def _load_labels(self):
30
+ return self.LABELS_IMAGENET_1K.splitlines()
31
+
32
+ @property
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):
45
+ input_blob = (image / 255.0 - self.mean) / self.std
46
+ input_blob = input_blob.transpose(2, 0, 1)
47
+ input_blob = input_blob[np.newaxis, :, :, :]
48
+ input_blob = input_blob.astype(np.float32)
49
+ return input_blob
50
+
51
+ def infer(self, image):
52
+ # Preprocess
53
+ input_blob = self._preprocess(image)
54
+
55
+ # Forward
56
+ self.model.setInput(input_blob, self.input_names)
57
+ output_blob = self.model.forward(self.output_names)
58
+
59
+ # Postprocess
60
+ results = self._postprocess(output_blob)
61
+
62
+ return results
63
+
64
+ def _postprocess(self, output_blob):
65
+ batched_class_id_list = []
66
+ for o in output_blob:
67
+ class_id_list = o.argsort()[::-1][:self.top_k]
68
+ batched_class_id_list.append(class_id_list)
69
+ if len(self._labels) > 0:
70
+ batched_predicted_labels = []
71
+ for class_id_list in batched_class_id_list:
72
+ predicted_labels = []
73
+ for class_id in class_id_list:
74
+ predicted_labels.append(self._labels[class_id])
75
+ batched_predicted_labels.append(predicted_labels)
76
+ return batched_predicted_labels
77
+ else:
78
+ return batched_class_id_list
79
+
80
+ LABELS_IMAGENET_1K = '''tench
81
  goldfish
82
  great white shark
83
  tiger shark
 
1076
  hen-of-the-woods
1077
  bolete
1078
  ear
1079
+ toilet tissue'''
mobilenet_v1.py DELETED
@@ -1,81 +0,0 @@
1
- import numpy as np
2
- import cv2 as cv
3
-
4
- class MobileNetV1:
5
- def __init__(self, modelPath, labelPath=None, topK=1, backendId=0, targetId=0):
6
- self.model_path = modelPath
7
- self.label_path = labelPath
8
- assert topK >= 1
9
- self.top_k = topK
10
- self.backend_id = backendId
11
- self.target_id = targetId
12
-
13
- self.model = cv.dnn.readNet(self.model_path)
14
- self.model.setPreferableBackend(self.backend_id)
15
- self.model.setPreferableTarget(self.target_id)
16
-
17
- self.input_names = ''
18
- self.output_names = ''
19
- self.input_size = [224, 224]
20
- self.mean=[0.485, 0.456, 0.406]
21
- self.std=[0.229, 0.224, 0.225]
22
-
23
- # load labels
24
- self._labels = self._load_labels()
25
-
26
- def _load_labels(self):
27
- labels = []
28
- if self.label_path is not None:
29
- with open(self.label_path, 'r') as f:
30
- for line in f:
31
- labels.append(line.strip())
32
- return labels
33
-
34
- @property
35
- def name(self):
36
- return self.__class__.__name__
37
-
38
- def setBackend(self, backendId):
39
- self.backend_id = backendId
40
- self.model.setPreferableBackend(self.backend_id)
41
-
42
- def setTarget(self, targetId):
43
- self.target_id = targetId
44
- self.model.setPreferableTarget(self.target_id)
45
-
46
- def _preprocess(self, image):
47
- input_blob = (image / 255.0 - self.mean) / self.std
48
- input_blob = input_blob.transpose(2, 0, 1)
49
- input_blob = input_blob[np.newaxis, :, :, :]
50
- input_blob = input_blob.astype(np.float32)
51
- return input_blob
52
-
53
- def infer(self, image):
54
- # Preprocess
55
- input_blob = self._preprocess(image)
56
-
57
- # Forward
58
- self.model.setInput(input_blob, self.input_names)
59
- output_blob = self.model.forward(self.output_names)
60
-
61
- # Postprocess
62
- results = self._postprocess(output_blob)
63
-
64
- return results
65
-
66
- def _postprocess(self, output_blob):
67
- batched_class_id_list = []
68
- for o in output_blob:
69
- class_id_list = o.argsort()[::-1][:self.top_k]
70
- batched_class_id_list.append(class_id_list)
71
- if len(self._labels) > 0:
72
- batched_predicted_labels = []
73
- for class_id_list in batched_class_id_list:
74
- predicted_labels = []
75
- for class_id in class_id_list:
76
- predicted_labels.append(self._labels[class_id])
77
- batched_predicted_labels.append(predicted_labels)
78
- return batched_predicted_labels
79
- else:
80
- return batched_class_id_list
81
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
mobilenet_v2.py DELETED
@@ -1,81 +0,0 @@
1
- import numpy as np
2
- import cv2 as cv
3
-
4
- class MobileNetV2:
5
- def __init__(self, modelPath, labelPath=None, topK=1, backendId=0, targetId=0):
6
- self.model_path = modelPath
7
- self.label_path = labelPath
8
- assert topK >= 1
9
- self.top_k = topK
10
- self.backend_id = backendId
11
- self.target_id = targetId
12
-
13
- self.model = cv.dnn.readNet(self.model_path)
14
- self.model.setPreferableBackend(self.backend_id)
15
- self.model.setPreferableTarget(self.target_id)
16
-
17
- self.input_names = ''
18
- self.output_names = ''
19
- self.input_size = [224, 224]
20
- self.mean=[0.485, 0.456, 0.406]
21
- self.std=[0.229, 0.224, 0.225]
22
-
23
- # load labels
24
- self._labels = self._load_labels()
25
-
26
- def _load_labels(self):
27
- labels = []
28
- if self.label_path is not None:
29
- with open(self.label_path, 'r') as f:
30
- for line in f:
31
- labels.append(line.strip())
32
- return labels
33
-
34
- @property
35
- def name(self):
36
- return self.__class__.__name__
37
-
38
- def setBackend(self, backendId):
39
- self.backend_id = backendId
40
- self.model.setPreferableBackend(self.backend_id)
41
-
42
- def setTarget(self, targetId):
43
- self.target_id = targetId
44
- self.model.setPreferableTarget(self.target_id)
45
-
46
- def _preprocess(self, image):
47
- input_blob = (image / 255.0 - self.mean) / self.std
48
- input_blob = input_blob.transpose(2, 0, 1)
49
- input_blob = input_blob[np.newaxis, :, :, :]
50
- input_blob = input_blob.astype(np.float32)
51
- return input_blob
52
-
53
- def infer(self, image):
54
- # Preprocess
55
- input_blob = self._preprocess(image)
56
-
57
- # Forward
58
- self.model.setInput(input_blob, self.input_names)
59
- output_blob = self.model.forward(self.output_names)
60
-
61
- # Postprocess
62
- results = self._postprocess(output_blob)
63
-
64
- return results
65
-
66
- def _postprocess(self, output_blob):
67
- batched_class_id_list = []
68
- for o in output_blob:
69
- class_id_list = o.argsort()[::-1][:self.top_k]
70
- batched_class_id_list.append(class_id_list)
71
- if len(self._labels) > 0:
72
- batched_predicted_labels = []
73
- for class_id_list in batched_class_id_list:
74
- predicted_labels = []
75
- for class_id in class_id_list:
76
- predicted_labels.append(self._labels[class_id])
77
- batched_predicted_labels.append(predicted_labels)
78
- return batched_predicted_labels
79
- else:
80
- return batched_class_id_list
81
-