When using torch.onnx.export(), errors occurs: AssertionError: Only output_size=[1, 1] is supported

Rizhao_Cai: <p>Environments:<br>
ubuntu 16.04<br>
cuda8.0<br>
python: 3.5.2<br>
pytorch: 1.0.1<br>
torchvision: 0.2.2</p>
<p>Hi guys, when I use torch.onnx.export, I get some problems. The code is just from the  " Example: End-to-end AlexNet from PyTorch to Caffe" (<a href="https://pytorch.org/docs/stable/onnx.html?highlight=export#torch.onnx.export" rel="nofollow noopener">https://pytorch.org/docs/stable/onnx.html?highlight=export#torch.onnx.export</a>)</p>
<pre><code>import torch
import torchvision

dummy_input = torch.randn(10, 3, 224, 224, device='cuda')
model = torchvision.models.alexnet(pretrained=True).cuda()
input_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ]
output_names = [ "output1" ]

torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names)
</code></pre>
<p>Here is the error:</p>
<p>Traceback (most recent call last):<br>
File “pytorch_to_onnx.py”, line 10, in <br>
torch.onnx.export(model, dummy_input, “alexnet.onnx”, verbose=True)#, input_names=input_names, output_names=output_names)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/<strong>init</strong>.py”, line 27, in export<br>
return utils.export(*args, **kwargs)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py”, line 104, in export<br>
operator_export_type=operator_export_type)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py”, line 281, in _export<br>
example_outputs, propagate)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py”, line 227, in _model_to_graph<br>
graph = _optimize_graph(graph, operator_export_type)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py”, line 155, in _optimize_graph<br>
graph = torch._C._jit_pass_onnx(graph, operator_export_type)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/<strong>init</strong>.py”, line 52, in _run_symbolic_function<br>
return utils._run_symbolic_function(*args, **kwargs)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py”, line 504, in _run_symbolic_function<br>
return fn(g, *inputs, **attrs)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/symbolic.py”, line 89, in wrapper<br>
return fn(g, *args)<br>
File “/usr/local/lib/python3.5/dist-packages/torch/onnx/symbolic.py”, line 600, in adaptive_avg_pool2d<br>
assert output_size == [1, 1], “Only output_size=[1, 1] is supported”<br>
AssertionError: Only output_size=[1, 1] is supported</p>
<p>By the way, the documents about torch.onnx.export()  are vague. I cannot understand what the arguments input_names and output_names are for?</p>
<p>Any cues would be appreciated!</p>

Rizhao_Cai: <p>Finally, I fix this problem by downgrading the torchvision from 0.2.2 to 0.2.1.<br>
Cannot believe that there are such huge differences</p>

VagrantVV: <p>Thans a lot !!!</p>

BeautyData: <p>Thank you very much!</p>

JCPARK: <p>The original AlexNet definition in v0.2.1 does not have AdaptiveAvgPool2d, but v0.2.2 does. As stated in the error log, the error is mainly caused by AdaptiveAvgPool2d having output size other than [1,1].</p>
<p>I had the same error as you, solved by changing it to fixed size pooling (nn.AvgPool2d).</p>

chenkangning: <p>I tried it ， it works. in"alexnet.py" row 34:self.avgpool = nn.AdaptiveAvgPool2d((6, 6)) to self.avgpool = nn.AvgPool2d((1, 1))</p>