美文网首页
Core ML(基础) - 添加模型到项目

Core ML(基础) - 添加模型到项目

作者: _蓝星 | 来源:发表于2019-06-12 10:29 被阅读0次

一、官方demo下载地址

Download

二、模型详情

MarsHabitatPricer_mlmodel.png

通过这个图片可以知道这个模型的用途、被训练模型的代码以及模型输入输出项。
例如,在源代码中可以找到后边返回预测结果使用的方法:

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_)
    }

三、添加模型

  1. .mlmodel文件添加到Xcode工程当中;
  2. 在代码中创建Model;
let model = MarsHabitatPricer()
  1. 向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)
  1. 获取Model返回的预测数据。
guard let marsHabitatPricerOutput = try? model.prediction(solarPanels: solarPanels, greenhouses: greenhouses, size: size) else {
    fatalError("Unexpected runtime error.")
}
  1. 刷新UI
let price = marsHabitatPricerOutput.price
priceLabel.text = priceFormatter.string(for: price)

注:
生成的预测(solarpanel:greenhouses:size:)方法可能会抛出错误。在处理Core ML时,最常见的错误类型发生在输入数据的细节与模型期望的细节不匹配时——例如,格式错误的图像。

相关文章

网友评论

      本文标题:Core ML(基础) - 添加模型到项目

      本文链接:https://www.haomeiwen.com/subject/iqvqfctx.html