Importing a Keras model into TensorFlow.js

saranya Mandava
3 min readSep 25, 2018

--

The purpose of this blog post is to demonstrate about a simple tool, where a user can upload image patches for predicting Invasive Ductal Carcinoma(IDC). This tool runs on browser without any installations. Model is trained in google colab and deployed using tensorflow.js

Entire code is available on my GitHub and live demo can be seen here.

Dataset

The dataset consists of 277,524 50x50 pixel RGB digital image patches that were derived from 162 H&E-stained breast histopathology samples. These images are small patches that were extracted from digital images of breast tissue samples. The breast tissue contains many cells but only some of them are cancerous. Patches that are labeled “1” contain cells that are characteristic of invasive ductal carcinoma. Dataset used for training can be found here.

Training

The model is trained on google colab with free GPU and then run on browser using tensorflow.js

Imports

Model contains keras layers with tensorflow backend.

Loading The Data

The dataset is Loaded from google drive into google colab. Since the memory on google colab is limited, model is trained and tested using 40,000 images out of which 20% of unseen data is used for testing.

Preprocess the Data

This step involves resizing the images to 50X50 and appending the labels based on the classnames as 0 or 1. The below method returns an array of resized images and an array of labels.

Create the model

The model contains 13 convolutional layers, 12 ZeroPadding layers and 4 MaxPooling layers.The model requires input batches of the shape [N,50,50,1].

Fit, Validate and Test

The model is trained by applying image augmentation techniques(rotation, horizontal flip, vertical flip) with ImageDataGenerator for 10 epochs, 128 batch size with 20% validation split.

Here are the results of the training.

The above model has achieved an accuracy of 90.35%.

Preparing the model for Web Format

After achieving good accuracy on the model, it should be saved it in order to convert it.

Importing a Keras model into TensorFlow.js is a two-step process. First, convert an existing Keras model to TF.js Layers format, and then load it into TensorFlow.js.

Save and Convert

Save the model in order to convert it.

This will create some weight files and the json file which contains the architecture of the model.

zip the model to prepare for downloading it to our local machine.

Let’s download the model.

Loading the model using Javascript

In order to use TensorFlow.js functions, import the following script in the html file.

This tool is hosted on github webpages. Next step is to load the model as json file.

The await keyword waits for the model to be loaded by the browser.

Preprocessing the Image

Preprocessing the input image uploaded is required before making the prediction. This step involves converting the image to tensor, resize it and then normalize.

For prediction we use model.predict which returns an array of probabilities.

The probability values are sorted and mapped to the appropriate classname to display the results on to the browser.

References:

--

--