Structure

MLObjectDetector

A model you train to detect objects within an image.

Declaration

struct MLObjectDetector

Overview

Use an object detector to train a machine learning model that you can include in your app to identify specific types of objects within images.

When you create an object detection model, you train it with images and annotations for each image. Each annotation contains a label and a region for an object within the image. For example, you can train an object detector with images of tables and annotations for specific objects such as bananas, croissants, and cups of coffee.

After training completes, you evaluate the trained model by showing the model a testing set of images with annotations that the model hasn’t seen before. The metrics that come from this evaluation, such as averagePrecision and meanAveragePrecision, tell you whether the model performs well enough. When the model makes too many mistakes, you can add more or better training data, or change the parameters, and try again.

When your model does perform well enough, you save it as a Core ML model file with the mlmodel extension. You can then import this model file into an app that uses a Core ML model file to detect objects in images.

Topics

Creating and Training an Object Detector

enum MLObjectDetector.Origin

The location within an annotation’s bounding box that corresponds to the annotation’s coordinates.

Deprecated
enum MLObjectDetector.DataSource

A data source for an object detector.

struct MLObjectDetector.ModelParameters

Parameters that affect the process of training an object detection model.

let modelParameters: MLObjectDetector.ModelParameters

The configuration parameters that the object detector used for training during initialization.

Assessing Model Accuracy

var trainingMetrics: MLObjectDetectorMetrics

Measurements of the object detector’s performance on the training data set.

var validationMetrics: MLObjectDetectorMetrics

Measurements of the object detector’s performance on the validation data set.

struct MLObjectDetectorMetrics

Metrics used to evaluate an object detector’s performance.

Evaluating an Object Detector

func evaluation(on: MLObjectDetector.DataSource) -> MLObjectDetectorMetrics

Generates metrics describing the object detector’s performance on annotated data provided in a data source.

func evaluation(on: MLDataTable, imageColumn: String, annotationColumn: String) -> MLObjectDetectorMetrics

Generates metrics describing the object detector’s performance on annotated data provided in a data table.

Testing an Object Detector

func prediction(from: URL) -> MLObjectDetector.DetectedObjects

Generates annotations for the detected objects in an image at the given URL.

func predictions(from: [URL]) -> [MLObjectDetector.DetectedObjects]

Generates annotations for the detected objects in each image in the given array of URLs.

typealias MLObjectDetector.DetectedObjects

An array of annotations for objects that the model has detected.

struct MLObjectDetector.ObjectAnnotation

The label, location, and confidence level of a detected object within an image.

Saving an Object Detector

func write(to: URL, metadata: MLModelMetadata?)

Exports the object detector as a Core ML model file at the given URL.

func write(toFile: String, metadata: MLModelMetadata?)

Exports the object detector as a Core ML model file at the given file path.

Describing an Object Detector

var model: MLModel

The underlying Core ML model of the object detector stored in memory.

var description: String

A text representation of the object detector.

var debugDescription: String

A text representation of the object detector that’s suitable for output during debugging.

var playgroundDescription: Any

A description of the object detector shown in a playground.

See Also

Computer Vision

Creating an Image Classifier Model

Train a machine learning model to classify images, and add it to your Core ML app.

struct MLImageClassifier

A model you train to classify images.