/web-to-ai-ml-integrations

Client-side Machine Learning with TensorFlow.js

Follow our step-by-step guide on client-side ML with TensorFlow.js and build AI models directly in your browser.

Book a free  consultation
4.9
Clutch rating 🌟
600+
Happy partners
17+
Countries served
190+
Team members
Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

Client-side Machine Learning with TensorFlow.js

Client-side Machine Learning with TensorFlow.js: An In-Depth Guide

 

This guide will walk you through technical challenges you might face when implementing machine learning directly within the client (browser) environment using TensorFlow.js. It covers loading the library, preparing data, constructing models, running inference, handling on-device training, integrating with UI events, and managing memory. Every technical term is explained along the way for clarity.

Loading and Configuring TensorFlow.js

 

  • TensorFlow.js is a JavaScript library that allows you to define, train, and run machine learning models entirely in the browser using WebGL acceleration for enhanced performance.
  • To begin, include the library directly in your HTML file. This simplifies deployment since the entire model runs in the client without the need for backend servers.

// Include TensorFlow.js via CDN in your HTML file


// Optionally, for pre-trained models, you can include additional libraries such as the layers library

  • This setup allows you to get started on data preprocessing, model creation, training, and inference right from the browser.

Data Preprocessing on the Client

 

  • Data preprocessing is the step in which raw input data is transformed into a format acceptable for the machine learning model.
  • For client-side image analysis, you might process images from a file input or a webcam. Converting pixel data into tensors is essential.

// Example: Converting an image from an HTML canvas into a tensor
const canvas = document.getElementById('canvasElement'); // Your canvas element
const imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);

// tf.browser.fromPixels converts image data (pixels) into a tensor representation
const tensor = tf.browser.fromPixels(imageData).toFloat();

// Normalize the tensor between 0 and 1 if necessary for your model
const normalizedTensor = tensor.div(tf.scalar(255));
  • The Tensor is a multi-dimensional array used to represent data in mathematical operations within TensorFlow.js.

Loading a Pre-trained Model

 

  • Often, you may not have the resources to train a model from scratch. TensorFlow.js allows you to load pre-trained models hosted remotely.
  • This step explains how to load a model exported from Python or trained with TensorFlow.js itself.

// Load a model from a URL
const modelUrl = 'https://example.com/path/to/your/model.json';
tf.loadLayersModel(modelUrl)
  .then(model => {
    // Model is loaded and ready for inference
    console.log('Model loaded successfully!');
    
    // Example: run inference on preprocessed tensor image
    const prediction = model.predict(normalizedTensor.expandDims(0));
    prediction.print();
  })
  .catch(error => {
    console.error('Error loading the model:', error);
  });
  • Layers refers to the building blocks of a neural network organized in sequential or functional forms.

Building and Training a Model in the Browser

 

  • You can create models directly in the browser using the TensorFlow.js layers API.
  • This example demonstrates constructing a small model to classify basic data and training it with client-sourced data.

// Define a simple sequential model
const model = tf.sequential();

// Add a dense layer with 16 neurons and input shape corresponding to your data features
model.add(tf.layers.dense({units: 16, activation: 'relu', inputShape: [10]}));

// Add an output layer with a softmax function for classification (e.g., 3 classes)
model.add(tf.layers.dense({units: 3, activation: 'softmax'}));

// Compile the model with an optimizer and loss function
model.compile({
  optimizer: 'adam',  // Adam is an optimization algorithm that adjusts learning rates dynamically
  loss: 'categoricalCrossentropy', // Suitable for multi-class classification
  metrics: ['accuracy']
});

// Dummy training data
const xs = tf.randomNormal([100, 10]);    // 100 samples, 10 features each
const ys = tf.oneHot(tf.floor(tf.randomUniform([100], 0, 3)), 3);   // 100 samples, 3 classes one-hot encoded

// Train the model in the browser
model.fit(xs, ys, {
  epochs: 10,  // Number of training iterations over the entire dataset
  callbacks: {
    onEpochEnd: (epoch, logs) => {
      console.log(`Epoch ${epoch}: loss = ${logs.loss}`);
    }
  }
}).then(() => {
  console.log('Training complete.');
});
  • This process allows live training in the client's browser using the available computational resources.

Performing Real-time Inference

 

  • Inference refers to using the trained model to predict outputs given new inputs.
  • Integrating real-time input (like from a webcam) and running predictions can be achieved with efficient processing loops.

// Assume you have a function that captures an image frame from a video element
async function runInference() {
  // Capture image data from a video element
  const video = document.getElementById('videoElement');
  
  // Create a tensor from the video frame
  const videoTensor = tf.browser.fromPixels(video).toFloat().div(tf.scalar(255));
  const resizedTensor = tf.image.resizeBilinear(videoTensor, [224, 224]);  // Resize to match model input
  
  // Expand dimensions to match model's input shape (batch size of 1)
  const input = resizedTensor.expandDims(0);
  
  // Run prediction
  const output = model.predict(input);
  
  // Process the output, e.g. pick the class with the highest probability
  output.argMax(-1).print();
  
  // Dispose unused tensors to free memory
  tf.dispose([videoTensor, resizedTensor, input, output]);
  
  // Request the next frame to prevent blocking UI
  requestAnimationFrame(runInference);
}

runInference();
  • This code demonstrates continuous inference from a live feed. The requestAnimationFrame function allows the process to integrate smoothly with the browser’s refresh cycle.

Memory Management with tf.tidy

 

  • In client environments, memory is a limited resource. TensorFlow.js provides the tf.tidy function to automatically release intermediate tensors to avoid memory leaks.
  • This is particularly important in iterative operations or real-time video processing.

function processFrame() {
  tf.tidy(() => {
    const video = document.getElementById('videoElement');
    const videoTensor = tf.browser.fromPixels(video).toFloat().div(tf.scalar(255));
    const resizedTensor = tf.image.resizeBilinear(videoTensor, [224, 224]);
    const input = resizedTensor.expandDims(0);
    
    // Make a prediction inside the tidy block
    const output = model.predict(input);
    output.argMax(-1).print();
  });
  
  requestAnimationFrame(processFrame);
}

processFrame();
  • The tf.tidy function automatically cleans up all intermediate tensors created inside its scope once the function finishes execution.

Integrating with UI Interactions

 

  • Integrate TensorFlow.js functions with your website’s UI to offer interactive experiences.
  • This can include starting model training on a button click, updating visualizations with inference results, or offering dynamic feedback.

// Example: Start training when a user clicks a button
document.getElementById('trainButton').addEventListener('click', () => {
  model.fit(xs, ys, {
    epochs: 10,
    callbacks: {
      onEpochEnd: (epoch, logs) => {
        // Update UI element with the current loss value
        document.getElementById('status').innerText = `Epoch ${epoch}: loss = ${logs.loss}`;
      }
    }
  }).then(() => {
    document.getElementById('status').innerText = 'Training complete!';
  });
});
  • By connecting UI events with machine learning operations, you can create responsive and interactive client-side applications.

Summary

 

  • TensorFlow.js opens the door for high-performance machine learning directly in the browser by leveraging WebGL acceleration and flexible APIs.
  • This guide provided a step-by-step walkthrough covering data preprocessing, model loading and training, inference, real-time updates, and memory management.
  • Understanding each component—from tensors to layers to memory management functions—ensures you can implement robust, efficient, and interactive ML applications in client-side JavaScript.


Recognized by the best

Trusted by 600+ businesses globally

From startups to enterprises and everything in between, see for yourself our incredible impact.

RapidDev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with.

They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

Arkady
CPO, Praction
Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost.

He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Donald Muir
Co-Founder, Arc
RapidDev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space.

They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Mat Westergreen-Thorne
Co-CEO, Grantify
RapidDev is an excellent developer for custom-code solutions.

We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Emmanuel Brown
Co-Founder, Church Real Estate Marketplace
Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 

This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Samantha Fekete
Production Manager, Media Production Company
The pSEO strategy executed by RapidDev is clearly driving meaningful results.

Working with RapidDev has delivered measurable, year-over-year growth. Comparing the same period, clicks increased by 129%, impressions grew by 196%, and average position improved by 14.6%. Most importantly, qualified contact form submissions rose 350%, excluding spam.

Appreciation as well to Matt Graham for championing the collaboration!

Michael W. Hammond
Principal Owner, OCD Tech

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â