Add options for demo scripts to select backend & targets (#43)
Browse files* add options for selecting backend & targets
* add eol
- demo.py +16 -1
- ppresnet.py +13 -5
demo.py
CHANGED
@@ -19,15 +19,29 @@ def str2bool(v):
|
|
19 |
else:
|
20 |
raise NotImplementedError
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
parser = argparse.ArgumentParser(description='Deep Residual Learning for Image Recognition (https://arxiv.org/abs/1512.03385, https://github.com/PaddlePaddle/PaddleHub)')
|
23 |
parser.add_argument('--input', '-i', type=str, help='Path to the input image.')
|
24 |
parser.add_argument('--model', '-m', type=str, default='image_classification_ppresnet50_2022jan.onnx', help='Path to the model.')
|
|
|
|
|
25 |
parser.add_argument('--label', '-l', type=str, default='./imagenet_labels.txt', help='Path to the dataset labels.')
|
26 |
args = parser.parse_args()
|
27 |
|
28 |
if __name__ == '__main__':
|
29 |
# Instantiate ResNet
|
30 |
-
model = PPResNet(modelPath=args.model, labelPath=args.label)
|
31 |
|
32 |
# Read image and get a 224x224 crop from a 256x256 resized
|
33 |
image = cv.imread(args.input)
|
@@ -40,3 +54,4 @@ if __name__ == '__main__':
|
|
40 |
|
41 |
# Print result
|
42 |
print('label: {}'.format(result))
|
|
|
|
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 = "Chose 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://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
|
33 |
+
|
34 |
parser = argparse.ArgumentParser(description='Deep Residual Learning for Image Recognition (https://arxiv.org/abs/1512.03385, https://github.com/PaddlePaddle/PaddleHub)')
|
35 |
parser.add_argument('--input', '-i', type=str, help='Path to the input image.')
|
36 |
parser.add_argument('--model', '-m', type=str, default='image_classification_ppresnet50_2022jan.onnx', help='Path to the model.')
|
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('--label', '-l', type=str, default='./imagenet_labels.txt', help='Path to the dataset labels.')
|
40 |
args = parser.parse_args()
|
41 |
|
42 |
if __name__ == '__main__':
|
43 |
# Instantiate ResNet
|
44 |
+
model = PPResNet(modelPath=args.model, labelPath=args.label, 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))
|
57 |
+
|
ppresnet.py
CHANGED
@@ -9,10 +9,15 @@ import numpy as np
|
|
9 |
import cv2 as cv
|
10 |
|
11 |
class PPResNet:
|
12 |
-
def __init__(self, modelPath, labelPath):
|
13 |
self._modelPath = modelPath
|
14 |
-
self._model = cv.dnn.readNet(self._modelPath)
|
15 |
self._labelPath = labelPath
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
self._inputNames = ''
|
18 |
self._outputNames = ['save_infer_model/scale_0.tmp_0']
|
@@ -35,10 +40,12 @@ class PPResNet:
|
|
35 |
return self.__class__.__name__
|
36 |
|
37 |
def setBackend(self, backend_id):
|
38 |
-
self.
|
|
|
39 |
|
40 |
def setTarget(self, target_id):
|
41 |
-
self.
|
|
|
42 |
|
43 |
def _preprocess(self, image):
|
44 |
image = image.astype(np.float32, copy=False) / 255.0
|
@@ -64,4 +71,5 @@ class PPResNet:
|
|
64 |
|
65 |
def _postprocess(self, outputBlob):
|
66 |
class_id = np.argmax(outputBlob[0])
|
67 |
-
return self._labels[class_id]
|
|
|
|
9 |
import cv2 as cv
|
10 |
|
11 |
class PPResNet:
|
12 |
+
def __init__(self, modelPath, labelPath, backendId=0, targetId=0):
|
13 |
self._modelPath = modelPath
|
|
|
14 |
self._labelPath = labelPath
|
15 |
+
self._backendId = backendId
|
16 |
+
self._targetId = targetId
|
17 |
+
|
18 |
+
self._model = cv.dnn.readNet(self._modelPath)
|
19 |
+
self._model.setPreferableBackend(self._backendId)
|
20 |
+
self._model.setPreferableTarget(self._targetId)
|
21 |
|
22 |
self._inputNames = ''
|
23 |
self._outputNames = ['save_infer_model/scale_0.tmp_0']
|
|
|
40 |
return self.__class__.__name__
|
41 |
|
42 |
def setBackend(self, backend_id):
|
43 |
+
self._backendId = backend_id
|
44 |
+
self._model.setPreferableBackend(self._backendId)
|
45 |
|
46 |
def setTarget(self, target_id):
|
47 |
+
self._targetId = target_id
|
48 |
+
self._model.setPreferableTarget(self._targetId)
|
49 |
|
50 |
def _preprocess(self, image):
|
51 |
image = image.astype(np.float32, copy=False) / 255.0
|
|
|
71 |
|
72 |
def _postprocess(self, outputBlob):
|
73 |
class_id = np.argmax(outputBlob[0])
|
74 |
+
return self._labels[class_id]
|
75 |
+
|