Pytorch to Onnx Dynamic axes not working?

NickKao: <p>The error message:</p>
<pre><code class="lang-auto">D:\Programs\anaconda3\envs\py36\lib\site-packages\torch\onnx\utils.py:822: UserWarning: Provided key input for dynamic axes is not a valid input/output name
  warnings.warn("Provided key {} for dynamic axes is not a valid input/output name".format(key))
D:\Programs\anaconda3\envs\py36\lib\site-packages\torch\onnx\utils.py:825: UserWarning: No names were found for specified dynamic axes of provided input.Automatically generated names will be applied to each dynamic axes of input actual_input_1
  'Automatically generated names will be applied to each dynamic axes of input {}'.format(key))
2020-02-29 19:45:35.5573036 [W:onnxruntime:Default, inference_session.cc:595 onnxruntime::InferenceSession::CheckShapes] Got invalid dimensions for input: input for the following indices
 index: 2 Got: 256 Expected: 224
 index: 3 Got: 256 Expected: 224
 Please fix either the inputs or the model.
2020-02-29 19:45:35.5939257 [E:onnxruntime:, sequential_executor.cc:127 onnxruntime::SequentialExecutor::Execute] Non-zero status code returned while running Node:  Status Message: GEMM: Dimension mismatch, W: {4096,9216} K: 12544 N:4096
Traceback (most recent call last):
  File "D:\WS_2019\env\JetBrains\PyCharm Community Edition 2018.3.3\helpers\pydev\pydevd.py", line 1741, in &lt;module&gt;
    main()
  File "D:\WS_2019\env\JetBrains\PyCharm Community Edition 2018.3.3\helpers\pydev\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "D:\WS_2019\env\JetBrains\PyCharm Community Edition 2018.3.3\helpers\pydev\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:\WS_2019\env\JetBrains\PyCharm Community Edition 2018.3.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/WS/WS_Python/Homeworks/Pytorch2Onnx/alexnet2onnx_dynamic_input.py", line 46, in &lt;module&gt;
    ort_outs = ort_session.run(None, ort_inputs)
  File "D:\Programs\anaconda3\envs\py36\lib\site-packages\onnxruntime\capi\session.py", line 72, in run
    return self._sess.run(output_names, input_feed, run_options)
RuntimeError: Method run failed due to: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Non-zero status code returned while running Node:  Status Message: GEMM: Dimension mismatch, W: {4096,9216} K: 12544 N:4096
</code></pre>
<p>My code:</p>
<pre><code class="lang-auto">import torch
import torchvision.models as models

# Use an existing model from Torchvision, note it
# will download this if not already on your computer (might take time)
model = models.alexnet(pretrained=True)


print(model)
# Create some sample input in the shape this model expects
dummy_input = torch.randn(1, 3, 224, 224)

# It's optional to label the input and output layers
input_names = [ "input" ] #+ [ "learned_%d" % i for i in range(16) ]
output_names = [ "output1" ]

dynamic_axes = {"input": [0,2,3]}

# Use the exporter from torch to convert to onnx
# model (that has the weights and net arch)

torch.onnx.export(
        model,
        dummy_input,
        "test.onnx",
        verbose=True,
        input_names=input_names,
        output_names=output_names,
        dynamic_axes=dynamic_axes,
    )

import onnxruntime
import torch
import torchvision.models as models
import numpy as np

ort_session = onnxruntime.InferenceSession("test.onnx")

def to_numpy(tensor):
    return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()


x = torch.randn(1, 3, 256, 256)
# compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
ort_outs = ort_session.run(None, ort_inputs)
</code></pre>
<p>What am I doing wrong?</p>

DodoI: <p>Hi, have you found out what’s wrong?</p>