0

Error Name: "Failed to load Core ML model." UserInfo={NSLocalizedDescription=Failed to load Core ML model.}

//Code
  /* 5. Declare a function for image classification */
    func detect(image: CIImage) {
        let config = MLModelConfiguration()
        
        do {
            guard let model = try? VNCoreMLModel(for: TransferLearning(configuration: config).model) else {
                throw NSError(domain: "in.vihorgupta.app", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to load Core ML model."])
            }
            let request = VNCoreMLRequest(model: model) { (request, error) in
                if let error = error {
                    print("Model failed to process image with error: \(error)")
                    return
                }
                guard let results = request.results as? [VNClassificationObservation] else {
                    print("Model failed to process image.")
                    return
                }
                if let firstResult = results.first {
                    self.textView.text = firstResult.identifier.capitalized
                    self.textView.textColor = .systemOrange
                }
            }
            let handler = VNImageRequestHandler(ciImage: image)
            try handler.perform([request])
        } catch {
            print("Error occurred: \(error)")
        }
    }

I want the app to load the MLModel and predict the image. But giving the error : 2023-03-01 12:04:12.804469+0530 Chapter-13[3098:150405] Metal API Validation Enabled Error occurred: Error Domain=in.vihorgupta.Chapter-13 Code=-1 "Failed to load Core ML model." UserInfo={NSLocalizedDescription=Failed to load Core ML model.}

  • Why is there a `try?` with a question mark? Remove it, just replace the `guard let model = try? VNCoreModel(...) else { ... }` with `let model = try VNCoreModel(...)` and read the real error thrown – Larme Mar 01 '23 at 10:23

0 Answers0