Error When Running Coreml in The Background: Error Computing Nn Outputs Error

Error when running coreml in the background: Error computing NN outputs error

In the end it was enough for us to set the usesCPUOnly flag. Using the GPU in the background seems prohibited in iOS. Apple actually wrote about this in their documentation as well. To specify this flag we couldn't use the generated model class anymore but had to call the raw coreml classes instead. I can imagine this changing in a future version however. The snippet below is taken from the generated model class, but with the added MLPredictionOptions specified.

let options = MLPredictionOptions()
options.usesCPUOnly = true // Can't use GPU in the background

// Copied from from the generated model class
let input = model_input(input: mlMultiArray)
let output = try generatedModel.model.prediction(from: input, options: options)
let result = model_output(output: output.featureValue(for: "output")!.multiArrayValue!).output

Xamarin.iOS coreML Get Prediction null reference error in background mode

Couple of things:

You have very limited time to execute arbitrary code when the app is Backgrounded.

  • You can check BackgroundTimeRemaining to determine how much time you have left.
  • You have to perform your predictions within a BeginBackgroundTask action

The foregrounded app (including Springboard) has priority over the GPU.

  • The foregrounded app GPU processing will not be interrupted to perform your background task-based prediction.

  • You can request predictions to use the CPU (UsesCpuOnly) by supplying the MLPredictionOptions to your prediction call.

Your model should be restricted to the CPU if it might run in the background or if your app has other GPU intensive tasks.

re: https://developer.apple.com/documentation/coreml/mlpredictionoptions/2921288-usescpuonly?language=objc

Classifing user activity using CoreML in background mode

Finally I found a solution that works on background. I was getting this issue for

Insufficient Permission (to submit GPU work from background)

I configured MLModelConfiguration to cpuOnly to fix this issue.

    let activityModel: MyActivityClassifier = {
do {
let config = MLModelConfiguration()
config.computeUnits = .cpuOnly
return try MyActivityClassifier(configuration: config)
} catch {
print(error)
fatalError("Couldn't create MyActivityClassifier")
}
}()


Related Topics



Leave a reply



Submit