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

Commit 928955a

Browse files
authored
update target_ios flag to minimum_ios_deployment_target (#512)
1 parent 1da82bb commit 928955a

13 files changed

+129
-127
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ pip install onnx-coreml==1.0
1717
```
1818

1919
Since 1.0 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'.
20+
the generic argument `minimum_ios_deployment_target` which can be used to target different versions of Core ML/iOS.
21+
The argument `minimum_ios_deployment_target` takes a string specifying the target deployment iOS version e.g. '11.2', '12' and '13'.
2222
By default, the converter uses the value of '12'.
2323

2424
For example:
2525

2626
```python
2727
from onnx_coreml import convert
28-
ml_model = convert(model='my_model.onnx', target_ios='13') # to use Core ML 3
28+
ml_model = convert(model='my_model.onnx', minimum_ios_deployment_target='13') # to use Core ML 3
2929
```
3030

3131
## Installation
@@ -82,7 +82,7 @@ def convert(model,
8282
predicted_feature_name='classLabel',
8383
add_custom_layers=False,
8484
custom_conversion_functions={},
85-
target_ios='12')
85+
minimum_ios_deployment_target='12')
8686
```
8787

8888
The function returns a Core ML model instance that can be saved to a `.mlmodel` file, e.g.:
@@ -162,17 +162,17 @@ __custom_conversion_fuctions__: dict (str: function)
162162
163163
__onnx_coreml_input_shape_map__: dict (str: List[int])
164164
(Optional)
165-
(only used if `target_ios` version is less than '13')
165+
(only used if `minimum_ios_deployment_target` version is less than '13')
166166
A dictionary with keys corresponding to the model input names. Values are a list of integers that specify
167167
how the shape of the input is mapped to Core ML. Convention used for Core ML shapes is:
168168
0: Sequence, 1: Batch, 2: channel, 3: height, 4: width.
169169
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.
170170
171-
__target_ios__: str
171+
__minimum_ios_deployment_target__: str
172172
Target Deployment iOS version (default: '12')
173173
Supported values: '11.2', '12', '13'
174174
Core ML 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 Core ML features released till iOS12
175+
e.g. if `minimum_ios_deployment_target` = '12', the converter would only utilize Core ML features released till iOS12
176176
(equivalently macOS 10.14, watchOS 5 etc).
177177
Release notes:
178178
* iOS 11 / Core ML 1: https://github.com/apple/coremltools/releases/tag/v0.8

examples/BERT.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@
10911091
],
10921092
"source": [
10931093
"# Step 2 - Convert from ONNX to CoreML MLModel\n",
1094-
"mlmodel = convert(model=onnx_model_path, target_ios=\"13\", )\n",
1094+
"mlmodel = convert(model=onnx_model_path, minimum_ios_deployment_target=\"13\", )\n",
10951095
"mlmodel.save(mlmodel_path)"
10961096
]
10971097
},
@@ -3139,7 +3139,7 @@
31393139
],
31403140
"source": [
31413141
"# ONNX to CoreML MLModel\n",
3142-
"mlmodel = convert(model=onnx_model_path, target_ios=\"13\")\n",
3142+
"mlmodel = convert(model=onnx_model_path, minimum_ios_deployment_target=\"13\")\n",
31433143
"mlmodel.save(mlmodel_path)"
31443144
]
31453145
},

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Converting PyTorch model into CoreML model is a two step process
4343
2. ONNX to CoreML
4444
```
4545
# Step 2 - ONNX to CoreML model
46-
mlmodel = convert(model='./small_model.onnx', target_ios='13')
46+
mlmodel = convert(model='./small_model.onnx', minimum_ios_deployment_target='13')
4747
# Save converted CoreML model
4848
mlmodel.save('small_model.mlmodel')
4949
```

examples/small_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ def forward(self, x):
2323
torch.onnx.export(model, dummy_input, './small_model.onnx')
2424

2525
# Step 2 - ONNX to CoreML model
26-
mlmodel = convert(model='./small_model.onnx', target_ios='13')
26+
mlmodel = convert(model='./small_model.onnx', minimum_ios_deployment_target='13')
2727
# Save converted CoreML model
2828
mlmodel.save('small_model.mlmodel')

onnx_coreml/_backend.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CoreMLBackend(Backend):
3636
def prepare(cls,
3737
model, # type: ModelProto
3838
device='CPU', # type: Text
39-
target_ios='12', # type: str
39+
minimum_ios_deployment_target='12', # type: str
4040
**kwargs # type: Any
4141
):
4242
# type: (...) -> CoreMLRep
@@ -45,11 +45,11 @@ def prepare(cls,
4545
with open('/tmp/node_model.onnx', 'wb') as f:
4646
s = model.SerializeToString()
4747
f.write(s)
48-
coreml_model = convert(model, target_ios=target_ios)
48+
coreml_model = convert(model, minimum_ios_deployment_target=minimum_ios_deployment_target)
4949
if DEBUG:
5050
coreml_model.save('/tmp/node_model.mlmodel')
5151
onnx_outputs_info = _get_onnx_outputs_info(model)
52-
return CoreMLRep(coreml_model, onnx_outputs_info, device == 'CPU', target_ios=target_ios)
52+
return CoreMLRep(coreml_model, onnx_outputs_info, device == 'CPU', minimum_ios_deployment_target=minimum_ios_deployment_target)
5353

5454
@classmethod
5555
def is_compatible(cls,
@@ -114,7 +114,7 @@ class CoreMLBackendND(Backend):
114114
def prepare(cls,
115115
model, # type: ModelProto
116116
device='CPU', # type: Text
117-
target_ios='13', # type: str
117+
minimum_ios_deployment_target='13', # type: str
118118
**kwargs # type: Any
119119
):
120120
# type: (...) -> CoreMLRep
@@ -123,11 +123,11 @@ def prepare(cls,
123123
with open('/tmp/node_model.onnx', 'wb') as f:
124124
s = model.SerializeToString()
125125
f.write(s)
126-
coreml_model = convert(model, target_ios=target_ios)
126+
coreml_model = convert(model, minimum_ios_deployment_target=minimum_ios_deployment_target)
127127
if DEBUG:
128128
coreml_model.save('/tmp/node_model.mlmodel')
129129
onnx_outputs_info = _get_onnx_outputs_info(model)
130-
return CoreMLRep(coreml_model, onnx_outputs_info, device == 'CPU', target_ios=target_ios)
130+
return CoreMLRep(coreml_model, onnx_outputs_info, device == 'CPU', minimum_ios_deployment_target=minimum_ios_deployment_target)
131131

132132
@classmethod
133133
def is_compatible(cls,

onnx_coreml/_backend_rep.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def __init__(self,
3535
coreml_model, # type: MLModel
3636
onnx_outputs_info, # type: Dict[Text, EdgeInfo]
3737
useCPUOnly=False, # type: bool
38-
target_ios='12' # type: str
38+
minimum_ios_deployment_target='12' # type: str
3939
):
4040
# type: (...) -> None
4141
super(CoreMLRep, self).__init__()
4242
self.model = coreml_model
4343
self.useCPUOnly = useCPUOnly
44-
self.target_ios = target_ios
44+
self.minimum_ios_deployment_target = minimum_ios_deployment_target
4545

4646
spec = coreml_model.get_spec()
4747
self.input_names = [str(i.name) for i in spec.description.input]
@@ -56,7 +56,7 @@ def run(self,
5656
super(CoreMLRep, self).run(inputs, **kwargs)
5757
inputs_ = inputs
5858
_reshaped = False
59-
if not SupportedVersion.is_nd_array_supported(self.target_ios):
59+
if not SupportedVersion.is_nd_array_supported(self.minimum_ios_deployment_target):
6060
for i, input_ in enumerate(inputs_):
6161
shape = input_.shape
6262
if len(shape) == 4 or len(shape) == 2:
@@ -80,7 +80,7 @@ def run(self,
8080
prediction = self.model.predict(input_dict, self.useCPUOnly)
8181
output_values = [prediction[name] for name in self.output_names]
8282

83-
if not SupportedVersion.is_nd_array_supported(self.target_ios):
83+
if not SupportedVersion.is_nd_array_supported(self.minimum_ios_deployment_target):
8484
for i, output_ in enumerate(output_values):
8585
shape = output_.shape
8686
#reshape the CoreML output to match Onnx's output shape

onnx_coreml/_error_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self,
2121
self.custom_conversion_functions = custom_conversion_functions
2222
self.custom_layer_nodes = custom_layer_nodes
2323

24-
self.rerun_suggestion = '\n Please try converting with higher target_ios.\n' \
24+
self.rerun_suggestion = '\n Please try converting with higher minimum_ios_deployment_target.\n' \
2525
'You can also provide custom function/layer to convert the model.'
2626

2727

onnx_coreml/converter.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,29 @@ class SupportedVersion():
4949
ND_ARRARY_SUPPORT = IOS_13_VERSION
5050

5151
@staticmethod
52-
def ios_support_check(target_ios):
53-
return target_ios in SupportedVersion.supported_ios_version
52+
def ios_support_check(minimum_ios_deployment_target):
53+
return minimum_ios_deployment_target in SupportedVersion.supported_ios_version
5454

5555
@staticmethod
56-
def is_nd_array_supported(target_ios):
57-
if not SupportedVersion.ios_support_check(target_ios):
58-
raise TypeError('{} not supported. Please provide one of target iOS: {}', target_ios, SupportedVersion.supported_ios_version)
56+
def is_nd_array_supported(minimum_ios_deployment_target):
57+
if not SupportedVersion.ios_support_check(minimum_ios_deployment_target):
58+
raise TypeError('{} not supported. Please provide one of target iOS: {}', minimum_ios_deployment_target, SupportedVersion.supported_ios_version)
5959

60-
target_ios_index = SupportedVersion.supported_ios_version.index(target_ios)
61-
return SupportedVersion.ND_ARRARY_SUPPORT <= target_ios_index
60+
minimum_ios_deployment_target_index = SupportedVersion.supported_ios_version.index(minimum_ios_deployment_target)
61+
return SupportedVersion.ND_ARRARY_SUPPORT <= minimum_ios_deployment_target_index
6262

6363
@staticmethod
6464
def get_supported_ios():
6565
return SupportedVersion.supported_ios_version
6666

6767
@staticmethod
68-
def get_specification_version(target_ios):
69-
if not SupportedVersion.ios_support_check(target_ios):
70-
raise TypeError('{} not supported. Please provide one of target iOS: {}', target_ios, SupportedVersion.supported_ios_version)
68+
def get_specification_version(minimum_ios_deployment_target):
69+
if not SupportedVersion.ios_support_check(minimum_ios_deployment_target):
70+
raise TypeError('{} not supported. Please provide one of target iOS: {}', minimum_ios_deployment_target, SupportedVersion.supported_ios_version)
7171

72-
if target_ios == '11.2':
72+
if minimum_ios_deployment_target == '11.2':
7373
return IOS_11_2_SPEC_VERSION
74-
elif target_ios == '12':
74+
elif minimum_ios_deployment_target == '12':
7575
return IOS_12_SPEC_VERSION
7676
else:
7777
return IOS_13_SPEC_VERSION
@@ -219,7 +219,7 @@ def _check_unsupported_ops(nodes, disable_coreml_rank5_mapping=False): # type: (
219219

220220
coreml_3_rerun_message = ''
221221
if not disable_coreml_rank5_mapping:
222-
coreml_3_rerun_message = '\nPlease try converting again with target_ios=13' \
222+
coreml_3_rerun_message = '\nPlease try converting again with minimum_ios_deployment_target=13' \
223223
' and coremltools 3.0 latest beta'
224224
if len(unsupported_op_types) > 0:
225225
raise NotImplementedError("Unsupported ONNX ops of type: %s %s" % (
@@ -385,7 +385,7 @@ def convert(model, # type: Union[onnx.ModelProto, Text]
385385
add_custom_layers = False, # type: bool
386386
custom_conversion_functions = {}, #type: Dict[Text, Any]
387387
onnx_coreml_input_shape_map = {}, # type: Dict[Text, List[int,...]]
388-
target_ios = '12'):
388+
minimum_ios_deployment_target = '12'):
389389
# type: (...) -> MLModel
390390
"""
391391
Convert ONNX model to CoreML.
@@ -426,12 +426,12 @@ def convert(model, # type: Union[onnx.ModelProto, Text]
426426
how the shape of the input is mapped to CoreML. Convention used for CoreML shapes is
427427
0: Sequence, 1: Batch, 2: channel, 3: height, 4: width.
428428
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.
429-
This is ignored if "target_ios" is set to 13.
430-
target_ios: str
429+
This is ignored if "minimum_ios_deployment_target" is set to 13.
430+
minimum_ios_deployment_target: str
431431
Target Deployment iOS Version (default: '12')
432432
Supported iOS version options: '11.2', '12', '13'
433433
CoreML model produced by the converter will be compatible with the iOS version specified in this argument.
434-
e.g. if target_ios = '12', the converter would only utilize CoreML features released till iOS12 (equivalently macOS 10.14, watchOS 5 etc).
434+
e.g. if minimum_ios_deployment_target = '12', the converter would only utilize CoreML features released till iOS12 (equivalently macOS 10.14, watchOS 5 etc).
435435
436436
iOS 11.2 (CoreML 0.8) does not support resize_bilinear, crop_resize layers
437437
- (Supported features: https://github.com/apple/coremltools/releases/tag/v0.8)
@@ -453,13 +453,13 @@ def convert(model, # type: Union[onnx.ModelProto, Text]
453453
"Model must be file path to .onnx file or onnx loaded model"
454454
)
455455

456-
if not SupportedVersion.ios_support_check(target_ios):
457-
raise TypeError('{} not supported. Please provide one of target iOS: {}', target_ios, SupportedVersion.get_supported_ios())
456+
if not SupportedVersion.ios_support_check(minimum_ios_deployment_target):
457+
raise TypeError('{} not supported. Please provide one of target iOS: {}', minimum_ios_deployment_target, SupportedVersion.get_supported_ios())
458458

459459

460460
global USE_SHAPE_MAPPING
461461
disable_coreml_rank5_mapping = False
462-
if SupportedVersion.is_nd_array_supported(target_ios):
462+
if SupportedVersion.is_nd_array_supported(minimum_ios_deployment_target):
463463
disable_coreml_rank5_mapping = True
464464

465465
if disable_coreml_rank5_mapping:
@@ -551,7 +551,7 @@ def __call__(self, graph):
551551
builder = NeuralNetworkBuilder(input_features, output_features, mode=mode, disable_rank5_shape_mapping=disable_coreml_rank5_mapping)
552552

553553
# TODO: To be removed once, auto-downgrading of spec version is enabled
554-
builder.spec.specificationVersion = SupportedVersion.get_specification_version(target_ios)
554+
builder.spec.specificationVersion = SupportedVersion.get_specification_version(minimum_ios_deployment_target)
555555

556556
'''
557557
Set CoreML input,output types (float, double, int) same as onnx types, if supported
@@ -715,7 +715,7 @@ def _add_informative_description(feature, raise_error=True):
715715
builder.add_optionals(graph.optional_inputs, graph.optional_outputs)
716716

717717
# Check for specification version and target ios compatibility
718-
if target_ios == '11.2' and builder.spec.WhichOneof('Type') == 'neuralNetwork':
718+
if minimum_ios_deployment_target == '11.2' and builder.spec.WhichOneof('Type') == 'neuralNetwork':
719719
nn_spec = builder.spec.neuralNetwork
720720
for layer in nn_spec.layers:
721721
if layer.WhichOneof('layer') == 'resizeBilinear' or layer.WhichOneof('layer') == 'cropResize':

tests/_test_utils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ def _shape_from_onnx_value_info(v): # type: (ValueInfoProto) -> Sequence[Tuple[
120120
def _coreml_forward_model(model, # type: ModelProto
121121
input_dict, # type: Dict[Text, np._ArrayLike[Any]]
122122
output_names, # type: Sequence[Text]
123-
target_ios='12'
123+
minimum_ios_deployment_target='12'
124124
):
125125
# type: (...) -> np.ndarray[Any]
126-
if not SupportedVersion.is_nd_array_supported(target_ios):
126+
if not SupportedVersion.is_nd_array_supported(minimum_ios_deployment_target):
127127
for k, arr in input_dict.items():
128128
if len(arr.shape) == 4:
129129
input_dict[k] = arr[0]
@@ -137,12 +137,12 @@ def _coreml_forward_model(model, # type: ModelProto
137137
def _coreml_forward_onnx_model(model, # type: ModelProto
138138
input_dict, # type: Dict[Text, np._ArrayLike[Any]]
139139
onnx_coreml_input_shape_map = {}, # type: Dict[Text, List[int,...]]
140-
target_ios='12'
140+
minimum_ios_deployment_target='12'
141141
):
142142
# type: (...) -> np.ndarray[Any]
143-
coreml_model = convert(model, onnx_coreml_input_shape_map=onnx_coreml_input_shape_map, target_ios=target_ios)
143+
coreml_model = convert(model, onnx_coreml_input_shape_map=onnx_coreml_input_shape_map, minimum_ios_deployment_target=minimum_ios_deployment_target)
144144
output_names = [o.name for o in model.graph.output]
145-
return _coreml_forward_model(coreml_model, input_dict, output_names, target_ios=target_ios)
145+
return _coreml_forward_model(coreml_model, input_dict, output_names, minimum_ios_deployment_target=minimum_ios_deployment_target)
146146

147147

148148
def _random_array(shape, random_seed=10): # type: (Tuple[int, ...], Any) -> np._ArrayLike[float]
@@ -225,7 +225,7 @@ def _test_onnx_model(model, # type: ModelProto
225225
decimal=5, # type: int
226226
onnx_coreml_input_shape_map = {}, # type: Dict[Text, List[int,...]]
227227
coreml_input_shape = {}, # type: Dict[Text, List[int,...]]
228-
target_ios='12'
228+
minimum_ios_deployment_target='12'
229229
):
230230
# type: (...) -> None
231231
if not test_name:
@@ -238,12 +238,12 @@ def _test_onnx_model(model, # type: ModelProto
238238
supported_ios_version = ['11.2', '12', '13']
239239
IOS_13_VERSION = supported_ios_version.index('13')
240240
for key, value in W.items():
241-
if supported_ios_version.index(target_ios) < IOS_13_VERSION and key in coreml_input_shape:
241+
if supported_ios_version.index(minimum_ios_deployment_target) < IOS_13_VERSION and key in coreml_input_shape:
242242
coreml_input_dict[key] = np.reshape(value, coreml_input_shape[key])
243243
else:
244244
coreml_input_dict[key] = value
245245
coreml_outputs = _coreml_forward_onnx_model(model, coreml_input_dict, onnx_coreml_input_shape_map=onnx_coreml_input_shape_map,
246-
target_ios=target_ios)
246+
minimum_ios_deployment_target=minimum_ios_deployment_target)
247247
_assert_outputs(c2_outputs, coreml_outputs, decimal=decimal)
248248

249249

@@ -255,7 +255,7 @@ def _test_single_node(op_type, # type: Text
255255
test_name = '', # type: Text
256256
onnx_coreml_input_shape_map = {}, # type: Dict[Text, List[int,...]]
257257
coreml_input_shape = {}, # type: Dict[Text, List[int,...]]
258-
target_ios='12',
258+
minimum_ios_deployment_target='12',
259259
**kwargs # type: Any
260260
):
261261
# type: (...) -> None
@@ -267,4 +267,4 @@ def _test_single_node(op_type, # type: Text
267267
_test_onnx_model(model, test_name=test_name, decimal=decimal,
268268
onnx_coreml_input_shape_map=onnx_coreml_input_shape_map,
269269
coreml_input_shape = coreml_input_shape,
270-
target_ios=target_ios)
270+
minimum_ios_deployment_target=minimum_ios_deployment_target)

tests/onnx_backend_models_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
from coremltools.models.utils import macos_version
1616

1717
# Default target iOS
18-
TARGET_IOS = '13'
18+
MINIMUM_IOS_DEPLOYMENT_TARGET = '13'
1919

2020
MIN_MACOS_VERSION_10_15 = (10, 15)
2121
# If MACOS version is less than 10.15
2222
# Then force testing on CoreML 2.0
2323
if macos_version() < MIN_MACOS_VERSION_10_15:
24-
TARGET_IOS = '12'
24+
MINIMUM_IOS_DEPLOYMENT_TARGET = '12'
2525

26-
if not SupportedVersion.ios_support_check(TARGET_IOS):
26+
if not SupportedVersion.ios_support_check(MINIMUM_IOS_DEPLOYMENT_TARGET):
2727
raise ValueError(
2828
"Invalid Target iOS version provided. Valid target iOS: {}".format(supported_ios_version)
2929
)
3030

3131
# import all test cases at global scope to make them visible to python.unittest
32-
backend_test = onnx.backend.test.BackendTest(CoreMLBackendND if SupportedVersion.is_nd_array_supported(TARGET_IOS) else CoreMLBackend, __name__)
32+
backend_test = onnx.backend.test.BackendTest(CoreMLBackendND if SupportedVersion.is_nd_array_supported(MINIMUM_IOS_DEPLOYMENT_TARGET) else CoreMLBackend, __name__)
3333

3434
# Only include the big models tests
3535
backend_test.include('test_resnet50')

0 commit comments

Comments
 (0)