3

I have one pre-trained model into format of .pth extension. I want to convert that into Tensorflow protobuf. But I am not finding any way to do that. I have seen onnx can convert models from pytorch into onnx and then from onnx to Tensorflow. But with that approach I got following error in the first stage of conversion.

from torch.autograd import Variable
import torch.onnx
import torchvision
import torch 

dummy_input = Variable(torch.randn(1, 3, 256, 256))
model = torch.load('./my_model.pth')
torch.onnx.export(model, dummy_input, "moment-in-time.onnx")`

It gives error like this.

File "t.py", line 9, in <module>
    torch.onnx.export(model, dummy_input, "moment-in-time.onnx")
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/__init__.py", line 75, in export
    _export(model, args, f, export_params, verbose, training)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/__init__.py", line 108, in _export
    orig_state_dict_keys = model.state_dict().keys()
AttributeError: 'dict' object has no attribute 'state_dict'

What is possible solution ?

Sukhi
  • 13,261
  • 7
  • 36
  • 53
Urvish
  • 643
  • 3
  • 10
  • 19
  • 4
    Your `.pth` file is a state dictionary and not the complete model. You will first need to create a model and then load that state dictionary and then start your conversion process. Check [this answer](https://stackoverflow.com/a/49942523/7102925) – layog Apr 24 '18 at 18:25
  • The approach shown in that requires to write the model. but I am having pre-trained model and I do not know the exact architecture of it. so I can not define model as done in that answer. what should I do? – Urvish Apr 25 '18 at 13:08
  • Then it gets really hard to determine the architecture. you can guess the architecture by seeing the parameters size, but guessing the correct architecture is really difficult even after looking at the size since residual networks will have same sized parameters as non-residual ones. Your best bet is to get the architecture definition from your pretrained weights source – layog Apr 25 '18 at 13:35
  • Okay. Let's see if I can get that. Thank you for the help.also if I have .pth.tar file then also will the process be the same or changed ? – Urvish Apr 25 '18 at 13:45

3 Answers3

1

try changing your code to this

from torch.autograd import Variable

import torch.onnx
import torchvision
import torch

dummy_input = Variable(torch.randn(1, 3, 256, 256))
state_dict = torch.load('./my_model.pth')
model.load_state_dict(state_dict)
torch.onnx.export(model, dummy_input, "moment-in-time.onnx")
1

The problem here is you are loading the weights of the model, But you need the architechture of your model here as well, for example if you are using mobilenet:

import torch
import torchvision.models as models

model=models.mobilenet_v3_large(weights)#Give your weights here
torch.onnx.export(model, torch.rand(1,3,640,640), "MobilenetV3.onnx")

For more refer this : https://pytorch.org/tutorials/recipes/recipes/saving_and_loading_models_for_inference.html

-2

That means your model is not a subclass of the torch.nn.Modules class. If you make it a subclass, this should work.

user129916
  • 46
  • 3