Module for implementing the MobileNET Convolutional Neural Network Model for Image Classification.
See
- 1.0) Project description
- 1.1) Requirements (For W10/11 OS)
- 1.2) Project execution
- 1.3) Possible solutions for library installation errors
See
This module implements the MobileNet convolutional neural network model for image classification using TensorFlow.js in Node.js. It allows efficient image classification with the pre-trained MobileNet model. The MobileNet architecture is optimized for mobile and embedded vision applications while maintaining high accuracy. This implementation includes:
- Integration with TensorFlow.js and Node.js for server-side image classification
- Pre-trained MobileNet model weights for fast inference without training
- Support for classifying common objects across 1000 categories
- Efficient processing pipeline for image loading and classification
- Example code showing how to use the model for real-world image classification tasks
- High performance with low computational requirements compared to larger CNN models
See
* [Microsoft Visual Studio](https://bobbyhadz.com/blog/npm-err-gyp-err-find-vs-you-need-to-install-the-latest-version) - Required for building native Node.js modules and TensorFlow.js bindings * [Python](https://www.python.org/downloads/) - Version 3.6 or higher, needed for TensorFlow.js and node-gyp * [Node.js](https://nodejs.org/) - Version 14.x or higher recommended * [npm](https://www.npmjs.com/) - Node.js package manager (comes with Node.js) * At least 2GB of RAM. * 1GB of free disk space.
See
- Create a working environment through an IDE (Visual Studio Code recommended for best TensorFlow.js integration)
- Clone the Project (
git clone https://github.com/andresWeitzel/Modulo_MobileNET_CNN_Tensorflow_NodeJs) IMPORTANT: For using tensorflow on w10/11 it is necessary to have the Desktop development with C++ add-on for visual studio installed. To install it follow these steps. In addition to having Python installed and configured its PATH.- Inside the directory install all implemented plugins
npm update(Update npm repositories)npm install -g npm@latest(Latest compatible version of npm)npm install -S @tensorflow/tfjs(TensorFlow.js Core) - Core TensorFlow.js library for building and training neural networksnpm install -S @tensorflow/tfjs-node(TensorFlow.js Node.js extension) - Node.js binding providing CPU/GPU accelerationnpm install -S @tensorflow-models/mobilenet(MobileNet Model) - Pre-trained MobileNet model for image classification
- Verify installations:
- Run
node -vto confirm Node.js version - Run
npm -vto verify npm version - Run
python --versionto check Python installation
- Run
- Test the setup:
- Create a test file and import TensorFlow.js
- Try loading the MobileNet model
- If any errors occur, check the troubleshooting section
See
//Imports
const tf = require('@tensorflow/tfjs');
const tfnode = require('@tensorflow/tfjs-node');
const fs = require('fs');
module.exports.exec = (path) => {
const imageBuffer = fs.readFileSync(path);
const tfimage = tfnode.node.decodeImage(imageBuffer);
return tfimage;
}- Reading the image file from the specified path
- Converting the image into a format that TensorFlow can process
- Decoding the image buffer into a tensor
Key components:
- Uses fs.readFileSync() to read the image file into a buffer
- Uses tfnode.node.decodeImage() to convert the buffer into a TensorFlow tensor
- Returns the tensor for use in classification
See
//Imports
const read= require('../fileSystem/read');
const mobileNet = require('@tensorflow-models/mobilenet');
const classificator = async(path) =>{
try {
const img = read.exec(path);
const model = await mobileNet.load();
const predictions = await model.classify(img);
console.log('Classification Results : ', predictions);
} catch (error) {
console.log(error);
}
}
if (process.argv.length !== 3) throw new Error('Incorrect arguments!');
classificator(process.argv[2]);- Images are stored in the
src/imgdirectory - We select an image for the model to classify.
- In my case I have removed the image metadata and name so that the model classifies according to its confidence level.
- We execute the model from
src/runners/classificatorwithnode classificator.js ../img/test04.jpg - Expected Output...
2022-11-20 20:09:04.459159: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in
performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Classification Results : [
{ className: 'espresso', probability: 0.9372739195823669 },
{ className: 'cup', probability: 0.02711259014904499 },
{ className: 'mortar', probability: 0.008303580805659294 }
]- High probability for
espresso(espresso coffee). High Confidence Interval and Correct Classification.
- In my case I have removed the image metadata and name so that the model classifies according to its confidence level.
- We execute the model from
src/runners/classificatorwithnode classificator.js ../img/test05.jpg - Expected Output...
2022-11-20 20:11:07.652384: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in
performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Classification Results : [
{
className: 'microwave, microwave oven',
probability: 0.3682105839252472
},
{ className: 'coffeepot', probability: 0.08597368746995926 },
{ className: 'paper towel', probability: 0.08168061077594757 }
]- High probability for
microwave(microwave oven). We can notice that the Model does not apply an acceptable confidence interval for several objects. The model managed to predict that there is acoffeepot(coffee maker) but with low probability. Low Confidence Interval and Correct Classification. For this case, a fine-tuning of model parameters should be applied.


