我是swift,TFlite和IOS的新手。我成功地转换了,运行了我的模型。然而,最后,我需要重建一个图像。我的TFlite模型返回一个TFLite.tensor Float32 4d形状(1,高,宽,3)。
let outputTensor: Tensor
outputTensor = try myInterpreter.output(at: 0)我正在寻找一个没有阿尔法的RGB图片。在python中,它将如下所示:
Image.fromarray((np.array(outputTensor.data) * 255).astype(np.uint8))据我所知,最好的方法是制作一个CVPixelBuffer,应用CoreOS转换(针对x255),最后制作UUImage。我深深地迷失在IOS文档中,它存在许多可能性,社区有什么建议吗?
++t
发布于 2021-08-02 15:25:34
在谷歌示例中,可以对UIImage的扩展进行编码:
extension UIImage {
convenience init?(data: Data, size: CGSize) {
let width = Int(size.width)
let height = Int(size.height)
let floats = data.toArray(type: Float32.self)
let bufferCapacity = width * height * 4
let unsafePointer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferCapacity)
let unsafeBuffer = UnsafeMutableBufferPointer<UInt8>(
start: unsafePointer,
count: bufferCapacity)
defer {
unsafePointer.deallocate()
}
for x in 0..<width {
for y in 0..<height {
let floatIndex = (y * width + x) * 3
let index = (y * width + x) * 4
let red = UInt8(floats[floatIndex] * 255)
let green = UInt8(floats[floatIndex + 1] * 255)
let blue = UInt8(floats[floatIndex + 2] * 255)
unsafeBuffer[index] = red
unsafeBuffer[index + 1] = green
unsafeBuffer[index + 2] = blue
unsafeBuffer[index + 3] = 0
}
}
let outData = Data(buffer: unsafeBuffer)
// Construct image from output tensor data
let alphaInfo = CGImageAlphaInfo.noneSkipLast
let bitmapInfo = CGBitmapInfo(rawValue: alphaInfo.rawValue)
.union(.byteOrder32Big)
let colorSpace = CGColorSpaceCreateDeviceRGB()
guard
let imageDataProvider = CGDataProvider(data: outData as CFData),
let cgImage = CGImage(
width: width,
height: height,
bitsPerComponent: 8,
bitsPerPixel: 32,
bytesPerRow: MemoryLayout<UInt8>.size * 4 * Int(size.width),
space: colorSpace,
bitmapInfo: bitmapInfo,
provider: imageDataProvider,
decode: nil,
shouldInterpolate: false,
intent: .defaultIntent
)
else {
return nil
}
self.init(cgImage: cgImage)
}
}然后,通过TFLite的推理,可以很容易地构造出图像。
let outputTensor: Tensor
outputTensor = try decoder.output(at: 0)
image = UIImage(data: outputTensor.data, size: size) ?? UIImage()https://stackoverflow.com/questions/67509720
复制相似问题