模型推理

  1. root用户远程登录开发者套件,将在PC训练模型获得的压缩包上传到开发者套件,上传方法可参见Windows与开发者套件的文件传输,进入压缩包上传目录执行以下命令解压压缩包。

    tar -zxvf infer_project.tar.gz

  2. 进入解压缩后目录执行以下命令将Windows格式文件转换为Unix、Linux格式。

    cd infer_project/ && dos2unix `find .` 

    若提示命令dos2unix not found,请使用apt install dos2unix命令安装。

  3. 进入解压缩目录执行如下命令进行模型转换,获得om模型。

    python3 onnx2om.py

    模型转换成功回显如下:

    bash common/onnx2om.sh --model=yolov5s --bs=1 --calib_bs=1 --soc=Ascend310B4 --type=fp16
    === onnx2om args ===
     version: 7.0
     model: yolov5s
     bs: 1
     type: fp16
     output_dir: output
     soc: Ascend310B4
    Starting onnx导出om模型(无后处理)
    ATC start working now, please wait for a moment.
    .................................ATC run success, welcome to the next use.
    onnx导出om模型 Success

  4. 执行推理。

    python3 om_infer.py --model=output/yolov5s_bs1.om --eval --visible 

    推理成功回显如下:

    [INFO] acl init success
    [INFO] open device 0 success
    [INFO] load model output/yolov5s_bs1.om success
    [INFO] create model description success
    100%|█████████████████████████████████████████████| 5/5 [00:02<00:00,  2.18it/s]
    Average infer time: 116.717 ms / 1 image
    ...
    get 5 images
    Running per image evaluation...
    Evaluate annotation type *bbox*
    DONE (t=0.06s).
    Accumulating evaluation results...
    DONE (t=0.02s).
     Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.579
     ...
     Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.652
    {'bbox_mAP': 0.579, 'bbox_mAP_50': 0.94, 'bbox_mAP_75': 0.71, 'bbox_mAP_s': -1.0, 'bbox_mAP_m': 0.6, 'bbox_mAP_l': 0.583, 'bbox_mAP_copypaste': '0.579 0.940 0.710 -1.000 0.600 0.583'}
    [INFO] unload model success, model Id is 1
    [INFO] end to destroy context
    [INFO] end to reset device is 0
    [INFO] end to finalize acl
    • model: 模型文件目录。
    • eval: 开启模型评估,自动计算在测试集上的精度。
    • visible: 保存模型在测试集上的推理结果。

    推理结果图如图1图2所示。

    图1 查看推理结果1
    图2 查看推理结果2