Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code=9 "Could not create inference context" CoreML iOS

Tags:

ios

swift

coreml

I am new to CoreMl and following the instruction from Classifying Images with Vision and Core ML . I ran into an issue while using a model (MobileNetV2) give code from the previous link, I had no issues. When I tried to use my own model that I created using CoreML, I ran into the issue and the localized description was "Could not create inference context" when using the iPhone simulator.

- some : Error Domain=com.apple.vis Code=9 "Could not create inference context" UserInfo={NSLocalizedDescription=Could not create inference context}
like image 833
Qazi Ammar Avatar asked Jul 14 '26 01:07

Qazi Ammar


1 Answers

According to Apple documentation:

Core ML optimizes on-device performance by leveraging the CPU, GPU, and Neural Engine while minimizing its memory footprint and power consumption.

You see Could not create inference context error because Neural Engine is not supported in the simulator.

You can use CPU only for VNRequest in simulator.

#if targetEnvironment(simulator)
   request.usesCPUOnly = true
#endif

An update for iOS 17:

Since usesCPUOnly property is deprecated in iOS 17 as mentioned in comment, I tried to find another way. So I ended up with the following solution.

#if targetEnvironment(simulator)
    if #available(iOS 17.0, *) {
      let allDevices = MLComputeDevice.allComputeDevices

      for device in allDevices {
        if(device.description.contains("MLCPUComputeDevice")){
          requests[0].setComputeDevice(.some(device), for: .main)
          break
        }
      }

    } else {
      // Fallback on earlier versions
      requests[0].usesCPUOnly = true
    }
#endif
like image 159
Oktay Avatar answered Jul 15 '26 14:07

Oktay