在Keras中,如何在构建模型时使两个张量相互缠绕

时间:2019-12-25 07:50:59

标签: tensorflow keras

我正在尝试复制和构建Keras的暹罗CNN模型。 在该网络的末尾,两个暹罗分支将相互缠绕以计算得分图。 在Pytorch版本的源代码中,实现很简单:

def xcorr_depthwise(x, kernel):
    """depthwise cross correlation
    """
    batch = kernel.size(0)
    channel = kernel.size(1)
    x = x.view(1, batch*channel, x.size(2), x.size(3))
    kernel = kernel.view(batch*channel, 1, kernel.size(2), kernel.size(3))
    out = F.conv2d(x, kernel, groups=batch*channel)
    out = out.view(batch, channel, out.size(2), out.size(3))
    return out

但是在Keras中,构建模型时,输入的批处理数量为'None'。因此,不能使用keras.models.Model将此部分(或整个网络)做成Keras Model

def DepthwiseXCorr(self, x, z):
    batch = z.shape[0]
    channel = z.shape[-1]

    x = tf.reshape(tensor=x, shape=[1, x.shape[1], x.shape[2], batch * channel])
    z = tf.reshape(tensor=z, shape=[z.shape[1], z.shape[2], batch * channel, 1])

    # outputs = DepthwiseConv2D()
    outputs = tf.nn.depthwise_conv2d(x, z, strides=[1, 1, 1, 1], padding='VALID')
    return outputs

是否可以将该网络实现为Keras模型? Architecture of this network in its paper

0 个答案:

没有答案