将Keras ConvLSTM2D输出转换为Conv2D的输入

时间:2019-05-29 15:29:46

标签: python-3.x tensorflow conv-neural-network keras-layer

我有一个用于输入图像的模型,该模型的目的是与作为输出的单个图像进行比较。

所有/每个图像的大小= [96,96,3]

模型进入一个文件夹,该文件夹中共有6张图像,其中5张类似于第6张图像,我们必须使用这5张图像(图像增强图像)正确预测第6张图像。 测试集和训练集具有多个这些文件夹

我决定使用CNN + LSTM解决此问题,我相信这将比仅使用纯CNN解决此问题做得更好,我已经尝试了在性能上达到顶峰的纯CNN和图像增强。

下面是我当前使用的模型,在此评论的部分是我遇到的问题。

config.height = 96
config.width = 96
model = Sequential()
model.add(ConvLSTM2D(filters=32, kernel_size=(3,3), padding='same', input_shape=(5, config.height, config.width, 3), return_sequences=True, stateful=False))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=32, kernel_size=(3,3), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=32, kernel_size=(3,3), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(ConvLSTM2D(filters=32, kernel_size=(3,3), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(ConvLSTM2D(filters=3, kernel_size=(3,3), padding='same', return_sequences=True))
model.add(BatchNormalization())
# LSTM_to_conv_dims = (-1, 96, 96, 3)
# model.add(Reshape(LSTM_to_conv_dims))
print(f'Shape of model {model.summary()}')
model.add(Conv2D(3, (3, 3), activation='relu', padding='same'))

要帮助此模型的输出用于上面的model.summary()

Layer (type)                 Output Shape              Param #   
=================================================================
conv_lst_m2d_1 (ConvLSTM2D)  (None, 5, 96, 96, 32)     40448     
_________________________________________________________________
batch_normalization_1 (Batch (None, 5, 96, 96, 32)     128       
_________________________________________________________________
conv_lst_m2d_2 (ConvLSTM2D)  (None, 5, 96, 96, 32)     73856     
_________________________________________________________________
batch_normalization_2 (Batch (None, 5, 96, 96, 32)     128       
_________________________________________________________________
conv_lst_m2d_3 (ConvLSTM2D)  (None, 5, 96, 96, 32)     73856     
_________________________________________________________________
batch_normalization_3 (Batch (None, 5, 96, 96, 32)     128       
_________________________________________________________________
dropout_1 (Dropout)          (None, 5, 96, 96, 32)     0         
_________________________________________________________________
conv_lst_m2d_4 (ConvLSTM2D)  (None, 5, 96, 96, 32)     73856     
_________________________________________________________________
batch_normalization_4 (Batch (None, 5, 96, 96, 32)     128       
_________________________________________________________________
dropout_2 (Dropout)          (None, 5, 96, 96, 32)     0         
_________________________________________________________________
conv_lst_m2d_5 (ConvLSTM2D)  (None, 5, 96, 96, 3)      3792      
_________________________________________________________________
batch_normalization_5 (Batch (None, 5, 96, 96, 3)      12        
_________________________________________________________________
reshape_1 (Reshape)          (None, 5, 96, 96, 3)      0         
=================================================================
Total params: 266,332
Trainable params: 266,070
Non-trainable params: 262
_________________________________________________________________
  • 当我尝试执行以下操作时,出现的错误是模型的最后一行 调整形状并将我的BatchNorm_5输出作为输入输入到Conv2D。

  • 我知道需要更改为 [无,96、96、3] 。 我需要社区的帮助。

  • 为什么需要这种形状,是因为我的输出图像是相同的
    需要比较的形状。

  • 我想以最少的信息丢失来进行此重塑。

我得到的错误,这是非常正确的,但我需要有关如何纠正此错误的想法

Traceback (most recent call last):
  File "train.py", line 154, in <module>
    model.add(Conv2D(3, (3, 3), activation='relu', padding='same'))
  File "/home/sandeeppanku/anaconda3/envs/tensorflowpy1p11/lib/python3.6/site-packages/keras/engine/sequential.py", line 181, in add
    output_tensor = layer(self.outputs[0])
  File "/home/sandeeppanku/anaconda3/envs/tensorflowpy1p11/lib/python3.6/site-packages/keras/engine/base_layer.py", line 414, in __call__
    self.assert_input_compatibility(inputs)
  File "/home/sandeeppanku/anaconda3/envs/tensorflowpy1p11/lib/python3.6/site-packages/keras/engine/base_layer.py", line 311, in assert_input_compatibility
    str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5

1 个答案:

答案 0 :(得分:0)

如果在最后一个convlstm2d层中使用conv2d,则必须将return_sequences=True更改为return_sequences=False。运行该程序后,您可以获得ndim=4