【问题标题】:How can I use layer.Timedistributed to process two input in Keras?如何使用 layer.Timedistributed 处理 Keras 中的两个输入?
【发布时间】:2020-03-06 11:45:27
【问题描述】:

我的 Keras 版本是 2.2.5,tensorflow 是 1.15,python 是 3.6。

现在,我想将 RetinaNet 转换为时间分布式网络来处理一些 3d 数据,但我发现当我使用 keras.layter.Timedistributed 时,我发现它无法处理具有两个输入的层,例如: layers.UpsampleLike(name='P5_upsampled')([P5, C4]) P5 的形状是(?, ?, x, y, 256)C4 的形状是(?, ?, a, b, 1024)。因为x,ya,b不同,P5需要加上C4,所以我需要调整P5的图片大小。这就是layer.UpsamleLike() 的剂量。我使用的代码是这样的:

P5 = keras.layers.TimeDistributed(keras.layers.Conv2D(feature_size, kernel_size=1, strides=1, padding='same'), name='C5_reduced')(C5)
P5_upsampled = keras.layers.TimeDistributed(layers.UpsampleLike(), name='P5_upsampled')([P5, C4])  

layer.UpsampleLike 是:

class UpsampleLike(keras.layers.Layer):
    """ Keras layer for upsampling a Tensor to be the same shape as another Tensor.
    """

    def call(self, inputs, **kwargs):
        source, target = inputs
        target_shape = keras.backend.shape(target)
        if keras.backend.image_data_format() == 'channels_first':
            source = backend.transpose(source, (0, 2, 3, 1))
            output = backend.resize_images(source, (target_shape[2], target_shape[3]), method='nearest')
            output = backend.transpose(output, (0, 3, 1, 2))
            return output
        else:
            return tensorflow.compat.v1.image.resize_images(source, (target_shape[1], target_shape[2]), method=tensorflow.image.ResizeMethod.NEAREST_NEIGHBOR)

我认为Timedistributed层是对输入进行unstack并输入它们,所以我希望Timedistributed将两个输入分开处理并输入到layer.UpsampleLike,我该怎么做?

【问题讨论】:

  • 两个输入 P5 和 C4 的形状是什么?是否可以将 TimeDistributed 单独应用于每个张量?您能否分享您的模型层或相应的代码,说明您需要时间分布式包装器的位置或方式?
  • 我添加我的代码并解释更多

标签: python keras keras-layer


【解决方案1】:

如果我正确理解了这个问题,这里有一个可能的解决方案来管理具有不同形状的多个输入:

input_a = Input(shape=(?, ?, x, y, 256))
input_b = Input(shape=(?, ?, a, b, 1024))

x = Dense(256)(input_a)
x = Dense(256)(x)
x = Model(inputs=input_a, outputs=x)

y = Dense(256)(input_b)
y = Dense(256)(y)
y = Model(inputs=input_b, outputs=y)

# combine the output of the two branches
combined = concatenate([x.output, y.output])
z = Dense(256)(combined)
z = Dense(2, activation='softmax')(z)

model = Model(inputs=[x.input, y.input], outputs=z)

图层、输出和输入都是示例...您可以根据需要更改它们。

【讨论】:

    猜你喜欢
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    相关资源
    最近更新 更多