Densenet in the browser

username: SharanSMenon
cooked: <p>Hello, I have a densenet model and I want to run it in the browser. Does anyone know any solutions I can use?</p>
<p>I tried onnx.js and I get the following error:</p>
<pre><code class="lang-auto">Uncaught (in promise) TypeError: cannot resolve operator 'Pad' with opsets: ai.onnx v9
</code></pre>

username: iamharshit
cooked: <p>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.</p>

username: anujd9
cooked: <p>Hi. I don’t know if you have a custom <code>densenet model</code> but assuming that you are using a pre-trained model from <code>torchvision</code> library, then you can use the following steps for running your model in the browser:</p>
<ol>
<li>Using ONNX/ONNX JS:</li>
</ol>
<pre><code class="lang-auto">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)
</code></pre>
<p>Now once you have the model in <code>ONNX</code> format, you can use this tutorial to serve the model: <a href="https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html" class="inline-onebox" rel="noopener nofollow ugc">(optional) Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime — PyTorch Tutorials 2.2.0+cu121 documentation</a></p>
<ol start="2">
<li>Using TensorFlow.js for model serving</li>
</ol>
<pre><code class="lang-auto">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)
</code></pre>
<p>Once you have the ONNX model, run this command in command line:</p>
<pre><code class="lang-auto"># 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
</code></pre>
<p>Once you have the TF Model, convert that to TFjs using the following command on terminal:</p>
<pre><code class="lang-auto"># Convert TensorFlow to TFjs
# https://github.com/tensorflow/tfjs/tree/master/tfjs-converter

tensorflowjs_wizard
</code></pre>
<p>In the above, select the model type as <code>TensorFlow Saved Model</code> and provide the rest of the things like model save location and all.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/0/e0d5f02cc62f0151c608459de7d70ec793cfee6d.png" data-download-href="https://discuss.pytorch.org/uploads/default/e0d5f02cc62f0151c608459de7d70ec793cfee6d" title="Screen Shot 2020-11-05 at 7.39.18 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e0d5f02cc62f0151c608459de7d70ec793cfee6d_2_690x265.png" alt="Screen Shot 2020-11-05 at 7.39.18 PM" data-base62-sha1="w4Zjsl5bynvds6GfI3VUhYeLhsV" width="690" height="265" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e0d5f02cc62f0151c608459de7d70ec793cfee6d_2_690x265.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e0d5f02cc62f0151c608459de7d70ec793cfee6d_2_1035x397.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/0/e0d5f02cc62f0151c608459de7d70ec793cfee6d.png 2x" data-dominant-color="101111"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2020-11-05 at 7.39.18 PM</span><span class="informations">1085×417 73.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Once you have the <code>TFjs model</code>, you can run inference using this in the browser. For some examples, look here: <a href="https://github.com/tensorflow/tfjs-examples" class="inline-onebox" rel="noopener nofollow ugc">GitHub - tensorflow/tfjs-examples: Examples built with TensorFlow.js</a></p>
<p>I hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: Zekun_Wang
cooked: <p>I’m working on a Yolo model and tried these approaches before.<br>
For PyTorch -&gt; onnx -&gt; 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.<br>
For PyTorch -&gt; onnx -&gt; TensorFlow -&gt; tfjs method. The issue is that when I’m trying to get the tfjs model, it shows that TensorScatterUpdate is not supported.<br>
I’m not sure is there any other approach to help me run the model in the browser…</p>