1

hello guys i am trying to make model with AlexNet + LSTM using raw image as input

but i got an error like this :

ValueError: Input 0 of layer lstm_5 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 43264)

my model code:

model = tf.keras.models.Sequential([
    # 1st conv
  tf.keras.layers.Conv2D(96, (11,11),strides=(4,4), activation='relu', input_shape=(227, 227, 3)),
  tf.keras.layers.BatchNormalization(),
  tf.keras.layers.MaxPooling2D(2, strides=(2,2)),
    # 2nd conv
  tf.keras.layers.Conv2D(256, (11,11),strides=(1,1), activation='relu',padding="same"),
  tf.keras.layers.BatchNormalization(),
     # 3rd conv
  tf.keras.layers.Conv2D(384, (3,3),strides=(1,1), activation='relu',padding="same"),
  tf.keras.layers.BatchNormalization(),
    # 4th conv
  tf.keras.layers.Conv2D(384, (3,3),strides=(1,1), activation='relu',padding="same"),
  tf.keras.layers.BatchNormalization(),
    # 5th Conv
  tf.keras.layers.Conv2D(256, (3, 3), strides=(1, 1), activation='relu',padding="same"),
  tf.keras.layers.BatchNormalization(),
  tf.keras.layers.MaxPooling2D(2, strides=(2, 2)),
  # To Flatten layer
  tf.keras.layers.Flatten(),
  # LSTM Layer
  tf.keras.layers.LSTM(3),
  # To FC layer 1
  tf.keras.layers.Dense(4096, activation='relu'),
    # add dropout 0.5 ==> tf.keras.layers.Dropout(0.5),
  #To FC layer 2
  tf.keras.layers.Dense(4096, activation='relu'),
    # add dropout 0.5 ==> tf.keras.layers.Dropout(0.5),
  tf.keras.layers.Dense(output_class_units, activation='softmax')
])

but when i do it with only the AlexNet its working fine so i think the problem are on the LSTM layers but i still have no clue how to fix it

kinda new to this so hope anyone can help me fix this

thank you so much !

JuniorPython
  • 113
  • 1
  • 8

1 Answers1

0

Let's understand the error:

ValueError: Input 0 of layer lstm_5 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 43264)

LSTM was expecting a 3D layer: first dim is the batch_size, second dim is the time is the third dim your data.

The output of tf.keras.layers.Flatten() was a 2D layer, where the first dim is the batch_size and the second dim is your data. We don't have the time dimension here.

To achieve what you want, you should wrap your layers around a TimeDistributed layer. We have an example here

Daquisu
  • 93
  • 1
  • 5
  • ah yea i forgot to wrap it with TimeDistributed, but now i got ` ValueError: Input 0 of layer conv2d_102 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (7264, 227, 3) ` error when i did the model.fit – JuniorPython Jun 22 '21 at 14:05