onnx模型简化 命令行
时间: 2023-07-26 13:05:11 浏览: 274
您可以使用以下命令行来简化ONNX模型:
```python
import onnx
from onnxsim import simplify
ONNX_MODEL_PATH = './model.onnx'
ONNX_SIM_MODEL_PATH = './model_simple.onnx'
if __name__ == "__main__":
onnx_model = onnx.load(ONNX_MODEL_PATH)
onnx_sim_model, check = simplify(onnx_model)
assert check, "Simplified ONNX model could not be validated"
onnx.save(onnx_sim_model, ONNX_SIM_MODEL_PATH)
print('ONNX file simplified!')
```
如果您要简化动态batch的ONNX模型,可以使用以下命令行:
```python
import onnx
from onnxsim import simplify
ONNX_MODEL_PATH = './model.onnx'
ONNX_SIM_MODEL_PATH = './model_simple.onnx'
if __name__ == "__main__":
onnx_model = onnx.load(ONNX_MODEL_PATH)
onnx_sim_model, check = simplify(onnx_model, dynamic_input_shape=True, input_shapes={"images":\[1, 3, 224, 224\]})
assert check, "Simplified ONNX model could not be validated"
onnx.save(onnx_sim_model, ONNX_SIM_MODEL_PATH)
print('ONNX file simplified!')
```
这些命令行使用了`onnxsim`库来简化ONNX模型。您需要将`ONNX_MODEL_PATH`替换为您的原始模型路径,将`ONNX_SIM_MODEL_PATH`替换为简化后的模型保存路径。如果您要简化动态batch的模型,可以将`dynamic_input_shape`设置为`True`,并使用`input_shapes`参数指定输入的形状。
#### 引用[.reference_title]
- *1* *2* [利用onnxsim对onnx模型进行简化](https://blog.csdn.net/hjxu2016/article/details/127265957)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [转换&优化 onnx模型](https://blog.csdn.net/weixin_41521681/article/details/112744642)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文