我有一个coreml模型,该模型运行后将返回带有1个“ MultiArray:Double 10 x IMG_SIZE x IMG_SIZE数组”的VNCoreMLFeatureValueObservation对象
如何将其转换为10个UIImage,每个UIImage具有IMG_SIZE x IMG_SIZE尺寸,并且它们的值是灰度的?
答案 0 :(得分:1)
窥探了一下之后,我发现我必须添加以下辅助函数:
https://github.com/hollance/CoreMLHelpers到我的Xcode项目。 并且来自MultiArray初始化问题:https://stackoverflow.com/a/44462908/403403
然后我将这个解决方案拼凑在一起:
let request = VNCoreMLRequest(model: model) { (request, error) in
guard let results = request.results as? [VNCoreMLFeatureValueObservation] else {
fatalError("Model failed to process image")
}
let obs : VNCoreMLFeatureValueObservation = (results.first)!
let m: MLMultiArray = obs.featureValue.multiArrayValue!
var mArrays = [MLMultiArray]()
for i in 1..<10 {
let start = i*(IMG_SIZE*IMG_SIZE)
guard let tmp : MLMultiArray = try? MLMultiArray(shape:[768,768], dataType:MLMultiArrayDataType.double) else {
fatalError("Unexpected runtime error. MLMultiArray")
}
for n in 0..<(IMG_SIZE*IMG_SIZE) {
tmp[n] = m[start+n]
}
mArrays.append(tmp)
}
guard let mlMultiArray : MLMultiArray = try? MLMultiArray(shape:[768,768], dataType:MLMultiArrayDataType.double) else {
fatalError("Unexpected runtime error. MLMultiArray")
}
for n in 0..<(IMG_SIZE*IMG_SIZE) {
mlMultiArray[n] = m[n]
}
let imagePredict : UIImage = mlMultiArray.image(offset: 0, scale: 255)!
self.imagePred1.image = imagePredict
self.imagePred2.image = mArrays[0].image(offset: 0, scale: 255)!
self.imagePred3.image = mArrays[1].image(offset: 0, scale: 255)!
self.imagePred4.image = mArrays[2].image(offset: 0, scale: 255)!
self.imagePred5.image = mArrays[3].image(offset: 0, scale: 255)!
self.imagePred6.image = mArrays[4].image(offset: 0, scale: 255)!
self.imagePred7.image = mArrays[5].image(offset: 0, scale: 255)!
self.imagePred8.image = mArrays[6].image(offset: 0, scale: 255)!
self.imagePred9.image = mArrays[7].image(offset: 0, scale: 255)!
}
希望有一种更清洁的方法,但是现在可以使用