Search code examples
swiftios11coreml

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


I'm running an mlmodel that is coming from keras on an iPhone 6. The predictions often fails with the error Error computing NN outputs. Does anyone know what could be the cause and if there is anything I can do about it?

do {
    return try model.prediction(input1: input)
} catch let err {
    fatalError(err.localizedDescription) // Error computing NN outputs error
}

Model inputs and outputs

EDIT: I tried apple's sample project and that one works in the background so it seems it's specific to either our project or model type.


Solution

  • 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