ONNX

coremltools.converters.onnx._converter._make_coreml_input_features(graph, onnx_coreml_input_shape_map, disable_coreml_rank5_mapping=False)

If “disable_coreml_rank5_mapping” is False, then:

ONNX shapes to CoreML static shapes mapping length==1: [C] length==2: [B,C] length==3: [C,H,W] or [Seq,B,C] length==4: [B,C,H,W]

If “disable_coreml_rank5_mapping” is True, then onnx shapes are mapped “as is” to CoreML.

coremltools.converters.onnx._converter._transform_coreml_dtypes(builder, inputs, outputs)

Make sure ONNX input/output data types are mapped to the equivalent CoreML types

coremltools.converters.onnx._converter.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={}, onnx_coreml_input_shape_map={}, minimum_ios_deployment_target='12')

Convert ONNX model to CoreML.

Parameters
model:

An ONNX model with parameters loaded in the ONNX package, or path to file with models.

mode: ‘classifier’, ‘regressor’ or None

Mode of the converted coreml model:

  • 'classifier': a NeuralNetworkClassifier spec will be constructed.

  • 'regressor': a NeuralNetworkRegressor spec will be constructed.

preprocessing_args:

The 'is_bgr', 'red_bias', 'green_bias', 'blue_bias', 'gray_bias', and 'image_scale' keys have the same meaning as the pre-processing arguments for NeuralNetworkBuilder.

deprocessing_args:

Same as 'preprocessing_args' but for de-processing.

class_labels:
  • 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:

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

Flag to turn on additional custom CoreML layers for unsupported ONNX ops or attributes within a supported op.

custom_conversion_functions: dict()
  • A dictionary with keys corresponding to the names/types of ONNX ops and values as functions taking an object of the coreml-tools class: 'NeuralNetworkBuilder', 'Graph' (see onnx-coreml/_graph.Graph), 'Node' (see onnx-coreml/_graph.Node), and 'ErrorHandling' (see onnx-coreml/_error_utils.ErrorHandling).

  • This custom conversion function gets full control and responsibility for converting a given ONNX op.

  • The function returns nothing and is responsible for adding an equivalent CoreML layer via 'NeuralNetworkBuilder'.

onnx_coreml_input_shape_map: dict() (Optional)
  • 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 CoreML.

  • Convention used for CoreML 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] (H,W) or [1,2] (B,C), and so on. This is ignored if minimum_ios_deployment_target is set to 13.

minimum_ios_deployment_target: str

Target Deployment iOS Version (default: '12'). Supported iOS version options: '11.2', '12', '13'. CoreML model produced by the converter will be compatible with the iOS version specified in this argument. For example, if minimum_ios_deployment_target = '12', the converter would utilize only CoreML features released up to version iOS12 (equivalent to macOS 10.14, watchOS 5, and so on). iOS 11.2 (CoreML 0.8) does not support resize_bilinear and crop_resize layers. See supported v0.8 features. iOS 12 (CoreML 2.0), see supported v2.0 features. iSO 13 (CoreML 3.0), see supported v3.0 features.

Returns
model: A coreml model.