question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unknown layer: KerasLayer

See original GitHub issue

TensorFlow.js version

1.2.9

Browser version

NodeJS 11.3.0

Describe the problem or feature request

After converting a model created with a layer from TF Hub (MobileNetV2) with TF 2.0RC, and running loadLayersModel, I get:

Unknown layer: KerasLayer. This may be due to one of the following reasons:
    1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
    2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().

Code to reproduce the bug / link to feature request

# model created like this during training (Python)
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
                                         input_shape=(224,224,3))

model = tf.keras.Sequential([
  feature_extractor_layer,
  layers.Dense(image_data.num_classes, activation='softmax')
])
# model then converted like this
$ tensorflowjs_converter --input_format=keras input_file.h5 output_dir
// model used like this in Node
const handler = tf.io.fileSystem('output_dir/model.json');
const model = await tf.loadLayersModel(handler);

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
caisqcommented, Nov 6, 2019

@aledalgrande Just to add to what @dsmilkov said: You need to save the Python using tf.keras.experimental.export_saved_model: https://www.tensorflow.org/api_docs/python/tf/keras/experimental/export_saved_model

Then use the --input_format tf_saved_model option with tensorflowjs_converter.

1reaction
dsmilkovcommented, Nov 6, 2019

Hi,

Support for KerasLayer will probably not happen in the next 3 months due to the complexity of the solution.

For a workaround, can you try serializing the model to a SavedModel, convert saved_model --> tfjs_graph_model using the converter, and use tf.loadGraphModel() instead?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError: Unknown layer: KerasLayer - Stack Overflow
While its trying to load the model, it might not be able to parse a layer named KerasLayer, there might be a mistake...
Read more >
Unknown layer: KerasLayer" while converting from .hdf5 to ...
Hello, I used a pretrained model from Tensorflow hub to train on my data and it gave me significantly higher val_acc.
Read more >
Tensorflow serving issue: "ValueError: Unknown layer - Reddit
... Unknown layer: KerasLayer". I am trying to serve a tensorflow model in the .h5 format, but I am experiencing this issue:.
Read more >
hub.KerasLayer | TensorFlow Hub
This layer wraps a callable object for use as a Keras layer. The callable object can be passed directly, or be specified by...
Read more >
ValueError: Unknown layer: KerasLayer : Forums
ValueError: Unknown layer: KerasLayer. I have the following code: from keras.models import model_from_json with open('modelS.json', ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found