0

I need to be able to deploy a keras model for Tensorflow.js prediction, but the Firebase docs only seem to support a TFLite object, which tf.js cannot accept. Tf.js appears to accept JSON files for loading (loadGraphModel() / loadLayersModel() ), but not a keras SavedModel (.pb + /assets + /variables).

How can I attain this goal?

Note for the Tensorflow.js portion: There are a lot of pointers to the tfjs_converter, but the closest API function offered to what I'm looking for is the loadFrozenModel() function, which requires both a .pb and a weights_manifest.json. It seem to me like I'd have to programmatically assemble this before before sending it up to GCloud as a keras SavedModel doesn't contain both (mine contains .pb + /assets + /variables).

This seems tedious for a straightforward deployment feature, and I'd imagine my question only hits upon common usage of each tool.

What I'm looking for is a simple pathway from Keras => Firebase/GCloud => Tensorflow.js.

1 Answer 1

2

So I understand your confusion but you have half part ready. So your keras model has the following files and folders if I understand correctly:

saved_model.pb
/assests
/variables

This is enough to convert the keras model to tensorflow.js model. Use the converter script in the following manner. Make sure you have the latest version of tfjs. If you do not have the latest version, try creating a virtual environment and install latest tfjs otherwise it will disrupt your tensorflow version.

import tensorflowjs as tfjs
import tensorflow as tf

model=tf.keras.models.load_model('path/to/keras/model')

tfjs.converters.save_keras_model(model, 'path/where/you/will/like/to/have/js/model/converted')

Once you have converted the model you will receive following files for js model.

model.json
something.bin

You will have to host those files using a webserver and just make it available for loadLayersModel API something like this:

const model = await tf.loadLayersModel(
     'location/of/model.json');

That is it and you have converted the model from Keras to Tensorflowjs and uploaded as well in js.

I hope my answer helps you.

Sign up to request clarification or add additional context in comments.

1 Comment

What about Custom models? NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.