Turicreate: Updatable Activity Classifier

Created on 12 Jun 2019  路  13Comments  路  Source: apple/turicreate

Hi Guys,
I've converted my ActivityClassifier trained with TuriCreate to updatable CoreML model. But now I have a compilation error in Xcode.

coremlc: Error: compiler error: Encountered an error while compiling a neural network model: kernel not found 'softmax_loss_grad' on platform 0

Here is the source code of python script which I use for converting

`import coremltools

spec = coremltools.utils.load_spec('SleepClassifier.mlmodel')

spec.description.trainingInput.extend([spec.description.input[0]])
spec.description.trainingInput.extend([spec.description.output[-1]])

builder = coremltools.models.neural_network.NeuralNetworkBuilder(spec=spec)
builder.make_updatable(['dense_layer1'])
builder.set_categorical_cross_entropy_loss(name='lossLayer', input='activityProbability', target='trueActivityLabel')

from coremltools.models.neural_network import SgdParams
builder.set_sgd_optimizer(SgdParams(lr=0.01, batch=32))

builder.set_epochs(10)

from coremltools.models import MLModel
mlmodel_updatable = MLModel(builder.spec)
mlmodel_updatable.save('UpdatableSleepClassifier.mlmodel')
`

Core ML activity classifier bug p2

Most helpful comment

We are trying to reproduce it now.

All 13 comments

Any progress or workaround for this problem?

@cyrilivargarcia still waiting...

We are trying to reproduce it now.

@mecid we could repro the issue. While we are investigating the root-cause, could you please try compiling the model on-device using MLModel.compileModelAtURL method? For this you might have to tell Xcode not to compile the model (in build phases) and use the .mlmodel from Bundle. Or, download .mlmodel in the app and then compile. We will keep this thread updated.

@mecid FWIW - we built everything for the _"download .mlmodel in the app and then compile"_ piece for you already at Skafos.ai (https://docs.skafos.ai/). That might help you until the good folks at TC figure out this issue. :) We also work with Activity Classifier (https://medium.com/skafosai/activity-classification-for-watchos-part-1-542d44388c40) but since we just updated our platform our example has not been refactored -- otherwise we also could send example. Either way, happy to assist. :)

@akatti-crypto should I try to compile updatable model or model which TC created(not updatable)?

@mecid Try compile the updatable model on-device (from your example: "UpdatableSleepClassifier.mlmodel").

@akatti-crypto I can't, when I put updatable model into app bundle I have project compilation error:
coremlc: Error: compiler error: Encountered an error while compiling a neural network model: kernel not found 'softmax_loss_grad' on platform 0
Command CoreMLModelCompile failed with a nonzero exit code

@mecid Yes, so, here are some steps to work around the issue:

First you have to tell Xcode not to compile the .mlmodel but still package with Xcode app bundle. Here are the steps:

  1. Drag and drop your "UpdatableSleepClassifier.mlmodel" into your Xcode project.
  2. Click on the project (in left panel) and then on your target.
  3. Go to "Build Phases" and click on the + sign on top left in that screen.
  4. Click on "New Copy File Phase" -> new phase appears with name (Copy Files).
  5. Open the new phase and add your "UpdatableSleepClassifier.mlmodel" into that.
  6. Open up "Compile Sources" phase (2nd from top) and remove "UpdatableSleepClassifier.mlmodel".

Next, you'll have to compile this .mlmodel to create .mlmodelc and use that to create an instance of MLModel on the device. Here's some quick code snippet I hacked up just now that you could use to do that although, you should re-work this for production.

let updatableModelURL = URL(fileURLWithPath: Bundle.main.path(forResource: "UpdatableSleepClassifier", ofType: "mlmodel")!)
let tmpLocation = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("UpdatableSleepClassifier.mlmodel")

try? FileManager.default.copyItem(at: updatableModelURL, to: tmpLocation)

do {
    let compiledModelURL = try MLModel.compileModel(at: tmpLocation)
    let model = try MLModel(contentsOf: compiledModelURL)
} catch {
    print("Failed with \(error)")
    return
}

Once the issue in Xcode is fixed, there's no need to do any of this. Xcode does all of this for you. Let me know if you have any questions.

This worked for me 馃憤馃徏

Thank you!

@akatti-crypto thanks for your support! It works!

@mecid FWIW - we built everything for the _"download .mlmodel in the app and then compile"_ piece for you already at Skafos.ai (https://docs.skafos.ai/). That might help you until the good folks at TC figure out this issue. :) We also work with Activity Classifier (https://medium.com/skafosai/activity-classification-for-watchos-part-1-542d44388c40) but since we just updated our platform our example has not been refactored -- otherwise we also could send example. Either way, happy to assist. :)

There's nothing there. docs.skafos.ai goes to skafos.ai, which appears to be some marketing mumbo jumbo.

Hi @BeenHijacked. If you are looking for a way to deploy your .mlmodels and don鈥檛 want to drop in the code to download the updated file and compile, you should check out Fritz.ai. They also have a great slack channel with lots of folks that can help.

Was this page helpful?
0 / 5 - 0 ratings