tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Unknown layer: KerasLayer

Open aledalgrande opened this issue 6 years ago • 20 comments
trafficstars

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);

aledalgrande avatar Sep 09 '19 01:09 aledalgrande

Any workaround for this?

aledalgrande avatar Sep 22 '19 15:09 aledalgrande

Still having this issue...any update?

lucnat avatar Oct 25 '19 15:10 lucnat

@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:

  1. signature-related issues
  2. re-serialization
  3. training support
  4. should their be a more generic type than KerasLayer, e.g., GraphModelLayer that KerasLayer can inherit from.

The design should be reviewed by the TF Hub team as well.

caisq avatar Oct 25 '19 15:10 caisq

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?

dsmilkov avatar Nov 06 '19 18:11 dsmilkov

@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.

caisq avatar Nov 06 '19 18:11 caisq

Thanks, I ended up switching library because I couldn't wait. Will leave the thread open so it can be tracked for others.

aledalgrande avatar Nov 07 '19 02:11 aledalgrande

@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?

lucnat avatar Nov 07 '19 09:11 lucnat

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

rthadur avatar Feb 13 '20 16:02 rthadur

was this implemented?

aledalgrande avatar Feb 14 '20 05:02 aledalgrande

This bug shouldn't be closed. The new layer type hasn't been implemented yet.

caisq avatar Feb 14 '20 14:02 caisq

Is there any update on when this might be implemented?

rasikanavarange avatar Jan 06 '21 16:01 rasikanavarange

I'm running into the same issue

JJwilkin avatar Feb 20 '21 22:02 JJwilkin

@rasikanavarange @JJwilkin This is fixed in more recent versions of tfjs. Consider updating.

lucnat avatar Feb 21 '21 13:02 lucnat

@lucnat I still get the Unknown Layer: KerasLayer with version 3.1.0 which appears to be the most recent

JJwilkin avatar Feb 21 '21 14:02 JJwilkin

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?

raffizulvian avatar May 16 '21 10:05 raffizulvian

i am running into the same issue

x110 avatar May 24 '21 02:05 x110

I'm running into this issue on version 3.8.0 which is the most recent version.

azeemhassni avatar Jul 18 '21 16:07 azeemhassni

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.

thibaut-d avatar Oct 16 '21 21:10 thibaut-d

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)

rodrigoGA avatar Nov 01 '21 22:11 rodrigoGA

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...

  1. Save the Keras trained model as a "Saved Model" via: `tf.saved_model.save('model')
  2. Convert it using tensorflowjs_converter: tensorflowjs_converter --input_format=tf_saved_model model tfjs_model
  3. Save the generated .bin and model.json file to S3 or your site's public folder.
  4. Load it in the browser (in web worker to prevent blocking): const model = tf.loadGraphModel('/model/model.json')

khuezy avatar Jul 24 '22 02:07 khuezy

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!

gaikwadrahul8 avatar May 01 '23 07:05 gaikwadrahul8

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.

google-ml-butler[bot] avatar May 08 '23 07:05 google-ml-butler[bot]

Closing as stale. Please @mention us if this needs more attention.

google-ml-butler[bot] avatar May 15 '23 08:05 google-ml-butler[bot]

It's 2024 and i am facing this problem, any solution ?

kalboussiKarim avatar Apr 02 '24 06:04 kalboussiKarim