Tensorflow + Node.js = Magic🎉💥
Tags: node, machinelearning, opensource, javascript
Published: 2020-01-16 16:50:02

If you're a Node.js developer as well as have hands in machine learning, then you know that to deploy a pre-trained TensorFlow SavedModel, you need to do model conversion, which at times proves costly. But, not anymore. . . 😀

![](https://miro.medium.com/max/1000/1*MAjYBEOmlPJzi_zhkL0jIw.jpeg)


ICYMI, [TensorFlow.js](http://tensorflow.org/js) is an open-source library that lets you define, train, and run machine learning models in Javascript. The library has empowered a new set of developers from the extensive JavaScript community to build and deploy machine learning models and has enabled new use cases of machine learning. For example TensorFlow.js runs in all major browsers, server-side in Node.js, and more recently, in [WeChat](https://github.com/tensorflow/tfjs-wechat) and [React Native](https://github.com/tensorflow/tfjs/tree/master/tfjs-react-native) providing hybrid mobile apps access to ML without having to leave the JS ecosystem. 

One of the key benefits of TensorFlow.js is that JavaScript developers can easily deploy a pre-trained TensorFlow model for inference. TensorFlow.js provides the [tfjs-converter](https://github.com/tensorflow/tfjs/tree/master/tfjs-converter) tool which can convert a TensorFlow SavedModel, TFHub module, or Keras model to a JavaScript compatible format. However the converter tool requires JavaScript developers to install the Python TensorFlow package and learn how to use it. Further, the converter tool does not support the full set of TensorFlow ops (supported ops), so if a model contains an unsupported op, it would not be convertible with the tool.

# 🐧 Native SavedModel execution in Node.js

> We are now excited to bring a new way for Node.js developers to easily deploy a pre-trained TensorFlow SavedModel directly with high performance and without any need for model conversion. *- TensorFlow Team* 

Now I'm excited to say that Tensorflow announced native TensorFlow SavedModel execution in Node.js. You can now bring a pre-trained TensorFlow model in SavedModel format, load it in Node.js through the [@tensorflow/tfjs-node](https://www.npmjs.com/package/@tensorflow/tfjs-node) (or [tfjs-node-gpu](https://www.npmjs.com/package/@tensorflow/tfjs-node-gpu)) package, and execute the model for inference without using tfjs-converter.

The TensorFlow SavedModel has one or several named functions, called [SignatureDef](https://www.tensorflow.org/guide/saved_model#identifying_a_signature_to_export). If you have a pre-trained TensorFlow SavedModel, you can load the model’s SignatureDef in JavaScript through one line of code, and the model is ready to use for inference.

```javascript
const model = await tf.node.loadSavedModel(path, [tag], signatureKey);
const output = model.predict(input);
```
You can also feed multiple inputs to the model as an array or a map:
```javascript
const model1 = await tf.node.loadSavedModel(path1, [tag], signatureKey);
const outputArray = model1.predict([inputTensor1, inputTensor2]);

const model2 = await tf.node.loadSavedModel(path2, [tag], signatureKey);
const outputMap = model2.predict({input1: inputTensor1, input2:inputTensor2});
```
If you want to inspect the details of a TensorFlow SavedModel to find the model tags and signatures information (aka MetaGraphs), they can be parsed through a JavaScript helper API, similar to TensorFlow SavedModel [client tool](https://www.tensorflow.org/guide/saved_model#show_command):
```javascript
const modelInfo = await tf.node.getMetaGraphsFromSavedModel(path);
```
This new feature is available in the [@tensorflow/tfjs-node](https://www.npmjs.com/package/@tensorflow/tfjs-node) package version 1.3.2 and newer, for both CPU and GPU. It supports TensorFlow SavedModel trained and exported in both TensorFlow Python versions 1.x and 2.0. Besides the benefit of not needing any conversion, native execution of TensorFlow SavedModel means that you can run models with ops that are not in TensorFlow.js yet, through loading the SavedModel as a TensorFlow session in the C++ bindings.

In addition to usability benefits, this also has performance implications. In our performance benchmarking tests with MobileNetV2 model (inference time), we see inference time improvements on both CPU and GPU when executing SavedModels directly in Node.js.
![Runtime & Inference time](https://1.bp.blogspot.com/-00haeTw4boo/XhzSH0X47bI/AAAAAAAACkE/N-UuL41c6_0-fjDBQVYBTdSs9vzfb_hjgCLcBGAsYHQ/s1600/gpu%2Bmodel.png)

Inputs: [Tensorflow Blog](blog.tensorflow.org).