onnx-mxnet
onnx-mxnet copied to clipboard
Reshape operator expects Shape(tuple)
Version: onnx-mxnet (0.4.2)
Breaking file: sequence-tiny.zip
I'm getting the following error when trying to import my ONNX model:
Invalid Parameter format for shape expect Shape(tuple) but value='<Symbol identity0>', in operator Reshape(name="", shape="<Symbol identity0>")
The model has a conv net that feeds into a GRU. The model is built for OCR, so it turns the horizontal axis into the sequence axis.
These are the two lines of PyTorch code that cause the error:
x = x.transpose(1, 2).contiguous() # swap NFilters, Width
x = x.view(x.size(0), x.size(1), -1) # squash NFilters x Height into one dimension
Specifically, the x.view
is what causes the error
During the PyTorch -> ONNX export, the relevant breaking lines are:
%27 : Float(1, 32, 32, 11) = onnx::Relu(%26), scope: Sequence/PureConv[conv]/Sequential[all]/ReLU[2]
%28 : Float(1, 32, 32, 11) = onnx::Transpose[perm=[0, 2, 1, 3]](%27), scope: Sequence
%29 : Dynamic = onnx::Constant[value= 1 32 -1 [ CPULongTensor{3} ]](), scope: Sequence
%30 : Float(1, 32, 352) = onnx::Reshape(%28, %29), scope: Sequence
Any ideas what's going wrong here?
The Reshape operator expects a shape attribute to be present. But the reshape operator in you model has no shape attribute. This is what is causing the error.
I checked your model, it seems to have shape
as an additional input to the Reshape operator, as opposed to an attribute of the operator. I will get back on why this discrepancy might be there, need to check if pytorch is exporting the model correctly.
Also mxnet's ONNX import has been moved into mxnet here - https://github.com/apache/incubator-mxnet/tree/master/python/mxnet/contrib/onnx
I would encourage you to use the new code.
MXNet supports ONNX release v1.1 I see that the reshape operator definition has changed since then with this PR - https://github.com/onnx/onnx/pull/608
Reshape now expects shape as an input and not an attribute. We will fix this in the latest code. But I think if you use ONNX v1.1 for exporting your pytorch model, it should work fine.
Let me know if you have further questions.
Thanks, will try those suggestions tonight
Hi @anirudhacharya, I'm not sure I understand your previous suggestion. You suggested using ONNX 1.1 to export my pytorch model, but I've looked through the pytorch code on master, and I don't find any mention of ONNX versions in there. The sample that I submitted in this ticket was generated by pytorch 0.4.0, which was released 8 days ago, and I don't find any mention in the pytorch history of a change to the reshape export, in the last 8 days. Are you suggesting that I wait for a fix in mxnet? It's no problem for me to wait. I want to use mxnet for CPU deployment, but I'm OK with using GPUs for now.
@bmharper I should have been more clear. The definition of Reshape changed on March 16th with this PR. MXNet supports ONNX v1.1.1 which includes commits till March 9th. So MXNet's import expects the older definition of Reshape.
Whereas Pytorch v0.4 includes this new change, so when you export with pytorch v0.4 your model will get serialized with the new definition of the reshape operator. This is what is causing the inconsistency.
We are working on fixing this. It would be great if you could wait.