一、官方demo下载地址
二、模型详情

通过这个图片可以知道这个模型的用途、被训练模型的代码以及模型输入输出项。
例如,在源代码中可以找到后边返回预测结果使用的方法:
func prediction(solarPanels: Double, greenhouses: Double, size: Double) throws -> MarsHabitatPricerOutput {
let input_ = MarsHabitatPricerInput(solarPanels: solarPanels, greenhouses: greenhouses, size: size)
return try self.prediction(input: input_)
}
三、添加模型
- 将.mlmodel文件添加到Xcode工程当中;
- 在代码中创建Model;
let model = MarsHabitatPricer()
- 向Model中添加输入项数据;
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)
- 获取Model返回的预测数据。
guard let marsHabitatPricerOutput = try? model.prediction(solarPanels: solarPanels, greenhouses: greenhouses, size: size) else {
fatalError("Unexpected runtime error.")
}
- 刷新UI
let price = marsHabitatPricerOutput.price
priceLabel.text = priceFormatter.string(for: price)
注:
生成的预测(solarpanel:greenhouses:size:)方法可能会抛出错误。在处理Core ML时,最常见的错误类型发生在输入数据的细节与模型期望的细节不匹配时——例如,格式错误的图像。
网友评论