tensorflow / tfjs

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

Any workarounds for converting a keras graph model with custom layers into tf.js? #3466

Closed MyJumperBroke23 closed 4 years ago

MyJumperBroke23 commented 4 years ago

TensorFlow.js version

Keras: 2.3.1 Tensorflow: 2.20 Tensorflow.js: 1.7.2

Describe the problem or feature request

I'm trying to convert a keras HDF5 graph model with custom layers into tensorflow.js. If I don't specify the output format to be a graph model, the command line converter automatically converts it into a layers model, and lets me create the custom layers in javascript. However, this results in errors when trying to input something into the tensor. When I specify the output to be a graph model, it gives me the error:

ValueError: Unknown layer: Layer-Name

Are there any workarounds to this?

Code to reproduce the bug / link to feature request

Convert to layers model:

tensorflowjs_converter --input_format=keras --quantization_bytes=1 --weight_shard_size_bytes=9999999999999 ~/PycharmProjects/TrueSky/models/unet_70.h5 ~/PycharmProjects/TrueSky/models/tfjs_model2

Convert to graph model:

tensorflowjs_converter --input_format=keras --output_format=tfjs_graph_model --quantization_bytes=1 --weight_shard_size_bytes=9999999999999 ~/PycharmProjects/TrueSky/models/unet_70.h5 ~/PycharmProjects/TrueSky/models/tfjs_model2

pyu10055 commented 4 years ago

@MyJumperBroke23 What is the error you getting when you converting the model to tfjs_graph_model output format?

MyJumperBroke23 commented 4 years ago

@pyu10055 The exact error is

ValueError: Unknown layer: FixedDropout

pyu10055 commented 4 years ago

@MyJumperBroke23 Can you try to save the model to saved model first and then convert the saved model to tfjs_graph_model?

MyJumperBroke23 commented 4 years ago

@pyu10055 Checking on the json file, it seems like it is now graph-model, but I am getting a different error now, so I will open a separate issue