Bring Machine Learning to the Browser with TensorFlow.js — Part I
Tags: tensorflow, javascript, machinelearning, opensource
Published: 2019-02-19 14:47:02

---
title: Bring Machine Learning to the Browser with TensorFlow.js — Part I
published: true
tags: tensorflowjs,javascript,machinelearning,opensource
canonical_url: https://medium.com/ibm-watson-data-lab/bring-machine-learning-to-the-browser-with-tensorflow-js-part-i-16924457291c
---

> _cross-post from [medium.com](https://medium.com/ibm-watson-data-lab/bring-machine-learning-to-the-browser-with-tensorflow-js-part-i-16924457291c)_

> _**Edited 2019 Mar 11** to include changes introduced in TensorFlow.js 1.0. Additional information about some of these TensorFlow.js 1.0 updates can be found [here](https://gist.github.com/caisq/3fc0beb6597f42d66be806c6692f310d)._

#### Applying a web friendly format to a pre-trained model resulting in a web application.

![](https://cdn-images-1.medium.com/max/1024/0*gA9FhjIFE9h9BbrZ)<figcaption>“assorted-color leaf hanging decor” by <a href="https://unsplash.com/@chrislawton?utm_source=medium&amp;utm_medium=referral">Chris Lawton</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

[TensorFlow.js](https://js.tensorflow.org) brings machine learning and its possibilities to JavaScript. It is an open source library built to create, train, and run machine learning models in the browser (and Node.js).

Training and building complex models can take a considerable amount of resources and time. Some models require massive amounts of data to provide acceptable accuracy. And, if computationally intensive, may require hours or days of training to complete. Thus, you may not find the browser to be the ideal environment for building such models.

A more appealing use case is importing and running existing models. You train or get models trained in powerful, specialized environments then you import and run the models in the browser for [impressive user experiences](https://medium.com/ibm-watson-data-lab/taking-selfies-and-more-to-the-next-level-with-open-source-deep-learning-models-de9ac8da4480).

### Converting the model

Before you can use a pre-trained model in TensorFlow.js, the model needs to be in a web friendly format. For this, TensorFlow.js provides the [tensorflowjs\_converter](https://github.com/tensorflow/tfjs-converter) tool. The tool converts TensorFlow and Keras models to the required web friendly format. The converter is available after you install the `tensorflowjs` Python package.

{% gist https://gist.github.com/vabarbosa/f010e8efad07a8670a428c30000b9335 %}

The `tensorflowjs_converter` expects the model and the output directory as inputs. You can also pass optional parameters to further customize the conversion process.

{% gist https://gist.github.com/vabarbosa/74c13e54fad5ce5cdf262417b3757582 %}

The output of `tensorflowjs_converter` is a set of files:

- `model.json` — the dataflow graph
- A group of binary weight files called shards. Each shard file is small in size for easier browser caching. And the number of shards depends on the initial model.

![](https://cdn-images-1.medium.com/max/2000/1*SWgnSA-21aDkddoQTLaD2A.png)<figcaption>tensorflowjs_converter output files</figcaption>

> _**NOTE**: If using `tensorflowjs_converter` version before 1.0, output produced includes the graph (`tensorflowjs_model.pb`), weights manifest (`weights_manifest.json`), and the binary shards files._

### Run model run

Once converted, the model is ready to load into TensorFlow.js for predictions.

**Using Tensorflow.js version 0.x.x**:

{% gist https://gist.github.com/vabarbosa/01deb82adbf5e48fe6655a2d43a10e64 %}

**Using TensorFlow.js version 1.x.x**:

{% gist https://gist.github.com/vabarbosa/e9a119d7f473adf76d119e4814731250 %}

The imported model is the same as models trained and created with TensorFlow.js.

### Convert all models?

You may find it tempting to grab any and all models, convert them to the web friendly format, and run them in the browser. But this is not always possible or recommended. There are several factors for you to keep in mind.

The `tensorflowjs_converter` command can only convert Keras and TensorFlow models. Some supported model formats include [SavedModel](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md), [Frozen Model](https://www.tensorflow.org/extend/tool_developers/), and [HDF5](https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model).

TensorFlow.js does not support all TensorFlow operations. It currently has a limited set of [supported operations](https://github.com/tensorflow/tfjs-converter/blob/master/docs/supported_ops.md). As a result, the converter will fail if the model contains operations not supported.

Thinking and treating the model as a black box is not always enough. Because you can get the model converted and produce a web friendly model does not mean all is well.

Depending on a model’s size or architecture, its performance could be less than desirable. [Further optimization](https://www.tensorflow.org/performance/model_optimization) of the model is often required. In most cases, you will have to pre-process the input(s) to the model, as well as, process the model output(s). So, needing some understanding or inner workings of the model is almost a given.

### Getting to know your model

Presumably you have a model available to you. If not, resources exist with an ever growing collection of pre-trained models. A couple of them include:

- [TensorFlow Models](https://github.com/tensorflow/models) —a set of official and research models implemented in TensorFlow
- [Model Asset Exchange](https://developer.ibm.com/code/exchanges/models/) —a set of deep learning models covering different frameworks

These resources provide the model for you to download. They also can include information about the model, useful assets, and links to learn more.

You can review a model with tools such as [TensorBoard](https://www.tensorflow.org/guide/summaries_and_tensorboard). It’s [graph visualization](https://www.tensorflow.org/guide/graph_viz) can help you better understand the model.

Another option is [Netron](https://github.com/lutzroeder/Netron), a visualizer for deep learning and machine learning models. It provides an overview of the graph and you can inspect the model’s operations.

![](https://cdn-images-1.medium.com/max/1024/1*K-AuH-uThtUeJJxWPMrMAQ.png)<figcaption>visualizing a model with Netron</figcaption>

### To be continued…

Stay tuned for the follow up to this article to learn how to pull this all together. You will step through this process in greater detail with an actual model and you will [take a pre-trained model into web friendly format](https://medium.com/ibm-watson-data-lab/bring-machine-learning-to-the-browser-with-tensorflow-js-part-ii-7555ed9a999e) and [end up with a web application](https://medium.com/ibm-watson-data-lab/bring-machine-learning-to-the-browser-with-tensorflow-js-part-iii-62d2b09b10a3).

* * *