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.78k stars 968 forks source link

Keras h5 to caffemodel using tensorflow.keras API fails #913

Open rokopi-byte opened 3 years ago

rokopi-byte commented 3 years ago

Platform (like ubuntu 16.04/win10): Ubuntu 16.04

Python version: 3.5.2

Source framework with version (like Tensorflow 1.4.1 with GPU): Keras (Tensorflow.Keras)

Destination framework with version (like CNTK 2.3 with GPU): Caffe

Pre-trained model path (webpath or webdisk path):

Running scripts: mmconvert -sf keras -iw weights.h5 -in model.json -df caffe -om mynet

Hi, I used with success for the last 2 years mmconvert to convert my model from Keras H5 (pure Keras v. 2.2.0 with TF backend version 1.12) to caffe with the running script above. I switched now to tensorflow 1.15.4 and changed all my keras.X to tensorflow.keras.X to use the new keras built in tensorflow. I didn't change anything else and my model trains well like in the past (I save my model in the same way generating json and H5). Anyway I'm not able to convert to caffe now. I have latest mmconvert, but the above command fails with:

Using TensorFlow backend. Traceback (most recent call last): File "/home/rocco/.local/lib/python3.5/site-packages/mmdnn/conversion/keras/keras2_parser.py", line 69, in _load_model 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 "/home/rocco/.local/bin/mmconvert", line 8, in <module>
    sys.exit(_main())
  File "/home/rocco/.local/lib/python3.5/site-packages/mmdnn/conversion/_script/convert.py", line 102, in _main
    ret = convertToIR._convert(ir_args)
  File "/home/rocco/.local/lib/python3.5/site-packages/mmdnn/conversion/_script/convertToIR.py", line 46, in _convert
    parser = Keras2Parser(model)
  File "/home/rocco/.local/lib/python3.5/site-packages/mmdnn/conversion/keras/keras2_parser.py", line 126, in __init__
    model = self._load_model(model[0], model[1])
  File "/home/rocco/.local/lib/python3.5/site-packages/mmdnn/conversion/keras/keras2_parser.py", line 78, in _load_model
    'DepthwiseConv2D': layers.DepthwiseConv2D})
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/engine/saving.py", line 492, in model_from_json
    return deserialize(config, custom_objects=custom_objects)
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/engine/network.py", line 1022, in from_config
    process_layer(layer_data)
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/engine/network.py", line 1008, in process_layer
    custom_objects=custom_objects)
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/engine/base_layer.py", line 1109, in from_config
    return cls(**config)
  File "/home/rocco/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'ragged'

Does it mean that mmconvert don't support tensorflow.keras API but only keras ?