The ONNX network's output 'pred' dimensions should be non-negative

guu: <p>Hello,<br>
I am trained a <a href="https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix" rel="noopener nofollow ugc">CycleGAN</a> and converted it to onnx model using part of code below:</p>
<pre><code class="lang-auto">net.eval()
net.cuda()  # in this example using cuda()

batch_size = 1  
input_shape = (3, 512, 512) 
export_onnx_file = load_filename[:-4]+".onnx"  
save_path = os.path.join(self.save_dir, export_onnx_file)
input_names = ["image"]
output_names = ["pred"]

dinput = torch.randn(batch_size, *input_shape).cuda()   #same with net: cuda()
torch.onnx.export(net, dinput, save_path,
                  input_names = input_names, output_names = output_names,
                  opset_version = 11,
                  dynamic_axes={
                # 'image' : {0 : 'batch_size'},    # variable length axes 
                'pred' : {
                  0:'1',
                  1:'3',
                  2:'512',
                  3:'512'
                }})#{0 : 'batch_size'}})
  
  
# summary(net, input_shape)
print('The ONNX file ' + export_onnx_file + ' is saved at %s' % save_path)

</code></pre>
<p>My goal is use the onnx model in <code>Snap Lens Studio</code> as a lens filter, but everytime I import the model I got the error like this</p>
<pre><code class="lang-auto">18:32:04	Resource import for /Users/youjin/Downloads/latest_net_G (6).onnx failed: The ONNX network's output 'pred' dimensions should be non-negative 
</code></pre>
<p>I printed the input and output shapes and everything looks fine to me tho,</p>
<pre><code class="lang-auto">#the last line of torchsummary
             Tanh-91          [-1, 3, 512, 512]               0
#print(input.shape)
input shape torch.Size([1, 3, 512, 512])
#print(out.shape)
output shape torch.Size([1, 3, 512, 512])
</code></pre>
<p>I googled what ‘negative dimensioin’ implies and found <a href="https://github.com/onnx/onnx/issues/3673" rel="noopener nofollow ugc">this thread</a>.<br>
Initially the output shape was not determined when I analyse it with Netron<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c99164e6e0807185069c07139bb562615169328.png" data-download-href="https://discuss.pytorch.org/uploads/default/2c99164e6e0807185069c07139bb562615169328" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c99164e6e0807185069c07139bb562615169328_2_684x500.png" alt="image" data-base62-sha1="6mwZM3oBfj9e8hMTyiYlfrX2NwA" width="684" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c99164e6e0807185069c07139bb562615169328_2_684x500.png, https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c99164e6e0807185069c07139bb562615169328.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c99164e6e0807185069c07139bb562615169328.png 2x" data-dominant-color="ECECEC"><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">951×695 49.7 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>
So I used <code>dynamic_axes</code> arg of <code>torch.onnx.export</code> to set it [1,3,512,512].<br>
After config, It seemed right on Netron<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/6/56653a04c4727b4b047f4358f67c3cc5593fd3eb.png" data-download-href="https://discuss.pytorch.org/uploads/default/56653a04c4727b4b047f4358f67c3cc5593fd3eb" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/56653a04c4727b4b047f4358f67c3cc5593fd3eb_2_675x500.png" alt="image" data-base62-sha1="ckhWfwYgfkq4oR0o0fgdj9rq87p" width="675" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/56653a04c4727b4b047f4358f67c3cc5593fd3eb_2_675x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/56653a04c4727b4b047f4358f67c3cc5593fd3eb_2_1012x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/6/56653a04c4727b4b047f4358f67c3cc5593fd3eb.png 2x" data-dominant-color="EDEDED"><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">1021×756 49.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><br>
So I thought I fixed the error but I still getting the same error.</p>
<p>I wonder if I can get any help.</p>

azhanmohammed: <p>It is much easier to convert PyTorch models to ONNX without mentioning batch size, I personally use:</p>
<pre><code class="lang-python">import torch
import torchvision
import torch.onnx

# An instance of your model
net = #call model
net = net.cuda()
net = net.eval()

# An example input you would normally provide to your model's forward() method
x = torch.rand(1, 3, 512, 512)

# Export the model
torch_out = torch.onnx._export(net, x, "net.onnx", export_params=True)
</code></pre>

guu: <p>Thanks, <a class="mention" href="/u/azhanmohammed">@azhanmohammed</a> .<br>
But, the result does not change without setting batch size.</p>

azhanmohammed: <p><a class="mention" href="/u/guu">@guu</a> do you get the same error when keeping batch size of 1?</p>

guu: <p><a class="mention" href="/u/azhanmohammed">@azhanmohammed</a> yes, it gives me the same error message with/without batch size.</p>

azhanmohammed: <aside class="quote no-group" data-username="guu" data-post="1" data-topic="159065">
<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/guu/48/46944_2.png" class="avatar"> guu:</div>
<blockquote>
<p><code>The ONNX network's output 'pred' dimensions should be non-negative </code></p>
</blockquote>
</aside>
<p>Do you by any chance use a <code>.view()</code> or <code>.reshape()</code> operator in the forward call of the model? If that is the case, the issue arises because of this second common issues mentioned <a href="https://docs.snap.com/lens-studio/references/guides/lens-features/machine-learning/ml-frameworks/export-from-pytorch/" rel="noopener nofollow ugc">here</a>. Try changing your forward call, save the model, and try the export again.</p>

guu: <p><a class="mention" href="/u/azhanmohammed">@azhanmohammed</a><br>
Thanks for checking Lens Studio doc.<br>
Actually I don’t have <code>.view()</code> or <code>.shape()</code> operator in my model.<br>
However, I was thinking of using either of them since the error implies the model needs an explicit output dimension, I believe.<br>
Let me refer to the page when I put <code>.view()</code> or <code>.reshape()</code></p>

azhanmohammed: <p>Sure do give that a try, another thing you can try is to first create a JIT (just-in-time) traced model, and then export that to onnx, as per the <a href="https://pytorch.org/docs/stable/onnx.html#tracing-vs-scripting" rel="noopener nofollow ugc">docs</a> scripting preserves dynamic flow of the model, so that might help as well. Let me know if that works or not.</p>

guu: <p>Thanks for helping me!<br>
It turns out that Lens Studio has some limitations with onnx models that they can import. It’s better to use their provided models only.<br>
I found the CycleGAN model provided by Snap Research and it worked.</p>