onnx模型转换:ERROR: list index out of range

用户您好,请详细描述您所遇到的问题:

系统软件版本: (通过 cat /etc/version 获得)

Ai_Toolchain_Package-release-v1.18.2-OE-v2.4.2

openexplorer/ai_toolchain_centos_7_xj3:v1.15.2


  1. 问题涉及的技术领域: (硬件、操作系统、驱动、其他)

算法模型转换

  1. 问题描述:(尽可能详细的描述在进行什么功能的开发或者测试,发现了什么问题,问题现象,并且提供预期的结果)

* 根据文档进行onnx模型转换时,01_check.sh,02_preporcess.sh已经完成,03_build.sh出现下面的错误

模型输入的要求是Y only (1*1382400),尝试地平线模型转换配置脚本里配置输入数据为gray,input shape设为模型要求的1*1382400,转换脚本会报错:list index out of range,看脚本是像是需要4维,改shape为1*1*1449*960后又提示与从模型内获取的input shape不一致.
input_shape改为下面这三种都出错:

 input_shape: '1x1440x960x1;1x1x3x1'
 input_shape: '1x1382400;1x3'
 input_shape: '1x1x1440x960;1x1x1x3'

input format

single image W = 1440 H = 960 luminance channel (Y) from the planar YUV420 format:
full input size is 1440 * 960 = 1382400
normalized ranging from 0.0 to 1.0 in float32 (onnx runner) or ranging from 0 to 255 in uint8 (snpe runner)
camera calibration angles (roll, pitch, yaw) from liveCalibration: 3 x float32 inputs

错误日志

2023-08-08 10:50:41,313 ERROR Input shape of input_img has a dimension [1, 1440, 960, 1] different from [1, 1382400] which is parsered from onnx.
2023-08-08 10:50:41,326 ERROR *** ERROR-OCCUR-DURING {horizon_nn.build_onnx} ***
2023-08-08 10:50:41,327 INFO [Tue Aug  8 10:50:41 2023] End to parse the onnx model.
2023-08-08 10:50:41,327 INFO [Tue Aug  8 10:50:41 2023] End to Horizon NN Model Convert.

其他日志见附件

您好,地平线工具链在持续迭代优化,为了给您提供更好的服务,希望您能抽出3分钟左右的时间,将您在使用工具链期间的感受和建议告诉我们,您的宝贵意见对我们很重要,非常感谢!-
问卷链接:地平线算法工具链使用满意度反馈

您好,模型转换使用的yaml配置文件在参数preprocess_on未开启情况下(一般也不建议开启)不会自动对校准数据做预处理以匹配ONNX模型输入尺寸,需要您修改02_preporcess.sh脚本将校准数据处理到对齐ONNX模型输入尺寸并且重新运行才行;-

此外,input_shape配置应和模型真实输入保持一致,不配置的情况下也会自动获取ONNX模型输入

1.PadResizeTransformer(target_size=(1, 1382400), pad_value=0., pad_position=‘bottom_right’),

前处理成(1, 1382400),还是报错:-

2023-08-08 14:08:50,421 INFO Model has 2 inputs according to model file

2023-08-08 14:08:50,421 INFO Input shape [1, 1382400] has length: 2, make sure it is a featuremap input

2023-08-08 14:08:50,421 INFO Input shape [1, 3] has length: 2, make sure it is a featuremap input

2023-08-08 14:08:50,421 ERROR Parse ‘input_parameters’ failed!

2023-08-08 14:08:50,422 ERROR yaml file parse failed. Please double check your config file inputs

2023-08-08 14:08:50,423 ERROR list index out of range-

2.看python脚本:-

File “/usr/local/lib/python3.6/site-packages/horizon_tc_ui/config/mapper_conf_parser.py”, line 598, in _validate_input_type_and_layout-
and input_shape[3] != 1) or (train_layout == “NCHW”-
IndexError: list index out of range-

是需要四维的,但是我的是一维 gray,只取Y方向,因该是不用配置layout,但不配置会报错:-

2023-08-08 14:16:10,129 INFO Model has 2 inputs according to model file

2023-08-08 14:16:10,129 INFO Input shape [1, 1382400] has length: 2, make sure it is a featuremap input

2023-08-08 14:16:10,129 INFO Input shape [1, 3] has length: 2, make sure it is a featuremap input

2023-08-08 14:16:10,129 ERROR Parse ‘input_parameters’ failed!

2023-08-08 14:16:10,129 ERROR yaml file parse failed. Please double check your config file inputs

2023-08-08 14:16:10,131 ERROR Input_layout_rt missing. input 0 input_type_rt is: gray. input_layout_rt is required for this type-

3.关于calibration_parameters:-
cal_data_dir: ‘./calibration_data_gray_uint8;./calibration_data_angle_f32_bin’-
cal_data_type: ‘float32;float32’-
calibration_type: ‘default’-

cal_data_type和input_type_train必须要相同吗?

您好,1.我们这边测试了一下,能够复现您的问题,您将input_type_rt和input_type_train修改为featuremap,此外校准数据后缀修改为.feature再次尝试-

2.cal_data_type是指二进制校准数据的保存格式,为float32/uint8;input_type_train是指输入数据格式,为rgb/bgr/featuremap/…

补充:input_type_rt和input_type_train配置为featuremap后无需关注layout是NHWC/NCHW,工具会自动skip,但是需要配置,否则会报错

1.input_type_rt和input_type_train修改为featuremap后,在板子上运行的时候的输入的格式还能是gray吗?因为模型的原始输入是两个都是一维的

single image W = 1440 H = 960 luminance channel (Y) from the planar YUV420 format:

full input size is 1440 * 960 = 1382400

normalized ranging from 0.0 to 1.0 in float32 (onnx runner) or ranging from 0 to 255 in uint8 (snpe runner)

**camera calibration angles (roll, pitch, yaw) from liveCalibration: 3 x float32 inputs-

2.校准数据是需要**读取图像,将其转换为灰度格式,然后调整其大小为1440x960将图像扁平化为1x1382400的float32的吗?-

3.**input_type_rt和input_type_train修改为featuremap后,**校准数据后缀修改为.gray可以转换为bin文件,这和校准数据后缀修改为.feature有什么不同吗?

我试了一下input_type_rt和input_type_train修改为featuremap,此外校准数据后缀修改为.feature,还是报错:-
error info:-
2023-08-08 16:56:23,043 INFO First calibration picture md5:

107f1fd6f061b580ed14745b15fa1618 /open_explorer/ai_toolchain/horizon_model_convert_sample/02_selfdrive/01_dmonitor/mapper/calibration_data_angle_f32_bin/001.bin

2023-08-08 16:56:23,051 INFO *******************************************

2023-08-08 16:56:23,053 ERROR load cal data for input ‘input_img’ error

2023-08-08 16:56:23,055 ERROR cannot reshape array of size 345600 into shape (1,1382400)-
file shape info:-
[root@b0e8718db1d1 calibration_data_featuremap_uint8]# ls -al

total 135208

drwxr-xr-x 2 root root 4096 Aug 8 16:53 .

drwxrwxrwx 14 1010 1011 4096 Aug 8 16:53 ..

-rw-r–r-- 1 root root 1382400 Aug 8 16:53 00000.feature

-rw-r–r-- 1 root root 1382400 Aug 8 16:53 00001.feature-

因为02.preprocess.sh里边是针对生成gray的,需要改为生成featuremap吗?

您好,1.修改为featuremap后,板端运行输入格式就不能是gray了,参考 4.1.1.6. 模型量化与编译 — Horizon Open Explorer 转换内部过程解读;如果您希望板端输入gray格式的数据,建议您调整一下模型输入例如为1x1440x960x1,因为input_type_train/rt=gray需要指定四维

2.对的,需要您在02_preprocess.sh脚本里修改–pic_ext为.feature,并且需要将uint8的校准数据除255转为float32

3.后缀为.gray以uint8保存为二进制bin文件,后缀为.feature以float32保存为二进制bin文件

对的,这里的报错是因为校准数据尺寸和模型输入不匹配,需要调整一下校准数据的预处理。如果不是特别需求,建议您使用Resize而不是PadResize,因为使用Padresize resize后的图像映射回原图需要手动减去Padding的部分

您好,我按照配置:-

calibration_parameters:-
cal_data_dir: ‘./calibration_data_featuremap_uint8;./calibration_data_angle_f32_bin’-
cal_data_type: ‘uint8;float32’-
calibration_type: ‘default’-

input_parameters:-
input_name: “input_img;calib”-
input_type_rt: ‘gray;gray’-
input_layout_rt: ‘NHWC;NHWC’-
input_shape: ‘1x1440x960x1;1x1x3x1’-
input_type_train: ‘gray;gray’-

input_layout_train: ‘NHWC;NHWC’-

norm_type: ‘no_preprocess;no_preprocess’-
mean_value: ‘None;None’-
scale_value: ‘None;None’-

按照上面的配置后,input_shape报错不匹配:-

File “/usr/local/lib/python3.6/site-packages/horizon_nn/parser/onnx_parser.py”, line 119, in update_input_shapes-
self.final_input_shapes[input_name]))-
ValueError: Input shape of input_img has a dimension [1, 1440, 960, 1] different from [1, 1382400] which is parsered from onnx.-

这里面还有什么没有配置和修改的吗?谢谢

您好,如果期望板端以gray格式输入,yaml配置没有问题,但是您需要调整的是ONNX模型的输入,使ONNX模型的输入数据以1x1440x960x1排布

您好,-
1.我理解的是要修改onnx本身模型吗还是修改cal_data_dir到1x1440x960x1。

2.如果需要修改onnx本身模型的话,需要做什么工作。

对的,需要稍微修改模型就好。在导出ONNX模型时,对于输入分支input_img使用torch.randn(1, 1440, 960, 1)进行trace,在此之前还需要你进到模型源码,找到forward函数,在输入分支input_img后添加reshape节点,reshape成1x1382400,后续模型就无需修改。具体操作和细节还需要您去学习了解~