Importing Keras Models into TensorFlow.js

Hello Readers! In this blog, we will see how to import the Keras models into TensorFlow.js.
You all must have heard the name of TensorFlow.js. TensorFlow.js is a Javascript library, and it is used to integrate two great technology stacks which are – Machine Learning and Web development. This library helps to train and deploy models, and also to use pre-trained models of machine learning into our web applications.

Different Ways to Save the Model

Keras models can be saved in various forms which are –

  • Saving the complete model i.e both the weights and training configurations. This model file contains the architecture of the model, loss, optimizer, and state of the optimizer.
model.save(your_file_path)
  • Saving only the weights of a model which is done in HDF5 format. The below-mentioned code is used for the same. Keras uses the h5py Python package to save in HDF5 format.
model.save_weights('model_weights.h5')
  • Saving only the architecture of the model, and not its weights or its training configuration. The below lines of code explains the implementation of it,
json_str = model.to_json() // converting the model to the form of json strings 
from tensorflow.keras.models import model_from_json //then using this data to build a fresh model 
model = model_from_json(json_str)

The complete model is saved in a file format which can be converted into the Layers format of TensorFlow.js. This type of file can be directly used by TensorFlow.js to train and use the model in applications.

TensorFlow.js Layer Format

The Layer Format of TensorFlow.js contains a folder that contains a model.js file. This file consists of a vivid description of layers and the architecture of the model. The folder also contains the weight files which are in binary format.

Importing models – From Keras to TensorFlow.js

Note: The tensorflowjs module is necessary to convert the model in TF.js format. It can be installed by running the command – pip install tensorflowjs in your command prompt/bash.

First, we need to convert the Keras models into the Layers format of TensorFlow.js. To do this there are two ways:-

  • The complete model is saved in HDF5 format when Keras models are saved by running the – model.save(file_path) command. It contains the file with both weights and model architecture. On running the below command this file is converted to Layers format.
tensorflowjs_converter --input_format keras "/path/souce_directory/model.h5" "/path/target_directory"

In the above code, first, the path of the Keras model file is provided (where it was saved ) after which the path of the target folder is provided. This is the directory where the tensorflow.js files will be saved.

  • ¬†Using Python API can be another way to export the saved model in “.h5” to TF.Layers format. If the Keras model is made in Python language it can directly be exported using a one-line code.
import tensorflowjs as tfjs
tfjs.converters.save_keras_model(model, target_directory)

The final task is to load the model into TensorFlow.js. Consider the below code to load the model in your JavaScript code.

import * as tf from '@tensorflow/tfjs';
const model = await tf.loadLayersModel('https://link/tfjs/model.json');

The link mentioned above has to be the URL to the model.json file to load the model in TensorFlow.js. After writing these lines of code you can use your model in your web applications. We can retrain and evaluate the model. We can also make predictions using this imported Keras model.

The below line of code is used for predicting the output for a given example by the model.

const prediction = model.predict(example);

Now you are all set to try importing various models into TensorFlow.js yourself!

Leave a Reply

Your email address will not be published. Required fields are marked *