12from ppresnet
import PPResNet
15 if v.lower()
in [
'on',
'yes',
'true',
'y',
't']:
17 elif v.lower()
in [
'off',
'no',
'false',
'n',
'f']:
20 raise NotImplementedError
22backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
23targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
24help_msg_backends =
"Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
25help_msg_targets =
"Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
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"
32 print(
'This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
34parser = argparse.ArgumentParser(description=
'Deep Residual Learning for Image Recognition (https://arxiv.org/abs/1512.03385, https://github.com/PaddlePaddle/PaddleHub)')
35parser.add_argument(
'--input',
'-i', type=str, help=
'Path to the input image.')
36parser.add_argument(
'--model',
'-m', type=str, default=
'image_classification_ppresnet50_2022jan.onnx', help=
'Path to the model.')
37parser.add_argument(
'--backend',
'-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
38parser.add_argument(
'--target',
'-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
39parser.add_argument(
'--label',
'-l', type=str, default=
'./imagenet_labels.txt', help=
'Path to the dataset labels.')
40args = parser.parse_args()
42if __name__ ==
'__main__':
44 model = PPResNet(modelPath=args.model, labelPath=args.label, backendId=args.backend, targetId=args.target)
47 image = cv.imread(args.input)
48 image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
49 image = cv.resize(image, dsize=(256, 256))
50 image = image[16:240, 16:240, :]
53 result = model.infer(image)
56 print(
'label: {}'.format(result))