1. Setup the Anaconda environment
- Download, install and run Anaconda.
- Select the Environments tab on the left.
- Click the Update index… button and wait for the update to finish.
- Click the Create button.
- 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
- Open a new Terminal.
- Type the following command to activate the new environment:
conda activate mxnet2coreml
- Type the following command to install the mxnet-to-coreml tool:
pip install mxnet-to-coreml
Among others, this will install the following packages:
Package | Version |
coremltools | 3.1 |
mxnet | 1.5.1.post0 |
mxnet-to-coreml | 0.1.3 |
pyyaml | 5.1.2 |
3. Convert the model
- 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
- Type the following command to cd in the Documents folder:
cd ~/Documents
- 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'
- Open the file in a text editor (i use Visual Studio Code):
- Copy the file path from above.
- Open Finder.
- Select Go -> Go to Folder…
- Paste the file path and click Go.
- Right-click on the file and select Open With -> Visual Studio Code.
- Locate the following line of code (line 102):
target_shape = node['shape']
- 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.
- Open the file in Visual Studio Code (see the previous section for detailed instructions).
- 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.')
- 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.
- Open the /Users/anastasioscho/Documents/model-symbol.json file in Visual Studio Code.
- Use Command+F to find all the SoftmaxActivation JSON Objects.
- Replace SoftmaxActivation with softmax.
- Replace the following:
"attrs": {"mode": "channel"},
With the following:
"attrs": {"axis": "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, }
- Add the following line after SoftmaxOutput (line 31):
'softmax' : _layers.convert_softmax,
Leave a Reply