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 theMLPredictionOptions
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
Expressions Are Not Allowed at The Top Level
How to Detect Day Change in Swift
Uitableview Didselectrow Returns Wrong Row Index Value
How to Make UItabbar Image Rounded in Swift Programmatically
User Already Authenticated in App and Firebase Realtime Database Returns Failed: Permission_Denied
Uiview.Animatewithduration Not Animating
Anchor Constraints Not Honored in Xcode 10/iOS 12
Swift - Setting Calayer Bounds or Frame Not Working
How to Delay a for Loop in Swift Without Interrupting The Main Thread
Querying Geohashes in Firestore Returns Nothing
Custom Markers Disappear on Zoomin The Map and Appear on Zoomout The Map with Clustering
Spritekit and Swiftui, Change Scene a Better Way
How to List All Available Patterns in a Scdynamicstorage
Bar Button Item Tint Color Not Working
Remove Add Calendar Button from Calendar Chooser