Train a model in tf.keras with Colab, and run it in the browser with TensorFlow.js

Zaid Alyafeai
Jul 2, 2018 · 4 min read

We will create a simple tool that recognizes drawings and outputs the names of the current drawing. This app will run directly on the browser without any installations. We will use Google Colab for training the model, and we will deploy it on the browser using TensorFlow.js.

Code and Demo

Find the live demo and the code on GitHub. Also make sure to test the notebook on Google Colab here.


We will use a CNN to recognize drawings of different types. The CNN will be trained on the Quick Draw dataset. The dataset contains around 50 million drawings of 345 classes.

A subset of the classes


We will train the model on GPU for free on Google Colab using Keras then run it on the browser directly using TensorFlow.js(tfjs) . I created a tutorial on TensorFlow.js. Make sure to read it before continuing. Here is the pipeline of the project

The pipeline

Train on Colab

Google provides free processing power on a GPU. You can see this tutorial on how to create a notebook and activate GPU programming.


we will use keras with tensorflow backend

Load the Data

Since we have a limited memory we will not train on all the classes. We will only use 100 classes of the dataset. The data for each class is available on Google Cloud as numpy arrays of the shape where is the number of of the images for that particular class. We first download the dataset

Since our memory is limited we will only load to memory 5000 images per classes. We also reserve 20% of the data unseen for testing

Preprocess the Data

We preprocess the data to prepare it for training. The model will take batches of the shape and outputs probabilities of the shape

Create the Model

We will create a simple CNN. Notice that the simpler the model with lesser number of parameters the better. Indeed, we will run the model after conversion on the browser and we want the model to run fast for prediction. The following model contains 3 conv layers and 2 dense layers.

Fit, Validate and Test

After that we train the model for epochs and batches with validation split

Here is the results of the training

And the testing accuracy is top accuracy.

Prepare the model for Web Format

After we are satisfied about the accuracy of the model we save it in order to convert it

we install the tfjs package for conversion

then we convert the model

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

finally download the model

Inference on the Browser

In this section we show how to load the model and make inference. I will assume that we have a canvas of size . I will not go over the details of the interface and focus on TensorFlow.js part.

Loading the Model

In order to use TensorFlow.js first use the following script

You will need a running server on your local machine to host the weight files. You can create an apache server or host the page on GitHub as I did on my project.

After that, load the model to the browser using

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


We need to preprocess the data before making a prediction. First get the image data from the canvas

The will be explained later. The variable is used to stretch the crop of the canvas according to the density of the pixels of the screen.

We take the current image data of the canvas convert it to a tensor, resize and normalize

For prediction we use this will return probabilities of the shape

We can then use simple functions to find the top 5 probabilities.

Improve Accuracy

Remember that that our model accepts tensors of the shape . The drawing canvas we have is of size which might be two large for drawings or the user might draw a small figure. It will be better to crop only the box that contains the current drawing. To do that we extract the minimum bounding box around the drawing by finding the top left and the bottom right points

Testing Drawings

Here are some first time drawings and the highest percentage class. I made all the drawings with my mouse. You should get better accuracy using a pen …


TensorFlow is an end-to-end open source platform for…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store