无法从CoreML Model Deployment中检索MLModel

问题描述

我是CoreMl的新手,我想从今年在WWDC上发布的Coreml模型部署中检索模型。

我制作了一个仅对特殊和稀有事物进行分类的应用程序,并将该模型上传到CoreMl模型部署仪表板。

我成功部署了该模型并将其显示为活动状态。

现在的问题是我无法检索该模型,我做了很多尝试,甚至看到了该模型上的所有WWDC会话,甚至从该会话中复制了该代码,但都没有成功

这是我整个模型的加载和检索代码

我的视觉请求代码

 func coremlmodel(using: VNCoreMLModel) -> VNCoreMLRequest {

            let request = VNCoreMLRequest(model: using,completionHandler: { [weak self] request,error in
                self?.processClassifications(for: request,error: error)
            })
            request.imageCropAndScaleOption = .centerCrop
            return request
            
    }

我的分类课将拍摄图像

 func updateClassifications(for image: UIImage) {
        classificationLabel.text = "Classifying..."
        
        var models = try? VNCoreMLModel(for: SqueezeNet().model)
        
        if let modelsss = models {
            extensionofhandler(ciimage: image,vnmodel: modelsss)
            return
        }
        
        _ = MLModelCollection.beginAccessing(identifier: "TestingresnetModel") { [self] result in

            var modelUrl: URL?

            switch result {
            case .success(let collection):
                modelUrl = collection.entries["class"]?.modelURL

            case .failure(let error):

                fatalError("sorry \(error)")
            }
            let result = loadfishcallisier(from: modelUrl)
            
            switch result {
            case .success(let modelesss):
                models = try? VNCoreMLModel(for: modelesss)
                extensionofhandler(ciimage: image,vnmodel: models!)
            case .failure(let error):
                fatalError("plz \(error)")
            }
        }
       
    }

func loadfishcallisier(from modelUrl: URL?) -> Result<MLModel,Error> {
        if let modelUrl = modelUrl {
            return Result { try MLModel(contentsOf: modelUrl)}
        } else {
            return Result { try MLModel(contentsOf: modelUrl!,configuration: .init())}
        }
    }
    
    func extensionofhandler(ciimage: UIImage,vnmodel: VNCoreMLModel) {
        let orientation = CGImagePropertyOrientation(ciimage.imageOrientation)
        guard let ciImage = CIImage(image: ciimage) else { fatalError("Unable to create \(CIImage.self) from \(ciimage).")
        }
        dispatchQueue.global(qos: .userInitiated).async { [self] in
            
            let handler = VNImageRequestHandler(ciImage: ciImage,orientation: orientation)
            
            do {
                
            try handler.perform([coremlmodel(using: vnmodel)])
            
            } catch {
                fatalError("Check the error")
            }
        }
    }

我的分类代码

 func processClassifications(for request: VNRequest,error: Error?) {
        dispatchQueue.main.async {
            guard let results = request.results else {
                self.classificationLabel.text = "Unable to classify image.\n\(error!.localizedDescription)"
                return
            }
            // The `results` will always be `VNClassificationObservation`s,as specified by the Core ML model in this project.
            let classifications = results as! [VNClassificationObservation]
        
            if classifications.isEmpty {
                self.classificationLabel.text = "nothing recognized."
            } else {
                // display top classifications ranked by confidence in the UI.
                let topClassifications = classifications.prefix(2)
                let descriptions = topClassifications.map { classification in
                    // Formats the classification for display; e.g. "(0.37) cliff,drop,drop-off".
                   return String(format: "  (%.2f) %@",classification.confidence,classification.identifier)
                }
                self.classificationLabel.text = "Classification:\n" + descriptions.joined(separator: "\n")
            }
        }
    }

Xcode不会引发任何错误,但无法识别任何内容

如果我在代码中做错了什么,我会谦虚地请你向我展示并解决

并且有任何教程可用于从coreml模型部署中检索模型。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)