Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Error while converting op of type: BatchNormalization. Error message: provided number axes -1 not supported #572

Open
daylight0998 opened this issue May 22, 2020 · 1 comment
Labels
bug Unexpected behaviour that should be corrected (type)

Comments

@daylight0998
Copy link

🐞Describe the bug

(1) I try to convert onnx model to mlmodel, but it's error:
Error while converting op of type: BatchNormalization. Error message: provided number axes -1 not supported

Trace

Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1741, in
main()
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/vipkid/PycharmProjects/ai/ai-teacher/apps/landmark_to_image/onnx_to_mlModel.py", line 27, in
minimum_ios_deployment_target='13')
File "/Users/vipkid/anaconda3/lib/python3.7/site-packages/onnx_coreml/converter.py", line 627, in convert
_convert_node_nd(builder, node, graph, err)
File "/Users/vipkid/anaconda3/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py", line 2395, in _convert_node_nd
return converter_fn(builder, node, graph, err)
File "/Users/vipkid/anaconda3/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py", line 349, in _convert_bn
err.unsupported_op_configuration(builder, node, graph, "provided number axes {} not supported".format(rank))
File "/Users/vipkid/anaconda3/lib/python3.7/site-packages/onnx_coreml/_error_utils.py", line 62, in unsupported_op_configuration
self.rerun_suggestion)
TypeError: Error while converting op of type: BatchNormalization. Error message: provided number axes -1 not supported
Please try converting with higher minimum_ios_deployment_target.
You can also provide custom function/layer to convert the model.

it may be a break at the name of BatchNormalization_31, the input[0] rank name is -1

To Reproduce

  • If a python script can reproduce the error, please paste the code snippet
(2) convert onnx model to mlmodel code is:
import os
import onnx
from onnx import onnx_pb
from onnx_coreml import convert

dataset_dir = "./"

model_in = os.path.join(dataset_dir, "model.patched.onnx")
model_out = os.path.join(dataset_dir, "vgnet.mlmodel")
model_file = open(model_in, 'rb')
model_proto = onnx_pb.ModelProto()
model_proto.ParseFromString(model_file.read())
print("prepare to convert...")
model = onnx.load_model('vgnet.onnx')
coreml_model = convert(model_proto,
minimum_ios_deployment_target='13')

-  attach ONNX model.png
![model patched onnx(2)](https://user-images.githubusercontent.com/5449858/82629368-18603200-9c22-11ea-96d7-d59c2bd0364a.png)


## System environment (please complete the following information):
 - coremltools version  (e.g., 3.0b5): 3.3
    onnx: 1.6.0
 - onnx-coreml version (e.g. 1.0b2): 1.3
 - OS (e.g., MacOS, Linux): MacOS
 - macOS version : 10.15.4
    install python: anaconda
 - python version: 3.7
 - torch version: 1.5.0

## I  had us the following code to convert model:

import onnx
import onnx.helper
import onnx.shape_inference

model = onnx.load("./vgnet.onnx")

# Add graph input for each initializer
existing_inputs = {v.name for v in model.graph.input}
model.graph.input.extend([
    onnx.helper.make_tensor_value_info(
        name = tensor.name,
        elem_type = tensor.data_type,
        shape = list(tensor.dims),
    )
    for tensor in model.graph.initializer
    if tensor.name not in existing_inputs
])

# Check if it passes shape inference now
onnx.shape_inference.infer_shapes(model)

# Save the resulting model
onnx.save(model, "model.patched.onnx")
@daylight0998 daylight0998 added the bug Unexpected behaviour that should be corrected (type) label May 22, 2020
@daylight0998
Copy link
Author

What can be done to resolve this error?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Unexpected behaviour that should be corrected (type)
Projects
None yet
Development

No branches or pull requests

1 participant