Compile a CoreML model locally (coreml.compile_model)
Declaration
compiled_path, err = coreml.compile_model(model_path)
Parameters
-
model_path
String. Path to the model to compile. iOS 15 and later support.mlpackage; iOS 12, 13, and 14 support legacy.mlmodelonly.YOLO11 to CoreML documentation
macOS command examples for converting YOLO11 formats
# Assume you already have Python 3.10. If not, create one (tested with Python 3.10 only; higher versions have not been verified)
# Install ultralytics if needed
pip install ultralytics
# On iOS 13 or iOS 14, export the legacy .mlmodel format
yolo export format=mlmodel nms=True model=best.pt
# On iOS 15+, export the .mlpackage format
yolo export format=coreml nms=True model=best.pt
# nms=True is optional
# Enable it if you want NMS baked into the exported model
# Skip it if you plan to use coreml.nms / coreml.rotated_nms in XXTouch
Returns
-
compiled_path
String | nil. On success, returns the compiled model directory path; on failure, returnsnil. -
err
String | nil. On success, returnsnil; on failure, returns the error message.
Notes
-
Available since app version 1.3.8+.
-
Not supported on iOS versions below 12.
-
This function compiles a
.mlpackageor.mlmodelinto a device-loadable.mlmodelcbundle. -
A compiled
.mlmodelcbundle is not guaranteed to be portable across different devices or iOS versions.YOLO11 training commands on an NVIDIA GPU PC
# Install the Visual Studio C++ Desktop Development workload if needed
# https://visualstudio.microsoft.com/
# Install the CUDA 11.8 Toolkit if needed
# https://developer.nvidia.com/cuda-11-8-0-download-archive
# Assume Python 3.10 is available (tested with Python 3.10 only; higher versions have not been verified)
pip install ultralytics
# Reinstall torch builds compatible with CUDA 11.8
pip uninstall torch torchvision torchaudio
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118
yolo train model=yolo11m.pt data=COCO.yml epochs=300 imgsz=640 device=0YOLO11 training commands on macOS
# Assume Python 3.10 is available (tested with Python 3.10 only; higher versions have not been verified)
pip install ultralytics
# Install CPU/Metal torch builds
pip install torch torchvision torchaudio
yolo train model=yolo11m.pt data=COCO.yml epochs=300 imgsz=640 device=mps
Example
compiled_model_path = XXT_HOME_PATH..'/models/yolo11m.mlmodelc' -- Where to store the compiled model
file.remove(compiled_model_path) -- Always recompile during testing; comment this out once the model is stable
if not file.exists(compiled_model_path) then -- Compile if it does not exist yet
local tmp_path, err = coreml.compile_model(XXT_HOME_PATH..'/models/yolo11m.mlpackage')
if not tmp_path then
error(err)
end
file.move(tmp_path, compiled_model_path, 'mo')
end