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
- demo.py +27 -24
- ppresnet.py +3 -5
demo.py
CHANGED
@@ -11,36 +11,39 @@ import cv2 as cv
|
|
11 |
|
12 |
from ppresnet import PPResNet
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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='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,
|
36 |
-
|
37 |
-
parser.add_argument('--
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
args = parser.parse_args()
|
40 |
|
41 |
if __name__ == '__main__':
|
|
|
|
|
42 |
# Instantiate ResNet
|
43 |
-
model = PPResNet(modelPath=args.model, backendId=
|
44 |
|
45 |
# Read image and get a 224x224 crop from a 256x256 resized
|
46 |
image = cv.imread(args.input)
|
|
|
11 |
|
12 |
from ppresnet import PPResNet
|
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='Deep Residual Learning for Image Recognition (https://arxiv.org/abs/1512.03385, https://github.com/PaddlePaddle/PaddleHub)')
|
28 |
+
parser.add_argument('--input', '-i', type=str,
|
29 |
+
help='Usage: Set input path to a certain image, omit if using camera.')
|
30 |
+
parser.add_argument('--model', '-m', type=str, default='image_classification_ppresnet50_2022jan.onnx',
|
31 |
+
help='Usage: Set model path, defaults to image_classification_ppresnet50_2022jan.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 |
args = parser.parse_args()
|
41 |
|
42 |
if __name__ == '__main__':
|
43 |
+
backend_id = backend_target_pairs[args.backend_target][0]
|
44 |
+
target_id = backend_target_pairs[args.backend_target][1]
|
45 |
# Instantiate ResNet
|
46 |
+
model = PPResNet(modelPath=args.model, backendId=backend_id, targetId=target_id)
|
47 |
|
48 |
# Read image and get a 224x224 crop from a 256x256 resized
|
49 |
image = cv.imread(args.input)
|
ppresnet.py
CHANGED
@@ -36,12 +36,10 @@ class PPResNet:
|
|
36 |
def name(self):
|
37 |
return self.__class__.__name__
|
38 |
|
39 |
-
def
|
40 |
-
self._backendId =
|
|
|
41 |
self._model.setPreferableBackend(self._backendId)
|
42 |
-
|
43 |
-
def setTarget(self, target_id):
|
44 |
-
self._targetId = target_id
|
45 |
self._model.setPreferableTarget(self._targetId)
|
46 |
|
47 |
def _preprocess(self, image):
|
|
|
36 |
def name(self):
|
37 |
return self.__class__.__name__
|
38 |
|
39 |
+
def setBackendAndTarget(self, backendId, targetId):
|
40 |
+
self._backendId = backendId
|
41 |
+
self._targetId = targetId
|
42 |
self._model.setPreferableBackend(self._backendId)
|
|
|
|
|
|
|
43 |
self._model.setPreferableTarget(self._targetId)
|
44 |
|
45 |
def _preprocess(self, image):
|