Core ML Tools is an open-source Python package developed by Apple for converting, optimizing, and validating machine learning models into Apple's Core ML format. It supports models from popular frameworks like TensorFlow, PyTorch, scikit-learn, XGBoost, and LibSVM. The library is actively maintained, with frequent releases, and is currently at version 9.0, adding support for Python 3.13, PyTorch 2.7, and new iOS/macOS deployment targets.
pip install coremltoolsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to convert a pre-trained PyTorch MobileNetV2 model to the Core ML format using `coremltools`. It involves defining a PyTorch model, tracing it with a dummy input, and then using `ct.convert` to generate the `.mlpackage` file. Explicitly defining input types and preprocessing parameters is crucial for correct conversion.
Migrate your conversion workflows to use the `coremltools.convert` API. For PyTorch, convert directly without an intermediate ONNX step. For Keras/TensorFlow, use the TF2 backend with `ct.convert`.
Ensure your tools and Xcode versions are compatible with the `.mlpackage` format. The `mlmodel.save()` method will automatically save in the new format by default.
Ensure your `protobuf` package is up-to-date and compatible with `coremltools` 8.0+. Check `coremltools` release notes for specific version requirements of `protobuf`, `torch`, `numpy`, and `scikit-learn`.
Carefully review the training pipeline of your source model and ensure the `ct.ImageType` parameters (e.g., `scale`, `bias`, `red_bias`, `green_bias`, `blue_bias`, `is_bgr`, `channel_first`) exactly match the preprocessing used during training.
For optimal ANE performance, use `EnumeratedShapes` with a limited, predetermined set of input shapes when possible. If more flexibility is needed, use `RangeDim` but be aware of potential performance tradeoffs and ANE limitations.
For production and critical workloads, `torch.jit.trace` is recommended. Experiment with `torch.export` for newer models or specific features, but be prepared for potential issues or performance regressions, and report them on GitHub.
Install the package using pip: 'pip install coremltools'.
Ensure that the Keras version supports 'ParametricSoftPlus' or replace it with a supported activation function.
Reinstall 'coremltools' ensuring all dependencies are correctly installed: 'pip install --force-reinstall coremltools'.
Use the unified conversion API: 'import coremltools as ct; ct.convert(...)'.
Ensure the PyTorch model is in FP32 before conversion; the model will be converted to FP16 during the Core ML conversion process unless specified otherwise.