Convert/import Torch model to PyTorch

Hey,
Is there a way to do this in reverse? Since saving in python pickles the file and they cant be directly read in lua using torch.load . I have existing code base in lua and Ive worked on some data using pytorch and want to go back to the lua code.
Thanks!

@ashkamath you will have to save weights to HDF5 files and load them in lua using torch-hdf5

1 Like

Hi Hantang,
how did you solve this problem? “AttributeError: type object ‘FloatStorage’ has no attribute ‘from_buffer’” message.
I am having the same message.
thanks

1 Like

@sdalaman I am having the same error. Did you manage to find any solutions?

Sorry, I didn’t read the thread fully. One of the above comments solves the issue.

“Thanks, that solves my problem. Previously I used cudnn.convert(model, nn), but did not use model:float(), so there was still cuda tensor.”

I have a question. Once I got the load_lua() working and load a torch model into pytorch, how can I used it? (because it is not an object like this one: http://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html#sphx-glr-beginner-blitz-neural-networks-tutorial-py)

What I obtain from load_lua() is a nn.Sequential object. How can I use this nn.Sequential object?

@alexis-jacq @apaszke Do you have a way to use nn.Sequential after using the load_lua()? I am not sure how to use the loaded model.

Pretty much what I am asking is how to convert a torch.legacy.nn module into a torch.nn module ?

no there is no automatic way to convert legacy.nn.* to nn.*

Hi
I want to convert the pix2pix Image to Image translation model (https://github.com/phillipi/pix2pix) which is built using nngraph. I tried conversion to both pytorch and caffe.

When I try to convert it to caffe model using torch to caffe tool (https://github.com/facebook/fb-caffe-exts#torch2caffe) I get the error “unknown class nn.gModule”.

I also try to load torch model in pytorch by load_lua from torch.utils.serialization where I get similar error.

Since, it seems nngraph is not supported for conversion. Is there a way to convert nngraph model to nn model without the need of rewriting or retraining? Down the line, I want to convert the pix2pix model into various formats like caffe, caffe2, coreML and others to test the performance in mobile environment.

Thanks.

2 Likes

Hi,
Please help. I tried to replicate loading a “.t7” checkpoint model and here is what I got:

Python 2.7.13 |Anaconda 4.4.0 (x86_64)| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

import torch
from torch.utils.serialization import load_lua
a = load_lua(‘torch/torch-rnn/cv/checkpoint_1000.t7’)
Traceback (most recent call last):
File “”, line 1, in
File “/Users/yusuf/anaconda2/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py”, line 599, in load_lua
return reader.read()
File “/Users/yusuf/anaconda2/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py”, line 586, in read
return self.read_table()
File “/Users/yusuf/anaconda2/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py”, line 514, in wrapper
result = fn(self, *args, **kwargs)
File “/Users/yusuf/anaconda2/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py”, line 563, in read_table
v = self.read()
File “/Users/yusuf/anaconda2/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py”, line 584, in read
return self.read_object()
File “/Users/yusuf/anaconda2/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py”, line 514, in wrapper
result = fn(self, *args, **kwargs)
File “/Users/yusuf/anaconda2/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py”, line 543, in read_object
“constructor”).format(cls_name))
torch.utils.serialization.read_lua_file.T7ReaderException: don’t know how to deserialize Lua class nn.LanguageModel. If you want to ignore this error and load this object as a dict, specify unknown_classes=True in reader’s constructor

1 Like

hey, I got the same error “keyError: Torch.CudaTensor” , have you find a solution? thanks~

@visonpon Is your torch model saved as cuda tensors? In that case save the torch model as CPU tensors and then use load_lua to load in PyTorch.

thanks for your reply. But somtimes i want to use pretrianed model from others and their models are saved as cuda tensor. in this case ,do you have any suggestions?

@visonpon Possible that you can open the pretrained model in torch first, convert to CPU, and save it. So that you can load it in PyTorch?

thanks ,i will try it~

I am trying to convert these models to PyTorch (GitHub - szagoruyko/cvpr15deepcompare: Code and models for "Learning to Compare Image Patches via Convolutional Neural Networks") however, it always returns this error:

AttributeError: ‘SpatialConvolution’ object has no attribute ‘padW’

Whether I use load_lua, or the torch to PyTorch projects listed above, I still get this error. The models are provided in .t7 and .bin format. Is there any other way to load that in PyTorch?

I’m trying to import a t7 as well, but I get this error.

“T7ReaderException: don’t know how to deserialize Lua class nn.DataParallelTable. If you want to ignore this error and load this object as a dict, specify unknown_classes=True in reader’s constructor”

Any clue how to solve this?

@minsukchang seems the DataParallelTable model is not saved correctly. Do you know how the model was saved? https://github.com/soumith/imagenet-multiGPU.torch/blob/master/util.lua shows how to save a DPT model. Try opening the model in Lua torch and save it properly.

1 Like

@SelvamArul Thanks, Selvam. I didn’t save the model, it’s someone else’s pretrained model actually. But I’ll try to load in Lua torch as suggested. I was wondering if I was getting the error because PyTorch didn’t support DPT, but I guess that’s not the issue?