I am using CNN-LSTM network for image classification. My image size is (224, 224, 3) and batch size is 90. I m getting this error when i passing input to LSTM layer. Following is my code snippet:
input1 = Input(shape=(224, 224,3))
x = Conv2D(8, (3,3), activation ="relu")(input1)
x = MaxPooling2D(2,2)(x)
x = Conv2D(16, (3,3), activation ="relu")(x)
x = MaxPooling2D(2,2)(x)
x = Conv2D(32, (3,3), activation ="relu")(x)
x = MaxPooling2D(2,2)(x)
x = Conv2D(64, (3,3), activation ="relu")(x)
x = Dropout(0.2)(x)
x = MaxPooling2D(2,2)(x)
x = LSTM(units= 64, activation= 'tanh', input_shape= [None, 144], return_sequences = True)(x)
error:
---> 10 x = LSTM(units= 64, activation= 'tanh', input_shape= [None, 144], return_sequences = True)(x)
ValueError: Input 0 of layer lstm_14 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 12, 12, 64]
Thanks if someone can sort my issue.
According to the documentation page, LSTM layer input should have the following shape:
[batch, timesteps, feature]
. Your LSTM layer receives input of shape[None, 12, 12, 64]
, this is why you obtain an error about 3D/4D shapes. You need to reshape your tensor:[None, 12, 12, 64] -> [None, 144, 64]
. To do this, you can insertReshape
layer between your lastMaxPooling2D
andLSTM
layers.