Incubator-mxnet: Error: shape inconsistent while converting PyTorch model to mxnet model with onnx

Created on 21 Jan 2019  ·  16Comments  ·  Source: apache/incubator-mxnet

Description

I tried to convert a pretrained pytorch resnet18 to onnx format, then to mxnet model. But shape inconsistent error occurred when I load the onnx file with mxnet.contrib.onnx module.

Environment info (Required)

  • PyTorch 1.0, mxnet 1.3.0 (for windows)

```

Error Message:

File “D:\software\Anaconda3\lib\site-packages\mxnetcontrib\onnx\onnx2mx\import_model.py”, line 53, in import_model
sym, arg_params, aux_params = graph.from_onnx(model_proto.graph)

File “D:\software\Anaconda3\lib\site-packages\mxnetcontrib\onnx\onnx2mx\import_onnx.py”, line 96, in from_onnx
self._params[init_tensor.name] = self._parse_array(init_tensor)

File “D:\software\Anaconda3\lib\site-packages\mxnetcontrib\onnx\onnx2mx\import_onnx.py”, line 200, in _parse_array
return nd.array(np_array)

File “D:\software\Anaconda3\lib\site-packages\mxnet\ndarray\utils.py”, line 146, in array
return _array(source_array, ctx=ctx, dtype=dtype)

File “D:\software\Anaconda3\lib\site-packages\mxnet\ndarray\ndarray.py”, line 2435, in array
arr[:] = source_array

File “D:\software\Anaconda3\lib\site-packages\mxnet\ndarray\ndarray.py”, line 444, in setitem
self._set_nd_basic_indexing(key, value)

File “D:\software\Anaconda3\lib\site-packages\mxnet\ndarray\ndarray.py”, line 710, in _set_nd_basic_indexing
self._sync_copyfrom(value)

File “D:\software\Anaconda3\lib\site-packages\mxnet\ndarray\ndarray.py”, line 872, in _sync_copyfrom
str(self.shape), str(source_array.shape)))

ValueError: Shape inconsistent: expected () vs got (1,)

Minimum reproducible example

import torch
import torchvision

dummy_input = torch.randn(1, 3, 224, 224)
model = torchvision.models.resnet18(pretrained=True)
input_names = [ “input_1" ]
output_names = [ “output1” ]

torch.onnx.export(model, dummy_input, “resnet.onnx”, verbose=True, input_names=input_names, output_names=output_names)

from mxnet.contrib import onnx as onnx_mxnet
sym, arg, aux = onnx_mxnet.import_model(“resnet.onnx”)

ONNX pr-awaiting-response

Most helpful comment

@wangliye00 @Con-Mi However, I do see the error that you facing with MXNet v1.3.1. For fixing this, could you try pulling in the commit #13413 and checking if you are able to proceed further?

I am facing similar issue when loading pytorch-densenet onnx model into mxnet. The error message reads:

/usr/local/lib/python3.6/site-packages/mxnet/contrib/onnx/onnx2mx/import_onnx.py in _convert_operator(self, node_name, op_name, attrs, inputs)
     59         """
     60         if op_name in convert_map:
---> 61             op_name, new_attrs, inputs = convert_map[op_name](attrs, inputs, self)
     62         else:
     63             raise NotImplementedError("Operator {} not implemented.".format(op_name))

/usr/local/lib/python3.6/site-packages/mxnet/contrib/onnx/onnx2mx/_op_translations.py in reshape(attrs, inputs, proto_obj)
    432     if len(inputs) == 1:
    433         return 'reshape', attrs, inputs[0]
--> 434     reshape_shape = list(proto_obj._params[inputs[1].name].asnumpy())
    435     reshape_shape = [int(i) for i in reshape_shape]
    436     new_attrs = {'shape': reshape_shape}

KeyError: 'concat51'

I tried mxnet 1.3.1 (after patched import_onnx.py following your suggestion) and 1.4.0 with no luck, both raised similar exception.

All 16 comments

Hey, this is the MXNet Label Bot.
Thank you for submitting the issue! I will try and suggest some labels so that the appropriate MXNet community members can help resolve it.
Here are my recommended labels: ONNX

@wangliye00 Thanks for raising this issue.
I'm tagging MXNet ONNX experts to look at this. @Roshrini @vandanavk Can you have a look at this ?

@mxnet-label-bot Add [ONNX, Bug]

I phase the same error on version 1.3.1 for Linux.
Any updates?

Traceback (most recent call last):
  File "/Users/vandanavk/test.py", line 79, in <module>
    sym, arg, aux = onnx_mxnet.import_model("resnet.onnx")
  File "/Users/vandanavk/Documents/mxnet/incubator-mxnet/python/mxnet/contrib/onnx/onnx2mx/import_model.py", line 59, in import_model
    sym, arg_params, aux_params = graph.from_onnx(model_proto.graph)
  File "/Users/vandanavk/Documents/mxnet/incubator-mxnet/python/mxnet/contrib/onnx/onnx2mx/import_onnx.py", line 116, in from_onnx
    mxnet_sym = self._convert_operator(node_name, op_name, onnx_attr, inputs)
  File "/Users/vandanavk/Documents/mxnet/incubator-mxnet/python/mxnet/contrib/onnx/onnx2mx/import_onnx.py", line 62, in _convert_operator
    op_name, new_attrs, inputs = convert_map[op_name](attrs, inputs, self)
  File "/Users/vandanavk/Documents/mxnet/incubator-mxnet/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py", line 460, in reshape
    reshape_shape = list(proto_obj._params[inputs[1].name].asnumpy())
KeyError: 'concat0'

I get this error. (MXNet built from source).
This error is the same as https://github.com/apache/incubator-mxnet/issues/13395 and similar to https://github.com/apache/incubator-mxnet/issues/13774. All these issues depends on dynamic shape support.

@wangliye00 @Con-Mi However, I do see the error that you facing with MXNet v1.3.1. For fixing this, could you try pulling in the commit https://github.com/apache/incubator-mxnet/pull/13413 and checking if you are able to proceed further?

@mxnet-label-bot remove[Bug]

@mxnet-label-bot add [pr-awaiting-response]

Please reopen a new issue if you still face problems after pulling in the commit that @vandanavk suggested and we'll take investigate further.

@wangliye00 @Con-Mi However, I do see the error that you facing with MXNet v1.3.1. For fixing this, could you try pulling in the commit #13413 and checking if you are able to proceed further?

I am facing similar issue when loading pytorch-densenet onnx model into mxnet. The error message reads:

/usr/local/lib/python3.6/site-packages/mxnet/contrib/onnx/onnx2mx/import_onnx.py in _convert_operator(self, node_name, op_name, attrs, inputs)
     59         """
     60         if op_name in convert_map:
---> 61             op_name, new_attrs, inputs = convert_map[op_name](attrs, inputs, self)
     62         else:
     63             raise NotImplementedError("Operator {} not implemented.".format(op_name))

/usr/local/lib/python3.6/site-packages/mxnet/contrib/onnx/onnx2mx/_op_translations.py in reshape(attrs, inputs, proto_obj)
    432     if len(inputs) == 1:
    433         return 'reshape', attrs, inputs[0]
--> 434     reshape_shape = list(proto_obj._params[inputs[1].name].asnumpy())
    435     reshape_shape = [int(i) for i in reshape_shape]
    436     new_attrs = {'shape': reshape_shape}

KeyError: 'concat51'

I tried mxnet 1.3.1 (after patched import_onnx.py following your suggestion) and 1.4.0 with no luck, both raised similar exception.

@nswamy I think we should open this issue, since it still persists.

@nswamy I think we should open this issue, since it still persists.

I have the same problem when load resnet50 onnx model

same problem with you @JohnLee168

@vandanavk , i have the same problem with you: "KeyError: 'concat0'", and i test many onnx model which export from pytorch, Does the problem have any solution ,

thank u

I too have the same problem. Is there any solution? Thanks

I too have run into the same issue. Hoping the fix can be released ASAP.

Was this page helpful?
0 / 5 - 0 ratings