gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
857 stars 143 forks source link

Input names mismatch while converting ResNet-18(number of input names provided (1) did not match number of inputs (103)) #96

Closed vamsidharmuthireddy closed 5 years ago

vamsidharmuthireddy commented 5 years ago

Describe the bug I am trying to convert ResNet-18 from pytorch to keras. But, I am getting the following error.

Traceback (most recent call last): File "convert_py2keras.py", line 100, in <module> model_ke = pytorch_to_keras(model_py, input_var, [(3, 224, 224,)]) File "/home/vamsidhar/miniconda3/lib/python3.6/site-packages/pytorch2keras/converter.py", line 76, in pytorch_to_keras torch.onnx.export(model, args, stream, verbose=verbose, input_names=input_names, output_names=output_names) File "/home/vamsidhar/miniconda3/lib/python3.6/site-packages/torch/onnx/__init__.py", line 25, in export return utils.export(*args, **kwargs) File "/home/vamsidhar/miniconda3/lib/python3.6/site-packages/torch/onnx/utils.py", line 84, in export _export(model, args, f, export_params, verbose, training, input_names, output_names) File "/home/vamsidhar/miniconda3/lib/python3.6/site-packages/torch/onnx/utils.py", line 142, in _export _set_input_and_output_names(trace.graph(), input_names, output_names) File "/home/vamsidhar/miniconda3/lib/python3.6/site-packages/torch/onnx/utils.py", line 201, in _set_input_and_output_names set_names(list(graph.inputs()), input_names, 'input') File "/home/vamsidhar/miniconda3/lib/python3.6/site-packages/torch/onnx/utils.py", line 197, in set_names % (descriptor, len(name_list), descriptor, len(node_list))) RuntimeError: number of input names provided (1) did not match number of inputs (103)

To fix it, I modified input_names in converter.py of pytorch2keras from input_names = ['input_{0}'.format(i) for i in range(len(args))] to input_names = ['input_0']+['layer_{0}'.format(i) for i in range(len(model.state_dict().items()))].

This fixed the above error and the model got converted to onnx, but failed to convert to keras with the following error.

Traceback (most recent call last): File "convert_py2keras.py", line 100, in <module> model_ke = pytorch_to_keras(model_py, input_var, [(3, 224, 224,)]) File "/home/vamsidhar/miniconda3/lib/python3.6/site-packages/pytorch2keras/converter.py", line 82, in pytorch_to_keras verbose=verbose, change_ordering=change_ordering) File "/home/vamsidhar/miniconda3/lib/python3.6/site-packages/onnx2keras/converter.py", line 94, in onnx_to_keras input_shape = input_shapes[i] IndexError: list index out of range

Giving the input name of one input layer is not working as it expects names for all the 103 layers(conv,pool,relu). Leaving the input_names parameter is also not working. Do I need to give shapes of all the layers for the conversion to work?

gmalivenko commented 5 years ago

Hello @vamsidharmuthireddy.

Please, show your converter function call. Do you use modified resnet-18 or torchvision version?

vamsidharmuthireddy commented 5 years ago

I tried on both. I am getting the same error even while using test code from repo

gmalivenko commented 5 years ago

Please try again with libraries versions listed below:

tensorflow                       1.14.0
Keras                            2.3.0 
onnx2keras                       0.0.12
pytorch2keras                    0.2.3
torch                            1.2.0 
onnx                             1.5.0
vamsidharmuthireddy commented 5 years ago

Thanks for the swift response. Models are getting generated now. Just to avoid similar issues in future, please update your requirements.txt with versions.