Core ML Model Conversion: what it is, stages and methods of conversion

Author: IT Sectr Published: 2026-07-17 Reading time: 6 min

Core ML Model Conversion is the process of transforming trained machine learning models from popular frameworks into the Core ML (.mlmodel) format, optimized for execution on Apple devices. Conversion is necessary because PyTorch, TensorFlow, and other frameworks use their own formats incompatible with Core ML Runtime. According to the coremltools documentation, 2025, the library supports conversion from PyTorch, TensorFlow 1.x and 2.x, Keras, ONNX, scikit-learn, and libsvm. coremltools automatically replaces unsupported operations with equivalent ones, preserving the model's numerical accuracy.

Key Takeaways

  • Core ML Model Conversion — transformation of ML models into .mlmodel format for execution on Apple devices.
  • The main tool is coremltools, an open-source Python library from Apple.
  • Supports PyTorch, TensorFlow, Keras, ONNX and scikit-learn.
  • The process includes graph tracing, operation replacement, and accuracy verification.
  • After conversion, the model can be quantized to FP16 or INT8 for acceleration.

What is Core ML Model Conversion

Core ML Model Conversion is the process of transforming a trained machine learning model from the source framework format into the .mlmodel format understood by Core ML Runtime on Apple devices. Without conversion, a model trained in PyTorch or TensorFlow cannot be loaded and executed on iOS or macOS directly.

The conversion process includes computation graph translation: each operator from the source framework (Conv2D, BatchNorm, ReLU) is mapped to the corresponding Core ML operator. If no direct replacement exists, coremltools uses composite operations or custom layers. According to Apple ML Research, the library covers over 200 operators from various frameworks.

After conversion, the model is saved in the .mlmodel bundle format, which includes a protobuf graph description, weights in binary form, and metadata. This file is then compiled into mlmodelc for execution on the target device.

Supported Frameworks and Formats

coremltools version 7.x supports conversion from six sources. PyTorch — via torch.jit.trace or torch.export, TensorFlow 2.x — via SavedModel and Keras H5, TensorFlow 1.x — via frozen graph .pb. For ONNX, an intermediate representation is used, which is then translated into Core ML.

Framework Compatibility Matrix

FrameworkInput Formatcoremltools API
PyTorchTorchScript, torch.exportCTConverter / convert()
TensorFlow 2.xSavedModel, Keras H5convert()
TensorFlow 1.xFrozen .pbconvert()
ONNX.onnxonnx_to_coreml()
scikit-learn.pkl / Pipelineconverters.sklearn.convert()
Keras.h5 / .kerasconvert()

Conversion Tools: coremltools

coremltools is the official open-source Python library from Apple, available via pip install coremltools. The library provides a unified API for conversion from all supported frameworks, as well as post-processing tools: quantization, accuracy checking, and graph visualization.

Installation and basic model conversion from PyTorch:

python
import coremltools as ct
import torch
import torchvision

model = torchvision.models.resnet18(pretrained=True)
model.eval()

example_input = torch.rand(1, 3, 224, 224)
traced_model = torch.jit.trace(model, example_input)

mlmodel = ct.convert(
    traced_model,
    source="pytorch",
    inputs=[ct.ImageType(shape=example_input.shape)]
)
mlmodel.save("ResNet18.mlmodel")

For conversion from TensorFlow, use SavedModel as the source. coremltools automatically determines input and output tensors based on signature_def:

python
import coremltools as ct

mlmodel = ct.convert(
    "saved_model_dir",
    source="tensorflow",
    @minimum_deployment_target=ct.target.iOS16
)
mlmodel.save("MyTFModel.mlmodel")

Model Conversion Stages

The conversion process consists of four stages. In the first stage, coremltools loads the source model and performs tracing or graph scanning. For PyTorch, torch.jit.trace is used, which runs a sample input through the model and records the sequence of operations.

In the second stage, operator mapping is performed. Each operator from the source graph is mapped to a Core ML operator. If no direct replacement exists, coremltools splits the operator into a sequence of supported ones. According to the coremltools documentation, PyTorch operator coverage exceeds 95% for typical architectures.

The third stage is graph optimization. coremltools performs operation fusion (e.g., conv + batch norm), removal of unnecessary transformations, and operator reordering to improve efficiency. The fourth stage is serialization into the .mlmodel format with weights and metadata preservation.

Common Problems and Solutions

The most frequent conversion issue is unsupported operations. If the model contains an operator missing in Core ML, coremltools reports an error with the operation name. The solution is to replace the operator with an equivalent combination of supported ones or implement a custom layer through the custom layer API.

The second issue is dimension mismatch. PyTorch uses the NCHW format, while Core ML uses NHWC by default. coremltools automatically inserts transposition, but sometimes the axis order is determined incorrectly. Check the input and output dimensions in the conversion logs and, if necessary, specify input_features with correct names.

The third issue is accuracy loss after quantization. When converting with FP16 or INT8 palette, model accuracy may decrease. coremltools provides the ct.models.CompiledModel utility for comparing outputs of the source and converted model on the same input data. If the discrepancy exceeds 1%, use quantization with FP16 palette without calibration or skip quantization altogether.

Conversion Examples from PyTorch and TensorFlow

An example of converting the MobileNetV3 model from PyTorch with input type specification and minimum iOS version. Use ct.ImageType for automatic image normalization:

python
import coremltools as ct
import torchvision

model = torchvision.models.mobilenet_v3_small(
    pretrained=True
)
model.eval()
example = torch.rand(1, 3, 224, 224)
traced = torch.jit.trace(model, example)

mlmodel = ct.convert(
    traced,
    source="pytorch",
    inputs=[ct.ImageType(
        shape=example.shape,
        scale=1.0/255.0,
        bias=[0, 0, 0]
    )],
    @minimum_deployment_target=ct.target.iOS16
)

# Save to .mlmodel for later compilation in Xcode
mlmodel.save("MobileNetV3.mlmodel")

An example of conversion from TensorFlow Keras with FP16 quantization. Specify minimum_deployment_target to enable FP16 support on devices with Apple A13 and newer:

python
import coremltools as ct
from tensorflow import keras

keras_model = keras.applications.EfficientNetB0(
    weights="imagenet"
)

mlmodel = ct.convert(
    keras_model,
    source="tensorflow",
    @minimum_deployment_target=ct.target.iOS17
)

# Quantize weights to FP16 for 2x size reduction
mlmodel_fp16 = ct.models.neural_network.quantization_utils.quantize_weights(
    mlmodel, 16
)
mlmodel_fp16.save("EfficientNetB0_fp16.mlmodel")

Frequently Asked Questions

Can I convert a model without access to the source code?

Yes, if the model is saved in TorchScript, SavedModel, or ONNX format. coremltools loads these formats without needing the source code and performs conversion based on the computation graph.

How do I know which operations are not supported in Core ML?

coremltools outputs the list of unsupported operations in the log during conversion. Use ct.utils.get_coreml_operations() to get the full list of available Core ML operators.

What is a quantization palette in coremltools?

A quantization palette is a set of parameters for compressing model weights: fp16, int8, or palettization. coremltools supports 8-bit, 16-bit palette, and LUT quantization with different bit depths.

Is compilation required after conversion?

Yes, .mlmodel must be compiled into mlmodelc before execution on the device. Compilation is performed automatically in Xcode during build or on the device via MLModel.compile(at:).

How do I verify the accuracy of the converted model?

Use ct.models.CompiledModel to compare the outputs of the source and converted model. Provide the same input data and compare the results using MSE or cosine similarity metrics.

Summary

  • Core ML Model Conversion is the process of transforming ML models into .mlmodel format for execution on Apple devices.
  • The main tool is coremltools, supporting PyTorch, TensorFlow, Keras, ONNX, and scikit-learn.
  • Conversion includes graph tracing, operator mapping, optimization, and serialization.
  • Unsupported operations are replaced with equivalent combinations or implemented via custom layer API.
  • After conversion, FP16 and INT8 quantization is available for size reduction and inference acceleration.
  • Before deployment, verify model accuracy through ct.models.CompiledModel.
  • Plan the conversion pipeline as part of CI/CD for automatic model updates in the application.

We will develop a mobile application turnkey

IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.

Discuss the project

Read also