onnx / onnx-coreml

ONNX to Core ML Converter
MIT License
399 stars 80 forks source link

Error when 'Converting Node Type Slice' #543

Open fellowProgrammer opened 4 years ago

fellowProgrammer commented 4 years ago

I am trying to convert my .onnx model into CoreML. However when I try to run the script below I get this error:

...
147/229: Converting Node Type Reshape
148/229: Converting Node Type Transpose
149/229: Converting Node Type Reshape
150/229: Converting Node Type Slice
Traceback (most recent call last):
  File "convert2.py", line 11, in <module>
    coreml_model = convert(model_proto, image_input_names=['inputImage'], image_output_names=['outputImage'], minimum_ios_deployment_target='13')
  File "/usr/local/lib/python3.6/dist-packages/onnx_coreml/converter.py", line 626, in convert
    _convert_node_nd(builder, node, graph, err)
  File "/usr/local/lib/python3.6/dist-packages/onnx_coreml/_operators_nd.py", line 2387, in _convert_node_nd
    return converter_fn(builder, node, graph, err)
  File "/usr/local/lib/python3.6/dist-packages/onnx_coreml/_operators_nd.py", line 2011, in _convert_slice
    end_masks=end_masks
  File "/usr/local/lib/python3.6/dist-packages/coremltools/models/neural_network/builder.py", line 4219, in add_slice_static
    assert len(strides) == rank
AssertionError

This is the script I am using:

import sys
from onnx import onnx_pb
from onnx_coreml import convert

model_in = sys.argv[1]
model_out = sys.argv[2]

model_file = open(model_in, 'rb')
model_proto = onnx_pb.ModelProto()
model_proto.ParseFromString(model_file.read())
coreml_model = convert(model_proto, image_input_names=['inputImage'], image_output_names=['outputImage'])
coreml_model.save(model_out)

I am using onnx-coreml-1.2. I am also quite new to Machine Learning (mainly an iOS Dev) so a good step by step solution with explanation would go a long way. Thanks in advance!

howiezhang commented 4 years ago

I have the same problems.

bhushan23 commented 4 years ago

@howiezhang @fellowProgrammer could you please build from source and try? this should have been fixed with https://github.com/onnx/onnx-coreml/pull/545

fellowProgrammer commented 4 years ago

I tried to build from source but I am getting some errors.

I used these commands (on google colab):

!git clone --recursive https://github.com/onnx/onnx-coreml.git
%cd onnx-coreml
!chmod +x install-develop.sh
!./install-develop.sh

But I am getting this error:

-- Configuring incomplete, errors occurred!
See also "/content/drive/My Drive/onnx-coreml/third_party/onnx/.setuptools-cmake-build/CMakeFiles/CMakeOutput.log".
See also "/content/drive/My Drive/onnx-coreml/third_party/onnx/.setuptools-cmake-build/CMakeFiles/CMakeError.log".
Traceback (most recent call last):
  File "setup.py", line 331, in <module>
    'backend-test-tools = onnx.backend.test.cmd_tools:main',
  File "/usr/local/lib/python3.6/dist-packages/setuptools/__init__.py", line 145, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/dist-packages/setuptools/command/install.py", line 67, in run
    self.do_egg_install()
  File "/usr/local/lib/python3.6/dist-packages/setuptools/command/install.py", line 109, in do_egg_install
    self.run_command('bdist_egg')
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/dist-packages/setuptools/command/bdist_egg.py", line 172, in run
    cmd = self.call_command('install_lib', warn_dir=0)
  File "/usr/local/lib/python3.6/dist-packages/setuptools/command/bdist_egg.py", line 158, in call_command
    self.run_command(cmdname)
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/dist-packages/setuptools/command/install_lib.py", line 11, in run
    self.build()
  File "/usr/lib/python3.6/distutils/command/install_lib.py", line 107, in build
    self.run_command('build_py')
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "setup.py", line 206, in run
    self.run_command('cmake_build')
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "setup.py", line 192, in run
    subprocess.check_call(cmake_args)
  File "/usr/lib/python3.6/subprocess.py", line 311, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/local/bin/cmake', '-DPYTHON_INCLUDE_DIR=/usr/include/python3.6m', '-DPYTHON_EXECUTABLE=/usr/bin/python3', '-DBUILD_ONNX_PYTHON=ON', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON', '-DONNX_NAMESPACE=onnx', '-DPY_EXT_SUFFIX=.cpython-36m-x86_64-linux-gnu.so', '-DCMAKE_BUILD_TYPE=Release', '-DONNX_ML=1', '/content/drive/My Drive/onnx-coreml/third_party/onnx']' returned non-zero exit status 1.

Any ideas what I am doing wrong?

fellowProgrammer commented 4 years ago

There is good news and bad news.

The good news is that I finally figured out how to install from source (the error I was getting above was due to me using install-develop.sh instead of install.sh). I also got past my original error. The Slice Node converts perfectly. However now I am getting another error!

I am using the same script as in my original post but this time I am getting this error:

...
160/229: Converting Node Type Mul
161/229: Converting Node Type Div
162/229: Converting Node Type Conv
163/229: Converting Node Type LeakyRelu
164/229: Converting Node Type Resize
Traceback (most recent call last):
  File "convert2.py", line 11, in <module>
    coreml_model = convert(model_proto, image_input_names=['inputImage'], image_output_names=['outputImage'], minimum_ios_deployment_target='13')
  File "/Users/fellowProgrammer/.local/share/virtualenvs/ONNXToCoreML-8TPYSbW4/lib/python3.7/site-packages/onnx_coreml/converter.py", line 626, in convert
    _convert_node_nd(builder, node, graph, err)
  File "/Users/fellowProgrammer//.local/share/virtualenvs/ONNXToCoreML-8TPYSbW4/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py", line 2389, in _convert_node_nd
    return converter_fn(builder, node, graph, err)
  File "/Users/fellowProgrammer//.local/share/virtualenvs/ONNXToCoreML-8TPYSbW4/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py", line 1738, in _convert_resize
    mode=mode
  File "/Users/fellowProgrammer//.local/share/virtualenvs/ONNXToCoreML-8TPYSbW4/lib/python3.7/site-packages/coremltools/models/neural_network/builder.py", line 1454, in add_upsample
    spec_layer_params.scalingFactor.append(scaling_factor_h)
TypeError: 2.0 has type numpy.float32, but expected one of: int, long
mohitnihalani commented 4 years ago

There is so some issue in the Onnx conversion of the PyTorch model as some nodes are not able to infer the shape correctly. I would suggest you create a new onnx model using the existing one using Onnx Simplifier and make sure you choose opset 9 while converting the model. It worked for me.