Unknown layer: KerasLayer
See original GitHub issueTensorFlow.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:
- Created 4 years ago
- Reactions:1
- Comments:20 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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_modelThen use the
--input_format tf_saved_model
option withtensorflowjs_converter
.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 usetf.loadGraphModel()
instead?