Convert to ONNX. Why it fails?

username: Renziglov
cooked: <p>Hi there<br>
Cannot use the following code</p>
<p>from torch.autograd import Variable<br>
import torch.onnx<br>
import torch<br>
dummy_input = Variable(torch.randn(1, 3, 256, 256))<br>
model = torch.load(‘Paramecium.pth’, map_location=torch.device(‘cpu’))<br>
torch.onnx.export(model, dummy_input, “fromTorch.onnx”)</p>
<p>to convert the pth to onnx. It throws the <strong>‘collections.OrderedDict’ object has no attribute ‘state_dict’</strong> at the last line execution.</p>
<p>The way I create the .pth file is following (Colab):</p>
<p>from detecto import core, utils, visualize<br>
dataset = core.Dataset(’/content/drive/My Drive/Paramecium’)<br>
model = core.Model([‘Paramecium’, ‘Noise’])<br>
model.fit(dataset)<br>
model.save(‘Paramecium.pth’)</p>
<p>The model looks good, works and can be visualized with visualize.show_labeled_image<br>
How come it throws the error when converting to ONNX?<br>
Thanks.</p>

username: chetan_patil
cooked: <p>The <code>model</code> is actually the weights.!<br>
You need to load the weights inside an existing network.<br>
I don’t see any code where u create a network and then load the weights into it.<br>
For example, you need to something like this.!</p>
<pre><code class="lang-auto">weights = torch.load("Paramecium.pth", map_location = torch.device(‘cpu’))
model = core.Model([‘Paramecium’, ‘Noise’])
model.load_state_dict(weights)
torch.onnx.export(model, dummy_input, "*.onnx")
</code></pre>
<p>You are actually missing the <code>model.load_state_dict()</code></p>
<p>The <code>Paramecium.pth</code> just contains the weights, so you need to load these weights inside a model <code>core.Model([‘Paramecium’, ‘Noise’])</code></p>

username: Renziglov
cooked: <p>Hm… What is core? core.Model - where it comes from?<br>
My imports are as following:</p>
<p>from torch.autograd import Variable<br>
import torch.onnx<br>
import torchvision<br>
import torch</p>
<p>My Visual Studio Python environment is shown in the picture.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/5/d57f096750c64c0ab8c90ae5e7e58eb5cd94e16e.jpeg" alt="1" data-base62-sha1="usFSjV8nsq0BbnnO0PoVdnIm65M" width="448" height="382"></p>
<p>Thoughts?<br>
Thanks.</p>

username: Renziglov
cooked: <p>Ooops… I found it, it is just a part of detecto.<br>
Anyway, the line</p>
<p>model.load_state_dict(weights)</p>
<p>throws the following exception</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1ba8741b8af807b27885f256ff8c9ce35e474a46.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1ba8741b8af807b27885f256ff8c9ce35e474a46" title="1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1ba8741b8af807b27885f256ff8c9ce35e474a46_2_690x200.jpeg" alt="1" data-base62-sha1="3WFNHXt1tjIO7LaaPYMbWtJ7dFI" width="690" height="200" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1ba8741b8af807b27885f256ff8c9ce35e474a46_2_690x200.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1ba8741b8af807b27885f256ff8c9ce35e474a46_2_1035x300.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/b/1ba8741b8af807b27885f256ff8c9ce35e474a46.jpeg 2x" data-dominant-color="EAE5E6"><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">1</span><span class="informations">1038×302 81.5 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>Thoughts?<br>
Thanks.</p>

username: chetan_patil
cooked: <p>Try<br>
<code>model= core.Model.load("*pth", ["Paramecium", "Noise"] )</code> instead of those two lines involving <code>model</code></p>

username: Renziglov
cooked: <aside class="quote no-group" data-username="chetan_patil" data-post="5" data-topic="86290">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chetan_patil/48/25122_2.png" class="avatar"> chetan_patil:</div>
<blockquote>
<p><code>model= core.Model.load("*pth", ["Paramecium", "Noise"] )</code></p>
</blockquote>
</aside>
<p>No go.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/2/125cd3ea84cdd93b35d16293a07e5d02fc767175.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/125cd3ea84cdd93b35d16293a07e5d02fc767175" title="1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/125cd3ea84cdd93b35d16293a07e5d02fc767175_2_690x147.jpeg" alt="1" data-base62-sha1="2CrtzZOoPKGknlrad7aeuyohLuZ" width="690" height="147" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/125cd3ea84cdd93b35d16293a07e5d02fc767175_2_690x147.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/125cd3ea84cdd93b35d16293a07e5d02fc767175_2_1035x220.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/125cd3ea84cdd93b35d16293a07e5d02fc767175_2_1380x294.jpeg 2x" data-dominant-color="ECEAE4"><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">1</span><span class="informations">1414×303 106 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>Thoughts?</p>

username: chetan_patil
cooked: <p>Hey, use <code>Paramecium.pth</code> instead of <code>*pth</code></p>

username: Renziglov
cooked: <p>Tried it already. It brings me back to the initial problem:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/4/b4c532722340ce41165f069cbb5a45fda6d9972a.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b4c532722340ce41165f069cbb5a45fda6d9972a" title="1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/4/b4c532722340ce41165f069cbb5a45fda6d9972a_2_690x126.jpeg" alt="1" data-base62-sha1="pNar7rBGaJYP0SKmBIwDLFS09Dk" width="690" height="126" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/4/b4c532722340ce41165f069cbb5a45fda6d9972a_2_690x126.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/4/b4c532722340ce41165f069cbb5a45fda6d9972a_2_1035x189.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/4/b4c532722340ce41165f069cbb5a45fda6d9972a.jpeg 2x" data-dominant-color="E7E4E0"><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">1</span><span class="informations">1338×246 101 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>It looks like I’m getting more and more involved in voodoo programming.<br>
Let me try to get both tasks (model training and ONNX conversion) done in one place - Colab. So far once the model trained in Colab, I switch to my local environment to Visual Studio finishing the conversion.<br>
Let’s see.</p>

username: Renziglov
cooked: <p>No. The Colab code below<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/e/5eb7c9c7a0da3d973fdaa76c183c1ccd1a3d263e.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/5eb7c9c7a0da3d973fdaa76c183c1ccd1a3d263e" title="1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/e/5eb7c9c7a0da3d973fdaa76c183c1ccd1a3d263e_2_690x243.jpeg" alt="1" data-base62-sha1="dvUE5DXDR92LtEe3PEdwooDi09M" width="690" height="243" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/e/5eb7c9c7a0da3d973fdaa76c183c1ccd1a3d263e_2_690x243.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/5/e/5eb7c9c7a0da3d973fdaa76c183c1ccd1a3d263e.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/e/5eb7c9c7a0da3d973fdaa76c183c1ccd1a3d263e.jpeg 2x" data-dominant-color="EDEBEC"><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">1</span><span class="informations">882×311 58.3 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><br>
is not working either throwing</p>
<p>AttributeError: ‘Model’ object has no attribute ‘state_dict’</p>
<p>I’m stuck. Help!</p>

username: chetan_patil
cooked: <p>I’m not sure what is exactly wrong, but you can refer to this link and see what’s the mistake<br>
<a href="https://detecto.readthedocs.io/en/latest/api/core.html" class="onebox" target="_blank" rel="nofollow noopener">https://detecto.readthedocs.io/en/latest/api/core.html</a></p>

username: chetan_patil
cooked: <aside class="quote no-group" data-username="Renziglov" data-post="9" data-topic="86290">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/82dd89/48.png" class="avatar"> Renziglov:</div>
<blockquote>
<p>AttributeError: ‘Model’ object has no attribute ‘state_dict</p>
</blockquote>
</aside>
<p>Hi, after<br>
<code>model = core.Model.load("Paramecium.pth", ["Paramecium", "Noise"])</code><br>
could you try this line,<br>
<code>torch.onnx.export(model.get_internal_model(), dummy_input, "from_onnx.onnx")</code>  ?</p>

username: Renziglov
cooked: <p>It moved somewhere ahead but throws the exception after:</p>
<p>RuntimeError: Failed to export an ONNX attribute ‘onnx::Sub’, since it’s not constant, please try to make things (e.g., kernel size) static if possible</p>
<p>Stack:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/9/b9c8269a6c1a991a8e90340ca05e11bb2584bc11.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b9c8269a6c1a991a8e90340ca05e11bb2584bc11" title="1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/9/b9c8269a6c1a991a8e90340ca05e11bb2584bc11_2_690x242.jpeg" alt="1" data-base62-sha1="qvv9uDqkUCFtJAV4rzcVqZIjJjX" width="690" height="242" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/9/b9c8269a6c1a991a8e90340ca05e11bb2584bc11_2_690x242.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/9/b9c8269a6c1a991a8e90340ca05e11bb2584bc11_2_1035x363.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/9/b9c8269a6c1a991a8e90340ca05e11bb2584bc11.jpeg 2x" data-dominant-color="F0F0F3"><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">1</span><span class="informations">1236×435 146 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>Thoughts?</p>

username: Renziglov
cooked: <p>One more peace of information from the Colab debugger:</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_helper.py:243: UserWarning: You are trying to export the model with onnx:Upsample for ONNX opset version 9.<br>
This operator might cause results to not match the expected results by PyTorch.<br>
ONNX’s Upsample/Resize operator did not match Pytorch’s Interpolation until opset 11.<br>
Attributes to determine how to transform the input were added in onnx:Resize in opset 11<br>
to support Pytorch’s behavior (like coordinate_transformation_mode and nearest_mode).<br>
We recommend using opset 11 and above for models using this operator.<br>
“” + str(_export_onnx_opset_version) + ". "</p>
<p>How can I get the opset 11+ ?<br>
Thanks.</p>

username: Renziglov
cooked: <p>It looks like I fixed it:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/f/2ff52b7573c132445d0fa4e2c5ea6ac327bea53f.png" data-download-href="https://discuss.pytorch.org/uploads/default/2ff52b7573c132445d0fa4e2c5ea6ac327bea53f" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/f/2ff52b7573c132445d0fa4e2c5ea6ac327bea53f.png" alt="image" data-base62-sha1="6QfIk50Et89JFlVMCABVgQuc72D" width="690" height="171" data-dominant-color="F2F0F1"><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">image</span><span class="informations">808×201 5.97 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>Thanks.</p>