title: Keras neural net for video classification layer incompatible with the layer: expected ndim=5, found ndim=4. Full shape received: (None, 120, 120, 3)
body: <p>I am working on a video classification problem. My neural network stack looks like below:</p>
  <pre><code>model = Sequential()
  
  model.add(TimeDistributed(Conv2D(64, (3,3), padding='same', activation='relu'), input_shape=(60, 120, 120, 3)))
  model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))
  
  model.add(TimeDistributed(Conv2D(128, (3,3), padding='same', activation='relu')))
  model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))
  
  model.add(TimeDistributed(Conv2D(256, (3,3), padding='same', activation='relu')))
  model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))
  
  model.add(TimeDistributed(Flatten()))
  
  model.add(Dropout(0.5))
  model.add(LSTM(256, dropout=0.5))
  model.add(Dense(6, activation='softmax'))
  </code></pre>
  <p>I compile the model using the following options:</p>
  <pre><code>model.compile(Adam(0.0001), 'sparse_categorical_crossentropy', metrics=['accuracy'])
  </code></pre>
  <p>The problem comes here:</p>
  <pre><code>X_train.shape # (60, 120, 120, 3) -&gt; 60 frames, 120x120 height and width, 3 rgb channels
  X_target.shape # (60,) -&gt; each of the 60 frames have an integer label
  model.fit(X_train, x_target, epochs=20) -&gt; gives error, expected ndim=5, found ndim=4. Full shape received: (None, 120, 120, 3)
  </code></pre>
  <p>This is how I understand it. I defined the input shape to be (60, 120, 120, 3), and it adds a batch size to it internally, making it 5D. Then, when fitting to the model, it needs a 5D shape, but the shape of X_train is 4D. I tried np.expand_dims(X_train, axis=0) to make it 5D, did not work, and explored a lot on the internet. Please help.</p>