tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.51k stars 1.94k forks source link

Add support to Custom Layers for TF.js converter #3440

Closed leoffx closed 4 years ago

leoffx commented 4 years ago

TensorFlow.js version

2.0.0 Python

Describe the problem or feature request

When converting a .h5 model with custom layers to json, it's common to have the error ... File "/usr/local/lib/python3.6/dist-packages/tensorflowjs/converters/converter.py", line 133, in dispatch_keras_h5_to_tfjs_graph_model_conversion model = tf.keras.models.load_model(h5_path, compile=False) ... ValueError: Unknown layer: LayerName (using LayerName as a general name for the custom layer), which can be fixed by changing in converter.py the line 133 from model = tf.keras.models.load_model(h5_path, compile=False) to custom_objects_dict = {'customLayer': library.customLayer} model = tf.keras.models.load_model(h5_path, compile=False, custom_objects=custom_objects_dict) I understand that not all Custom Layers can be converted, but this could either be done automatically for the one which can, and only rise an error if it isn't supported; or it could be added an optional argument for passing custom layers .

Code to reproduce the bug / link to feature request

!tensorflowjs_converter --input_format=keras --output_format=tfjs_graph_model --weight_shard_size_bytes=4194304 /content/model.h5 /content/model/

pyu10055 commented 4 years ago

@leoffx you can try to save the model as saved model then use tfjs converter to convert the saved model to tfjs_graph_model

rthadur commented 4 years ago

Closing this due to lack of activity, feel to reopen. Thank you