CreateML图像分类模型在iOS应用中输出错误结果求助
CoreML图像分类模型在iOS应用中输出错误结果问题
我使用CreateML创建了一个图像分类模型,可将图像分为猫、狗、兔子三类。在iOS应用中,点击imageView从相册选择图像,由该视觉模型处理后将结果显示在Label上。
在CreateML中测试模型时一切正常,但在应用中,每次启动后无论选择什么图像,都会依次输出错误结果:
- 77% Dog
- 83% Cat
- 71% Cat
- 56% Cat ...
我已尝试以下方法但问题未解决:
- 在不同线程执行操作
- 调整所选图像尺寸
- 重新制作模型
- 修改PHPicker配置
应用相关截图:
- 应用首次打开界面
- 选择相册图像界面
- 首次输出结果
- 第二次输出结果
相关代码
import UIKit import Photos import PhotosUI import CoreML import Vision class ViewController: UIViewController, PHPickerViewControllerDelegate, UINavigationBarDelegate { @IBOutlet weak var imageView: UIImageView! @IBOutlet weak var resultLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() imageView.isUserInteractionEnabled = true let gesture = UITapGestureRecognizer(target: self, action: #selector(selectImage)) imageView.addGestureRecognizer(gesture) } @objc func selectImage(){ imageView.image = UIImage() var configuration = PHPickerConfiguration(photoLibrary: .shared()) configuration.selectionLimit = 1 configuration.filter = PHPickerFilter.images let vc = PHPickerViewController(configuration: configuration) vc.delegate = self present(vc, animated: true) } func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { picker.dismiss(animated: true) DispatchQueue.global().async { results[0].itemProvider.loadObject(ofClass: UIImage.self) { [weak self] (reading, error) in guard var imageSelected = reading as? UIImage, error == nil else {return} print("Selected image = \(imageSelected)") guard let ciimage = CIImage(image: imageSelected) else {fatalError("Problem while converting to CIImage")} self?.detectImage(image: ciimage) DispatchQueue.main.async { self?.imageView.image = imageSelected } } } } func detectImage(image : CIImage){ let config = MLModelConfiguration() guard let model = try? VNCoreMLModel(for: DogCatRabbitMLTry_1.init(configuration: config).model) else {fatalError("Loading CoreML Model Failed")} let request = VNCoreMLRequest(model: model) { (request, error) in guard let results = request.results as? [VNClassificationObservation] else { fatalError("Model failed to process image") } if let firstResult = results.first{ print(firstResult.identifier) DispatchQueue.main.async { self.resultLabel.text = "%\(Int(firstResult.confidence*100)) \(firstResult.identifier)" } } } let handler = VNImageRequestHandler(ciImage: image) do{ try handler.perform([request]) } catch{ fatalError("ciimage was not handling") } } /* func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? { let size = image.size let widthRatio = targetSize.width / size.width let heightRatio = targetSize.height / size.height // Figure out what our orientation is, and use that to form the rectangle var newSize: CGSize if(widthRatio > heightRatio) { newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio) } else { newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio) } // This is the rect that we've calculated out and this is what is actually used below let rect = CGRect(origin: .zero, size: newSize) // Actually do the resizing to the rect using the ImageContext stuff UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) image.draw(in: rect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } */ }
内容的提问来源于stack exchange,提问作者Muammer Akca
相关产品推荐
相关产品推荐

