• Skip to main content
  • Skip to primary sidebar

Anastasios Chondrogiannis

Software developer with a focus on iOS

MXNet to CoreML with mxnet-to-coreml package

November 8, 2019 by Anastasios Chondrogiannis Leave a Comment

1. Setup the Anaconda environment

  1. Download, install and run Anaconda.
  2. Select the Environments tab on the left.
  3. Click the Update index… button and wait for the update to finish.
  4. Click the Create button.
  5. Create a new environment with the following settings:
    • Name: mxnet2coreml
    • Leave Python checked and select the 2.7 version.
    • Leave R unchecked.

2. Install the dependencies

  1. Open a new Terminal.
  2. Type the following command to activate the new environment:
conda activate mxnet2coreml
  1. Type the following command to install the mxnet-to-coreml tool:
pip install mxnet-to-coreml

Among others, this will install the following packages:

PackageVersion
coremltools3.1
mxnet1.5.1.post0
mxnet-to-coreml0.1.3
pyyaml5.1.2

3. Convert the model

  1. Place the model in your Documents folder like so:
  • /Users/anastasioscho/Documents/model-symbol.json
  • /Users/anastasioscho/Documents/model-0012.params
  • /Users/anastasioscho/Documents/labels.txt
  1. Type the following command to cd in the Documents folder:
cd ~/Documents
  1. Run the following command to convert the model:
mxnet_coreml_converter.py --model-prefix='model' --epoch=12 --input-shape='{"data":"3,224,224"}' --mode=classifier --pre-processing-arguments='{"image_input_names":"data"}' --class-labels labels.txt --output-file="model.mlmodel"

4. Common Errors

4.1 KeyError: ‘shape’

You may get the following error:

File "/Users/anastasioscho/opt/anaconda3/envs/blog1/lib/python2.7/site-packages/converter/_layers.py", line 102, in convert_reshape
target_shape = node['shape']
KeyError: 'shape'
  1. Open the file in a text editor (i use Visual Studio Code):
    1. Copy the file path from above.
    2. Open Finder.
    3. Select Go -> Go to Folder…
    4. Paste the file path and click Go.
    5. Right-click on the file and select Open With -> Visual Studio Code.
  2. Locate the following line of code (line 102):
target_shape = node['shape']
  1. Replace it with the following line of code:
target_shape = literal_eval(node['attrs']['shape'])

4.2 Special dimensional values less than or equal to 0 are not supported yet.

You may get the following error:

File "/Users/anastasioscho/opt/anaconda3/envs/blog1/lib/python2.7/site-packages/converter/_layers.py", line 105, in convert_reshape
raise NotImplementedError('Special dimensional values less than or equal to 0 are not supported yet.'
NotImplementedError: Special dimensional values less than or equal to 0 are not supported yet.Feel free to file an issue here: https://github.com/dmlc/mxnet/issues.
  1. Open the file in Visual Studio Code (see the previous section for detailed instructions).
  2. Locate the following lines of code (starting from line 104):
if any(item <= 0 for item in target_shape):
        raise NotImplementedError('Special dimensional values less than or equal to 0 are not supported yet.'
                                  'Feel free to file an issue here: https://github.com/dmlc/mxnet/issues.')
  1. Delete them.

4.3 MXNet layer of type SoftmaxActivation is not supported.

You may get the following error:

File "/Users/anastasioscho/opt/anaconda3/envs/blog1/lib/python2.7/site-packages/converter/_mxnet_converter.py", line 98, in _get_layer_converter_fn
raise TypeError("MXNet layer of type %s is not supported." % layer)
TypeError: MXNet layer of type SoftmaxActivation is not supported.
  1. Open the /Users/anastasioscho/Documents/model-symbol.json file in Visual Studio Code.
  2. Use Command+F to find all the SoftmaxActivation JSON Objects.
  3. Replace SoftmaxActivation with softmax.
  4. Replace the following:
"attrs": {"mode": "channel"},

With the following:

"attrs": {"axis": "1"},
  1. Find the following lines of code:
_MXNET_LAYER_REGISTRY  = {
    'FullyConnected' : _layers.convert_dense,
    'Activation'     : _layers.convert_activation,
    'SoftmaxOutput'  : _layers.convert_softmax,
    'Convolution'    : _layers.convert_convolution,
    'Pooling'        : _layers.convert_pooling,
    'Flatten'        : _layers.convert_flatten,
    'transpose'      : _layers.convert_transpose,
    'Concat'         : _layers.convert_concat,
    'BatchNorm'      : _layers.convert_batchnorm,
    'elemwise_add'   : _layers.convert_elementwise_add,
    'Reshape'        : _layers.convert_reshape,
    'Deconvolution'  : _layers.convert_deconvolution,
}
  1. Add the following line after SoftmaxOutput (line 31):
'softmax'  : _layers.convert_softmax,

5. References

  • https://aws.amazon.com/blogs/machine-learning/bring-machine-learning-to-ios-apps-using-apache-mxnet-and-apple-core-ml/
  • https://github.com/apache/incubator-mxnet/pull/8484

Filed Under: Machine Learning

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Let’s connect!

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Recent Posts

  • Android OpenGL ES – Part 10: Specular lighting
  • Android OpenGL ES – Part 9: Diffuse lighting
  • Android OpenGL ES – Part 8: Ambient Lighting
  • Android OpenGL ES – Part 7: Textures
  • Android OpenGL ES – Part 6: Camera View

Archives

  • March 2020
  • February 2020
  • November 2019
  • October 2019
  • July 2019

Categories

  • Android Development
  • Machine Learning

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in