How to Convert Yolov5 model to tensorflow.js

Written by - Aionlinecourse6416 times views

Yolov5 is a convolutional neural network that was developed by Google. It is the latest generation of the YOLO family of object detection algorithms.

Tensorflow.js is an open-source library for machine intelligence that allows developers to run machine learning models in the browser and on Node.js, or in a JavaScript engine like V8 or ChakraCore.

This article will help you convert the YOLOV5 model to tensorflow.js and use it with your web application.

How to Convert Yolov5 model to tensorflow.js

Solution 1:

Make a TensorFlow model first in google Colab or another environment, train it, and then convert to tensorflow.js model.

!pip install tensorflowjs

import tensorflowjs as tfjs
async function loadModel() {
model = undefined;
model = await tf.loadLayerModel("https://raw.githubusercontent.com/<user name>/<repository>/<branch_name>/model.json"); //you can use your model.json path here
console.log("model loaded");
}
loadModel();

Then import tensorflowjs as tfjs

tfjs.converters.save_keras_model(model, 'models')

After training, convert your model using tensorflowjs converter. Which gives you these two files below.

Load your model in index.html with a script or in an external js file.

Then make an input tensor like the below template(you have to customize your input tensor as your need)

Then predict your result using the model.predict(input_tensor)

function make_prediction() { 
var a, b, output; 
a = Number(document.getElementById("first").value); 
b = Number(document.getElementById("second").value);
input_xs = tf.tensor2d([[a, b] ]);
output = model.predict(input_xs);
const outputData = output.dataSync();
 document.getElementById("answer").value = Number(outputData[0] > 0.5);
}

Solution 2:

You can check - https://github.com/zldrobit/tfjs-yolov5-example

Inside the YoloV5 repo, run the export.py command.

python export.py --weights yolov5s.pt --include tfjs

Then cd into the above linked repo and copy the weights folder to the public:

cp ./yolov5s_web_model public/web_model

Don't forget, you'll have to change the names array in src/index.js to match your custom model.

But unfortunately, it seems painfully slow at about 1-2 seconds. I don't think I was able to get WebGL working.


Solution 3:

With few interim steps, but most of the times it works:

  1. Export PyTorch to ONNX
  2. Convert ONNX to TF Saved Model
  3. Convert TF Saved Model to TFJS Graph Model

When converting from ONNX to TF, you might need to adjust target version if you run into unsupported ops.

Also, make sure to set input resolutions to a fixed values, any dynamic inputs get messed up in this multi-step conversion.


Thank you for reading the article. If you face any problem please comment below.

Recommended Projects

Deep Learning Interview Guide

Topic modeling using K-means clustering to group customer reviews

Have you ever thought about the ways one can analyze a review to extract all the misleading or useful information?...

Natural Language Processing
Deep Learning Interview Guide

Medical Image Segmentation With UNET

Have you ever thought about how doctors are so precise in diagnosing any conditions based on medical images? Quite simply,...

Computer Vision
Deep Learning Interview Guide

Build A Book Recommender System With TF-IDF And Clustering(Python)

Have you ever thought about the reasons behind the segregation and recommendation of books with similarities? This project is aimed...

Machine LearningDeep LearningNatural Language Processing
Deep Learning Interview Guide

Automatic Eye Cataract Detection Using YOLOv8

Cataracts are a leading cause of vision impairment worldwide, affecting millions of people every year. Early detection and timely intervention...

Computer Vision
Deep Learning Interview Guide

Crop Disease Detection Using YOLOv8

In this project, we are utilizing AI for a noble objective, which is crop disease detection. Well, you're here if...

Computer Vision
Deep Learning Interview Guide

Vegetable classification with Parallel CNN model

The Vegetable Classification project shows how CNNs can sort vegetables efficiently. As industries like agriculture and food retail grow, automating...

Machine LearningDeep Learning
Deep Learning Interview Guide

Banana Leaf Disease Detection using Vision Transformer model

Banana cultivation is a significant agricultural activity in many tropical and subtropical regions, providing a vital source of income and...

Deep LearningComputer Vision