<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">While trying to convert resnet18 based pytorch model from <a href="https://github.com/zllrunning/face-parsing.PyTorch">here</a>. Below error comes up:</p>
<blockquote>
<p dir="auto">RuntimeError: Failed to export an ONNX attribute 'onnx::Gather', since it's not constant, please try to make things (e.g., kernel size) static if possible</p>
</blockquote>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>Download <a href="https://github.com/zllrunning/face-parsing.PyTorch">this repo</a>.<br>
<em>Also download the model weights 79999_iter.pth from the repo page and place it in res/cp.</em></li>
<li>In Google Collab run conversion script:</li>
</ol>
<blockquote>
<p dir="auto">from model import BiSeNet<br>
import torch.onnx<br>
import torch</p>
</blockquote>
<blockquote>
<p dir="auto">net = BiSeNet(19)<br>
net.cuda()<br>
net.load_state_dict(torch.load('/content/drive/My Drive/Collab/fp/res/cp/79999_iter.pth'))<br>
net.eval()</p>
</blockquote>
<blockquote>
<p dir="auto">dummy = torch.rand(1,3,512,512).cuda()<br>
torch.onnx.export(net, dummy, "Model.onnx", input_names=["image"], output_names=["output"])</p>
</blockquote>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">Should not produce any errors during conversion</p>
<h2 dir="auto">Environment</h2>
<p dir="auto">collect_env output:</p>
<blockquote>
<p dir="auto">Collecting environment information...<br>
PyTorch version: 1.4.0<br>
Is debug build: No<br>
CUDA used to build PyTorch: 10.1</p>
</blockquote>
<blockquote>
<p dir="auto">OS: Ubuntu 18.04.3 LTS<br>
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0<br>
CMake version: version 3.12.0</p>
</blockquote>
<blockquote>
<p dir="auto">Python version: 3.6<br>
Is CUDA available: Yes<br>
CUDA runtime version: 10.0.130<br>
GPU models and configuration: GPU 0: Tesla T4<br>
Nvidia driver version: 418.67<br>
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5</p>
</blockquote>
<blockquote>
<p dir="auto">Versions of relevant libraries:<br>
[pip3] numpy==1.17.5<br>
[pip3] torch==1.4.0<br>
[pip3] torchsummary==1.5.1<br>
[pip3] torchtext==0.3.1<br>
[pip3] torchvision==0.5.0<br>
[conda] Could not collect</p>
</blockquote>
<h2 dir="auto">Additional context</h2>
<p dir="auto">I added <code class="notranslate">print (v.node ())</code> to symbolic_helper.py just before the runtime error is raised to see what's causing the error.</p>
<p dir="auto">This is the output: <code class="notranslate">%595 : Long() = onnx::Gather[axis=0](%592, %594) # /content/drive/My Drive/Collab/fp/model.py:111:0</code></p>
<p dir="auto">And that line in 111 in model.py is: <code class="notranslate">avg = F.avg_pool2d(feat32, feat32.size()[2:])</code></p>
<p dir="auto">Based on my further research I found this <a href="https://blog.csdn.net/weixin_43902449/article/details/90515528" rel="nofollow">source</a> stating that:</p>
<blockquote>
<p dir="auto">Both resNet50 and 32 are fine, but to resNet18, ONNX model cannot be exported.</p>
</blockquote>
<p dir="auto">The source suggests the following changes:</p>
<p dir="auto">From this:</p>
<blockquote>
<p dir="auto">import torch.nn.functional as F<br>
def forward(self, x):<br>
feat = self.base(x)<br>
feat = F.avg_pool2d(feat, feat.size()[2:])</p>
</blockquote>
<p dir="auto">To this:</p>
<blockquote>
<p dir="auto">class Model(nn.Module):<br>
def init():<br>
…<br>
self.avg_pool2d = nn.AvgPool2d(kernel_size=k_s, ceil_mode=False)<br>
…<br>
def forward(self, x):<br>
…<br>
feat = self.avg_pool2d(feat, feat.size()[2:])<br>
…</p>
</blockquote>
<p dir="auto">This change however yield other errors</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/houseroad/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/houseroad">@houseroad</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/spandantiwari/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/spandantiwari">@spandantiwari</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/lara-hdr/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/lara-hdr">@lara-hdr</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/BowenBao/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/BowenBao">@BowenBao</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/neginraoof/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/neginraoof">@neginraoof</a></p>
<p dir="auto">Trying to convert <a href="https://github.com/zllrunning/face-parsing.PyTorch">this</a> pytorch  model with ONNX gives me this error. I've searched github and this error came up before in version 1.1.0 but was apparently rectified. Now I'm on torch 1.4.0. (python 3.6.9) and I see this error.</p>
<p dir="auto">File "/usr/local/lib/python3.6/dist-packages/torch/onnx/<strong>init</strong>.py", line 148, in export<br>
strip_doc_string, dynamic_axes, keep_initializers_as_inputs)<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 66, in export<br>
dynamic_axes=dynamic_axes, keep_initializers_as_inputs=keep_initializers_as_inputs)<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 416, in _export<br>
fixed_batch_size=fixed_batch_size)<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 296, in _model_to_graph<br>
fixed_batch_size=fixed_batch_size, params_dict=params_dict)<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 135, in _optimize_graph<br>
graph = torch._C._jit_pass_onnx(graph, operator_export_type)<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/<strong>init</strong>.py", line 179, in _run_symbolic_function<br>
return utils._run_symbolic_function(*args, **kwargs)<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 657, in _run_symbolic_function<br>
return op_fn(g, *inputs, **attrs)<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_helper.py", line 128, in wrapper<br>
args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_helper.py", line 128, in <br>
args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]<br>
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_helper.py", line 81, in _parse_arg<br>
"', since it's not constant, please try to make "<br>
RuntimeError: Failed to export an ONNX attribute 'onnx::Gather', since it's not constant, please try to make things (e.g., kernel size) static if possible</p>
<p dir="auto">How to fix it?  I've also tried latest nightly build,  same error comes up.</p>
<p dir="auto">My code:<br>
<code class="notranslate">from model import BiSeNet</code><br>
<code class="notranslate">import torch.onnx</code><br>
<code class="notranslate">import torch</code></p>
<p dir="auto"><code class="notranslate">net = BiSeNet(19)</code><br>
<code class="notranslate">net.cuda()</code><br>
<code class="notranslate">net.load_state_dict(torch.load('/content/drive/My Drive/Collab/fp/res/cp/79999_iter.pth'))</code><br>
<code class="notranslate">net.eval()</code></p>
<p dir="auto"><code class="notranslate">dummy = torch.rand(1,3,512,512).cuda()</code><br>
<code class="notranslate">torch.onnx.export(net, dummy, "Model.onnx", input_names=["image"], output_names=["output"])</code><br>
`<br>
I also tried downgrading to torch 1.0.0 and the error is still there "RuntimeError: ONNX symbolic expected a constant value in the trace"</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/houseroad/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/houseroad">@houseroad</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/spandantiwari/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/spandantiwari">@spandantiwari</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/lara-hdr/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/lara-hdr">@lara-hdr</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/BowenBao/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/BowenBao">@BowenBao</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/neginraoof/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/neginraoof">@neginraoof</a></p>