Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keras custom layer conversion #731

Closed
arvkr opened this issue Sep 6, 2019 · 3 comments
Closed

keras custom layer conversion #731

arvkr opened this issue Sep 6, 2019 · 3 comments

Comments

@arvkr
Copy link

arvkr commented Sep 6, 2019

Platform (like ubuntu 16.04/win10): Ubuntu 16.04

Python version: Python3.5

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

Destination framework with version (like CNTK 2.3 with GPU): IR (eventually Pytorch 1.2.0)

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

Running scripts: mmtoir -f keras -d ir_best_model -n best_model.json -w best_model_weights.h5

Hi,
I am trying to convert a Keras model to IR. I am getting an Unknown layer error. The layer is a custom layer implemented in Keras. My question - Can MMdnn handle custom Keras layers for conversion?

Detailed error info:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-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 "/usr/local/bin/mmtoir", line 11, in <module>
    sys.exit(_main())
  File "/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/_script/convertToIR.py", line 192, in _main
    ret = _convert(args)
  File "/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/_script/convertToIR.py", line 46, in _convert
    parser = Keras2Parser(model)
  File "/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/keras/keras2_parser.py", line 126, in __init__
    model = self._load_model(model[0], model[1])
  File "/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/keras/keras2_parser.py", line 78, in _load_model
    'DepthwiseConv2D': layers.DepthwiseConv2D})
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py", line 492, in model_from_json
    return deserialize(config, custom_objects=custom_objects)
  File "/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
    list(custom_objects.items())))
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1022, in from_config
    process_layer(layer_data)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1008, in process_layer
    custom_objects=custom_objects)
  File "/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 138, in deserialize_keras_object
    ': ' + class_name)
ValueError: Unknown layer: BilinearUpSampling2D

Thanks in advance!

@rainLiuplus
Copy link
Contributor

Hi @arvkr, Now MMdnn can not handle the custom keras layer automatically. But if you still want to convert your model via MMdnn, you could:

  1. Modify the MMdnn code to load your custom model.
    Please refer to this to change the load model code.

  2. Add the custom layer.
    Please refer to contribution guideline.

Thanks!

@arvkr
Copy link
Author

arvkr commented Sep 10, 2019

Thank you for this info! I will try this method and hopefully it works!

@arvkr
Copy link
Author

arvkr commented Sep 10, 2019

I converted the custom layer to an inbuilt Keras layer and converted the model successfully to pytorch. I did not have to resort to modifying the Keras parser after all.

@arvkr arvkr closed this as completed Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants