Train a model in tf.keras with Colab, and run it in the browser with TensorFlow.js
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.
Dataset
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.
Pipeline
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
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.
Imports
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 [N,784]
where N
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 [N, 28, 28, 1]
and outputs probabilities of the shape [N, 100]
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 5
epochs and 256
batches with 10%
validation split
Here is the results of the training
And the testing accuracy is 92.20%
top 5
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 300 x 300
. 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 await
keyword waits for the model to be loaded by the browser.
Preprocessing
We need to preprocess the data before making a prediction. First get the image data from the canvas
The getMinBox()
will be explained later. The variable dpi
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 model.predict
this will return probabilities of the shape [N, 100]
We can then use simple functions to find the top 5 probabilities.
Improve Accuracy
Remember that that our model accepts tensors of the shape [N, 28, 28,1]
. The drawing canvas we have is of size 300 x 300
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 …