CNN + LSTM implementation error for image classification

Viewed 24

I am trying to implement a CNN network + LSTM to be able to predict 4 different classes based on the sequence of x-ray images, which were preprocessed to 150x150x3 shape. My X-train shape is (4067, 150, 150, 3). When I am executing the code model.fit(), i am getting the error.

# x_train = np.reshape(x_train, (4067, 150, 150, 3))
# y_train = np.reshape(y_train, (4067, 4))

model = Sequential()
model.add(TimeDistributed(Conv2D(filters = 32, 
                                 kernel_size=(3,3), 
                                 padding='same', 
                                 activation = 'relu'), 
                          input_shape=(None, 150, 150, 3)))
model.add(TimeDistributed(AveragePooling2D()))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dense(24, activation='relu',name='output'))
model.add(Dense(4, activation = 'softmax'))

from tensorflow.keras.optimizers import Adam

optimizer = Adam(lr=0.001)
model.compile(optimizer = optimizer, 
                       loss = 'categorical_crossentropy',
                       metrics=['accuracy'])
from tensorflow.keras.callbacks import ReduceLROnPlateau

reduce_lr = ReduceLROnPlateau(monitor = 'val_accuracy',
                            factor = 0.3, 
                            patience = 2,
                            min_delta = 0.001,
                            mode = 'auto',
                            verbose = 1)

hist_cnn_lstm = model.fit(x_train, y_train, batch_size=64, epochs=15,
                                   validation_data = (x_valid, y_valid),
                          callbacks=reduce_lr
                         )

ERROR:

Epoch 1/15
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-3ec61fbabcf1> in <module>()
      1 hist_cnn_lstm = model.fit(x_train, y_train, batch_size=64, epochs=15,
      2                                    validation_data = (x_valid, y_valid),
----> 3                           callbacks=reduce_lr
      4                          )

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is 

    ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, None, 150, 150, 3), found shape=(None, 150, 150, 3)
0 Answers
Related