|
| 1 | +import fastdeploy as fd |
| 2 | +import cv2 |
| 3 | +import os |
| 4 | + |
| 5 | + |
| 6 | +def parse_arguments(): |
| 7 | + import argparse |
| 8 | + import ast |
| 9 | + parser = argparse.ArgumentParser() |
| 10 | + parser.add_argument( |
| 11 | + "--model_dir", |
| 12 | + default=None, |
| 13 | + help="Path of PaddleDetection model directory") |
| 14 | + parser.add_argument( |
| 15 | + "--image", default=None, help="Path of test image file.") |
| 16 | + parser.add_argument( |
| 17 | + "--device", |
| 18 | + type=str, |
| 19 | + default='cpu', |
| 20 | + help="Type of inference device, support 'cpu' or 'gpu'.") |
| 21 | + parser.add_argument( |
| 22 | + "--use_trt", |
| 23 | + type=ast.literal_eval, |
| 24 | + default=False, |
| 25 | + help="Wether to use tensorrt.") |
| 26 | + return parser.parse_args() |
| 27 | + |
| 28 | + |
| 29 | +def build_option(args): |
| 30 | + option = fd.RuntimeOption() |
| 31 | + |
| 32 | + if args.device.lower() == "gpu": |
| 33 | + # option.use_gpu() |
| 34 | + print( |
| 35 | + """GPU inference with Backend::Paddle in python has not been supported yet. \ |
| 36 | + \nWill ignore this option.""") |
| 37 | + |
| 38 | + if args.use_trt: |
| 39 | + # TODO(qiuyanjun): may remove TRT option |
| 40 | + # Backend::TRT has not been supported yet. |
| 41 | + print( |
| 42 | + """Backend::TRT has not been supported yet, will ignore this option.\ |
| 43 | + \nPaddleDetection/MaskRCNN has only support Backend::Paddle now.""" |
| 44 | + ) |
| 45 | + |
| 46 | + if args.device.lower() == "kunlunxin": |
| 47 | + option.use_kunlunxin() |
| 48 | + |
| 49 | + if args.device.lower() == "ascend": |
| 50 | + option.use_ascend() |
| 51 | + |
| 52 | + return option |
| 53 | + |
| 54 | + |
| 55 | +args = parse_arguments() |
| 56 | + |
| 57 | +if args.model_dir is None: |
| 58 | + model_dir = fd.download_model(name='mask_rcnn_r50_1x_coco') |
| 59 | +else: |
| 60 | + model_dir = args.model_dir |
| 61 | + |
| 62 | +model_file = os.path.join(model_dir, "model.pdmodel") |
| 63 | +params_file = os.path.join(model_dir, "model.pdiparams") |
| 64 | +config_file = os.path.join(model_dir, "infer_cfg.yml") |
| 65 | + |
| 66 | +# 配置runtime,加载模型 |
| 67 | +runtime_option = build_option(args) |
| 68 | +model = fd.vision.detection.MaskRCNN( |
| 69 | + model_file, params_file, config_file, runtime_option=runtime_option) |
| 70 | + |
| 71 | +image_file_path = "../dataset/coco/val2017" |
| 72 | +annotation_file_path = "../dataset/coco/annotations/instances_val2017.json" |
| 73 | + |
| 74 | +res = fd.vision.evaluation.eval_detection(model, image_file_path, |
| 75 | + annotation_file_path) |
| 76 | +print(res) |
0 commit comments