DEV Community

Paul
Paul

Posted on • Updated on

Convert Keras models to TensorFlow.js

If you developed ML applications using TensorFlow (Python) and want to move them to the web, you can easily do it with the TensorFlow js framework now. There is no need to retrain the models, as long as they don't use any custom layers, which is currently still not supported.

To start with the conversion, you have to load the model into python, for example like this:

from tensorflow.keras.applications import resnet50

model = resnet50.ResNet50(include_top=True, weights='imagenet')
model.save('./ResNet50.h5')
Enter fullscreen mode Exit fullscreen mode

After loading the model, save it including weights into an hdf5 file. [1]

For the conversion of the model, you have to install the tensorflowjs python package: pip install tensorflowjs

Then you can convert the Keras model using the following command.

tensorflowjs_converter \
    --input_format=keras \
    --output_format=tfjs_layers_model \
    ./ResNet50.h5 \
    ./ResNet50
Enter fullscreen mode Exit fullscreen mode

This command converts your model to a model.json file with the architecture configuration and multiple .bin files with the stored weights.

After the command finished, you can simply import the model into node with the following lines of code.

const tf = require('@tensorflow/tfjs-node');

const ResNet50URI = `file:///${__dirname}/ResNet50/model.json`
const ResNet50 = await tf.loadLayersModel(ResNet50URI);
Enter fullscreen mode Exit fullscreen mode

Or alternatively in the web:

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script>

<script> 
    const ResNet50URI = `file:///${__dirname}/ResNet50/model.json`
    const ResNet50 = await tf.loadLayersModel(ResNet50URI);
</script>
Enter fullscreen mode Exit fullscreen mode

Afterward, you can just use the predict function as usual to execute the model.

[1] P.S.
There is one thing you need to take care of when doing that. The python kernel has to be restarted every time you load the model because of the layer naming. This GitHub issue explains the problem well.
Additionally, some custom models build in TensorFlow (python) are not ready to be converted. You can only convert models with layers, initializers and operations implemented in TensorFlow js.

The results can be found here:
https://github.com/paulsp94/tfjs_resnet_imagenet/tree/master/ResNet50

Latest comments (0)