【发布时间】:2020-06-30 19:54:55
【问题描述】:
我已经训练了一个模型来区分恶性和良性皮肤病变,以潜在地检测患者是否患有皮肤癌,并将我的 keras 模型转换为 coreML。现在,我正在尝试使用 swift(通过 Xcode)将我的模型应用于 ios 应用程序,而我完全没有经验(仍在通过反复试验学习)。
目前我正试图通过一个简单的应用程序让模型工作,该应用程序只从手机的摄像头获取图像以获取预测的标签作为输出,但我非常坚持让摄像头实际工作来做到这一点。
import UIKit
import CoreML
import Vision
import Social
@UIApplicationMain
class ViewControl: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIApplicationDelegate {
@IBOutlet weak var imageView: UIImageView!
var classificationResults : [VNClassificationObservation] = []
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
func detect(image: CIImage) {
// Load the ML model through its generated class
guard let model = try? VNCoreMLModel(for: weights_skin_cancer().model) else {
fatalError("can't load ML model")
}
let request = VNCoreMLRequest(model: model) { request, error in
guard let results = request.results as? [VNClassificationObservation],
let topResult = results.first
else {
fatalError("unexpected result type from VNCoreMLRequest")
}
if topResult.identifier.contains("malignant") {
DispatchQueue.main.async {
self.navigationItem.title = "mal!"
self.navigationController?.navigationBar.barTintColor = UIColor.green
self.navigationController?.navigationBar.isTranslucent = false
}
}
else {
DispatchQueue.main.async {
self.navigationItem.title = "benign!"
self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false
}
}
}
let handler = VNImageRequestHandler(ciImage: image)
do { try handler.perform([request]) }
catch { print(error) }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imageView.image = image
imagePicker.dismiss(animated: true, completion: nil)
guard let ciImage = CIImage(image: image) else {
fatalError("couldn't convert uiimage to CIImage")
}
detect(image: ciImage)
}
}
@IBAction func cameraTapped(_ sender: Any) {
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
}
这也是用于将我的模型转换为 coreML 的代码以供参考:
import coremltools
output_labels = ['benign', 'malignant']
scale = 1/255.
coreml_model = coremltools.converters.keras.convert('/Users/Grampun/Desktop/ISIC-Archive-Downloader-master/trained_models/lr_0.00006-400_DS-20_epochs/weights.best.from_scratch.6.hdf5',
input_names='image',
image_input_names='image',
output_names='output',
class_labels=output_labels,
image_scale=scale)
coreml_model.author = 'Jack Bugeja'
coreml_model.short_description = 'Model used to identify between benign and malignant skin lesions'
coreml_model.input_description['image'] = 'Dermascopic image of skin lesion to evaluate'
coreml_model.input_description['output'] = 'Malignant/Benign'
coreml_model.save(
'/Users/Grampun/Desktop/ISIC-Archive-Downloader-master/trained_models/model_for_ios/lr_0.00006-400_DS-20_epochs/weights_skin_cancer.mlmodel')
任何一般的帮助将不胜感激。 谢谢!
【问题讨论】:
-
你问的实际问题是什么?
-
@MatthijsHollemans 抱歉,如果不清楚,我在问如何让相机工作 - 我的代码中的什么导致我的相机一旦按下就无法激活。
标签: ios swift xcode machine-learning coreml