tfjs
tfjs copied to clipboard
Unknown layer: KerasLayer
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);
Any workaround for this?
Still having this issue...any update?
@dsmilkov @pyu10055 Supporting KerasLayer will require some design. IIUC, KerasLayer wraps a TF Hub module as a Keras Layer. TF.js should have all the machinery to execute a GraphModel as a part of LayerModel, but all the parts need to be wired together properly. How that should be done needs to be thought through. Potential subtleties include:
- signature-related issues
- re-serialization
- training support
- should their be a more generic type than
KerasLayer, e.g.,GraphModelLayerthatKerasLayercan inherit from.
The design should be reviewed by the TF Hub team as well.
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?
@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.
Thanks, I ended up switching library because I couldn't wait. Will leave the thread open so it can be tracked for others.
@caisq Thanks, the conversion works but when loading the model in tfjs I get an error Provided weight data has no target variable: bn_Conv1/moving_mean
Any ideas?
Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!
was this implemented?
This bug shouldn't be closed. The new layer type hasn't been implemented yet.
Is there any update on when this might be implemented?
I'm running into the same issue
@rasikanavarange @JJwilkin This is fixed in more recent versions of tfjs. Consider updating.
@lucnat I still get the Unknown Layer: KerasLayer with version 3.1.0 which appears to be the most recent
I am facing the same issue while trying to load the model that I trained on python. The model using the MobileNetV3 feature vector version from TF Hub.
- TensorFlow.js version = 3.6.0
Have there been any developments?
i am running into the same issue
I'm running into this issue on version 3.8.0 which is the most recent version.
I also get "Error: Unknown layer: KerasLayer" when exporting with tfjs.converters.save_keras_model(model, "./test_tfjs"). I will test with other export options.
I have generated a model using tensorflow modelmaker and exported it
model = image_classifier.create(
train_data,
validation_data=validation_data,
shuffle = True,
epochs=50
)
model.export(export_dir='./tfjs-model', export_format=ExportFormat.TFJS)
and then
model = await tf.loadLayersModel("./tfjs-model/model.json");
throw the following error
Unknown layer: HubKerasLayerV1V2. 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().
at deserializeKerasObject (generic_utils.js:227)
at deserialize (serialization.js:25)
at fromConfig (models.js:861)
at deserializeKerasObject (generic_utils.js:258)
at deserialize (serialization.js:25)
at loadLayersModelFromIOHandler (models.js:222)
From my understanding, and correct me if I'm wrong (new to tensorflow), the custom layer generated by Keras can't be serialized from python to js, so you have to implement the KerasLayer custom layer in JS. I haven't been able to find a port on the web yet.
A workaround is to not use Layers, but use Graph instead. Assuming you're not doing training on the browser and only doing predictions...
- Save the Keras trained model as a "Saved Model" via: `tf.saved_model.save('model')
- Convert it using
tensorflowjs_converter:tensorflowjs_converter --input_format=tf_saved_model model tfjs_model - Save the generated
.bin and model.jsonfile to S3 or your site's public folder. - Load it in the browser (in web worker to prevent blocking):
const model = tf.loadGraphModel('/model/model.json')
Hi, @aledalgrande
Apologize for the delayed response and Could you please try with latest version of @tensorflow/[email protected] and check whether this issue still exists or not ? We have updated official documentation w.r.t tfjs-converter here ? if issue still persists please let us know with model folder and tfjs-converter command to replicate the same issue from our end ? Thank you!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.
Closing as stale. Please @mention us if this needs more attention.
It's 2024 and i am facing this problem, any solution ?