Skip to main content

Compile a CoreML model locally (coreml.compile_model)

Declaration

compiled_path, err = coreml.compile_model(model_path)

Parameters

  • model_path
    String. Path of the model to compile. iOS 15+ supports .mlpackage models; iOS 13 and iOS 14 support legacy .mlmodel only.

    YOLO11 to CoreML documentation

    macOS command examples for converting YOLO11 formats
    # Assume you already have Python 3.10. If not, create one. (Only tested on Python 3.10)
    # Install ultralytics if not installed
    pip install ultralytics

    # For iOS 13 or iOS 14, export legacy .mlmodel by setting format=mlmodel
    yolo export format=mlmodel nms=True model=best.pt

    # For iOS 15+, export .mlpackage by setting format=coreml
    yolo export format=coreml nms=True model=best.pt

    # nms=True is required

Returns

  • compiled_path
    String | nil. On success, the compiled model directory path; nil on failure.
  • err
    String | nil. nil on success; error message on failure.

Notes

  • Available since app version 1.3.8+.

  • Not supported on iOS versions below 13.

  • Compiles a .mlpackage or .mlmodel into a device-loadable .mlmodelc bundle.

  • .mlmodelc compiled on different devices or iOS versions may not be interchangeable.

    YOLO11 training commands on an NVIDIA (CUDA) PC
    # Install Visual Studio C++ Desktop Development workload (if not already)
    # https://visualstudio.microsoft.com/
    # Install CUDA 11.8 Toolkit (if not already)
    # https://developer.nvidia.com/cuda-11-8-0-download-archive

    # Assume Python 3.10 is available
    pip install ultralytics

    # Reinstall torch libraries 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=0
    YOLO11 training commands on macOS
    # Assume Python 3.10 is available
    pip install ultralytics

    # CPU/Metal torch
    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

local noexecute = require('no_os_execute')
noexecute.rm_rf(compiled_model_path) -- During testing, always recompile; comment out for stable models

if not file.exists(compiled_model_path) then -- Compile if not compiled yet
local tmp_path, err = coreml.compile_model(XXT_HOME_PATH..'/models/yolo11m.mlpackage')
if not tmp_path then
error(err)
end
local noexecute = require('no_os_execute')
noexecute.cp_r(tmp_path, compiled_model_path)
noexecute.rm_rf(tmp_path)
end