TensorFlow.js Part 2 - Convert Model

1 minute read

This is part two of a three part series on how to use a TensorFlow model in JavaScript. In part one we have developed and trained a simple fully convolutional neural network which reconstructs images. In this part we are going to convert this model into the TensorFlow.js format so that we can use it in part three.

To follow along you can get the code. If you haven’t done part one you can just use the model fullyConvolutionalModel from the models folder.

I’ve also made a video with pretty much the same content as this post:


To convert the model we have to install TensorFlow.js first.

pip install Tensorflowjs

This allows us to use the tensorflowjs_converter.

The following assumes that the model was previously saved as a SavedModel. If you have a Keras HDF5 model the API is different.

We call the converter with three arguments. We specify the input format as tf_saved_model (or keras if you have a Keras HDF5 model) and provide the folder where the model is located as input and a folder named fullyConvolutionalModelTf as output.

tensorflowjs_converter –-input_format=tf_saved_model ./models/fullyConvolutionalModel ./models/fullyConvolutionalModelTfjs

The result is a .json file which contains the topology of the model and a .bin file with the weights.

The model is now ready to be used by a JavaScript web application, which we develop in part three of this series.