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

Commit b6221b4

Browse files
1duobhushan23
authored andcommitted
Improve documentations (#480)
* Update README.md
1 parent d5795a8 commit b6221b4

File tree

1 file changed

+87
-80
lines changed

1 file changed

+87
-80
lines changed

README.md

Lines changed: 87 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
# Convert ONNX models into Apple CoreML format.
1+
# Convert ONNX models into Apple Core ML format.
22

33
[![Build Status](https://travis-ci.org/onnx/onnx-coreml.svg?branch=master)](https://travis-ci.org/onnx/onnx-coreml)
44

5-
This tool converts [ONNX](https://onnx.ai/) models to Apple CoreML format. To convert CoreML models to ONNX, use [ONNXMLTools](https://github.com/onnx/onnxmltools).
6-
7-
There's a comprehensive [Tutorial](https://github.com/onnx/tutorials/tree/master/examples/CoreML/ONNXLive/README.md) showing how to convert PyTorch style transfer models through ONNX to CoreML models and run them in an iOS app.
5+
This tool converts [ONNX](https://onnx.ai/) models to Apple Core ML format. To convert Core ML models to ONNX, use [ONNXMLTools](https://github.com/onnx/onnxmltools).
86

7+
There's a comprehensive [Tutorial](https://github.com/onnx/tutorials/tree/master/examples/CoreML/ONNXLive/README.md) showing how to convert PyTorch style transfer models through ONNX to Core ML models and run them in an iOS app.
98

109
## [New] Beta onnx-coreml converter with Core ML 3
1110

12-
To try out the new beta converter with CoreML 3 (>= iOS 13, >= macOS 15),
11+
To try out the new beta converter with Core ML 3 (>= iOS 13, >= macOS 15),
1312
install coremltools 3.0b6 and coremltools 1.0b3
1413

1514
```shell
1615
pip install coremltools==3.0b6
1716
pip install onnx-coreml==1.0b3
1817
```
1918

20-
In beta 3, the flag `disable_coreml_rank5_mapping` (which was part of beta 2) has been removed and instead replaced by
21-
the generic argument `target_ios` which can be used to target different versions of CoreML/iOS.
22-
23-
`target_ios` takes a string specifying target deployment iOS version e.g. '11.2', '12' and '13'.
24-
By default, the converter uses the value of '12'.
19+
In beta 3, the flag `disable_coreml_rank5_mapping` (which was part of beta 2) has been removed and instead replaced by
20+
the generic argument `target_ios` which can be used to target different versions of Core ML/iOS.
21+
The argument `target_ios` takes a string specifying the target deployment iOS version e.g. '11.2', '12' and '13'.
22+
By default, the converter uses the value of '12'.
2523

2624
For example:
25+
2726
```python
2827
from onnx_coreml import convert
29-
ml_model = convert(model='my_model.onnx', target_ios='13') # to use CoreML 3
28+
ml_model = convert(model='my_model.onnx', target_ios='13') # to use Core ML 3
3029
```
3130

3231
## Installation
@@ -40,6 +39,7 @@ pip install -U onnx-coreml
4039
### Install From Source
4140

4241
To get the latest version of the converter, install from source by cloning the repository along with its submodules and running the install.sh script. That is,
42+
4343
```bash
4444
git clone --recursive https://github.com/onnx/onnx-coreml.git
4545
cd onnx-coreml
@@ -49,6 +49,7 @@ cd onnx-coreml
4949
### Install From Source (for contributors)
5050

5151
To get the latest version of the converter, install from source by cloning the repository along with its submodules and running the install-develop.sh script. That is,
52+
5253
```bash
5354
git clone --recursive https://github.com/onnx/onnx-coreml.git
5455
cd onnx-coreml
@@ -62,9 +63,9 @@ cd onnx-coreml
6263
* coremltools (3.0+)
6364
* onnx (1.5.0+)
6465

65-
## How to use
66+
## How to Use
6667

67-
To convert models use single function "convert" from onnx_coreml:
68+
To convert models use single function `convert` from onnx_coreml:
6869

6970
```python
7071
from onnx_coreml import convert
@@ -79,20 +80,19 @@ def convert(model,
7980
deprocessing_args={},
8081
class_labels=None,
8182
predicted_feature_name='classLabel',
82-
add_custom_layers = False,
83-
custom_conversion_functions = {},
84-
target_ios='12'
85-
)
83+
add_custom_layers=False,
84+
custom_conversion_functions={},
85+
target_ios='12')
8686
```
8787

88-
The function returns a coreml model instance that can be saved to a .mlmodel file, e.g.:
88+
The function returns a Core ML model instance that can be saved to a `.mlmodel` file, e.g.:
8989

9090
```python
9191
mlmodel = convert(onnx_model)
92-
mlmodel.save('coreml_model.mlmodel')
92+
mlmodel.save('model.mlmodel')
9393
```
9494

95-
CoreML model spec can be obtained from the model instance, which can be used to update model properties such as output names, input names etc. For e.g.:
95+
Core ML model spec can be obtained from the model instance, which can be used to update model properties such as output names, input names etc. For e.g.:
9696

9797
```python
9898
import coremltools
@@ -104,83 +104,89 @@ coremltools.utils.rename_feature(spec, 'old_output_name', 'new_output_name')
104104
coremltools.utils.save_spec(spec, 'model_new_output_name.mlmodel')
105105
```
106106

107-
For more details see coremltools [documentation](https://apple.github.io/coremltools/#).
107+
For more details see coremltools [documentation](https://apple.github.io/coremltools/#).
108108

109109
### Parameters
110-
__model__: ONNX model | str
111-
An ONNX model with parameters loaded in onnx package or path to file
112-
with models.
113110

114-
__mode__: str ('classifier', 'regressor' or None)
115-
Mode of the converted coreml model:
116-
'classifier', a NeuralNetworkClassifier spec will be constructed.
117-
'regressor', a NeuralNetworkRegressor spec will be constructed.
111+
```
112+
__model__: ONNX model | str
113+
An ONNX model with parameters loaded in onnx package or path to file
114+
with models.
115+
116+
__mode__: str ('classifier', 'regressor' or None)
117+
Mode of the converted coreml model:
118+
'classifier', a NeuralNetworkClassifier spec will be constructed.
119+
'regressor', a NeuralNetworkRegressor spec will be constructed.
118120
119-
__image_input_names__: list of strings
120-
Name of the inputs to be defined as image type. Otherwise, by default all inputs are MultiArray type.
121+
__image_input_names__: list of strings
122+
Name of the inputs to be defined as image type. Otherwise, by default all inputs are MultiArray type.
121123
122-
__preprocessing_args__: dict
123-
Specify preprocessing parameters, that are be applied to all the image inputs specified through the "image_input_names" parameter.
124-
'is_bgr', 'red_bias', 'green_bias', 'blue_bias', 'gray_bias',
125-
'image_scale' keys with the same meaning as
124+
__preprocessing_args__: dict
125+
Specify preprocessing parameters, that are be applied to all the image inputs specified through the "image_input_names" parameter.
126+
'is_bgr', 'red_bias', 'green_bias', 'blue_bias', 'gray_bias',
127+
'image_scale' keys with the same meaning as
126128
127-
https://apple.github.io/coremltools/generated/coremltools.models.neural_network.html#coremltools.models.neural_network.NeuralNetworkBuilder.set_pre_processing_parameters
129+
https://apple.github.io/coremltools/generated/coremltools.models.neural_network.html#coremltools.models.neural_network.NeuralNetworkBuilder.set_pre_processing_parameters
128130
129-
__image_output_names__: list of strings
130-
Name of the outputs to be defined as image type. Otherwise, by default all outputs are MultiArray type.
131+
__image_output_names__: list of strings
132+
Name of the outputs to be defined as image type. Otherwise, by default all outputs are MultiArray type.
131133
132-
__deprocessing_args__: dict
133-
Same as 'preprocessing_args' but for the outputs.
134+
__deprocessing_args__: dict
135+
Same as 'preprocessing_args' but for the outputs.
134136
135-
__class_labels__: A string or list of strings.
136-
As a string it represents the name of the file which contains
137-
the classification labels (one per line).
138-
As a list of strings it represents a list of categories that map
137+
__class_labels__: A string or list of strings.
138+
As a string it represents the name of the file which contains
139+
the classification labels (one per line).
140+
As a list of strings it represents a list of categories that map
139141
the index of the output of a neural network to labels in a classifier.
140-
141-
__predicted_feature_name__: str
142-
Name of the output feature for the class labels exposed in the Core ML
143-
model (applies to classifiers only). Defaults to 'classLabel'
144142
145-
__add_custom_layers__: bool
143+
__predicted_feature_name__: str
144+
Name of the output feature for the class labels exposed in the Core ML
145+
model (applies to classifiers only). Defaults to 'classLabel'
146+
147+
__add_custom_layers__: bool
146148
If True, then ['custom'](https://developer.apple.com/documentation/coreml/core_ml_api/integrating_custom_layers?language=objc) layers will be added to the model in place of unsupported onnx ops or for the ops
147-
that have unsupported attributes.
148-
Parameters for these custom layers should be filled manually by editing the mlmodel
149+
that have unsupported attributes.
150+
Parameters for these custom layers should be filled manually by editing the mlmodel
149151
or the 'custom_conversion_functions' argument can be used to do the same during the process of conversion
150152
151-
__custom_conversion_fuctions__: dict (str: function)
153+
__custom_conversion_fuctions__: dict (str: function)
152154
Specify custom function to be used for conversion for given op.
153155
User can override existing conversion function and provide their own custom implementation to convert certain ops.
154156
Dictionary key must be string specifying ONNX Op name or Op type and value must be a function implementation available in current context.
155157
Example usage: {'Flatten': custom_flatten_converter, 'Exp': exp_converter}
156158
`custom_flatten_converter()` and `exp_converter()` will be invoked instead of internal onnx-coreml conversion implementation for these two Ops;
157159
Hence, User must provide implementation for functions specified in the dictionary. If user provides two separate functions for node name and node type, then custom function tied to node name will be used. As, function tied to node type is more generic than one tied to node name.
158160
`custom_conversion_functions` option is different than `add_custom_layers`. Both options can be used in conjuction in which case, custom function will be invoked for provided ops and custom layer will be added for ops with no respective conversion function.
159-
This option gives finer control to user. One use case could be to modify input attributes or certain graph properties before calling
160-
existing onnx-coreml conversion function. Note that, It is custom conversion function's responsibility to add respective CoreML layer into builder(coreml tools's NeuralNetworkBuilder).
161+
This option gives finer control to user. One use case could be to modify input attributes or certain graph properties before calling
162+
existing onnx-coreml conversion function. Note that, It is custom conversion function's responsibility to add respective Core ML layer into builder(coreml tools's NeuralNetworkBuilder).
161163
Examples: https://github.com/onnx/onnx-coreml/blob/master/tests/custom_layers_test.py#L43
162164
163-
__onnx_coreml_input_shape_map__: dict (str: List[int])
164-
(Optional)
165-
(only used if `target_ios` version is less than '13')
165+
__onnx_coreml_input_shape_map__: dict (str: List[int])
166+
(Optional)
167+
(only used if `target_ios` version is less than '13')
166168
A dictionary with keys corresponding to the model input names. Values are a list of integers that specify
167-
how the shape of the input is mapped to CoreML. Convention used for CoreML shapes is:
168-
0: Sequence, 1: Batch, 2: channel, 3: height, 4: width.
169-
For example, an input of rank 2 could be mapped as [3,4] (i.e. H,W) or [1,2] (i.e. B,C) etc.
170-
171-
__target_ios__: str
172-
Target Deployment iOS version (default: '12')
173-
Supported values: '11.2', '12', '13'
174-
CoreML model produced by the converter will be compatible with the iOS version specified in this argument.
175-
e.g. if `target_ios` = '12', the converter would only utilize CoreML features released till iOS12
176-
(equivalently macOS 10.14, watchOS 5 etc).
177-
Release notes:
178-
* iOS 11 / CoreML 1: https://github.com/apple/coremltools/releases/tag/v0.8
179-
* iOS 12 / CoreML 2: https://github.com/apple/coremltools/releases/tag/v2.0
180-
* iOS 13 / CoreML 3: https://github.com/apple/coremltools/releases/tag/v3.0-beta
181-
169+
how the shape of the input is mapped to Core ML. Convention used for Core ML shapes is:
170+
0: Sequence, 1: Batch, 2: channel, 3: height, 4: width.
171+
For example, an input of rank 2 could be mapped as [3,4] (i.e. H,W) or [1,2] (i.e. B,C) etc.
172+
173+
__target_ios__: str
174+
Target Deployment iOS version (default: '12')
175+
Supported values: '11.2', '12', '13'
176+
Core ML model produced by the converter will be compatible with the iOS version specified in this argument.
177+
e.g. if `target_ios` = '12', the converter would only utilize Core ML features released till iOS12
178+
(equivalently macOS 10.14, watchOS 5 etc).
179+
Release notes:
180+
* iOS 11 / Core ML 1: https://github.com/apple/coremltools/releases/tag/v0.8
181+
* iOS 12 / Core ML 2: https://github.com/apple/coremltools/releases/tag/v2.0
182+
* iOS 13 / Core ML 3: https://github.com/apple/coremltools/releases/tag/v3.0-beta
183+
```
184+
182185
### Returns
183-
__model__: A coreml model.
186+
187+
```
188+
__model__: A Core ML model.
189+
```
184190

185191
### CLI
186192
Also you can use command-line script for simplicity:
@@ -192,7 +198,7 @@ The command-line script currently doesn't support all options mentioned above. F
192198

193199
## Running Unit Tests
194200

195-
In order to run unit tests, you need pytest.
201+
In order to run unit tests, you need `pytest`.
196202

197203
```shell
198204
pip install pytest
@@ -211,16 +217,17 @@ To run a specific unit test, for instance the custom layer test, run
211217
pytest -s custom_layers_test.py::CustomLayerTest::test_unsupported_ops_provide_functions
212218
```
213219

214-
## Currently supported
220+
## Currently Supported
221+
215222
### Models
216223
Models from https://github.com/onnx/models that have been tested to work with this converter:
217224

218225
- BVLC Alexnet
219226
- BVLC Caffenet
220227
- BVLC Googlenet
221228
- BVLC reference_rcnn_ilsvrc13
222-
- Densenet
223-
- Emotion-FERPlus
229+
- Densenet
230+
- Emotion-FERPlus
224231
- Inception V1
225232
- Inception V2
226233
- MNIST
@@ -232,13 +239,13 @@ Models from https://github.com/onnx/models that have been tested to work with th
232239

233240

234241
### Operators
235-
List of [ONNX operators supported in CoreML 2.0 via the converter](https://github.com/onnx/onnx-coreml/blob/4d8b1cc348e2d6a983a6d38bb6921b6b77b47e76/onnx_coreml/_operators.py#L1893)
242+
List of [ONNX operators supported in Core ML 2.0 via the converter](https://github.com/onnx/onnx-coreml/blob/4d8b1cc348e2d6a983a6d38bb6921b6b77b47e76/onnx_coreml/_operators.py#L1893)
236243

237-
List of [ONNX operators supported in CoreML 3.0 via the converter](https://github.com/onnx/onnx-coreml/blob/4d8b1cc348e2d6a983a6d38bb6921b6b77b47e76/onnx_coreml/_operators_nd.py#L1821)
244+
List of [ONNX operators supported in Core ML 3.0 via the converter](https://github.com/onnx/onnx-coreml/blob/4d8b1cc348e2d6a983a6d38bb6921b6b77b47e76/onnx_coreml/_operators_nd.py#L1821)
238245

239246
Some of the operators are partially compatible with Core ML, for example gemm with more than 1 non constant input is not supported in Core ML 2, or scale as an input for upsample layer is not supported in Core ML 3 etc.
240-
For unsupported ops or unsupported attributes within supported ops, CoreML custom layers or custom functions can be used.
241-
See the testing script `tests/custom_layers_test.py` on how to produce CoreML models with custom layers and custom functions.
247+
For unsupported ops or unsupported attributes within supported ops, Core ML custom layers or custom functions can be used.
248+
See the testing script `tests/custom_layers_test.py` on how to produce Core ML models with custom layers and custom functions.
242249

243250
## License
244251
Copyright © 2018 by Apple Inc., Facebook Inc., and Prisma Labs Inc.

0 commit comments

Comments
 (0)