特别说明:参考地平线的官方文档,如有侵权告知删,谢谢。

完整转换代码参见github。代码

1 工具链

  地平线工具链支持使用Docker和手工安装两种方式,“地平线强烈建议使用 Docker 方式,以避免在安装过程中出现问题”。
  地平线提供的Docker有CPU版本的和GPU版本的,官方提供的下载链接工具链。
  地平线工具链目前支持 Caffe、ONNX的加载转换。以下以ONNX的转为例进行说明,使用的工具链版本v1.11.4。以下示例以 ONNX 进行说明。

通过Docker镜像安装

# 镜像 load
sudo docker load -i docker_cpu_openexplorer_centos_7_xj3_v1.11.4.tar
# 启动镜像
sudo docker run -v `pwd`/convert_model:/data -it hub.hobot.cc/aitools/ai_toolchain_centos_7_xj3:v1.11.4

2 模型转换

  地平线工具链的模型完整开发过程,需要经过 浮点模型准备、模型验证、模型转换、 性能评估 和 精度评估 共几个重要阶段。

2.1 浮点模型准备

准备好 caffemodel 或 onnx 模型, onnx 目前主要支持的 opset 版本是opset10和opset11。

2.2 验证模型

  为了确保模型能顺利在地平线平台高效运行,模型中所使用的算子需要符合平台的算子约束。

  每个算子具体的参数限制,参考 supported_op_list_and_restrictions/ 路径下《supported_op_list_and_restrictions_release》Excel表格。

  考虑到地平线支持的算子较多,为了避免人工逐条校对的麻烦, 提供了 hb_mapper checker 工具用于验证模型所使用算子的支持情况。

使用 hb_mapper checker 工具验证模型

hb_mapper checker 参数解释:
--model-type
用于指定检查输入的模型类型,目前只支持设置 caffe 或者 onnx。--march
用于指定需要适配的AI芯片类型,X/J3芯片应设置为 bernoulli2。--proto
此参数仅在 model-type 指定 caffe 时有效,取值为Caffe模型的prototxt文件名称。--model
在 model-type 被指定为 caffe 时,取值为Caffe模型的caffemodel文件名称。 在 model-type 被指定为 onnx 时,取值为ONNX模型文件名称。--input-shape
可选参数,明确指定模型的输入shape。 取值为 {input_name} {NxHxWxC/NxCxHxW} ,input_name 与shape之间以空格分隔。 例如模型输入名称为 data1,输入shape为 [1,224,224,3], 则配置应该为 --input_shape data1 1x224x224x3。 如果此处配置shape与模型内shape信息不一致,以此处配置为准。***说明***
注意一个 --input-shape 只接受一个name和shape组合,如果您的模型有多个输入节点, 在命令中多次配置 --input-shape 参数即可。--output
可选参数,接受设置值为一个日志文件名称。 指定该参数情况下,检查的结果将输出到指定的日志文件。

使用demo 中的 01_chek.sh 示例,打印日志中有模型的基本信息,和 BPU 支持性,那些层在 BPU 上运算,那些层在CPU上运行,考虑到效率推荐将所有的层都调整层在 BPU 上运行。

由于示例中的模型是经过适配,所有的层都是在 BPU 上运行。

2.3 模型转换

2.3.1 校准数据准备

  在进行模型转换时,校准阶段会需要100份左右的样本输入,每一份样本都是一个独立的数据文件。 为了确保转换后模型的精度效果,这些校准样本来自于训练模型使用的训练集或验证集,场景覆盖尽量丰富, 不要使用非常少见的异常样本,例如纯色图片、不含任何检测或分类目标的图片等。

使用 02_preprocess.sh 进行数据转换
  下一步模型转换所用的换配置文件中的 preprocess_on 参数,该参数启用和关闭状态下分别对应了两种不同的预处理样本要求。preprocess_on 关闭状态下,需要把取自训练集/验证集的样本做与inference前一样的前处理, 处理完后的校准样本会与原始模型具备一样的数据类型(input_type_train)、尺寸(input_shape)和 layout(input_layout_train)。

以下用 rgb 格式输入为例,预处理的数据放在cal_data文件下。预处理的具体代码在 data_preprocess.py 文件中。

特别说明:在后续的示例中模型使用的实际输入为 bgr 图像,示例中直接在cal_dataw文件夹下放入的是.jpg图像。

2.3.2 使用 hb_mapper makertbin 工具转换模型

hb_mapper makertbin参数解释:
--model-type
用于指定转换输入的模型类型,目前支持设置 caffe 或者 onnx。--config
模型编译的配置文件,内容采用yaml格式,文件名使用.yaml后缀。一份完整的配置文件模板如下:说明:
此处配置文件仅作展示,在实际模型配置文件中 caffe_model 与 onnx_model 两个参数只存在其中之一。 即,要么是Caffe模型,要么是ONNX模型。

使用 03_build.sh 脚本进行转换

转换完成示例:打印日志
INFO Convert to runtime bin file successfully!
INFO End Model Convert

生成的模型

config 文件说明:

# 模型转化相关的参数
# ------------------------------------
# model conversion related parameters
model_parameters:# Onnx浮点网络数据模型文件# -----------------------------------------------------------# the model file of floating-point ONNX neural network dataonnx_model: './model/yolov5_p6_512x512_6head.onnx'# 适用BPU架构# --------------------------------# the applicable BPU architecturemarch: "bernoulli2"# 指定模型转换过程中是否输出各层的中间结果,如果为True,则输出所有层的中间输出结果,# --------------------------------------------------------------------------------------# specifies whether or not to dump the intermediate results of all layers in conversion# if set to True, then the intermediate results of all layers shall be dumpedlayer_out_dump: False# 日志文件的输出控制参数,# debug输出模型转换的详细信息# info只输出关键信息# warn输出警告和错误级别以上的信息# ----------------------------------------------------------------------------------------# output control parameter of log file(s),# if set to 'debug', then details of model conversion will be dumped# if set to 'info', then only important imformation will be dumped# if set to 'warn', then information ranked higher than 'warn' and 'error' will be dumpedlog_level: 'debug'# 模型转换输出的结果的存放目录# -----------------------------------------------------------# the directory in which model conversion results are storedworking_dir: 'model_output'# 模型转换输出的用于上板执行的模型文件的名称前缀# -----------------------------------------------------------------------------------------# model conversion generated name prefix of those model files used for dev board executionoutput_model_file_prefix: 'yolov5_p6_512x512'# 模型输入相关参数, 若输入多个节点, 则应使用';'进行分隔, 使用默认缺省设置则写None
# --------------------------------------------------------------------------
# model input related parameters,
# please use ";" to seperate when inputting multiple nodes,
# please use None for default setting
input_parameters:# (选填) 模型输入的节点名称, 此名称应与模型文件中的名称一致, 否则会报错, 不填则会使用模型文件中的节点名称# --------------------------------------------------------------------------------------------------------# (Optional) node name of model input,# it shall be the same as the name of model file, otherwise an error will be reported,# the node name of model file will be used when left blankinput_name: ""# 网络实际执行时,输入给网络的数据格式,包括 nv12/rgb/bgr/yuv444/gray/featuremap,# ------------------------------------------------------------------------------------------# the data formats to be passed into neural network when actually performing neural network# available options: nv12/rgb/bgr/yuv444/gray/featuremap,input_type_rt: 'rgb'# 网络实际执行时输入的数据排布, 可选值为 NHWC/NCHW# 若input_type_rt配置为nv12,则此处参数不需要配置# ------------------------------------------------------------------# the data layout formats to be passed into neural network when actually performing neural network, available options: NHWC/NCHW# If input_type_rt is configured as nv12, then this parameter does not need to be configuredinput_layout_rt: 'NCHW'# 网络训练时输入的数据格式,可选的值为rgb/bgr/gray/featuremap/yuv444# --------------------------------------------------------------------# the data formats in network training# available options: rgb/bgr/gray/featuremap/yuv444input_type_train: 'rgb'# 网络训练时输入的数据排布, 可选值为 NHWC/NCHW# ------------------------------------------------------------------# the data layout in network training, available options: NHWC/NCHWinput_layout_train: 'NCHW'# (选填) 模型网络的输入大小, 以'x'分隔, 不填则会使用模型文件中的网络输入大小,否则会覆盖模型文件中输入大小# -------------------------------------------------------------------------------------------# (Optional)the input size of model network, seperated by 'x'# note that the network input size of model file will be used if left blank# otherwise it will overwrite the input size of model fileinput_shape: ''# 网络实际执行时,输入给网络的batch_size, 默认值为1# ---------------------------------------------------------------------# the data batch_size to be passed into neural network when actually performing neural network, default value: 1#input_batch: 1# 网络输入的预处理方法,主要有以下几种:# no_preprocess 不做任何操作# data_mean 减去通道均值mean_value# data_scale 对图像像素乘以data_scale系数# data_mean_and_scale 减去通道均值后再乘以scale系数# -------------------------------------------------------------------------------------------# preprocessing methods of network input, available options:# 'no_preprocess' indicates that no preprocess will be made # 'data_mean' indicates that to minus the channel mean, i.e. mean_value# 'data_scale' indicates that image pixels to multiply data_scale ratio# 'data_mean_and_scale' indicates that to multiply scale ratio after channel mean is minusednorm_type: 'data_scale'# 图像减去的均值, 如果是通道均值,value之间必须用空格分隔# --------------------------------------------------------------------------# the mean value minused by image# note that values must be seperated by space if channel mean value is usedmean_value: ''# 图像预处理缩放比例,如果是通道缩放比例,value之间必须用空格分隔# ---------------------------------------------------------------------------# scale value of image preprocess# note that values must be seperated by space if channel scale value is usedscale_value: 0.003921568627451# 模型量化相关参数
# -----------------------------
# model calibration parameters
calibration_parameters:# 模型量化的参考图像的存放目录,图片格式支持Jpeg、Bmp等格式,输入的图片# 应该是使用的典型场景,一般是从测试集中选择20~100张图片,另外输入# 的图片要覆盖典型场景,不要是偏僻场景,如过曝光、饱和、模糊、纯黑、纯白等图片# 若有多个输入节点, 则应使用';'进行分隔# -------------------------------------------------------------------------------------------------# the directory where reference images of model quantization are stored# image formats include JPEG, BMP etc.# should be classic application scenarios, usually 20~100 images are picked out from test datasets# in addition, note that input images should cover typical scenarios# and try to avoid those overexposed, oversaturated, vague, # pure blank or pure white images# use ';' to seperate when there are multiple input nodescal_data_dir: './cal_data'# 如果输入的图片文件尺寸和模型训练的尺寸不一致时,并且preprocess_on为true,# 则将采用默认预处理方法(skimage resize),# 将输入图片缩放或者裁减到指定尺寸,否则,需要用户提前把图片处理为训练时的尺寸# ---------------------------------------------------------------------------------# In case the size of input image file is different from that of in model training# and that preprocess_on is set to True,# shall the default preprocess method(skimage resize) be used# i.e., to resize or crop input image into specified size# otherwise user must keep image size as that of in training in advancepreprocess_on: True# 模型量化的算法类型,支持kl、max、default、load,通常采用default即可满足要求, 若为QAT导出的模型, 则应选择load# ----------------------------------------------------------------------------------# types of model quantization algorithms, usually default will meet the need# available options:kl, max, default and load# if converted model is quanti model exported from QAT , then choose `load`calibration_type: 'default'# 编译器相关参数
# ----------------------------
# compiler related parameters
compiler_parameters:# 编译策略,支持bandwidth和latency两种优化模式;# bandwidth以优化ddr的访问带宽为目标;# latency以优化推理时间为目标# -------------------------------------------------------------------------------------------# compilation strategy, there are 2 available optimization modes: 'bandwidth' and 'lantency'# the 'bandwidth' mode aims to optimize ddr access bandwidth# while the 'lantency' mode aims to optimize inference durationcompile_mode: 'latency'# 设置debug为True将打开编译器的debug模式,能够输出性能仿真的相关信息,如帧率、DDR带宽占用等# -----------------------------------------------------------------------------------# the compiler's debug mode will be enabled by setting to True# this will dump performance simulation related information# such as: frame rate, DDR bandwidth usage etc.debug: False# 编译模型指定核数,不指定默认编译单核模型, 若编译双核模型,将下边注释打开即可# -------------------------------------------------------------------------------------# specifies number of cores to be used in model compilation # as default, single core is used as this value left blank# please delete the "# " below to enable dual-core mode when compiling dual-core model# core_num: 2# 优化等级可选范围为O0~O3# O0不做任何优化, 编译速度最快,优化程度最低,# O1-O3随着优化等级提高,预期编译后的模型的执行速度会更快,但是所需编译时间也会变长。# 推荐用O2做最快验证# ----------------------------------------------------------------------------------------------------------# optimization level ranges between O0~O3# O0 indicates that no optimization will be made # the faster the compilation, the lower optimization level will be# O1-O3: as optimization levels increase gradually, model execution, after compilation, shall become faster# while compilation will be prolonged# it is recommended to use O2 for fastest verificationoptimize_level: 'O3'

2.3.3 模型性能分析与调优

  本节介绍了使用地平线提供的工具评估模型性能,这些工具得到的都是与实际执行基本无异的性能效果,如果此阶段发现评估结果不符合预期,强烈建议您尽量在此阶段根据地平线的优化建议解决性能问题。

# 使用 hb_perf
hb_perf yolov5_p6_512x512.bin


执行结果

详细分析方式参考官方提供的文档,这里就不作展开。

2.3.4 模型精度分析与调优(产出物进行推理)

如何使用模型转换的产出物进行推理。前文提到模型成功转换的产出物包括以下四个部分:

***_original_float_model.onnx
***_optimized_float_model.onnx
***_quantized_model.onnx
***.bin

虽然bin模型才是将部署到AI芯片的模型,考虑到方便在Ubuntu/CentOS开发机上完成精度评测, 提供了**_quantized_model.onnx完成这个精度评测的过程。quantized模型已经完成了量化,与最后的bin模型具有一致的精度效果。

本示例对后处理代码是进行了重写的,具体参考github。完整代码代码链接

# 仿真
04_inference.sh # 输出检测结果 result.jpg

参考文献

[1] 《Horizon AI Toolchain User Guide》
[2] https://developer.horizon.ai/forumDetail/136488103547258769

地平线Horizon模型转换和PC端仿真测试相关推荐

  1. 瑞芯微RKNN模型转换和PC端仿真

    1 模型转换 特别说明:以下内容参考来自rknn官网文档 Rockchip_User_Guide_RKNN_Toolkit2_CN-1.3.0.pdf.如有侵权告知删除. 完整代码放在github上. ...

  2. javascript 微信企业号APP应用URL转换成PC端可正常访问的URL

    时间过得飞快,准备写些东西的时候才发现自己快一年半没有写blog了,写这些东西只是为了记录下来自己走过的坑,为下次遇见同类的问题能够迅速解决,毕竟人脑有限,存储下来以备不时之需. 一:背景(功能需求) ...

  3. yolov8 瑞芯微RKNN和地平线Horizon芯片仿真测试部署

    特别说明:参考官方开源的yolov8代码.瑞芯微官方文档.地平线的官方文档,如有侵权告知删,谢谢. 模型和完整仿真测试代码,放在github上参考链接 模型和代码. 跟上技术的步伐,yolov8 首个 ...

  4. yolov7 瑞芯微RKNN和地平线Horizon芯片仿真测试部署

    特别说明:参考官方开源的yolov7代码.瑞芯微官方文档.地平线的官方文档,如有侵权告知删,谢谢. 模型和完整仿真测试代码,放在github上参考链接 模型和代码. 1 模型和训练   训练代码参考官 ...

  5. yolov8seg 瑞芯微RKNN芯片、地平线Horizon芯片、TensorRT部署

      特别说明:参考官方开源的yolov8代码.瑞芯微官方文档.地平线的官方文档,如有侵权告知删,谢谢.   模型.测试图像.测试结果.完整代码,放在github上,参考链接 模型和代码.   由于之前 ...

  6. 【地平线开发板 模型转换】将pytorch生成的onnx模型转换成.bin模型

    文章目录 1 获取onnx模型 2 启动docker容器 3 onnx模型检查 3.1 为什么要检查? 3.2 如何操作 4 图像数据预处理 4.1 一些问题的思考 4.2 图片挑选与放置 4.2 使 ...

  7. 软件测试常见问题 开发模型 PC端qq登录测试用例 BUG的相关问题 测试用例设计的常用方法

    测试基础 1.软件测试和软件研发的区别 2.软件测试和软件调试的区别 软件测试在不同公司的定位 一个优秀的软件测试人员所具备的素质 3. 什么是需求 4.PC端qq登录测试用例 5.测试用例 5.1 ...

  8. html px转换,pc端px转换为rem针对屏幕分辨率进行页面适配

    常用的pc端网站适配方案是什么?用的最多的大概就是父元素按照设计图的宽度进行固定宽度,margin:0 auto居中,两边留白.但是有的设计图不适合这样两边留白的适配方案. 最近接手了一个pc端的项目 ...

  9. yolov5模型转换(pt=>onnx=>rknn)和板端验证测试

    测试环境说明: (1)由于模型转换工具需要onnx版本和rknn的tool工具需要的版本相互矛盾需要创建量开发环境,当前测试转换的模型是yolov5_v5.0的模型 (2)由于在搭建开发环境时还存在部 ...

最新文章

  1. JavaScript的“ this”通过成立一个高中乐队来解释
  2. Struts1.x系列教程(20):使用EventDispatchAction类处理一个form多个submit
  3. 字符设备驱动代码完整分析
  4. mysql创建非聚集索引_一文看懂聚集索引和非聚集索引的区别
  5. 【PAT乙级】1007 素数对猜想 (20 分)
  6. QT的QFileSelector类的使用
  7. 关于Chrome支持http header最大长度限制的讨论
  8. 善用工具_如何善用色彩心理学
  9. Magicodes.IE之快速导出Excel
  10. 静态与非静态(转改)
  11. java快速获取大图片的分辨率(大图片格式JPG,tiff ,eg)
  12. informatica中元数据管理
  13. yii2 js加载顺序呢
  14. 糖果传递(信息学奥赛一本通-T1432)
  15. java锁的膨胀过程和优化
  16. 大白菜无法打开计算机硬盘,关于大白菜pe一键装机找不到硬盘怎么办的问题解答 情况一:分区不合理 解决方案:使用大白菜diskgenius工具...
  17. sdk 今日头条_今日头条大数据分析平台艰辛成长路
  18. python挑选以元音字母开头的单词,在Python中查找以特定字母开头的列表元素
  19. canvas教程13-使用图片
  20. 冲激函数与卷积的多次邂逅

热门文章

  1. 机器学习应用——无监督学习(实例:31省市居民家庭消费调查学生上网时间分布聚类鸢尾花数据人脸数据特征提取)
  2. Java、JSP汽车销售管理系统的设计与实现
  3. dnsmasq.conf局域网配置,不让下面员工访问
  4. 中移动“屠龙”术一刀封喉 免费WAP祸福莫名 【时评】
  5. 浪漫故事:常见HTTP状态码的另类解析
  6. Burpsuite抓取安卓模拟器的包实现app抓包
  7. Opencv项目实战:19 手势控制鼠标
  8. Linux操作系统介绍(简记)
  9. 浅谈SQL Server索引视图(物化视图)以及索引视图与查询重写
  10. 【GIS开发】基于C++绘制三维数字地球Earth(OpenGL、glfw、glut)