原文链接:部署到Core ML
部署到CoreML
在Turi Create中创建的目标检测模型可以很容易的部署到Core ML
在iOS12 和 macOS10.14(Turi Create 5)中部署模型
使用Turi Create 5.0+ 、iOS12 和macOS10.14,你可以通过Vision Framework直接集成目标检测模型。
你可以直接在Turi Create5中导出到Core ML,如下所示
model.export_coreml('MyDetector.mlmodel')
let mlmodel = MyDetector()
let visionModel = try VNCoreMLModel(for: mlmodel.model)
let objectRecognition = VNCoreMLRequest(model: visionModel,
completionHandler: {
(request, error) in
guard let results = request.results else {
return }
for case let foundObject as VNRecognizedObjectObservation in results {
let bestLabel = foundObject.labels.first! // Label with highest confidence
let objectBounds = foundObject.boundingBox // Normalized between [0,1]
let confidence = foundObject.confidence // Confidence for the predicted class
// Use the computed values.
print(bestLabel.identifier, confidence, objectBounds)
}
})
objectRecognition.imageCropAndScaleOption = .scaleFill
有关于Core ML的集成和示例应用程序的更多细节,请查看Recognizing Objects in Live Capture小节,请注意上面链接的演示demo使用归一化的置信度,该置信度强制所有类别的得分总和为1。
- 注意:边界框对象
VNRecognizedObjectObservation.boundingBox
和 Turi Create所使用的边界框定义不一样。首先,位置的坐标是在边界框左下角,而不是中心点。其次,坐标空间的原点位于图像的左下角。最后,边界框的位置坐标、宽度和高度都通过图像的尺寸归一化在[0,1]之间 - 注意:仅仅使用了
non-maximum suppression
参数导出的模型(Turi Create 5.0+ 默认为true)才能与上面的例子一起使用。较旧的模型或指定了include_non_maximum_suppression = False
参数的模型不会将结果作为VNRecognizedObjectObservation
对象,而是将结果作为VNCoreMLFeatureValueObservation
对象。而VNCoreMLFeatureValueObservation
对象在上面的例子中是被忽略的。要了解如何使用VNCoreMLFeatureValueObservation
对象,请继续阅读。
在iOS11 和 macOS10.13中部署模型
在iOS11和macOS10.13中部署目标检测模型,你需要执行一些额外的步骤(这些步骤已经合并到iOS12 和 macOS 10.14中)
你可以在Turi Create5中按照如下步骤导出到Core ML
model.export_coreml