模型转换
本章节主要介绍模型转换工具的使用细节。转换期间,工具会完成模型的格式转换、结构优化以及校准量化(如果您还指定了模型量化配置文件)等流程。转换完成后,工具将输出一个可以在进迭时空芯片端
运行的模型。
【说明】
(当前)进迭时空模型文件后缀名为.onnx
,兼容标准 ONNX 格式模型;
如果您还指定了模型量化配置文件,那您还将得到一个量化后的模型文件。
1 使用说明
$ spine convert -h
usage: spine convert [-h] {onnx,tf1,tf2,paddle,caffe}...
positional arguments:
{onnx,tf1,tf2,paddle,caffe}
optional arguments:
-h, --help show this help message and exit
转换工具目前预置了 ONNX、TensorFlow、Paddle、Caffe 四种框架的模型格式转换支持。由于进迭时空 AI 推理引擎兼容 ONNX 模型格式(opset >= 7),故对于 Pytorch、MXNet 等其他框架下的模型,您可以通过先将模型转换成 ONNX 格式,再调用转换工具(详情可以参阅 3.1.5 其他模型 章节内容)。
1.1 ONNX 模型
除模型量化外,转换工具针对 ONNX 模型还提供了子图提取、shape 修改、模型文件检查等功能。
1.1.1 使用说明
$ spine convert onnx -h
usage: spine convert onnx [-h] --input INPUT --output OUTPUT [--checker] [--onnxsim] [--verbose] [--inputs INPUTS] [--outputs OUTPUTS]
[--free_dim_param FREE_DIM_PARAM [FREE_DIM_PARAM...]]
[--free_dim_denotation FREE_DIM_DENOTATION [FREE_DIM_DENOTATION...]] [--config CONFIG]
optional arguments:
-h, --help show this help message and exit
--input INPUT input onnx model path
--output OUTPUT output jdsk model path
--checker enable onnx.checker.check_model
--onnxsim enable onnxsim(https://github.com/daquexian/onnx-simplifier) to simplify the onnx model
--verbose, -v verbose message, option is additive
--inputs INPUTS expected input tensor names with shapes(option)
--outputs OUTPUTS expected output tensor names with shapes(option)
--free_dim_param FREE_DIM_PARAM [FREE_DIM_PARAM...], -f FREE_DIM_PARAM [FREE_DIM_PARAM...]
[dimension_name:override_value] specify a value(must > 0) to override a free dimension by name(dim.dim_param).
--free_dim_denotation FREE_DIM_DENOTATION [FREE_DIM_DENOTATION...], -F FREE_DIM_DENOTATION [FREE_DIM_DENOTATION...]
[dimension_denotation:override_value] specify a value(must > 0) to override a free dimension by
denotation(dim.denotation).
--config CONFIG, -c CONFIG
config file path for calibration
1.1.2 参数说明
参数 | 必要/可选 | 默认值 | 说明 |
---|---|---|---|
-h, --help | 可选 | 无 | 打印使用说明 |
--input | 必要 | 无 | 输入模型路径 |
--output | 必要 | 无 | 输出模型路径 |
--checker | 可选 | False | 使能模型检查 |
--onnxsim | 可选 | False | 使能 onnxsim(https://github.com/daquexian/onnx-simplifier)优化模型 |
--verbose, -v | 可选 | 0 | 使能调试信息 |
--inputs | 可选 | None | 重新指定模型输入节点及 shape(可选),格式示例:input_1[n1,n2],input2[n3,n4],... |
--outputs | 可选 | None | 重新指定模型输出节点及 shape(可选),格式示例:input_1[n1,n2],input2[n3,n4],... |
-f, --free_dim_param | 可选 | None | 重新指定某些输入数据 shape,格式示例:dimension_name:override_value [...] |
-F, --free_dim_denotation | 可选 | None | 重新指定某些输入数据 shape,格式示例:dimension_den0tation:override_value [...] |
--config, -c | 可选 | None | 模型校准配置文件路径(详情参阅 4. 模型量化 章节) |
1.1.3 使用示例
- 1.1.3.1 提取模型 backbone 示例模型:https://github.com/onnx/models/blob/main/validated/vision/object_detection_segmentation/yolov3/model/yolov3-12.onnx
$ spine convert onnx --input yolov3-12.onnx \
--output yolov3-12-backbone.onnx \
--inputs input_1 \
--outputs Transpose__467:0,Transpose__469:0,Transpose__472:0
【提示】您可以通过 Netron 可视化工具,查看需要提取的网络的输入输出名称(及 shape)。
- 1.1.3.2 修改模型输入 shape
spine convert onnx
当前提供了两种方式来修改模型的输入和/或输出 shape 信息:- 通过
--inputs
和/或--outputs
- 通过