{"id":4255,"date":"2023-11-04T23:14:10","date_gmt":"2023-11-04T23:14:10","guid":{"rendered":"http:\/\/localhost:10003\/how-to-use-tensorflow-js-for-image-classification\/"},"modified":"2023-11-05T05:47:55","modified_gmt":"2023-11-05T05:47:55","slug":"how-to-use-tensorflow-js-for-image-classification","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-use-tensorflow-js-for-image-classification\/","title":{"rendered":"How to Use TensorFlow.js for Image Classification"},"content":{"rendered":"
Image classification is a common task in machine learning where we train a model to classify images into different categories or labels. TensorFlow.js is a powerful library that allows us to run machine learning models directly in the browser using JavaScript. In this tutorial, we will learn how to use TensorFlow.js for image classification.<\/p>\n
To follow along with this tutorial, you need to have the following prerequisites:<\/p>\n
To begin, let’s set up a new project with TensorFlow.js and create a basic HTML file.<\/p>\n
Open your terminal and create a new directory for your project.<\/p>\n
mkdir tfjs-image-classification\n<\/code><\/pre>\nNavigate into the project directory.<\/p>\n
cd tfjs-image-classification\n<\/code><\/pre>\nStep 2: Initialize a New Node.js Project<\/h3>\n
Next, let’s initialize a new Node.js project in the project directory.<\/p>\n
npm init -y\n<\/code><\/pre>\nThis will create a package.json<\/code> file with default values.<\/p>\nStep 3: Install TensorFlow.js<\/h3>\n
Install TensorFlow.js as a project dependency.<\/p>\n
npm install @tensorflow\/tfjs @tensorflow-models\/mobilenet\n<\/code><\/pre>\nThis will install TensorFlow.js and the MobileNet image classification model.<\/p>\n
Step 4: Create the HTML File<\/h3>\n
Create a new file called index.html<\/code> in the project directory and add the following code:<\/p>\n<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <title>Image Classification with TensorFlow.js<\/title>\n <\/head>\n <body>\n <input type=\"file\" id=\"image-selector\" accept=\"image\/*\" \/>\n <button id=\"predict-button\">Predict<\/button>\n <div id=\"prediction-container\"><\/div>\n\n <script src=\"index.js\"><\/script>\n <\/body>\n<\/html>\n<\/code><\/pre>\nThis HTML file includes an input element for selecting an image, a predict button, and a container for displaying the predictions. We also included a JavaScript file called index.js<\/code> that we will create in the next step.<\/p>\nStep 5: Create the JavaScript File<\/h3>\n
Create a new file called index.js<\/code> in the project directory and add the following code:<\/p>\n\/\/ Code goes here\n<\/code><\/pre>\nWe will now write the JavaScript code to load the MobileNet model, make predictions, and display the results.<\/p>\n
Writing the JavaScript Code<\/h2>\n
Let’s start writing the JavaScript code in index.js<\/code>.<\/p>\nLoading the MobileNet Model<\/h3>\n
To use the MobileNet model for image classification, we need to load it first. Add the following code inside the index.js<\/code> file:<\/p>\nlet model;\n\nasync function loadModel() {\n model = await tf.loadGraphModel('https:\/\/tfhub.dev\/google\/tfjs-model\/imagenet\/mobilenet_v2_100_224\/feature_vector\/4\/default\/1', {\n fromTFHub: true\n });\n\n console.log('Model loaded');\n\n \/\/ Enable predict button\n document.querySelector('#predict-button').disabled = false;\n}\n\nloadModel();\n<\/code><\/pre>\nThis code defines the loadModel<\/code> function, which loads the MobileNet model using tf.loadGraphModel<\/code> function from TensorFlow.js. We pass the URL of the model and set fromTFHub<\/code> option to true<\/code> to load the model from TensorFlow Hub. Once the model is loaded, we log a message to the console and enable the predict button.<\/p>\nPreprocessing the Image<\/h3>\n
Before we can make predictions with the loaded model, we need to preprocess the selected image. Add the following code after the loadModel<\/code> function:<\/p>\nlet inputImage;\n\nfunction preprocessImage(image) {\n \/\/ Resize the image to 224x224\n const resizedImage = tf.image.resizeBilinear(image, [224, 224]);\n\n \/\/ Normalize the pixels\n const mean = tf.tensor1d([123.68, 116.779, 103.939]);\n const normalizedImage = resizedImage.sub(mean);\n\n \/\/ Expand the dimensions for the model input\n inputImage = normalizedImage.expandDims(0);\n\n return inputImage;\n}\n<\/code><\/pre>\nThis code defines the preprocessImage<\/code> function, which takes an image tensor as input and applies the necessary preprocessing steps. We resize the image to 224x224<\/code> pixels, normalize the pixel values using the mean values [123.68, 116.779, 103.939]<\/code>, and expand the dimensions to match the input shape of the model.<\/p>\nMaking Predictions<\/h3>\n
Next, let’s write the code to make predictions using the loaded model. Add the following code after the preprocessImage<\/code> function:<\/p>\nasync function predict() {\n const imageSelector = document.getElementById('image-selector');\n const image = imageSelector.files[0];\n\n const reader = new FileReader();\n\n reader.onload = async function(event) {\n const imageElement = document.createElement('img');\n imageElement.src = event.target.result;\n document.body.appendChild(imageElement);\n\n await tf.nextFrame();\n\n const imageData = tf.browser.fromPixels(imageElement);\n const preprocessedImage = preprocessImage(imageData);\n\n const predictions = await model.predict(preprocessedImage);\n\n \/\/ Display top 5 predictions\n const topPredictions = Array.from(predictions.dataSync())\n .map((prediction, index) => ({\n probability: prediction,\n className: IMAGENET_CLASSES[index]\n }))\n .sort((a, b) => b.probability - a.probability)\n .slice(0, 5);\n\n const predictionContainer = document.getElementById('prediction-container');\n predictionContainer.innerHTML = '';\n\n topPredictions.forEach(({ className, probability }) => {\n const row = document.createElement('div');\n row.innerHTML = `${className}: ${(probability * 100).toFixed(2)}%`;\n predictionContainer.appendChild(row);\n });\n };\n\n reader.readAsDataURL(image);\n}\n\nconst predictButton = document.getElementById('predict-button');\npredictButton.addEventListener('click', predict);\n<\/code><\/pre>\nThis code defines the predict<\/code> function, which is called when the predict button is clicked. Inside the predict<\/code> function, we get the selected image from the input element, read the image using FileReader<\/code>, and create an img<\/code> element to display the selected image on the page.<\/p>\nAfter that, we preprocess the image using the preprocessImage<\/code> function we defined earlier. We then make predictions using the model’s predict<\/code> function on the preprocessed image. The predictions are an array of probabilities for each class in the ImageNet dataset.<\/p>\nFinally, we display the top 5 predictions with the highest probabilities in the prediction-container<\/code> div on the webpage.<\/p>\nAdding the ImageNet Classes<\/h3>\n
To display the class names alongside the predictions, we need to define the class names for the ImageNet dataset. Add the following code above the loadModel<\/code> function:<\/p>\nconst IMAGENET_CLASSES = [\n 'tench',\n 'goldfish',\n 'great_white_shark',\n \/\/ ...\n \/\/ List of 1000 class names\n \/\/ ...\n];\n<\/code><\/pre>\nReplace the comment with the complete list of 1000 class names from the ImageNet dataset. You can find the complete list of class names in the TensorFlow.js repository.<\/p>\n
Testing the Classification Model<\/h2>\n
Now that our project is set up and the JavaScript code is written, it’s time to test the image classification model.<\/p>\n
To run the project, open your terminal and navigate to the project directory. Then, run the following command:<\/p>\n
npm start\n<\/code><\/pre>\nThis will start a local server and display a URL. Open the URL in your web browser to see the webpage.<\/p>\n
\n- Click on the “Choose File” button to select an image from your local machine.<\/li>\n
- Click the “Predict” button to make predictions based on the selected image.<\/li>\n
- The top 5 predictions with their respective probabilities will be displayed on the webpage.<\/li>\n<\/ol>\n
Congratulations! You have successfully built an image classification model using TensorFlow.js.<\/p>\n
Conclusion<\/h2>\n
In this tutorial, we learned how to use TensorFlow.js for image classification. We set up a project with TensorFlow.js, loaded the MobileNet model, preprocessed the image, and made predictions using the model. TensorFlow.js makes it easy to run machine learning models in the browser using JavaScript, opening up new possibilities for creating interactive machine learning applications.<\/p>\n","protected":false},"excerpt":{"rendered":"
Image classification is a common task in machine learning where we train a model to classify images into different categories or labels. TensorFlow.js is a powerful library that allows us to run machine learning models directly in the browser using JavaScript. In this tutorial, we will learn how to use Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[39,325,768,41,328,49,1882],"yoast_head":"\nHow to Use TensorFlow.js for Image Classification - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n