Ios CoreML模型不返回结果
我正在使用CoreML模型测试草图识别,但它不会返回任何结果。这是我的密码:Ios CoreML模型不返回结果,ios,swift,coreml,Ios,Swift,Coreml,我正在使用CoreML模型测试草图识别,但它不会返回任何结果。这是我的密码: func predict(){ guard let image = UIImage(named: "test.png") else{ print("Image not found") return } do{ let model = try VNCoreMLModel(for: sketch2().model) let reques
func predict(){
guard let image = UIImage(named: "test.png") else{
print("Image not found")
return
}
do{
let model = try VNCoreMLModel(for: sketch2().model)
let request = VNCoreMLRequest(model: model, completionHandler: displayPredictions)
let handler = VNImageRequestHandler(cgImage: (image.cgImage!))
try handler.perform([request])
}catch{
}
}
func displayPredictions(request: VNRequest, error: Error?) {
if let results = request.results as? [VNClassificationObservation]{
print(results)
}else{
print("some error")
}
}
在viewDidLoad()
中调用predict()
方法
当在
displayPredictions()
中打印results()
时,它返回空数组[]
。我知道模型接受哪种格式的图像,他说这是128x128灰度,这可能是问题所在,如果是,我如何将test.png
文件转换为128x128灰度。如果您对这个问题有任何其他想法,可能是使用coremltools或任何其他工具转换模型的问题,请让我知道。谢谢您的帮助。这是分类模型吗?当您在Xcode中打开.mlmodel文件时,Core ML表示期望的输出是什么?请注意,Vision目前似乎不适用于非分类器的模型。是的,我相当确定它是一个分类器,该模型需要一个图像
作为输入,并输出一个多数组
。因此,我猜它会输出中的一个标签。我认为它与转换/输出本身有关,而与输入无关,因为即使我尝试使用模型在其上训练的一些数据,它仍然输出空数组。如果输出是多数组
,则它不是分类器(在Core ML中)。Core ML中的分类器返回一个String:Double
字典。转换模型时,是否指定了class\u labels='map.txt'
?这是我使用的脚本:import coremltools coreml\u model=coremltools.converters.caffe.convert('sketchANet\u full\u iter\u 7500.caffemodel','deploy.prototxt'),predicted\u feature\u name='map.txt',image\u input\u name='data')coreml\u model.save('sketch2.mlmodel'))
我应该尝试用class\u labels='map.txt'
替换predicted\u feature\u name='map.txt'
@MatthijsHollemansYes,predicted\u feature\u name
是文档中的一个bug。它应该是class\u标签
。