简体   繁体   English

如何在Keras中添加带LSTM的Conv2D图层?

[英]How to add Conv2D layers with LSTM in Keras?

I'm trying to identify the sequence of images. 我正在尝试识别图像序列。 I've 2 images and I need to identify the 3rd one. 我有2张图片,我需要识别第3张图片。 All are color images. 都是彩色图像。

I'm getting below error: 我收到以下错误:

ValueError: Error when checking input: expected time_distributed_1_input to have 5 dimensions, but got array with shape (32, 128, 128, 6) ValueError:检查输入时出错:预期time_distributed_1_input有5个维度,但得到的数组有形状(32,128,128,6)

This is my layer: 这是我的图层:

batch_size = 32
height = 128
width = 128
model = Sequential()
model.add(TimeDistributed(Conv2D(32, (3, 3), activation = 'relu'), input_shape=(batch_size, height, width, 2 * 3)))
model.add(TimeDistributed(MaxPooling2D(2, 2)))
model.add(TimeDistributed(BatchNormalization()))
model.add(TimeDistributed(Conv2D(32, (3, 3), activation='relu', padding='same')))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(LSTM(256, return_sequences=True, dropout=0.5))
model.add(Conv2D(3, (3, 3), activation='relu', padding='same'))
model.compile(optimizer='adam')
model.summary()

My input images shapes are: (128, 128, 2*3) [as I'm concatenating 2 input images] 我的输入图像形状是:(128,128,2 * 3)[因为我连接2个输入图像]

My output image shape is: (128, 128, 3) 我的输出图像形状是:(128,128,3)

You have applied the conv layer after Flatten() . 您已经在Flatten()之后应用了conv层。 This causes error because after flattening the data flowing through the Network is no more a 2D object. 这会导致错误,因为在展平后,流经网络的数据不再是2D对象。

I suggest you to keep the convolutional and the recurrent phases separated. 我建议你保持卷积和循环阶段分开。 First, you apply convolution to images, training the model to extract their relevant features. 首先,将卷积应用于图像,训练模型以提取其相关特征。 Later, you push these features into LSTM layers, so that you can capture also the information hidden in their sequence. 稍后,您将这些功能推送到LSTM图层,以便您还可以捕获其序列中隐藏的信息。

Hope this helps, otherwise let me know. 希望这会有所帮助,否则请告诉我。

-- -

EDIT : 编辑

According to the error that you get, it seems that you are also not feeding the exact input shape. 根据你得到的错误,似乎你也没有提供确切的输入形状。 Keras is saying: "I need 5 dimensions, but you gave me 4". 凯拉斯说:“我需要5个维度,但你给了我4个”。 A TimeDistributed() layers needs a shape such as: (sample, time, width, length, channel) . TimeDistributed()图层需要一个形状,例如:( (sample, time, width, length, channel) Your input lacks time , apparently. 显然,你的投入缺乏time

I suggest you to print your model.summary() before running, and check the layer called time_distributed_1_input . 我建议你在运行之前打印你的model.summary() ,并检查名为time_distributed_1_input That's the one your compiler is upset with. 那就是你的编译器烦恼的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM