项目地址:https://github.com/ENOT-AutoDL/onnx2torch
1. 安装 onnx2torch
pip install onnx2torch -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
或者
conda install -c conda-forge onnx2torch
2. 转换模型
import onnx
import torch
from onnx2torch import convert
# Path to ONNX model
onnx_model_path = "/some/path/mobile_net_v2.onnx"
# You can pass the path to the onnx model to convert it or...
torch_model_1 = convert(onnx_model_path)
# Or you can load a regular onnx model and pass it to the converter
onnx_model = onnx.load(onnx_model_path)
torch_model_2 = convert(onnx_model)