.pth to .onnx
i have a pytorch model xxxx.pth, but inside there is only parameters, and no the structure of the model. can it be translate to .onnx file ? how ? i used the code to load the model:
model=torch.load(model_pth_path) print type(model) dummy_input = Variable(torch.randn(1, *input_shape)) output = torch_onnx.export(model,dummy_input,model_onnx_path,verbose=True) print("Export of torch_model.onnx complete!")
but it report an error:
Traceback (most recent call last):
File "convert_to_onnx.py", line 31, in
torch.onnx need both the architure and the parameters. You should define the network before load xxx.pth.
I have the same problem,it need architure, as follows: 【My code】 import torch from torch.autograd import Variable from backbone.resnet import get_pose_net from backbone.vovnetv2 import VoVNet39_slim
output_onnx = 'centernet.onnx' pytorch_model = 'net_resume.pth'
model = VoVNet39_slim(eSE=False) #model.cuda() model.load_state_dict(torch.load(pytorch_model)) model.train(False) dummy_input = Variable(torch.randn(1, 3, 416, 416)).cpu() torch.onnx.export(model, dummy_input, output_onnx, verbose = True, input_names = ['data'], output_names = ['hm', 'reg', 'wh'])
I have the same problem,how to modify it。。
torch.onnx need both the architure and the parameters. You should define the network before load xxx.pth.
Yes,you are right, acording to your guidance,it works,thanks