{"componentChunkName":"component---src-templates-post-js","path":"/blog/2018/11/27/get-started-with-tensorflowjs/","result":{"data":{"post":{"html":"<p>If you are like me, who have been wondering what is machine learning (aka ML) and scaped from all those mathematical jargon, it’s an exciting time since the <code class=\"language-text\">TensorFlow.js</code> is here to the rescue.</p>\n<!--more-->\n<p>You can find the <a href=\"https://github.com/yashints/mobilenet\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">code for this post here</a>.</p>\n<h2 id=\"intro\" style=\"position:relative;\"><a href=\"#intro\" aria-label=\"intro permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Intro</h2>\n<p>I’ve been a front end developer for may years, but also curious about machine learning and AI. However, never had a chance to go deeper into it because I have always been busy and most of the machine learning frameworks have been written in languages that I had no exposure to.</p>\n<p>Things has changed, and people are now using machine learning in mobile devices or in the browser. <code class=\"language-text\">TensorFlow.js</code> is one of the libraries which allows us to create, train and deploy ML models in the browser or in <code class=\"language-text\">Node.js</code>. To be honest, I am really excited about this since it allows me to play with models and neural networks without need to go very low level or learn a new language 🤯.</p>\n<p>Don’t get me wrong, I am not afraid of learning a new language and in fact it is one of my hobbies. But since I have been busy with lot’s of different tasks, simply haven’t had time to dive into that area.</p>\n<p>The good thing about this library is that you don’t need to have prior knowledge on ML, you just deal with some high level APIs that have simplified a lot of concepts for you, so you can focus on deploying the models and solving problems rather than reinventing the wheel 🔥.</p>\n<p>But there are more options which open up a whole lot of opportunities for us. Basically you can:</p>\n<ul>\n<li>Import a pre-trained model and use it (predict or deploy it)</li>\n<li>Import a pre-trained models (Keras or Python) and train it with your dataset</li>\n<li>Create, train, predict, and deploy privacy friendly models (data never leaves the app)</li>\n<li>And probably much more that I am not aware of 🤷‍.</li>\n</ul>\n<h2 id=\"core-concepts\" style=\"position:relative;\"><a href=\"#core-concepts\" aria-label=\"core concepts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Core concepts</h2>\n<p>Before we go further into how to use it, we need to get familiar with some basic concepts so to be on the same page.</p>\n<p><strong>Tensor</strong></p>\n<p>The central unit of data in TensorFlow.js is the tensor: a set of numerical values shaped into an array of one or more dimensions. A <code class=\"language-text\">Tensor</code> instance has a shape attribute that defines the array <code class=\"language-text\">shape</code> (i.e., how many values are in each dimension of the array).</p>\n<p><strong>Variables</strong></p>\n<p>Variables are initialized with a tensor of values. Unlike Tensors, however, their values are mutable.</p>\n<p><strong>Operations (Ops)</strong></p>\n<p>While tensors allow you to store data, operations (ops) allow you to manipulate that data. TensorFlow.js provides a wide variety of ops suitable for linear algebra and machine learning that can be performed on tensors. Because tensors are immutable, these ops do not change their values; instead, ops return new tensors.</p>\n<p><strong>Models and Layers</strong></p>\n<p>Conceptually, a model is a function that given some input will produce some desired output. In <code class=\"language-text\">TensorFlow.js</code> there are two ways to create models. You can use ops directly to represent the work the model does.</p>\n<p>Or you can use the high-level API <code class=\"language-text\">tf.model</code> to construct a model out of layers, which are a popular abstraction in deep learning.</p>\n<div class=\"custom-block warning\"><div class=\"custom-block-body\"><strong>Warning:</strong> Because TensorFlow.js uses the GPU to accelerate math operations, it’s necessary to manage GPU memory when working with tensors and variables. TensorFlow.js provide two functions to help with this: <code class=\"language-text\">dispose</code> and <code class=\"language-text\">tf.tidy</code>. We will dive into these later.</div></div>\n<h2 id=\"how-to-use-a-pre-trained-model\" style=\"position:relative;\"><a href=\"#how-to-use-a-pre-trained-model\" aria-label=\"how to use a pre trained model permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to use a pre-trained model</h2>\n<p>You can use pre-trained models from the highly-popular <a href=\"https://keras.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Keras</a> Python library to make predictions. This means you don’t need to be a data scientist to be able to use the models or even to build powerful ML-powered apps.</p>\n<p>Fortunately there are many models available and ready to use which are being exposed via <code class=\"language-text\">TensorFlow.js</code>. For this you will need to use <code class=\"language-text\">TensorFlow.js converter</code> which is an open source library to load a pre-trained model into a format which is understandable by <code class=\"language-text\">TenserFlow.js</code>.</p>\n<div class=\"custom-block info\"><div class=\"custom-block-body\"><strong>Note:</strong> For this you will need to know a tiny bit of <a href=\"https://www.python.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Python</a> and have it installed locally.</div></div>\n<p>You will need to perform two operations to have a ready to use model. First save the model locally, and then run the convertor to convert it. But I found an easier way which doesn’t require running a conversion. The conversion logic is already implemented in the <code class=\"language-text\">tensorflowjs</code> Python package 😁, so I am just going to use that.</p>\n<h3 id=\"save-the-model\" style=\"position:relative;\"><a href=\"#save-the-model\" aria-label=\"save the model permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Save the model</h3>\n<p>Let’s go through an example, say we want to import the MobileNet network (which is used to detect objects in images) and save it locally. First follow the <a href=\"https://www.tensorflow.org/install/pip\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">instructions here to install the pre-requisites</a>.</p>\n<p>Then we can write the below code to save the model locally:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"26429827171553910000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`from keras.applications import mobilenet\r\nimport tensorflowjs as tfjs\r\n\r\nmobilenet = mobilenet.MobileNet()\r\n\r\nsave_path = &quot;output\\\\mobilenet&quot;\r\ntfjs.converters.save_keras_model(mobilenet, save_path)`, `26429827171553910000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"py\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-py line-numbers\"><code class=\"language-py\"><span class=\"token keyword\">from</span> keras<span class=\"token punctuation\">.</span>applications <span class=\"token keyword\">import</span> mobilenet\r\n<span class=\"token keyword\">import</span> tensorflowjs <span class=\"token keyword\">as</span> tfjs\r\n\r\nmobilenet <span class=\"token operator\">=</span> mobilenet<span class=\"token punctuation\">.</span>MobileNet<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\r\n\r\nsave_path <span class=\"token operator\">=</span> <span class=\"token string\">\"output\\\\mobilenet\"</span>\r\ntfjs<span class=\"token punctuation\">.</span>converters<span class=\"token punctuation\">.</span>save_keras_model<span class=\"token punctuation\">(</span>mobilenet<span class=\"token punctuation\">,</span> save_path<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>َAfter running this, you should have a <code class=\"language-text\">mobilenet</code> folder in the <code class=\"language-text\">output</code> folder which contains the following files:</p>\n<p><img src=\"/6c51a28e6620e02bffe60079f03545cd/savedtensormodel.jpg\" alt=\"Saved keras model in TensorFlow.js format\"></p>\n<p>And guess what, you’re ready to use this and write your first <code class=\"language-text\">TensorFlow.js</code> application.</p>\n<p>If you’re keen to convert the model using the CLI commands, have a look at <a href=\"https://js.tensorflow.org/tutorials/import-keras.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this tutorial</a>.</p>\n<h2 id=\"use-the-model-in-your-application\" style=\"position:relative;\"><a href=\"#use-the-model-in-your-application\" aria-label=\"use the model in your application permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use the model in your application</h2>\n<p>I am using <a href=\"https://angular.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Angular</a> in my example, but feel free to use any framework you prefer or even vanilla JS 🙃.</p>\n<h3 id=\"create-the-app\" style=\"position:relative;\"><a href=\"#create-the-app\" aria-label=\"create the app permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create the app</h3>\n<p>I’ve got the Angular CLI installed already, so I am just going to open up a command prompt and create a new application:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"38411562986123470000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`ng new mobilenet\r\n\r\ncd mobilenet`, `38411562986123470000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-bash line-numbers\"><code class=\"language-bash\">ng new mobilenet\r\n\r\n<span class=\"token builtin class-name\">cd</span> mobilenet</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<h3 id=\"install-the-tensorflowjs\" style=\"position:relative;\"><a href=\"#install-the-tensorflowjs\" aria-label=\"install the tensorflowjs permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Install the TensorFlow.js</h3>\n<p>This will take a few moments (creating the app and installing all the npm packages). When finished add the <code class=\"language-text\">TensorFlow.js</code> package as well:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"48059949657148480000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`npm install @tensorflow/tfjs --save`, `48059949657148480000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-bash line-numbers\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> @tensorflow/tfjs <span class=\"token parameter variable\">--save</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>Once this is finished, copy the content of your saved model into your asset folder and open the folder with your editor of choice (<a href=\"https://code.visualstudio.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">VS Code</a> 😁).</p>\n<p>You will also need to have the image classes for prediction purposes. So create a file somewhere in the project and add the content of <a href=\"https://github.com/tensorflow/tfjs-examples/blob/master/mobilenet/imagenet_classes.js\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this file</a> into it. I’ve put it in my assets folder.</p>\n<h3 id=\"import-the-model-and-use-it\" style=\"position:relative;\"><a href=\"#import-the-model-and-use-it\" aria-label=\"import the model and use it permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Import the model and use it</h3>\n<p>Open your <code class=\"language-text\">app.component.ts</code> file and add the import statement:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"4611194998146483700\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import * as tf from '@tensorflow/tfjs'\r\nimport { IMAGENET_CLASSES } from '../assets/imagenet-classes'`, `4611194998146483700`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-ts line-numbers\"><code class=\"language-ts\"><span class=\"token keyword\">import</span> <span class=\"token operator\">*</span> <span class=\"token keyword\">as</span> tf <span class=\"token keyword\">from</span> <span class=\"token string\">'@tensorflow/tfjs'</span>\r\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> <span class=\"token constant\">IMAGENET_CLASSES</span> <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'../assets/imagenet-classes'</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>We will need some variables to hold our data and some references to out HTML elements we have to use:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"80833110128081850000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`model: tf.Model;\r\nclasses: any[];\r\nimageData: ImageData;\r\n\r\n@ViewChild('chosenImage') img: ElementRef;\r\n@ViewChild('fileUpload') fileUpload: ElementRef;`, `80833110128081850000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-ts line-numbers\"><code class=\"language-ts\">model<span class=\"token operator\">:</span> tf<span class=\"token punctuation\">.</span>Model<span class=\"token punctuation\">;</span>\r\nclasses<span class=\"token operator\">:</span> <span class=\"token builtin\">any</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\nimageData<span class=\"token operator\">:</span> ImageData<span class=\"token punctuation\">;</span>\r\n\r\n<span class=\"token decorator\"><span class=\"token at operator\">@</span><span class=\"token function\">ViewChild</span></span><span class=\"token punctuation\">(</span><span class=\"token string\">'chosenImage'</span><span class=\"token punctuation\">)</span> img<span class=\"token operator\">:</span> ElementRef<span class=\"token punctuation\">;</span>\r\n<span class=\"token decorator\"><span class=\"token at operator\">@</span><span class=\"token function\">ViewChild</span></span><span class=\"token punctuation\">(</span><span class=\"token string\">'fileUpload'</span><span class=\"token punctuation\">)</span> fileUpload<span class=\"token operator\">:</span> ElementRef<span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>The model will have our model, classes is used for our prediction result, and <code class=\"language-text\">imageData</code> is used to convert our file upload file to it and show it in an image on the page.</p>\n<p>And two global variables, the image size is the size which model is trained on, and the number of predictions we want to pick from prediction result (since it is an array):</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"40459306663294620000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const IMAGE_SIZE = 224\r\nconst TOPK_PREDICTIONS = 5`, `40459306663294620000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-ts line-numbers\"><code class=\"language-ts\"><span class=\"token keyword\">const</span> <span class=\"token constant\">IMAGE_SIZE</span> <span class=\"token operator\">=</span> <span class=\"token number\">224</span>\r\n<span class=\"token keyword\">const</span> <span class=\"token constant\">TOPK_PREDICTIONS</span> <span class=\"token operator\">=</span> <span class=\"token number\">5</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<p>Time to add a method to load the model and call it in the <code class=\"language-text\">ngOnInit</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"73654342719948350000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`ngOnInit() {\r\n  this.loadModel();\r\n}\r\n\r\nasync loadModel() {\r\n  this.model = await tf.loadModel('/assets/model.json');\r\n}`, `73654342719948350000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-ts line-numbers\"><code class=\"language-ts\"><span class=\"token function\">ngOnInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">loadModel</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span>\r\n\r\n<span class=\"token keyword\">async</span> <span class=\"token function\">loadModel</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>model <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> tf<span class=\"token punctuation\">.</span><span class=\"token function\">loadModel</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/assets/model.json'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>We will need a file upload and an image in our HTML, so let’s add them:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"62052133948489720000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<div class=&quot;container&quot;>\r\n  <h2>\r\n    Using MobileNet neural network in Angular\r\n  </h2>\r\n  <div class=&quot;upload&quot; [hidden]=&quot;!model&quot;>\r\n    Upload an image:\r\n    <input\r\n      #fileUpload\r\n      type=&quot;file&quot;\r\n      id=&quot;files&quot;\r\n      name=&quot;files[]&quot;\r\n      (change)=&quot;fileChangeEvent(\\$event)&quot;\r\n    />\r\n  </div>\r\n  <div>\r\n    <img\r\n      #chosenImage\r\n      width=&quot;224&quot;\r\n      height=&quot;224&quot;\r\n      class=&quot;sample-image&quot;\r\n    />\r\n    <div class=&quot;predictions&quot;>\r\n      <div\r\n        *ngFor=&quot;let class of classes&quot;\r\n        class=&quot;row&quot;\r\n      >\r\n        <div class=&quot;col-sm-6&quot;>\r\n          {{class.className}}\r\n        </div>\r\n        <div class=&quot;col-sm-6&quot;>\r\n          {{class.probability.toFixed(3)}}\r\n        </div>\r\n      </div>\r\n    </div>\r\n  </div>\r\n</div>`, `62052133948489720000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>container<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h2</span><span class=\"token punctuation\">></span></span>\r\n    Using MobileNet neural network in Angular\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h2</span><span class=\"token punctuation\">></span></span>\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>upload<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">[hidden]</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>!model<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n    Upload an image:\r\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>input</span>\r\n      <span class=\"token attr-name\">#fileUpload</span>\r\n      <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>file<span class=\"token punctuation\">\"</span></span>\r\n      <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>files<span class=\"token punctuation\">\"</span></span>\r\n      <span class=\"token attr-name\">name</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>files[]<span class=\"token punctuation\">\"</span></span>\r\n      <span class=\"token attr-name\">(change)</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>fileChangeEvent($event)<span class=\"token punctuation\">\"</span></span>\r\n    <span class=\"token punctuation\">/></span></span>\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span><span class=\"token punctuation\">></span></span>\r\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>img</span>\r\n      <span class=\"token attr-name\">#chosenImage</span>\r\n      <span class=\"token attr-name\">width</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>224<span class=\"token punctuation\">\"</span></span>\r\n      <span class=\"token attr-name\">height</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>224<span class=\"token punctuation\">\"</span></span>\r\n      <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>sample-image<span class=\"token punctuation\">\"</span></span>\r\n    <span class=\"token punctuation\">/></span></span>\r\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>predictions<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n      <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span>\r\n        <span class=\"token attr-name\">*ngFor</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>let class of classes<span class=\"token punctuation\">\"</span></span>\r\n        <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>row<span class=\"token punctuation\">\"</span></span>\r\n      <span class=\"token punctuation\">></span></span>\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>col-sm-6<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n          {{class.className}}\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>col-sm-6<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\r\n          {{class.probability.toFixed(3)}}\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\r\n      <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\r\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\r\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\r\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>And a bit of CSS to make it pretty:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"96974716805044700000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`.container {\r\n  padding: 200px;\r\n\r\n  .sample-image {\r\n    margin: 20px;\r\n  }\r\n\r\n  .predictions {\r\n    margin-top: 20px;\r\n    width: 100%;\r\n  }\r\n}`, `96974716805044700000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"scss\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-scss line-numbers\"><code class=\"language-scss\"><span class=\"token selector\">.container </span><span class=\"token punctuation\">{</span>\r\n  <span class=\"token property\">padding</span><span class=\"token punctuation\">:</span> 200px<span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token selector\">.sample-image </span><span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">margin</span><span class=\"token punctuation\">:</span> 20px<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token selector\">.predictions </span><span class=\"token punctuation\">{</span>\r\n    <span class=\"token property\">margin-top</span><span class=\"token punctuation\">:</span> 20px<span class=\"token punctuation\">;</span>\r\n    <span class=\"token property\">width</span><span class=\"token punctuation\">:</span> 100%<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>The predictions section in the HTML will have the prediction results in it, we assume it is stored in a variable called <code class=\"language-text\">classes</code>.</p>\n<p>Now let’s go back to our TypeScript file and add the prediction code. Let’s create a handler for our file input first, that will trigger our prediction logic:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"44748891335203480000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`fileChangeEvent(event: any) {\r\n  const file = event.target.files[0];\r\n  if (!file || !file.type.match('image.*')) {\r\n    return;\r\n  }\r\n\r\n  this.classes = [];\r\n\r\n  const reader = new FileReader();\r\n  reader.onload = e => {\r\n    this.img.nativeElement.src = e.target['result'];\r\n    this.predict(this.img.nativeElement);\r\n  };\r\n\r\n  reader.readAsDataURL(file);\r\n}`, `44748891335203480000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-ts line-numbers\"><code class=\"language-ts\"><span class=\"token function\">fileChangeEvent</span><span class=\"token punctuation\">(</span>event<span class=\"token operator\">:</span> <span class=\"token builtin\">any</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">const</span> file <span class=\"token operator\">=</span> event<span class=\"token punctuation\">.</span>target<span class=\"token punctuation\">.</span>files<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>file <span class=\"token operator\">||</span> <span class=\"token operator\">!</span>file<span class=\"token punctuation\">.</span>type<span class=\"token punctuation\">.</span><span class=\"token function\">match</span><span class=\"token punctuation\">(</span><span class=\"token string\">'image.*'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>classes <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token keyword\">const</span> reader <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">FileReader</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  reader<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">onload</span> <span class=\"token operator\">=</span> e <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>img<span class=\"token punctuation\">.</span>nativeElement<span class=\"token punctuation\">.</span>src <span class=\"token operator\">=</span> e<span class=\"token punctuation\">.</span>target<span class=\"token punctuation\">[</span><span class=\"token string\">'result'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">predict</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>img<span class=\"token punctuation\">.</span>nativeElement<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\r\n\r\n  reader<span class=\"token punctuation\">.</span><span class=\"token function\">readAsDataURL</span><span class=\"token punctuation\">(</span>file<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>All we are doing in this method is to read the selected file, put as the source of the image on the page so it’s visible which image is selected and call the predict method. We have call that in the <code class=\"language-text\">onload</code> event handler asynchronously because file has to be read first.</p>\n<p>Let’s now write the prediction code:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"4155439158229423600\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`async predict(imageData: ImageData): Promise<any> {\r\n  this.fileUpload.nativeElement.value = '';\r\n  const startTime = performance.now();\r\n  const logits = tf.tidy(() => {\r\n    // tf.fromPixels() returns a Tensor from an image element.\r\n    const img = tf.fromPixels(imageData).toFloat();\r\n\r\n    const offset = tf.scalar(127.5);\r\n    // Normalize the image from [0, 255] to [-1, 1].\r\n    const normalized = img.sub(offset).div(offset);\r\n\r\n    // Reshape to a single-element batch so we can pass it to predict.\r\n    const batched = normalized.reshape([1, IMAGE_SIZE, IMAGE_SIZE, 3]);\r\n\r\n    // Make a prediction through mobilenet.\r\n    return this.model.predict(batched);\r\n  });\r\n\r\n  // Convert logits to probabilities and class names.\r\n  this.classes = await this.getTopKClasses(logits, TOPK_PREDICTIONS);\r\n  const totalTime = performance.now() - startTime;\r\n  console.log(\\`Done in \\${Math.floor(totalTime)}ms\\`);\r\n}`, `4155439158229423600`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-ts line-numbers\"><code class=\"language-ts\"><span class=\"token keyword\">async</span> <span class=\"token function\">predict</span><span class=\"token punctuation\">(</span>imageData<span class=\"token operator\">:</span> ImageData<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">Promise</span><span class=\"token operator\">&lt;</span><span class=\"token builtin\">any</span><span class=\"token operator\">></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>fileUpload<span class=\"token punctuation\">.</span>nativeElement<span class=\"token punctuation\">.</span>value <span class=\"token operator\">=</span> <span class=\"token string\">''</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">const</span> startTime <span class=\"token operator\">=</span> performance<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">const</span> logits <span class=\"token operator\">=</span> tf<span class=\"token punctuation\">.</span><span class=\"token function\">tidy</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token comment\">// tf.fromPixels() returns a Tensor from an image element.</span>\r\n    <span class=\"token keyword\">const</span> img <span class=\"token operator\">=</span> tf<span class=\"token punctuation\">.</span><span class=\"token function\">fromPixels</span><span class=\"token punctuation\">(</span>imageData<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">toFloat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n    <span class=\"token keyword\">const</span> offset <span class=\"token operator\">=</span> tf<span class=\"token punctuation\">.</span><span class=\"token function\">scalar</span><span class=\"token punctuation\">(</span><span class=\"token number\">127.5</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n    <span class=\"token comment\">// Normalize the image from [0, 255] to [-1, 1].</span>\r\n    <span class=\"token keyword\">const</span> normalized <span class=\"token operator\">=</span> img<span class=\"token punctuation\">.</span><span class=\"token function\">sub</span><span class=\"token punctuation\">(</span>offset<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">div</span><span class=\"token punctuation\">(</span>offset<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n    <span class=\"token comment\">// Reshape to a single-element batch so we can pass it to predict.</span>\r\n    <span class=\"token keyword\">const</span> batched <span class=\"token operator\">=</span> normalized<span class=\"token punctuation\">.</span><span class=\"token function\">reshape</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">IMAGE_SIZE</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">IMAGE_SIZE</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n    <span class=\"token comment\">// Make a prediction through mobilenet.</span>\r\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>model<span class=\"token punctuation\">.</span><span class=\"token function\">predict</span><span class=\"token punctuation\">(</span>batched<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token comment\">// Convert logits to probabilities and class names.</span>\r\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>classes <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">getTopKClasses</span><span class=\"token punctuation\">(</span>logits<span class=\"token punctuation\">,</span> <span class=\"token constant\">TOPK_PREDICTIONS</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">const</span> totalTime <span class=\"token operator\">=</span> performance<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> startTime<span class=\"token punctuation\">;</span>\r\n  <span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">Done in </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>Math<span class=\"token punctuation\">.</span><span class=\"token function\">floor</span><span class=\"token punctuation\">(</span>totalTime<span class=\"token punctuation\">)</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">ms</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>If you don’t understand the code here at the first glance, it’s perfectly OK. Since we have imported the model we have to know a bit about its internals.</p>\n<p>First of all I am using the <code class=\"language-text\">tidy</code> function as I mentioned earlier to make sure we don’t have any memory leak.</p>\n<p>Then I will convert the image to a float data type which is expected in this model.</p>\n<p>Once that is done we need to normalise the data from [0, 255] range to [-1, 1]. The <code class=\"language-text\">scalar</code> operation creates a tensor with provided value.</p>\n<p>And at last we need to reshape our input to a [1, IMAGE_SIZE, IMAGE_SIZE, 3] tensor and call predict.</p>\n<p>Once we got the result we have to extract the top <code class=\"language-text\">x</code> classes from the result, so let’s implement that:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"25771632503235707000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`async getTopKClasses(logits, topK): Promise<any[]> {\r\n  const values = await logits.data();\r\n\r\n  const valuesAndIndices = [];\r\n  for (let i = 0; i < values.length; i++) {\r\n    valuesAndIndices.push({ value: values[i], index: i });\r\n  }\r\n  valuesAndIndices.sort((a, b) => {\r\n    return b.value - a.value;\r\n  });\r\n  const topkValues = new Float32Array(topK);\r\n  const topkIndices = new Int32Array(topK);\r\n  for (let i = 0; i < topK; i++) {\r\n    topkValues[i] = valuesAndIndices[i].value;\r\n    topkIndices[i] = valuesAndIndices[i].index;\r\n  }\r\n\r\n  const topClassesAndProbs = [];\r\n  for (let i = 0; i < topkIndices.length; i++) {\r\n    topClassesAndProbs.push({\r\n      className: IMAGENET_CLASSES[topkIndices[i]],\r\n      probability: topkValues[i]\r\n    });\r\n  }\r\n  return topClassesAndProbs;\r\n}`, `25771632503235707000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-ts line-numbers\"><code class=\"language-ts\"><span class=\"token keyword\">async</span> <span class=\"token function\">getTopKClasses</span><span class=\"token punctuation\">(</span>logits<span class=\"token punctuation\">,</span> topK<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">Promise</span><span class=\"token operator\">&lt;</span><span class=\"token builtin\">any</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token operator\">></span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token keyword\">const</span> values <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> logits<span class=\"token punctuation\">.</span><span class=\"token function\">data</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n\r\n  <span class=\"token keyword\">const</span> valuesAndIndices <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> values<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    valuesAndIndices<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> value<span class=\"token operator\">:</span> values<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> index<span class=\"token operator\">:</span> i <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  valuesAndIndices<span class=\"token punctuation\">.</span><span class=\"token function\">sort</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">,</span> b<span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> b<span class=\"token punctuation\">.</span>value <span class=\"token operator\">-</span> a<span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">const</span> topkValues <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Float32Array</span><span class=\"token punctuation\">(</span>topK<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">const</span> topkIndices <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Int32Array</span><span class=\"token punctuation\">(</span>topK<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> topK<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    topkValues<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> valuesAndIndices<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">;</span>\r\n    topkIndices<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> valuesAndIndices<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>index<span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n\r\n  <span class=\"token keyword\">const</span> topClassesAndProbs <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> topkIndices<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\r\n    topClassesAndProbs<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\r\n      className<span class=\"token operator\">:</span> <span class=\"token constant\">IMAGENET_CLASSES</span><span class=\"token punctuation\">[</span>topkIndices<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n      probability<span class=\"token operator\">:</span> topkValues<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\r\n  <span class=\"token punctuation\">}</span>\r\n  <span class=\"token keyword\">return</span> topClassesAndProbs<span class=\"token punctuation\">;</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Alright, remember we called this method with the result of predict in shape of a <code class=\"language-text\">logit</code>. Logits are normally either a <code class=\"language-text\">Tensor</code> or an array or a typed array.</p>\n<p>So basically we get the result by awaiting the <code class=\"language-text\">data</code> method. Then we create a new array using the value and index of the output of that.</p>\n<p>And based on that we calculate the resulting classes based on the formed array. Again you don’t need to understand what the code does, at a high level it is just mapping the result to one of the classes we got from the GitHub repository earlier.</p>\n<h2 id=\"running-the-app\" style=\"position:relative;\"><a href=\"#running-the-app\" aria-label=\"running the app permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Running the app</h2>\n<p>You can now run the app and go to <code class=\"language-text\">localhost:4200</code> and upload an image and see what predictions you get 😎.</p>\n<p><video controls playsinline webkit-playsinline \r\nstyle=\"max-width: 900px\"\r\nsrc=\"/f74ed381f4512a2f1ac7a1979d68396d/Mobilenet.webm\" preload=\"auto\">\r\nYour browser does not support the video tag.\r\n</video></p>\n<p>And that’s how easy it is to get started. Hope you’ve enjoyed this post and be sure there will be more.</p>\n<p>You can find the <a href=\"https://github.com/yashints/mobilenet\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">code for this post here</a>.</p>","timeToRead":8,"frontmatter":{"unformattedDate":"2018-11-27T00:00:00.000Z","date":"November 27, 2018","title":"Getting started on TensorFlow.js","subtitle":null,"id":null,"author":"Yaser Adel Mehraban","gravatar":null,"canonical_url":null,"tags":["tensorflowjs","machine learning"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQBAv/EABcBAAMBAAAAAAAAAAAAAAAAAAACAwX/2gAMAwEAAhADEAAAAeKJ0NMwN//EABcQAAMBAAAAAAAAAAAAAAAAAAADEiD/2gAIAQEAAQUCJXn/xAAYEQACAwAAAAAAAAAAAAAAAAABAhARQf/aAAgBAwEBPwErex//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAXEAADAQAAAAAAAAAAAAAAAAAAASAx/9oACAEBAAY/AjXP/8QAGRAAAQUAAAAAAAAAAAAAAAAAAQARICEx/9oACAEBAAE/IRtuiCN//9oADAMBAAIAAwAAABDI3//EABYRAQEBAAAAAAAAAAAAAAAAAAEREP/aAAgBAwEBPxBFaM//xAAWEQEBAQAAAAAAAAAAAAAAAAABEBH/2gAIAQIBAT8QHJ//xAAcEAADAAEFAAAAAAAAAAAAAAABESEAIDFBYcH/2gAIAQEAAT8QEGgBcB3vETvu55p//9k="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/3822b8d06991f778b87e87318f8d2676/d49f5/tfjs.jpg","srcSet":"/static/3822b8d06991f778b87e87318f8d2676/d49f5/tfjs.jpg 650w","sizes":"(min-width: 650px) 650px, 100vw"},"sources":[{"srcSet":"/static/3822b8d06991f778b87e87318f8d2676/b7f59/tfjs.webp 650w","type":"image/webp","sizes":"(min-width: 650px) 650px, 100vw"}]},"width":650,"height":360}}},"cover":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB8ElEQVR42h3P2W7TQBQGYL814hW4RgLxBtwh0UogFEpLGjvxDE5bJ7bjeN+XmbE9422yiGmlT//NOf+RjvQcYCNnRk7NnPpojJopwmNExpCMPh59NKXNnHVz2r7KuznCg1/3iRiVrQTT1sZcsOpJ8+t9hNyS2XkrWGlzEEcT4pTUyRovJ2FN0+6U0ks9XGM8Squ42VajieZdwW6X2s09uIf7O1VfbPQ7sFd23nJr3am7P2C/UJ/FyKuHpDtn7BLUgyTHDahGWIz7avLbS0gvQXtO2DWiV5ExvUbsEvcirwG9Bt05aE8u4Qd8MvJekpMGFgMIWuA1Wtjtsv5YTx6ZA3KKO+HskpOLuYtn543fvP5oIq5nVJKdCrpEtbEog6CDAdVCqifMLicHccGuuYW4Rbj11jEQPzTzgfCXjEmyWWge0RwEvXbt4JVZPJrF2m3UkKkBBQEDIQNJD8oJIg5rrqbT6jjplShTSXHI5lHWfn7755HfW//7o36jGA9mBiMGhPgtX1GQDE9kul3O79+dvz7wp7qTgN/eLuRPHz8rx0qxy7WHN2G7OlYgpDDuYczEFZj0MO1B1G+r4dfL/OEL//E0qRGRlnq8sqp7o1SOtSw4SPbJ2q4UM1OsXDFEFrKLZA8rHhYLa68GCdr4+O8+/w9jCBBi2K1sxwAAAABJRU5ErkJggg=="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/f204acbc37d9c8b9f9c05cc493495d64/a2195/socialpreview.png","srcSet":"/static/f204acbc37d9c8b9f9c05cc493495d64/cadb4/socialpreview.png 750w,\n/static/f204acbc37d9c8b9f9c05cc493495d64/a2195/socialpreview.png 800w","sizes":"(min-width: 800px) 800px, 100vw"},"sources":[{"srcSet":"/static/f204acbc37d9c8b9f9c05cc493495d64/11ba1/socialpreview.webp 750w,\n/static/f204acbc37d9c8b9f9c05cc493495d64/5f042/socialpreview.webp 800w","type":"image/webp","sizes":"(min-width: 800px) 800px, 100vw"}]},"width":800,"height":400}}},"img":{"publicURL":"/static/3822b8d06991f778b87e87318f8d2676/tfjs.jpg"},"socialPreview":{"publicURL":"/static/f204acbc37d9c8b9f9c05cc493495d64/socialpreview.png"}}}},"pageContext":{"title":"Getting started on TensorFlow.js","postPath":"/blog/2018/11/27/get-started-with-tensorflowjs","nextPost":"/blog/2018/12/06/angular-virtual-scroll","previousPost":"/blog/2018/11/23/web-perf-5"}},"staticQueryHashes":[],"slicesMap":{}}