Search code examples
swiftcoremlcreateml

CoreML - On device model training


I'm currently creating a hello world, in order to get an overview about the great features of CoreML and CreateML. My goal is to use Apples data table example in my hello world project, in order to predict the genre of lyric using given parameter like author, number of pages and the title:

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"]
]

I was able to create a mlmodel using CreateML in a playground with these additional lines of code:

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)

This way you can provide input like a title, author as well as the number of pages and the model will predict the genre as output using these lines of code:

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)

Now I'd like to do this model creation / training on a device, in order to create a new or update the existing model each time the user adds new data in the app. I pasted the code into my app but unfortunately the needed CreateML framework is not available on device.

I read about tabular classification and on device training as well as tried the emoji intelligence example but I wasn't able to create my own on device tabular classification unfortunately. But through the information I was able to gather from these articles, it looks like it should be possible because the data set is very small and the calculations needed are very basic and fast. Existing examples like the emoji intelligence, TouchID or the Photos App are showing that it must be possible meanwhile.

I'd be very happy if anyone had examples or hints how to do the on device training for any example (also image classification, etc.), I'm not focused on tabular data only.

Thank you in advance!


Solution

  • Currently your options are:

    • use Metal Performance Shaders to train on the device
    • write your own training code

    Neither is compatible with Core ML (although if you really wanted to, you could write your own mlmodel file and then compile it on the device).