使用技巧
shape_inference: 模型推理,主要是为了得出网络各层的 shape 信息;
onnxoptimizer:做一些网络的优化,比如 bn 融合、图优化等;
onnx-simplify: 之前版本主要是做一些常量折叠,现在有了 onnxoptimizer 和 shape_inference 功能。
因此,可使用 onnx-simplify 接口实现 shape 可视化跟网络优化功能,该接口也可强制固定 batch 纬度信息
batch 转换
1、对于 固定 batch 转动态 batch:
一般可使用两种方法(dim_param、update_inputs_outputs_dims),两种方法都可以实现简单举例如下:
a、model.graph.input[0].type.tensor_type.shape.dim[0].dim_param = 'batch_size'
b、update_model_dims.update_inputs_outputs_dims(model, input_dict, output_dict)
其中,input_dict、output_dict 分别为输入输出的 name:shape 字典值
2、对于固定 batch 转其他固定 batch:
这种情况要先从固定 batch 转换为动态 batch,然后利用 onnx-simplify 使用参数 overwrite_input_shapes
强制转换 batch 纬度信息。
注:对于固定 batch 转其他固定 batch 或者转动态 batch 时,有些网络层是不支持的,目前测试到的网络层有: resize(经过该成后输出维度中有一维度信息为 UNK__%d,即当 resize 的输入不确定时,无法推断出输出维度)