apache / mxnet

Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more
https://mxnet.apache.org
Apache License 2.0
20.78k stars 6.79k forks source link

mxnet model convert to onnx is scuucess but ort.InferenceSession failed #20727

Closed JiaoPaner closed 2 years ago

JiaoPaner commented 3 years ago

Ask a Question

I success convert mxnet model to onnx but it failed when inference .The model 's shape is (1,1,100,100) convert code

sym = 'single-symbol.json'
params = '/single-0090.params'
input_shape = (1, 1, 100, 100)
onnx_file = './model.onnx'
converted_model_path = onnx_mxnet.export_model(sym, params, [input_shape], np.float32, onnx_file,verbose=True)
model= onnx.load_model(converted_model_path)
checker.check_graph(model.graph)
checker.check_model(model)

output

INFO:root:Input shape of the model [(1, 1, 100, 100)] 
INFO:root:Exported ONNX file ./model.onnx saved to disk

inference code

sess = ort.InferenceSession("./model.onnx") 

output

onnxruntime.capi.onnxruntime_pybind11_state.RuntimeException:
 [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : 
Exception during initialization: 
/onnxruntime/core/providers/cpu/nn/pool_attributes.h:77 
onnxruntime::PoolAttributes::PoolAttributes(const OpNodeProtoHelper<onnxruntime::ProtoHelperNodeContext> &,
                                                  const std::string &, int) pads[dim] < kernel_shape[dim] &&
                                                  pads[dim + kernel_shape.size()] < kernel_shape[dim] was false. 
Pad should be smaller than kernel.

Question

mxnet pooling node json

{
  "op": "Pooling", 
  "name": "pool1_fwd", 
  "attrs": {
    "count_include_pad": "True", 
    "global_pool": "False", 
    "kernel": "(4, 4)", 
    "layout": "NCHW", 
    "pad": "(4, 4)", 
    "pool_type": "avg", 
    "pooling_convention": "valid", 
    "stride": "(4, 4)"
  }, 
  "inputs": [[46, 0, 0]]
}

I change the "pad": "(4, 4)" to "pad": "(3, 3)" smaller than "kernel": "(4, 4), then try convert again.

sess = ort.InferenceSession("./model.onnx")
output = sess.run(None, {"data": data.astype(np.float32)})

it worked,but the output value is not right. how to fix it ? BTW:convert the mxnet model to ncnn all is right(not change anything,pad=(4,4),kernel=(4,4))

Further information

python:3.8 onnx:1.10.2 mxnet:1.8.0

github-actions[bot] commented 3 years ago

Welcome to Apache MXNet (incubating)! We are on a mission to democratize AI, and we are glad that you are contributing to it by opening this issue. Please make sure to include all the relevant context, and one of the @apache/mxnet-committers will be here shortly. If you are interested in contributing to our project, let us know! Also, be sure to check out our guide on contributing to MXNet and our development guides wiki.

JiaoPaner commented 2 years ago

I fix it,recode model by pytorch and copy weights,use nn.ZeroPad2d(4) before avgpooling:

self.pad = nn.ZeroPad2d(4)
self.pool = nn.AvgPool2d(kernel_size=(4,4),stride=(4,4))

X = self.pool(self.pad(self.conv(X)))