Integrating a Core ML Model into Your App

Sample Code

Integrating a Core ML Model into Your App

Add a simple model to an app, pass input data to the model, and process the model’s predictions.

Download

 

Overview

This sample app uses a trained model, MarsHabitatPricer.mlmodel, to predict habitat prices on Mars.

Add a Model to Your Xcode Project

Add the model to your Xcode project by dragging the model into the project navigator.

You can see information about the model—including the model type and its expected inputs and outputs—by opening the model in Xcode. In this sample, the inputs are the number of solar panels and greenhouses, as well as the lot size of the habitat (in acres). The output is the predicted price of the habitat.

Create the Model in Code

Xcode also uses information about the model’s inputs and outputs to automatically generate a custom programmatic interface to the model, which you use to interact with the model in your code. For MarsHabitatPricer.mlmodel, Xcode generates interfaces to represent the model (MarsHabitatPricer), the model’s inputs (MarsHabitatPricerInput), and the model’s output (MarsHabitatPricerOutput).

Use the generated MarsHabitatPricer class’s initializer to create the model:

let model = MarsHabitatPricer()

Get Input Values to Pass to the Model

This sample app uses a UIPickerView to get the model’s input values from the user:

func selectedRow(for feature: Feature) -> Int {
    return pickerView.selectedRow(inComponent: feature.rawValue)
}

let solarPanels = pickerDataSource.value(for: selectedRow(for: .solarPanels), feature: .solarPanels)
let greenhouses = pickerDataSource.value(for: selectedRow(for: .greenhouses), feature: .greenhouses)
let size = pickerDataSource.value(for: selectedRow(for: .size), feature: .size)

Use the Model to Make Predictions

The MarsHabitatPricer class has a generated prediction(solarPanels:greenhouses:size:) method that’s used to predict a price from the model’s input values—in this case, the number of solar panels, the number of greenhouses, and the size of the habitat (in acres). The result of this method is a MarsHabitatPricerOutput instance.

guard let marsHabitatPricerOutput = try? model.prediction(solarPanels: solarPanels, greenhouses: greenhouses, size: size) else {
    fatalError("Unexpected runtime error.")
}

Access the price property of marsHabitatPricerOutput to get a predicted price and display the result in the app’s UI.

let price = marsHabitatPricerOutput.price
priceLabel.text = priceFormatter.string(for: price)

Note

The generated prediction(solarPanels:greenhouses:size:) method can throw an error. The most common type of error you’ll encounter when working with Core ML occurs when the details of the input data don’t match the details the model is expecting—for example, an image in the wrong format.

Build and Run a Core ML App

Xcode compiles the Core ML model into a resource that’s been optimized to run on a device. This optimized representation of the model is included in your app bundle and is what’s used to make predictions while the app is running on a device.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值