0

我正在尝试在 MPSNNGraph 上输入两个图像。

但是,即使我在“withSourceImages”上输入了一个类似 [input1, input2] 的数组,我也只能输入“input1”作为输入图像。理想情况下,当创建如下图时,我想将“inputImage1”设为“input1”,将“inputImage2”设为“input2”。

实际上,当我像这样运行它并查看“concat”的结果时,我能够看到连接的是“input1”,而不是“input2”。

该图如下所示:

let inputImage1 = MPSNNImageNode(handle: nil)
let inputImage2 = MPSNNImageNode(handle: nil)
let scale = MPSNNBilinearScaleNode(source: inputImage1,
                                   outputSize: MTLSize(width:256,
                                                       height: 256,
                                                       depth: 3))
let scale2 = MPSNNBilinearScaleNode(source: inputImage1,
                                   outputSize: MTLSize(width:64,
                                                       height: 64,
                                                       depth: 3))
...

let concat = MPSNNConcatenationNode(sources: [conv3.resultImage, scale2.resultImage])

...

if let graph = MPSNNGraph(device: commandQueue.device,
                          resultImage: tanh.resultImage,
                          resultImageIsNeeded: true){
        self.graph = graph
}

和编码图的一部分看起来像:

let input1 = MPSImage(texture: texture, ...)
let input2 = MPSImage(texture: texture2, ...)
graph.executeAsync(withSourceImages: [input1, input2]) { outputImage, error in
    ...
}

如何输入第二个输入并且图形接收它?

你能给我一些建议吗?

4

2 回答 2

0

在此示例中,Scale2 使用与 Scale1 相同的输入图像。当 MPSNNGraph 分析这组节点时,它永远不会遇到 inputImage2,因为它没有被图使用。因为它不在图表中,所以图表界面不会接受第二个输入。它不知道该怎么处理它。

在我看来,如果您将 scale2 初始化更改为使用 inputImage2 ,那么它可能会按照您的预期工作。

于 2018-12-06T19:40:21.273 回答
0

您提供的代码实际上看起来是正确的。在此处引用 MPSNNGraph.h 标头:

*  @param  sourceImages    A list of MPSImages to use as the source images for the graph.
*                          These should be in the same order as the list returned from
*                          MPSNNGraph.sourceImageHandles. They should be allocated against
*                          the same MTLDevice. There must be at least one source image.
*                          Note: this array is intended to handle the case where multiple
*                          input images are required to generate a single graph result.
*                          That is, the graph itself has multiple inputs.  If you need to
*                          execute the graph multiple times, then call this API multiple
*                          times, or better yet use [encodeToCommandBuffer:sourceImages:]
*                          multiple times.

不过我想指出,MPSNNConcatenationNode 的行为方式非常独特。它总是在深度(通道)维度上连接。当拼接不同空间尺寸的图像时,它将尊重较小的一个(即2x2x10 concat 4x4x15 -> 2x2x25)。也许这就是你的问题的来源。

于 2018-08-02T18:35:25.183 回答