Skip to content

Reference for ultralytics/nn/backends/coreml.py

Improvements

This page is sourced from https://github.com/ultralytics/ultralytics/blob/main/ultralytics/nn/backends/coreml.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏


class ultralytics.nn.backends.coreml.CoreMLBackend

CoreMLBackend()

Bases: BaseBackend

CoreML inference backend for Apple hardware.

Loads and runs inference with CoreML models (.mlpackage files) using the coremltools library. Supports both static and dynamic input shapes and handles NMS-included model outputs.

Methods

NameDescription
forwardRun CoreML inference with automatic input format handling.
load_modelLoad a CoreML model from a .mlpackage file.
Source code in ultralytics/nn/backends/coreml.pyView on GitHub
class CoreMLBackend(BaseBackend):


method ultralytics.nn.backends.coreml.CoreMLBackend.forward

def forward(self, im: torch.Tensor) -> np.ndarray | list[np.ndarray]

Run CoreML inference with automatic input format handling.

Args

NameTypeDescriptionDefault
imtorch.TensorInput image tensor in BHWC format (converted from BCHW by AutoBackend).required

Returns

TypeDescription
np.ndarray | list[np.ndarray]Model predictions as numpy array(s).
Source code in ultralytics/nn/backends/coreml.pyView on GitHub
def forward(self, im: torch.Tensor) -> np.ndarray | list[np.ndarray]:
    """Run CoreML inference with automatic input format handling.

    Args:
        im (torch.Tensor): Input image tensor in BHWC format (converted from BCHW by AutoBackend).

    Returns:
        (np.ndarray | list[np.ndarray]): Model predictions as numpy array(s).
    """
    im = im.cpu().numpy()
    h, w = im.shape[1:3]

    im = im.transpose(0, 3, 1, 2) if self.dynamic else Image.fromarray((im[0] * 255).astype("uint8"))
    y = self.model.predict({"image": im})
    if "confidence" in y:  # NMS included
        from ultralytics.utils.ops import xywh2xyxy

        box = xywh2xyxy(y["coordinates"] * [[w, h, w, h]])
        cls = y["confidence"].argmax(1, keepdims=True)
        y = np.concatenate((box, np.take_along_axis(y["confidence"], cls, axis=1), cls), 1)[None]
    else:
        y = list(y.values())
    if len(y) == 2 and len(y[1].shape) != 4:  # segmentation model
        y = list(reversed(y))
    return y


method ultralytics.nn.backends.coreml.CoreMLBackend.load_model

def load_model(self, weight: str | Path) -> None

Load a CoreML model from a .mlpackage file.

Args

NameTypeDescriptionDefault
weightstr | PathPath to the .mlpackage model file.required
Source code in ultralytics/nn/backends/coreml.pyView on GitHub
def load_model(self, weight: str | Path) -> None:
    """Load a CoreML model from a .mlpackage file.

    Args:
        weight (str | Path): Path to the .mlpackage model file.
    """
    check_requirements(["coremltools>=9.0", "numpy>=1.14.5,<=2.3.5"])
    import coremltools as ct

    LOGGER.info(f"Loading {weight} for CoreML inference...")
    self.model = ct.models.MLModel(weight)
    self.dynamic = self.model.get_spec().description.input[0].type.HasField("multiArrayType")

    # Load metadata
    self.apply_metadata(dict(self.model.user_defined_metadata))





📅 Created 0 days ago ✏️ Updated 0 days ago
Laughing-q