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:

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: https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html 5

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.
Screen Shot 2020-11-05 at 7.39.18 PM1085×417 73.2 KB
Once you have the TFjs model, you can run inference using this in the browser. For some examples, look here: https://github.com/tensorflow/tfjs-examples 3
I hope this helps