2

model.GetPrediction method returns a null object when the iOS app is running in background mode. The method return correct predictions when app is in foreground. Has anyone else come across this issue?

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
Srini111
  • 31
  • 4

1 Answers1

2

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

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • 1
    Thanks for the quick reply. Enabling the UsesCpuOnly option returns valid predictions in the background. – Srini111 Feb 27 '18 at 20:59
  • 1
    The `usesCPUOnly` flag was the solution for us as well. Posted sample code of how to do it here: https://stackoverflow.com/questions/48480610/error-computing-nn-outputs-error – Simon Bengtsson Mar 16 '18 at 13:30