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.
Catch more episodes from Machine Learning for Web Developers (Web ML) → https://goo.gle/learn-WebML
where do you copy the link from storage googleapis?
Awesome & nice explanation!!!!!with live code session..
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
I get a 404 for the given model path.
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?
This is an awesome tutorial!
how can we use it in angular applications on GCP
How to access the code base ?
Where is the code to copy?