clcarwin / convert_torch_to_pytorch

Convert torch t7 model to pytorch model and source.
MIT License
539 stars 161 forks source link

load_lua TypeError: unhashable type: 'list' #37

Open jtscs opened 6 years ago

jtscs commented 6 years ago

Hi, I'm fairly new to nns and mainly trying to convert torch to caffe. Since torch2caffe and th2caffe frameworks bothg had issues I thought going from torch to pytorch and than via mmdnn to caffe.

When I tried the script I get the error: TypeError: unhashable type: 'list' Backtrace starts with line 315 and then 259 in the python script (which is the load_lua call) and then goes somewhere in the python package. Last line is anaconda2/lib/python2.7/site_packages/torch/utils/serialization/read_lua_file.py, line 573 in read_table table[k] = v Googeling the error, I found this https://github.com/NVIDIA/FastPhotoStyle/issues/21 but adding long_size = 8 with a comma to load_lua did not change anything. Maybe I did it wrong.

I work on a virtual machine without any graphics support and thus whithout cuda. Version Ubuntu 16.04., Python 2.7. First I tried pip and then conda, I think both led to the same issue. Backtrace goes into anaconda2 directory. conda list | grep torch returns pytorch-cpu 0.4.0 py27_cpu_1 pytorch torchvision-cpu 0.2.1 py27_1 pytorchpip pip list | grep torch torch 0.4.0 torchvision 0.2.1

Since it is not my model I am not sure if all layers of it are supported. Or am I still missig dependencies? Thanks in advance for any help

LancherJOJO commented 6 years ago

I got the same error. Have you solved it ?


I find the reason of this problem !!!! Reason: The function "load_lua" can load .t7 file where the data in the file must be keep a certain structure. Otherwise, the data may be in the mess.
For example, the structure may be in the form "str——value". But if your data of .t7 file are in the form of "str value value", the error will occur. The next k will be type of "value" instead of "str". Solution: You can add code " if type(k)==list: k=tuple(k) " in front of the code "table[k]=v ". This problem is solved ,but other error maybe occur.....

jtscs commented 6 years ago

So far I could not really solve the issue. I tried to work around the solution by using pytorch load_lua to load model and torch.save to save the model (which is supposed to be bad practice). Problem is I can not save a state_dict model which means I can not convert the pth file in mmdnn. I tried your fix but it caused another error: anaconda2/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py", line 599, in read "corrupted.".format(typeidx)) torch.utils.serialization.read_lua_file.T7ReaderException: unknown type id 9. The file may be corrupted.

LancherJOJO commented 6 years ago

I have abandoned this method to transform the model, which is not suitable for all models. Maybe there is a structure in your model that can not be identified by this method, so it does not work. I'm trying to write torch scripts to get data from the model, and then rebuild it myself. Maybe you can try it too. Good luck!