5.1.4. 模型效果演示

5.1.4.1. 模型效果测试

MLIR发布包中有用python写好的yolov5用例, 源码路径 $TPUC_ROOT/python/samples/detect_yolov5.py , 下面将使用示例代码分别来验证onnx/f16/int8的执行结果。

onnx模型的执行方式如下, 得到 dog_onnx.jpg :

$ detect_yolov5.py \
    --input ../image/dog.jpg \
    --model ../yolov5s.onnx \
    --output dog_onnx.jpg

对于bmodel模型文件,由于开发环境没有相关设备,因此将会使用x86的主机cpu模拟运行bmodel模型,仅用于准确性验证。

f16 bmodel的执行方式如下, 得到 dog_f16.jpg :

$ detect_yolov5.py \
    --input ../image/dog.jpg \
    --model yolov5s_1688_f16.bmodel \
    --output dog_f16.jpg

int8对称bmodel的执行方式如下, 得到 dog_int8_sym.jpg :

$ detect_yolov5.py \
    --input ../image/dog.jpg \
    --model yolov5s_1688_int8_sym_2core.bmodel \
    --output dog_int8_sym.jpg

对比结果如下:

../_images/yolov5s.png

图 5.4 TPU-MLIR对YOLOv5s编译效果对比

由于运行环境不同, 最终的效果和精度与 yolov5s_result 会有些差异。

5.1.4.2. 模型性能测试

以下操作需要在边缘服务器内执行,默认的出厂环境或者刷机完成后,可以使用 bmrt_test 来测试编译出的 bmodel 的正确性及性能。

可以根据 ``bmrt_test`` 输出的性能结果, 来估算模型最大的fps, 来选择合适的模型

# 下面测试上面编译出的bmodel
# --bmodel参数后面接bmodel文件,

$ cd $TPUC_ROOT/../model_yolov5s/workspace
# BM1688
$ bmrt_test --bmodel yolov5s_1688_f16.bmodel
$ bmrt_test --bmodel yolov5s_1688_int8_sym_2core.bmodel

以最后一个命令输出为例(此处对日志做了部分截断处理):

 1[BMRT][load_bmodel:1501] INFO:pre net num: 0, load net num: 1
 2[BMRT][show_net_info:1810] INFO: ########################
 3[BMRT][show_net_info:1814] INFO: NetName: yolov5s_v6.1_3output, Index=0
 4[BMRT][show_net_info:1817] INFO: ---- stage 0 ----
 5[BMRT][show_net_info:1827] INFO:   Input 0) 'images' shape=[ 1 3 640 640 ] dtype=FLOAT32 scale=1 zero_point=0 device_id=0
 6[BMRT][show_net_info:1838] INFO:   Output 0) 'output_Transpose_f32' shape=[ 1 3 80 80 85 ] dtype=FLOAT32 scale=1 zero_point=0 device_id=0
 7[BMRT][show_net_info:1838] INFO:   Output 1) '365_Transpose_f32' shape=[ 1 3 40 40 85 ] dtype=FLOAT32 scale=1 zero_point=0 device_id=0
 8[BMRT][show_net_info:1838] INFO:   Output 2) '385_Transpose_f32' shape=[ 1 3 20 20 85 ] dtype=FLOAT32 scale=1 zero_point=0 device_id=0
 9[BMRT][show_net_info:1841] INFO: ########################
10[BMRT][bmrt_test:844] INFO:==> running network #0, name: yolov5s_v6.1_3output, loop: 0
11[BMRT][bmrt_test:899] INFO:reading input #0, bytesize=4915200
12[BMRT][print_array:755] INFO:  --> input_data: < 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... > len=1228800
13[BMRT][bmrt_test:1030] INFO:reading output #0, bytesize=6528000
14[BMRT][print_array:755] INFO:  --> output ref_data: < 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... > len=1632000
15[BMRT][bmrt_test:1030] INFO:reading output #1, bytesize=1632000
16[BMRT][print_array:755] INFO:  --> output ref_data: < 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... > len=408000
17[BMRT][bmrt_test:1030] INFO:reading output #2, bytesize=408000
18[BMRT][print_array:755] INFO:  --> output ref_data: < 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... > len=102000
19[BMRT][bmrt_test:1065] INFO:net[yolov5s_v6.1_3output] stage[0], launch total time is 7083 us (npu 6387 us [6387 us, 2391 us], cpu 696 us), (issue 3000 us, sync 4136 us)
20[BMRT][bmrt_test:1068] INFO:+++ The network[yolov5s_v6.1_3output] stage[0] output_data +++
21[BMRT][print_array:755] INFO:output data #0 shape: [1 3 80 80 85 ] < -0.145839  ...
22[BMRT][print_array:755] INFO:output data #1 shape: [1 3 40 40 85 ] < -0.726845  ...
23[BMRT][print_array:755] INFO:output data #2 shape: [1 3 20 20 85 ] < 0.563809  ...
24[BMRT][bmrt_test:1129] INFO:load input time(s): 0.018420
25[BMRT][bmrt_test:1130] INFO:calculate  time(s): 0.007136
26[BMRT][bmrt_test:1131] INFO:get output time(s): 0.020361
27[BMRT][bmrt_test:1132] INFO:compare    time(s): 0.021095

从上面输出可以看到以下信息:

  1. 05-08行是bmodel的网络输入输出信息

  2. 19行是在智能视觉深度学习处理器上运行的时间, 其中智能视觉深度学习处理器用时7136us, central processing unit用时696us。这里central processing unit用时主要是指在HOST端调用等待时间

  3. 24行是加载数据到NPU的DDR的时间

  4. 25行中的calculate time表示的模型推理的时间

  5. 26行是输出数据从TPU取回到CPU的时间