Using a basic pre-trained TensorFlow.js saved model in the web browser

Posted by

Using a pre-trained TensorFlow.js model in the browser

Using a pre-trained TensorFlow.js model in the browser

TensorFlow.js is a library for machine learning in JavaScript. With TensorFlow.js, we can run machine learning models directly in the browser, which opens up a wide range of possibilities for creating interactive applications and experiences.

Using a simple raw TensorFlow.js pre-trained saved model

In this article, we will explore how to use a simple pre-trained TensorFlow.js model in the browser. Specifically, we will be using a saved model that has been trained to recognize handwritten digits.

Step 1: Load the pre-trained model

First, we need to load the pre-trained model into our application. We can do this by using the tf.loadLayersModel method provided by TensorFlow.js. This method allows us to load a model that has been saved in the .json format.


        const model = await tf.loadLayersModel('path/to/saved/model.json');
    

Step 2: Preprocess the input data

Before we can use the model to make predictions, we need to preprocess the input data. In the case of the handwritten digits model, we will need to convert the user’s input (a drawn digit) into a format that the model can understand. In this example, we will use the ImageData object from a canvas element to represent the input data.

Step 3: Make predictions with the model

Once we have loaded the model and preprocessed the input data, we can use the model to make predictions. We can do this by calling the model.predict method and passing in the preprocessed input data.


        const predictions = model.predict(preprocessedData);
    

Step 4: Display the predictions

Finally, we can display the predictions made by the model to the user. In the case of the handwritten digits model, we can display the predicted digit along with a confidence score for the prediction.

Conclusion

Using a pre-trained TensorFlow.js model in the browser is a powerful way to bring machine learning capabilities to web applications. With just a few lines of code, we can load a pre-trained model and use it to make predictions on user input. This opens up a wide range of possibilities for creating interactive and intelligent web experiences.

0 0 votes
Article Rating
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@GoogleDevelopers
6 months ago

Catch more episodes from Machine Learning for Web Developers (Web ML) → https://goo.gle/learn-WebML

@ateshirsizi2
6 months ago

where do you copy the link from storage googleapis?

@user-dv6jk8iz9y
6 months ago

Awesome & nice explanation!!!!!with live code session..

@faraazkhan
6 months ago

I am running my reactjs app on localhost and passing model.json file path to load model

I got error console .
Load local model not allowed

@rundeks
6 months ago

I get a 404 for the given model path.

@melaniearcher6269
6 months ago

Okay, perhaps I'm dim, but I can't find how you've run script.js on Glitch. Is it really sufficient to preview index.html to start up the loadModel() function?

@invisibellrobe
6 months ago

This is an awesome tutorial!

@jaynouoliverbeck6181
6 months ago

how can we use it in angular applications on GCP

@praveenvattikonda2740
6 months ago

How to access the code base ?

@monkwhocode
6 months ago

Where is the code to copy?