CoreML-设备上模型培训

时间:2018-08-04 14:43:46

标签: swift coreml createml

我目前正在创建一个hello世界,以概述CoreML和CreateML的强大功能。我的目标是在我的hello world项目中使用Apples data table example,以便使用给定的参数(例如作者,页数和标题)来预测歌词的类型:

let data: [String: MLDataValueConvertible] = [
    "title": ["Alice in Wonderland", "Hamlet", "Treasure Island", "Peter Pan"],
    "author": ["Lewis Carroll", "William Shakespeare", "Robert L. Stevenson", "J. M. Barrie"],
    "pageCount": [124, 98, 280, 94],
    "genre": ["Fantasy", "Drama", "Adventure", "Fantasy"]
]

我能够在Playground中使用CreateML使用以下附加代码行创建mlmodel:

let bookTable = try MLDataTable(dictionary: data)
let genreRegressor = try MLRegressor(trainingData: bookTable, targetColumn: "genre")
let meta = MLModelMetadata(author: "John Doe", shortDescription: "A model used to determine the genre of a book.", version: "1.0")
try genreRegressor.write(to: URL(fileURLWithPath: "/Path/MyModel.mlmodel"), metadata: meta)

通过这种方式,您可以提供诸如标题,作者以及页面数之类的输入,并且模型将使用以下这些代码行来预测流派作为输出:

let model = MyModel().model

// Create the input
let modelInput = MyModelInput(author: "Mark Twain", title: "Tom Sawyer", pageCount: 245)

// Predict the genre
let modelOutput = try? model.prediction(from: modelInput)
let genre = modelOutput?.featureValue(for: "genre")
print(genre)

现在,我想在设备上进行此模型创建/培训,以便每次用户在应用程序中添加新数据时创建新模型或更新现有模型。我将代码粘贴到我的应用程序中,但是很遗憾,所需的CreateML框架在设备上不可用。

我阅读了tabular classificationon device training并尝试了emoji intelligence示例,但不幸的是,我无法在设备表格上创建自己的分类。但是通过我从这些文章中收集到的信息,似乎应该有可能,因为数据集非常小,所需的计算非常基础且快速。现有的示例(例如表情符号智能,TouchID或“照片”应用程序)显示必须meanwhile

如果有人有示例或提示如何对任何示例(以及图像分类等)进行设备上的培训,我将非常高兴,我不仅专注于表格数据。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

当前您的选择是:

  • 使用Metal Performance Shaders在设备上进行训练
  • 编写您自己的培训代码

两者都不与Core ML兼容(尽管如果您确实愿意,您可以编写自己的mlmodel文件,然后在设备上进行编译)。