infer.py 3.9 KB
Newer Older
Y
yunyaoXYY 已提交
1 2 3
import fastdeploy as fd
import cv2
import os
Y
yunyaoXYY 已提交
4
from subprocess import run
Y
yunyaoXYY 已提交
5 6 7 8 9 10 11


def parse_arguments():
    import argparse
    import ast
    parser = argparse.ArgumentParser()
    parser.add_argument(
Y
yunyaoXYY 已提交
12 13 14 15 16 17
        "--auto",
        required=True,
        help="Auto download, convert, compile and infer if True")
    parser.add_argument("--model", required=True, help="Path of bmodel")
    parser.add_argument(
        "--config_file", required=True, help="Path of config file")
Y
yunyaoXYY 已提交
18 19 20 21 22 23 24 25
    parser.add_argument(
        "--image", type=str, required=True, help="Path of test image file.")
    parser.add_argument(
        "--topk", type=int, default=1, help="Return topk results.")

    return parser.parse_args()


Y
yunyaoXYY 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
def download():
    cmd_str = 'wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz'
    jpg_str = 'wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg'
    tar_str = 'tar xvf ResNet50_vd_infer.tgz'
    if not os.path.exists('ResNet50_vd_infer.tgz'):
        run(cmd_str, shell=True)
    if not os.path.exists('ILSVRC2012_val_00000010.jpeg'):
        run(jpg_str, shell=True)
    run(tar_str, shell=True)


def paddle2onnx():
    cmd_str = 'paddle2onnx --model_dir ResNet50_vd_infer \
            --model_filename inference.pdmodel \
            --params_filename inference.pdiparams \
            --save_file ResNet50_vd_infer.onnx \
            --enable_dev_version True'

    print(cmd_str)
    run(cmd_str, shell=True)


def mlir_prepare():
    mlir_path = os.getenv("MODEL_ZOO_PATH")
    mlir_path = mlir_path[:-13]
    cmd_list = [
        'mkdir ResNet50', 'cp -rf ' + os.path.join(
            mlir_path, 'regression/dataset/COCO2017/') + ' ./ResNet50',
        'cp -rf ' + os.path.join(mlir_path,
                                 'regression/image/') + ' ./ResNet50',
        'cp ResNet50_vd_infer.onnx ./ResNet50/', 'mkdir ./ResNet50/workspace'
    ]
    for str in cmd_list:
        print(str)
        run(str, shell=True)


def onnx2mlir():
    cmd_str = 'model_transform.py \
        --model_name ResNet50_vd_infer \
        --model_def ../ResNet50_vd_infer.onnx \
        --input_shapes [[1,3,224,224]] \
        --mean 0.0,0.0,0.0 \
        --scale 0.0039216,0.0039216,0.0039216 \
        --keep_aspect_ratio \
        --pixel_format rgb \
        --output_names save_infer_model/scale_0.tmp_1 \
        --test_input ../image/dog.jpg \
        --test_result ./ResNet50_vd_infer_top_outputs.npz \
        --mlir ./ResNet50_vd_infer.mlir'

    print(cmd_str)
    os.chdir('./ResNet50/workspace/')
    run(cmd_str, shell=True)
    os.chdir('../../')


def mlir2bmodel():
    cmd_str = 'model_deploy.py \
        --mlir ./ResNet50_vd_infer.mlir \
        --quantize F32 \
        --chip bm1684x \
        --test_input ./ResNet50_vd_infer_in_f32.npz \
        --test_reference ./ResNet50_vd_infer_top_outputs.npz \
        --model ./ResNet50_vd_infer_1684x_f32.bmodel'

    print(cmd_str)
    os.chdir('./ResNet50/workspace')
    run(cmd_str, shell=True)
    os.chdir('../../')


Y
yunyaoXYY 已提交
98 99
args = parse_arguments()

Y
yunyaoXYY 已提交
100 101 102 103 104 105 106 107
if (args.auto):
    download()
    paddle2onnx()
    mlir_prepare()
    onnx2mlir()
    mlir2bmodel()

# config runtime and load the model
Y
yunyaoXYY 已提交
108 109 110
runtime_option = fd.RuntimeOption()
runtime_option.use_sophgo()

Y
yunyaoXYY 已提交
111
model_file = './ResNet50/workspace/ResNet50_vd_infer_1684x_f32.bmodel' if args.auto else args.model
Y
yunyaoXYY 已提交
112
params_file = ""
Y
yunyaoXYY 已提交
113 114
config_file = './ResNet50_vd_infer/inference_cls.yaml' if args.auto else args.config_file
image_file = './ILSVRC2012_val_00000010.jpeg' if args.auto else args.image
Y
yunyaoXYY 已提交
115 116 117 118 119 120 121
model = fd.vision.classification.PaddleClasModel(
    model_file,
    params_file,
    config_file,
    runtime_option=runtime_option,
    model_format=fd.ModelFormat.SOPHGO)

Y
yunyaoXYY 已提交
122 123
# predict the results of image classification
im = cv2.imread(image_file)
Y
yunyaoXYY 已提交
124 125
result = model.predict(im, args.topk)
print(result)