0

I am very new to CoreMl and I want to retrieve a model from Coreml model deployment which was released this year at WWDC.

I made an app that just classifies special and rare things and I uploaded that model.archive to the CoreMl Model deployment dashboard.

I successfully deployed the model and its showing as active.

now the problem is I am unable to retrieve that model, I have tried a lot, I even saw all the WWDC sessions on that one and even copied that code from that session but all in vain

Here is my whole model loading and retrieving code

my Vision request code

 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
            
    }

my classification cade which will take an image

 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")
            }
        }
    }

my classification code

 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 throws no error but it's not recognising anything.

if I have done anything wrong in my code I humbly ask you to show it to me and solve it

and is there any tutorial for retrieving the model from coreml model deployment.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • Have you ensured the Model has been successfully downloaded? Also, you're meant to store a local copy of a model; which is the fall back if the remote fails. You should run a breakpoint in the `processClassifications` function & see if the results array is not empty. – Harry J Sep 29 '20 at 12:26
  • @HarryJ thanks I will check and inform you soon – fouzan ahmed Sep 29 '20 at 14:32
  • @HarryJ no model is not downloaded and beginAccessing method is not called soo can u please help me out. or if u have any examples on this then please share it with me. – fouzan ahmed Oct 02 '20 at 22:40
  • your `updateClassifications` function doesn't seem to be called. Have you looked through this video?https://developer.apple.com/videos/play/wwdc2020/10152/ – Harry J Oct 03 '20 at 03:23
  • @HarryJ sir I have looked through that video long ago and. have implemented that same code in this one too I noted that updateClassifications function is being called but the beginAccessing method and code associated with it is not being called. it will be great if u share ur code to retrieve the model. – fouzan ahmed Oct 03 '20 at 15:42

0 Answers0