You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 7, 2023. It is now read-only.
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).
8
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 Core ML models and run them in an iOS app.
9
8
10
9
## [New] Beta onnx-coreml converter with Core ML 3
11
10
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),
13
12
install coremltools 3.0b6 and coremltools 1.0b3
14
13
15
14
```shell
16
15
pip install coremltools==3.0b6
17
16
pip install onnx-coreml==1.0b3
18
17
```
19
18
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'.
25
23
26
24
For example:
25
+
27
26
```python
28
27
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
30
29
```
31
30
32
31
## Installation
@@ -40,6 +39,7 @@ pip install -U onnx-coreml
40
39
### Install From Source
41
40
42
41
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,
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,
To convert models use single function "convert" from onnx_coreml:
68
+
To convert models use single function `convert` from onnx_coreml:
68
69
69
70
```python
70
71
from onnx_coreml import convert
@@ -79,20 +80,19 @@ def convert(model,
79
80
deprocessing_args={},
80
81
class_labels=None,
81
82
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')
86
86
```
87
87
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.:
89
89
90
90
```python
91
91
mlmodel = convert(onnx_model)
92
-
mlmodel.save('coreml_model.mlmodel')
92
+
mlmodel.save('model.mlmodel')
93
93
```
94
94
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.:
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.
131
133
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.
134
136
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
139
141
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'
144
142
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
146
148
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
149
151
or the 'custom_conversion_functions' argument can be used to do the same during the process of conversion
Specify custom function to be used for conversion for given op.
153
155
User can override existing conversion function and provide their own custom implementation to convert certain ops.
154
156
Dictionary key must be string specifying ONNX Op name or Op type and value must be a function implementation available in current context.
155
157
Example usage: {'Flatten': custom_flatten_converter, 'Exp': exp_converter}
156
158
`custom_flatten_converter()` and `exp_converter()` will be invoked instead of internal onnx-coreml conversion implementation for these two Ops;
157
159
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.
158
160
`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).
Models from https://github.com/onnx/models that have been tested to work with this converter:
217
224
218
225
- BVLC Alexnet
219
226
- BVLC Caffenet
220
227
- BVLC Googlenet
221
228
- BVLC reference_rcnn_ilsvrc13
222
-
- Densenet
223
-
- Emotion-FERPlus
229
+
- Densenet
230
+
- Emotion-FERPlus
224
231
- Inception V1
225
232
- Inception V2
226
233
- MNIST
@@ -232,13 +239,13 @@ Models from https://github.com/onnx/models that have been tested to work with th
232
239
233
240
234
241
### 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)
236
243
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)
238
245
239
246
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.
0 commit comments