microsoft / MMdnn

MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.
MIT License
5.8k stars 965 forks source link

Error converting keras model to pytorch #915

Open nischa564 opened 3 years ago

nischa564 commented 3 years ago

Platform (like ubuntu 16.04/win10): win10

Python version: 3.8.5

Source framework with version (like Tensorflow 1.4.1 with GPU): Tensorflow 2.3.0 with GPU

Destination framework with version (like CNTK 2.3 with GPU): Pytorch 1.7.1

Pre-trained model path (webpath or webdisk path): https://keras.io/examples/vision/mnist_convnet/

Running scripts: mmconvert -sf keras -iw my_h5_model.h5 -df pytorch -om pytorch_model.pth

Hello, I try to convert a keras model to a pytorch model. The error I get is:

Traceback (most recent call last): File "c:\users\nisch\anaconda3\envs\jntorchenv\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "c:\users\nisch\anaconda3\envs\jntorchenv\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Users\Nisch\anaconda3\envs\jntorchenv\Scripts\mmconvert.exe__main.py", line 7, in File "c:\users\nisch\anaconda3\envs\jntorchenv\lib\site-packages\mmdnn\conversion_script\convert.py", line 102, in _main ret = convertToIR._convert(ir_args) File "c:\users\nisch\anaconda3\envs\jntorchenv\lib\site-packages\mmdnn\conversion_script\convertToIR.py", line 46, in _convert parser = Keras2Parser(model) File "c:\users\nisch\anaconda3\envs\jntorchenv\lib\site-packages\mmdnn\conversion\keras\keras2_parser.py", line 136, in init__ self.keras_graph.build() File "c:\users\nisch\anaconda3\envs\jntorchenv\lib\site-packages\mmdnn\conversion\keras\keras2_graph.py", line 48, in build for pred in node.inbound_layers: TypeError: 'InputLayer' object is not iterable

nischa564 commented 3 years ago

Hello, I solved this problem by using different versions:

Python version: 3.6.12

Source framework with version (like Tensorflow 1.4.1 with GPU): Tensorflow 1.15.2

Destination framework with version (like CNTK 2.3 with GPU): Pytorch 0.4.0

But now I am getting this error: Traceback (most recent call last): File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\mmdnn\conversion\keras\keras2_parser.py", line 104, in init from keras.applications.mobilenet import relu6 ImportError: cannot import name 'relu6'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "c:\users\nisch\anaconda3\envs\mmdnne\lib\runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Users\Nisch\anaconda3\envs\mmdnne\Scripts\mmconvert.exe__main.py", line 7, in File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\mmdnn\conversion_script\convert.py", line 102, in _main ret = convertToIR._convert(ir_args) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\mmdnn\conversion_script\convertToIR.py", line 46, in _convert parser = Keras2Parser(model) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\mmdnn\conversion\keras\keras2_parser.py", line 120, in init 'DepthwiseConv2D': layers.DepthwiseConv2D File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\engine\saving.py", line 419, in load_model model = _deserialize_model(f, custom_objects, compile) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\engine\saving.py", line 225, in _deserialize_model model = model_from_config(model_config, custom_objects=custom_objects) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\engine\saving.py", line 458, in model_from_config return deserialize(config, custom_objects=custom_objects) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\layers__init__.py", line 55, in deserialize printable_module_name='layer') File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object list(custom_objects.items()))) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\engine\sequential.py", line 300, in from_config custom_objects=custom_objects) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\layers\init.py", line 55, in deserialize printable_module_name='layer') File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object return cls.from_config(config['config']) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\engine\base_layer.py", line 1109, in from_config return cls(*config) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(args, **kwargs) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\layers\convolutional.py", line 490, in init **kwargs) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\layers\convolutional.py", line 117, in init__ self.kernel_initializer = initializers.get(kernel_initializer) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\initializers.py", line 508, in get return deserialize(identifier) File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\initializers.py", line 503, in deserialize printable_module_name='initializer') File "c:\users\nisch\anaconda3\envs\mmdnne\lib\site-packages\keras\utils\generic_utils.py", line 138, in deserialize_keras_object ': ' + class_name) ValueError: Unknown initializer: GlorotUniform