Densenet in the browser

Hello, I have a densenet model and I want to run it in the browser. Does anyone know any solutions I can use?

I tried onnx.js and I get the following error:

Uncaught (in promise) TypeError: cannot resolve operator 'Pad' with opsets: ai.onnx v9

I am converted my ConvNet model using opset v11 which I think is the latest but still I get the same error. It would be great if someone can help.

Hi. I don’t know if you have a custom densenet model but assuming that you are using a pre-trained model from torchvision library, then you can use the following steps for running your model in the browser:

  1. Using ONNX/ONNX JS:
import torch
import torch.onnx
import torchvision.models as models

# Load the pre-trained model
densenet = models.densenet161(pretrained=True)

# Dummy Input Data
dummy_input = torch.rand(1, 3, 256, 256)

# Define input / output names
input_names = ["features/conv0"]
output_names = ["classifier"]

# Convert PyTorch to ONNX
torch.onnx.export(densenet,
                  dummy_input,
                  "densenet_op11.onnx",
                  verbose=True,
                  input_names=input_names,
                  output_names=output_names,
                  opset_version= 11)

Now once you have the model in ONNX format, you can use this tutorial to serve the model: (optional) Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime — PyTorch Tutorials 2.2.0+cu121 documentation

  1. Using TensorFlow.js for model serving
import torch
import torch.onnx
import torchvision.models as models
import tensorflow as tf

# Load Pre-trained Model
densenet = models.densenet161(pretrained=True)

# Dummy Input Data
dummy_input = torch.rand(1, 3, 256, 256)

# Define input / output names
input_names = ["features/conv0"]
output_names = ["classifier"]

# Convert PyTorch to ONNX
torch.onnx.export(densenet,
                  dummy_input,
                  "densenet_op11.onnx",
                  verbose=True,
                  input_names=input_names,
                  output_names=output_names,
                  opset_version= 11)

Once you have the ONNX model, run this command in command line:

# Onnx to TensorFlow
#  TensorFlow = 2.3.0
# Install onnx-tf from here: https://github.com/onnx/onnx-tensorflow
# pip install tensorflow-addons

onnx-tf convert -i ./densenet_op11.onnx -o ./densenet_op11.pb

Once you have the TF Model, convert that to TFjs using the following command on terminal:

# Convert TensorFlow to TFjs
# https://github.com/tensorflow/tfjs/tree/master/tfjs-converter

tensorflowjs_wizard

In the above, select the model type as TensorFlow Saved Model and provide the rest of the things like model save location and all.

Once you have the TFjs model, you can run inference using this in the browser. For some examples, look here: GitHub - tensorflow/tfjs-examples: Examples built with TensorFlow.js

I hope this helps :slight_smile:

I’m working on a Yolo model and tried these approaches before.
For PyTorch -> onnx -> onnx.js method, it told me onnx.js doesn’t support int64. So I used onnxruntime and quantized it to be an int8 model. Then the issue becomes that it doesn’t support Resize.
For PyTorch -> onnx -> TensorFlow -> tfjs method. The issue is that when I’m trying to get the tfjs model, it shows that TensorScatterUpdate is not supported.
I’m not sure is there any other approach to help me run the model in the browser…