onnx转换bin时,多输入校准有问题

:red_circle: 问题定位:Layout 配置数量不匹配

错误信息很明确:

ERROR num of input_layout_rt given: 1, expect: 2
ERROR num of input_layout_train given: 1, expect: 2

你当前配置是 NCHW;(第二个留空),但工具链期望两个输入都指定 layout。我之前的建议有误,抱歉!


:white_check_mark: 正确解决方案

对于2 维特征输入 (1x157),需要将其reshape 成 4 维才能指定 layout。有两种方案:

方案 A:修改 ONNX 模型输入 shape(推荐)

在导出 ONNX 时,将特征输入从 1x157 改为 1x1x1x157

# 导出 ONNX 时
dummy_img = torch.randn(1, 3, 224, 224)
dummy_feat = torch.randn(1, 1, 1, 157)  # 4 维
torch.onnx.export(model, (dummy_img, dummy_feat), 'convnext.onnx', ...)

然后在模型 forward 中添加 reshape:

def forward(self, img, feat):
    feat = feat.reshape(-1, 157)  # 4D -> 2D
    # 后续处理...

方案 B:YAML 配置调整(不修改模型)

如果无法修改模型,尝试以下配置:

input_parameters:
  input_name: 'image;features'
  input_shape: '1x3x224x224;1x157'
  input_type_train: 'bgr;featuremap'
  input_layout_train: 'NCHW;NCHW'      # 两个都指定
  norm_type: 'data_mean_and_scale;no_preprocess'
  mean_value: '123.675 116.28 103.53;0'
  scale_value: '0.01712475 0.017507 0.01742919;1'
  input_type_rt: 'bgr;featuremap'
  input_layout_rt: 'NHWC;NHWC'         # 两个都指定

:wrench: 完整 YAML 配置(方案 B)

model_parameters:
  onnx_model: '/data/workspace/model_output/convnext.onnx'
  march: 'bernoulli2'
  working_dir: 'model_output'
  output_model_file_prefix: 'convnext_bubble'

input_parameters:
  input_name: 'image;features'
  input_shape: '1x3x224x224;1x157'
  input_type_train: 'bgr;featuremap'
  input_layout_train: 'NCHW;NCHW'      # ✅ 两个都指定
  norm_type: 'data_mean_and_scale;no_preprocess'
  mean_value: '123.675 116.28 103.53;0'
  scale_value: '0.01712475 0.017507 0.01742919;1'
  input_type_rt: 'bgr;featuremap'
  input_layout_rt: 'NHWC;NHWC'         # ✅ 两个都指定

calibration_parameters:
  cal_data_dir: './calibration_data/image;./calibration_data/features'
  calibration_type: 'max'
  # 删除 cal_data_num 和 cal_data_type

compiler_parameters:
  compile_mode: 'latency'
  debug: False
  optimize_level: 'O3'

:clipboard: 校准数据验证

# 图像输入 (224x224x3 = 150528 bytes)
ls -lh calibration_data/image/*.gray | head -3

# 特征输入 (157 * 4 = 628 bytes)
ls -lh calibration_data/features/*.feature | head -3

# 验证文件数量一致
echo "Image files: $(ls calibration_data/image/*.gray | wc -l)"
echo "Feature files: $(ls calibration_data/features/*.feature | wc -l)"

:warning: 如果方案 B 仍报错

如果上述配置还是报错 list index out of range,说明工具链确实无法处理 2 维输入的 layout。此时必须修改 ONNX 模型

  1. 将特征输入 shape 从 1x157 改为 1x1x1x157
  2. 校准数据也要对应改成 4 维二进制:
# 生成 4 维校准数据
feat = np.random.randn(1, 1, 1, 157).astype(np.float32)
feat.tofile('calibration_data/features/00000.feature')
# 文件大小:1*1*1*157*4 = 628 bytes(相同,但维度不同)

:books: 参考案例


:rocket: 下一步

  1. 先尝试方案 B(修改 YAML,两个 layout 都指定)
  2. 如果仍报错,提供完整错误日志
  3. 必要时修改 ONNX 模型输入 shape 为 4 维

请试一下方案 B,告诉我结果!