我正在与Swift一起在iOS应用中实现CoreML。在前台时,我希望CoreML使用GPU来增强性能,但是在切换到后台时,CoreML应该使用CPU,因为在iOS上,GPU无法在后台运行。

但是,在后台运行期间,GPU抛出权限错误,表明没有在后台使用GPU的权限。但是在代码中,我已经将CoreML注册为仅在应用程序在后台时才使用CPU,但是似乎CoreML类没有读取参数。

尽管将usesCpuOnly设置为true,但CoreML在后台运行时仍会收到错误消息

2019-07-23 12:04:21.747498+0800 Demo[351:9947] Execution of the command buffer was aborted due to an error during execution. Insufficient Permission (to submit GPU work from background) (IOAF code 6)
Error: command buffer exited with error status.
    The Metal Performance Shaders operations encoded on it may not have completed.
    Error:
    (null)
    Insufficient Permission (to submit GPU work from background) (IOAF code 6)
    <AGXA9FamilyCommandBuffer: 0x106b70720>
    label = <none>
    device = <AGXA9Device: 0x10bdc4000>
        name = Apple A9 GPU
    commandQueue = <AGXA9FamilyCommandQueue: 0x106a7f670>
        label = <none>
        device = <AGXA9Device: 0x10bdc4000>
            name = Apple A9 GPU
    retainedReferences = 1
2019-07-23 12:04:21.748800+0800 Demo[351:9982] [espresso] [Espresso::handle_ex_plan] exception=Espresso exception: "Generic error": Insufficient Permission (to submit GPU work from background) (IOAF code 6); code=7 status=-1
2019-07-23 12:04:21.749744+0800 Demo[351:9982] [StarMobile] [ERROR] CoreMLClassifier:112 processClassifications(for:error:) error: Unable to classify image. : Optional(Error Domain=com.apple.vis Code=9 "Could not run network (-1: ESPRESSO_STATUS_ERROR_GENERIC)" UserInfo={NSLocalizedDescription=Could not run network (-1: ESPRESSO_STATUS_ERROR_GENERIC)})
2019-07-23 12:04:21.750350+0800 Demo[351:9982] [StarMobile] [ERROR] CoreMLClassifier:105 updateClassifications(for:as:) Failed to perform classification.
Could not run network (-1: ESPRESSO_STATUS_ERROR_GENERIC)


请帮忙!

// AppDelegate.swift
func applicationWillResignActive(_ application: UIApplication) {
    Constants.APP_IS_IN_FOREGROUND = false
}
func applicationDidBecomeActive(_ application: UIApplication) {
    Constants.APP_IS_IN_FOREGROUND = true
}

// Main.swift
class Main() {
    func run_coreml(imageData: Data) {
        do {
            self.usesCpuOnly = Constants.APP_IS_IN_FOREGROUND
            let configuration = MLModelConfiguration()
            if self.usesCpuOnly {
                 configuration.computeUnits = .cpuOnly
            } else {
                 configuration.computeUnits = .all
            }
            let model = try VNCoreMLModel(for:
            CoreMLImageClassifier(configuration: configuration).model)

            let request = VNCoreMLRequest(model: model,
            completionHandler:
            { [weak self] request, error in
                 self?.process(for: request, error: error)
            })
            request.usesCPUOnly = self.usesCpuOnly

            let handler = VNImageRequestHandler(data: imageData)
            try handler.perform([request])
         } catch {}
    }

   // omit implementation
   func process() {}
}

最佳答案

当应用程序进入后台时,您将需要创建一个新的VNCoreMLModel实例,并将其配置设置为.cpuOnly,并创建一个新的VNCoreMLRequest。 Core ML不会自动从GPU切换到CPU。

10-06 14:55