When using the Core ML Tools Unified Conversion API, you can specify various properties for the model inputs and outputs using the
outputs parameters for
convert(). The following summarizes the key options.
convert() method generates by default a Core ML model with a multidimensional array (
MLMultiArray) as the type for both input and output. The data types, names, and shapes are picked up automatically from a TensorFlow source model. For a PyTorch model you must provide the input shape.
In Core ML Tools 7.0 and newer versions, the default input/output dtype for models converted to the
mlprogram type are float 16 for the
macOS13 and newer deployment targets. Also, the
convert() method produces an
mlprogram (ML program) by default with an
macOS12 or newer deployment target. For more details, see Convert Models to ML Programs.
For TensorFlow models, the shape is automatically picked up from the model. However, it is good practice to provide at least a static shape, which enables the converter to apply graph optimizations and produce a more efficient model. For variable input shapes use
EnumeratedShapes. For details and an example, see Select from Predetermined Shapes.
dtype parameter with
TensorType to override data types (such as float 32, float 16, and integer). The
dtype parameter can take either a NumPy
dtype (such as
np.int32) or an MIL type with
TensorType (such as
Starting in coremltools version 6, you can use the
np.float16 type with ML programs, which can reduce the overhead of input and output type conversions for
float16 typed models (which is the default precision for ML programs). For more information, see ML Programs.
For example, the following code snippet converts the
source_model to a Core ML model with float 16 multiarray (
TensorType) input and output:
# to produce a model with float 16 input and output of type multiarray mlmodel = ct.convert( source_model, inputs=[ct.TensorType(shape=input.shape, dtype=np.float16)], outputs=[ct.TensorType(dtype=np.float16)], minimum_deployment_target=ct.target.iOS16, )
For PyTorch model conversion, use
TensorType to set the input and output names of the converted model. For example, the following code snippet will produce a Core ML model with
mlmodel = ct.convert( source_torch_model, inputs=[ct.TensorType(shape=input.shape, name="my_input_name")], outputs=[ct.TensorType(name="my_output_name")], minimum_deployment_target=ct.target.iOS16, )
For TensorFlow conversions, the names are picked up automatically from the TF graph. Unlike PyTorch models in which the inputs and outputs are ordered, with TensorFlow models you can’t provide your own names, because in the TF graph the input and output tensors are referred to by the names. After converting the model, you can change the names of the inputs and outputs using the
rename_feature() method. For an example, see Rename a Feature.
Updated 4 days ago