我正在将两个训练有素的 Keras 模型转换为金属性能着色器。我必须重塑第一个图的输出并将其用作第二个图的输入。第一个图的输出是一个具有“形状”(1,1,8192) 的 MPSImage,第二个图的输入是一个“形状”(4,4,512) 的 MPSImage。
我将 graph1 的输出 image.texture 转换为 float16 数组,并将其传递给以下函数以将数据复制到“midImage”,即 4x4x512 MPSImage:
func reshapeTexture(imageArray:[Float16]) -> MPSImage{
let image = imageArray
image.withUnsafeBufferPointer { ptr in
let width = midImage.texture.width
let height = midImage.texture.height
for slice in 0..<128{
for w in 0..<width{
for h in 0..<height{
let region = MTLRegion(origin: MTLOriginMake(w, h, 0),
size: MTLSizeMake(1, 1, 1))
midImage.texture.replace(region: region, mipmapLevel: 0, slice: slice, withBytes: ptr.baseAddress!.advanced(by: ((slice * 4 * width * height) + ((w + h) * 4)), bytesPerRow: MemoryLayout<Float16>.stride * 4, bytesPerImage: 0)
}
}
}
}
return midImage
}
当我将 midImage 传递给 graph2 时,图形的输出是一个正方形,有 3/4 的乱码噪声,右下角有 1/4 的黑色。我想我不了解用于存储额外通道的 MPSImage 切片属性。谢谢!