1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
%global _empty_manifest_terminate_build 0
Name: python-onnx-coreml
Version: 1.3
Release: 1
Summary: Convert ONNX (Open Neural Network Exchange)models into Apple CoreML format.
License: MIT
URL: https://github.com/onnx/onnx-coreml/
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/3f/aa/ba8356aae41d889df39a4d6d0040c6f322efff227a9acb8daf63360b291b/onnx-coreml-1.3.tar.gz
BuildArch: noarch
Requires: python3-click
Requires: python3-numpy
Requires: python3-sympy
Requires: python3-onnx
Requires: python3-typing
Requires: python3-typing-extensions
Requires: python3-coremltools
%description
# Convert ONNX models into Apple Core ML format.
[](https://travis-ci.org/onnx/onnx-coreml)
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).
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.
To get the latest version of `onnx-coreml` from PyPI:
```shell
pip install --upgrade onnx-coreml
pip install --upgrade coremltools # onnx-coreml depends on the coremltools package
```
For the latest changes please see the [release notes](https://github.com/onnx/onnx-coreml/releases).
To get the latest version from source (master branch of this repository), please see the [installation section](#Installation).
## Usage
Please see the ONNX conversion section in the [Neural network guide](https://github.com/apple/coremltools/blob/master/examples/NeuralNetworkGuide.md)
on how to use the converter.
There are a few [notebook examples](https://github.com/apple/coremltools/tree/master/examples/neural_network_inference)
as well for reference.
### Parameters
```python
def convert(model,
mode=None,
image_input_names=[],
preprocessing_args={},
image_output_names=[],
deprocessing_args={},
class_labels=None,
predicted_feature_name='classLabel',
add_custom_layers=False,
custom_conversion_functions={},
minimum_ios_deployment_target='13')
```
```
__model__: ONNX model | str
An ONNX model with parameters loaded in onnx package or path to file
with models.
__mode__: str ('classifier', 'regressor' or None)
Mode of the converted coreml model:
'classifier', a NeuralNetworkClassifier spec will be constructed.
'regressor', a NeuralNetworkRegressor spec will be constructed.
__image_input_names__: list of strings
Name of the inputs to be defined as image type. Otherwise, by default all inputs are MultiArray type.
__preprocessing_args__: dict
Specify preprocessing parameters, that are be applied to all the image inputs specified through the "image_input_names" parameter.
'is_bgr', 'red_bias', 'green_bias', 'blue_bias', 'gray_bias',
'image_scale' keys with the same meaning as coremltools set_pre_processing_parameters: https://apple.github.io/coremltools/generated/coremltools.models.neural_network.builder.html#coremltools.models.neural_network.builder.NeuralNetworkBuilder.set_pre_processing_parameters
__image_output_names__: list of strings
Name of the outputs to be defined as image type. Otherwise, by default all outputs are MultiArray type.
__deprocessing_args__: dict
Same as 'preprocessing_args' but for the outputs.
__class_labels__: A string or list of strings.
As a string it represents the name of the file which contains
the classification labels (one per line).
As a list of strings it represents a list of categories that map
the index of the output of a neural network to labels in a classifier.
__predicted_feature_name__: str
Name of the output feature for the class labels exposed in the Core ML
model (applies to classifiers only). Defaults to 'classLabel'
__add_custom_layers__: bool
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
that have unsupported attributes.
Parameters for these custom layers should be filled manually by editing the mlmodel
or the 'custom_conversion_functions' argument can be used to do the same during the process of conversion
__custom_conversion_fuctions__: dict (str: function)
Specify custom function to be used for conversion for given op.
User can override existing conversion function and provide their own custom implementation to convert certain ops.
Dictionary key must be string specifying ONNX Op name or Op type and value must be a function implementation available in current context.
Example usage: {'Flatten': custom_flatten_converter, 'Exp': exp_converter}
`custom_flatten_converter()` and `exp_converter()` will be invoked instead of internal onnx-coreml conversion implementation for these two Ops;
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.
`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.
This option gives finer control to user. One use case could be to modify input attributes or certain graph properties before calling
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).
Examples: https://github.com/onnx/onnx-coreml/blob/master/tests/custom_layers_test.py#L43
__onnx_coreml_input_shape_map__: dict (str: List[int])
(Optional)
(only used if `minimum_ios_deployment_target` version is less than '13')
A dictionary with keys corresponding to the model input names. Values are a list of integers that specify
how the shape of the input is mapped to Core ML. Convention used for Core ML shapes is:
0: Sequence, 1: Batch, 2: channel, 3: height, 4: width.
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.
__minimum_ios_deployment_target__: str
Target Deployment iOS version (default: '12')
Supported values: '11.2', '12', '13'
Core ML model produced by the converter will be compatible with the iOS version specified in this argument.
e.g. if `minimum_ios_deployment_target` = '12', the converter would only utilize Core ML features released till iOS12
(equivalently macOS 10.14, watchOS 5 etc).
Release notes:
* iOS 11 / Core ML 1: https://github.com/apple/coremltools/releases/tag/v0.8
* iOS 12 / Core ML 2: https://github.com/apple/coremltools/releases/tag/v2.0
* iOS 13 / Core ML 3: https://github.com/apple/coremltools/releases/tag/v3.0-beta
```
### Returns
```
__model__: A Core ML model.
```
### CLI
Also you can use command-line script for simplicity:
```
convert-onnx-to-coreml [OPTIONS] ONNX_MODEL
```
The command-line script currently doesn't support all options mentioned above. For more advanced use cases, you have to call the python function directly.
## Installation
### Install From PyPI
```bash
pip install -U onnx-coreml
```
### Install From Source
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,
```bash
git clone --recursive https://github.com/onnx/onnx-coreml.git
cd onnx-coreml
./install.sh
```
### Install From Source (for contributors)
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,
```bash
git clone --recursive https://github.com/onnx/onnx-coreml.git
cd onnx-coreml
./install-develop.sh
```
## Running Unit Tests
In order to run unit tests, you need `pytest`.
```shell
pip install pytest
pip install pytest-cov
```
To run all unit tests, navigate to the `tests/` folder and run
```shell
pytest
```
To run a specific unit test, for instance the custom layer test, run
```shell
pytest -s custom_layers_test.py::CustomLayerTest::test_unsupported_ops_provide_functions
```
## Currently Supported
### Models
Models from https://github.com/onnx/models that have been tested to work with this converter:
- BVLC Alexnet
- BVLC Caffenet
- BVLC Googlenet
- BVLC reference_rcnn_ilsvrc13
- Densenet
- Emotion-FERPlus
- Inception V1
- Inception V2
- MNIST
- Resnet50
- Shufflenet
- SqueezeNet
- VGG
- ZFNet
### Examples
You can find examples for converting a model through ONNX -> CoreML [here](https://github.com/onnx/onnx-coreml/tree/master/examples)
### Operators
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)
List of [ONNX operators supported in Core ML 3.0 via the converter](https://github.com/onnx/onnx-coreml/blob/3af826dfb0f17de4310d989acc7d6c5aea42e407/onnx_coreml/_operators_nd.py#L2233)
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.
For unsupported ops or unsupported attributes within supported ops, Core ML custom layers or custom functions can be used.
See the testing script `tests/custom_layers_test.py` on how to produce Core ML models with custom layers and custom functions.
## License
Copyright © 2018 by Apple Inc., Facebook Inc., and Prisma Labs Inc.
Use of this source code is governed by the [MIT License](https://opensource.org/licenses/MIT) that can be found in the LICENSE.txt file.
%package -n python3-onnx-coreml
Summary: Convert ONNX (Open Neural Network Exchange)models into Apple CoreML format.
Provides: python-onnx-coreml
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-onnx-coreml
# Convert ONNX models into Apple Core ML format.
[](https://travis-ci.org/onnx/onnx-coreml)
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).
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.
To get the latest version of `onnx-coreml` from PyPI:
```shell
pip install --upgrade onnx-coreml
pip install --upgrade coremltools # onnx-coreml depends on the coremltools package
```
For the latest changes please see the [release notes](https://github.com/onnx/onnx-coreml/releases).
To get the latest version from source (master branch of this repository), please see the [installation section](#Installation).
## Usage
Please see the ONNX conversion section in the [Neural network guide](https://github.com/apple/coremltools/blob/master/examples/NeuralNetworkGuide.md)
on how to use the converter.
There are a few [notebook examples](https://github.com/apple/coremltools/tree/master/examples/neural_network_inference)
as well for reference.
### Parameters
```python
def convert(model,
mode=None,
image_input_names=[],
preprocessing_args={},
image_output_names=[],
deprocessing_args={},
class_labels=None,
predicted_feature_name='classLabel',
add_custom_layers=False,
custom_conversion_functions={},
minimum_ios_deployment_target='13')
```
```
__model__: ONNX model | str
An ONNX model with parameters loaded in onnx package or path to file
with models.
__mode__: str ('classifier', 'regressor' or None)
Mode of the converted coreml model:
'classifier', a NeuralNetworkClassifier spec will be constructed.
'regressor', a NeuralNetworkRegressor spec will be constructed.
__image_input_names__: list of strings
Name of the inputs to be defined as image type. Otherwise, by default all inputs are MultiArray type.
__preprocessing_args__: dict
Specify preprocessing parameters, that are be applied to all the image inputs specified through the "image_input_names" parameter.
'is_bgr', 'red_bias', 'green_bias', 'blue_bias', 'gray_bias',
'image_scale' keys with the same meaning as coremltools set_pre_processing_parameters: https://apple.github.io/coremltools/generated/coremltools.models.neural_network.builder.html#coremltools.models.neural_network.builder.NeuralNetworkBuilder.set_pre_processing_parameters
__image_output_names__: list of strings
Name of the outputs to be defined as image type. Otherwise, by default all outputs are MultiArray type.
__deprocessing_args__: dict
Same as 'preprocessing_args' but for the outputs.
__class_labels__: A string or list of strings.
As a string it represents the name of the file which contains
the classification labels (one per line).
As a list of strings it represents a list of categories that map
the index of the output of a neural network to labels in a classifier.
__predicted_feature_name__: str
Name of the output feature for the class labels exposed in the Core ML
model (applies to classifiers only). Defaults to 'classLabel'
__add_custom_layers__: bool
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
that have unsupported attributes.
Parameters for these custom layers should be filled manually by editing the mlmodel
or the 'custom_conversion_functions' argument can be used to do the same during the process of conversion
__custom_conversion_fuctions__: dict (str: function)
Specify custom function to be used for conversion for given op.
User can override existing conversion function and provide their own custom implementation to convert certain ops.
Dictionary key must be string specifying ONNX Op name or Op type and value must be a function implementation available in current context.
Example usage: {'Flatten': custom_flatten_converter, 'Exp': exp_converter}
`custom_flatten_converter()` and `exp_converter()` will be invoked instead of internal onnx-coreml conversion implementation for these two Ops;
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.
`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.
This option gives finer control to user. One use case could be to modify input attributes or certain graph properties before calling
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).
Examples: https://github.com/onnx/onnx-coreml/blob/master/tests/custom_layers_test.py#L43
__onnx_coreml_input_shape_map__: dict (str: List[int])
(Optional)
(only used if `minimum_ios_deployment_target` version is less than '13')
A dictionary with keys corresponding to the model input names. Values are a list of integers that specify
how the shape of the input is mapped to Core ML. Convention used for Core ML shapes is:
0: Sequence, 1: Batch, 2: channel, 3: height, 4: width.
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.
__minimum_ios_deployment_target__: str
Target Deployment iOS version (default: '12')
Supported values: '11.2', '12', '13'
Core ML model produced by the converter will be compatible with the iOS version specified in this argument.
e.g. if `minimum_ios_deployment_target` = '12', the converter would only utilize Core ML features released till iOS12
(equivalently macOS 10.14, watchOS 5 etc).
Release notes:
* iOS 11 / Core ML 1: https://github.com/apple/coremltools/releases/tag/v0.8
* iOS 12 / Core ML 2: https://github.com/apple/coremltools/releases/tag/v2.0
* iOS 13 / Core ML 3: https://github.com/apple/coremltools/releases/tag/v3.0-beta
```
### Returns
```
__model__: A Core ML model.
```
### CLI
Also you can use command-line script for simplicity:
```
convert-onnx-to-coreml [OPTIONS] ONNX_MODEL
```
The command-line script currently doesn't support all options mentioned above. For more advanced use cases, you have to call the python function directly.
## Installation
### Install From PyPI
```bash
pip install -U onnx-coreml
```
### Install From Source
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,
```bash
git clone --recursive https://github.com/onnx/onnx-coreml.git
cd onnx-coreml
./install.sh
```
### Install From Source (for contributors)
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,
```bash
git clone --recursive https://github.com/onnx/onnx-coreml.git
cd onnx-coreml
./install-develop.sh
```
## Running Unit Tests
In order to run unit tests, you need `pytest`.
```shell
pip install pytest
pip install pytest-cov
```
To run all unit tests, navigate to the `tests/` folder and run
```shell
pytest
```
To run a specific unit test, for instance the custom layer test, run
```shell
pytest -s custom_layers_test.py::CustomLayerTest::test_unsupported_ops_provide_functions
```
## Currently Supported
### Models
Models from https://github.com/onnx/models that have been tested to work with this converter:
- BVLC Alexnet
- BVLC Caffenet
- BVLC Googlenet
- BVLC reference_rcnn_ilsvrc13
- Densenet
- Emotion-FERPlus
- Inception V1
- Inception V2
- MNIST
- Resnet50
- Shufflenet
- SqueezeNet
- VGG
- ZFNet
### Examples
You can find examples for converting a model through ONNX -> CoreML [here](https://github.com/onnx/onnx-coreml/tree/master/examples)
### Operators
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)
List of [ONNX operators supported in Core ML 3.0 via the converter](https://github.com/onnx/onnx-coreml/blob/3af826dfb0f17de4310d989acc7d6c5aea42e407/onnx_coreml/_operators_nd.py#L2233)
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.
For unsupported ops or unsupported attributes within supported ops, Core ML custom layers or custom functions can be used.
See the testing script `tests/custom_layers_test.py` on how to produce Core ML models with custom layers and custom functions.
## License
Copyright © 2018 by Apple Inc., Facebook Inc., and Prisma Labs Inc.
Use of this source code is governed by the [MIT License](https://opensource.org/licenses/MIT) that can be found in the LICENSE.txt file.
%package help
Summary: Development documents and examples for onnx-coreml
Provides: python3-onnx-coreml-doc
%description help
# Convert ONNX models into Apple Core ML format.
[](https://travis-ci.org/onnx/onnx-coreml)
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).
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.
To get the latest version of `onnx-coreml` from PyPI:
```shell
pip install --upgrade onnx-coreml
pip install --upgrade coremltools # onnx-coreml depends on the coremltools package
```
For the latest changes please see the [release notes](https://github.com/onnx/onnx-coreml/releases).
To get the latest version from source (master branch of this repository), please see the [installation section](#Installation).
## Usage
Please see the ONNX conversion section in the [Neural network guide](https://github.com/apple/coremltools/blob/master/examples/NeuralNetworkGuide.md)
on how to use the converter.
There are a few [notebook examples](https://github.com/apple/coremltools/tree/master/examples/neural_network_inference)
as well for reference.
### Parameters
```python
def convert(model,
mode=None,
image_input_names=[],
preprocessing_args={},
image_output_names=[],
deprocessing_args={},
class_labels=None,
predicted_feature_name='classLabel',
add_custom_layers=False,
custom_conversion_functions={},
minimum_ios_deployment_target='13')
```
```
__model__: ONNX model | str
An ONNX model with parameters loaded in onnx package or path to file
with models.
__mode__: str ('classifier', 'regressor' or None)
Mode of the converted coreml model:
'classifier', a NeuralNetworkClassifier spec will be constructed.
'regressor', a NeuralNetworkRegressor spec will be constructed.
__image_input_names__: list of strings
Name of the inputs to be defined as image type. Otherwise, by default all inputs are MultiArray type.
__preprocessing_args__: dict
Specify preprocessing parameters, that are be applied to all the image inputs specified through the "image_input_names" parameter.
'is_bgr', 'red_bias', 'green_bias', 'blue_bias', 'gray_bias',
'image_scale' keys with the same meaning as coremltools set_pre_processing_parameters: https://apple.github.io/coremltools/generated/coremltools.models.neural_network.builder.html#coremltools.models.neural_network.builder.NeuralNetworkBuilder.set_pre_processing_parameters
__image_output_names__: list of strings
Name of the outputs to be defined as image type. Otherwise, by default all outputs are MultiArray type.
__deprocessing_args__: dict
Same as 'preprocessing_args' but for the outputs.
__class_labels__: A string or list of strings.
As a string it represents the name of the file which contains
the classification labels (one per line).
As a list of strings it represents a list of categories that map
the index of the output of a neural network to labels in a classifier.
__predicted_feature_name__: str
Name of the output feature for the class labels exposed in the Core ML
model (applies to classifiers only). Defaults to 'classLabel'
__add_custom_layers__: bool
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
that have unsupported attributes.
Parameters for these custom layers should be filled manually by editing the mlmodel
or the 'custom_conversion_functions' argument can be used to do the same during the process of conversion
__custom_conversion_fuctions__: dict (str: function)
Specify custom function to be used for conversion for given op.
User can override existing conversion function and provide their own custom implementation to convert certain ops.
Dictionary key must be string specifying ONNX Op name or Op type and value must be a function implementation available in current context.
Example usage: {'Flatten': custom_flatten_converter, 'Exp': exp_converter}
`custom_flatten_converter()` and `exp_converter()` will be invoked instead of internal onnx-coreml conversion implementation for these two Ops;
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.
`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.
This option gives finer control to user. One use case could be to modify input attributes or certain graph properties before calling
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).
Examples: https://github.com/onnx/onnx-coreml/blob/master/tests/custom_layers_test.py#L43
__onnx_coreml_input_shape_map__: dict (str: List[int])
(Optional)
(only used if `minimum_ios_deployment_target` version is less than '13')
A dictionary with keys corresponding to the model input names. Values are a list of integers that specify
how the shape of the input is mapped to Core ML. Convention used for Core ML shapes is:
0: Sequence, 1: Batch, 2: channel, 3: height, 4: width.
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.
__minimum_ios_deployment_target__: str
Target Deployment iOS version (default: '12')
Supported values: '11.2', '12', '13'
Core ML model produced by the converter will be compatible with the iOS version specified in this argument.
e.g. if `minimum_ios_deployment_target` = '12', the converter would only utilize Core ML features released till iOS12
(equivalently macOS 10.14, watchOS 5 etc).
Release notes:
* iOS 11 / Core ML 1: https://github.com/apple/coremltools/releases/tag/v0.8
* iOS 12 / Core ML 2: https://github.com/apple/coremltools/releases/tag/v2.0
* iOS 13 / Core ML 3: https://github.com/apple/coremltools/releases/tag/v3.0-beta
```
### Returns
```
__model__: A Core ML model.
```
### CLI
Also you can use command-line script for simplicity:
```
convert-onnx-to-coreml [OPTIONS] ONNX_MODEL
```
The command-line script currently doesn't support all options mentioned above. For more advanced use cases, you have to call the python function directly.
## Installation
### Install From PyPI
```bash
pip install -U onnx-coreml
```
### Install From Source
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,
```bash
git clone --recursive https://github.com/onnx/onnx-coreml.git
cd onnx-coreml
./install.sh
```
### Install From Source (for contributors)
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,
```bash
git clone --recursive https://github.com/onnx/onnx-coreml.git
cd onnx-coreml
./install-develop.sh
```
## Running Unit Tests
In order to run unit tests, you need `pytest`.
```shell
pip install pytest
pip install pytest-cov
```
To run all unit tests, navigate to the `tests/` folder and run
```shell
pytest
```
To run a specific unit test, for instance the custom layer test, run
```shell
pytest -s custom_layers_test.py::CustomLayerTest::test_unsupported_ops_provide_functions
```
## Currently Supported
### Models
Models from https://github.com/onnx/models that have been tested to work with this converter:
- BVLC Alexnet
- BVLC Caffenet
- BVLC Googlenet
- BVLC reference_rcnn_ilsvrc13
- Densenet
- Emotion-FERPlus
- Inception V1
- Inception V2
- MNIST
- Resnet50
- Shufflenet
- SqueezeNet
- VGG
- ZFNet
### Examples
You can find examples for converting a model through ONNX -> CoreML [here](https://github.com/onnx/onnx-coreml/tree/master/examples)
### Operators
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)
List of [ONNX operators supported in Core ML 3.0 via the converter](https://github.com/onnx/onnx-coreml/blob/3af826dfb0f17de4310d989acc7d6c5aea42e407/onnx_coreml/_operators_nd.py#L2233)
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.
For unsupported ops or unsupported attributes within supported ops, Core ML custom layers or custom functions can be used.
See the testing script `tests/custom_layers_test.py` on how to produce Core ML models with custom layers and custom functions.
## License
Copyright © 2018 by Apple Inc., Facebook Inc., and Prisma Labs Inc.
Use of this source code is governed by the [MIT License](https://opensource.org/licenses/MIT) that can be found in the LICENSE.txt file.
%prep
%autosetup -n onnx-coreml-1.3
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-onnx-coreml -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Wed May 10 2023 Python_Bot <Python_Bot@openeuler.org> - 1.3-1
- Package Spec generated
|