MRM
MRM

Reputation: 1179

Error when checking target: expected conv2d to have 4 dimensions, but got array with shape

I have built a Keras ConvLSTM neural network, and I want to predict one frame ahead based on a sequence of 10-time steps:

model = Sequential()
model.add(ConvLSTM2D(filters=128, kernel_size=(3, 3),
                   input_shape=(None, img_size, img_size, Channels),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())

model.add(ConvLSTM2D(filters=64, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())

model.add(ConvLSTM2D(filters=64, kernel_size=(3, 3),
                   padding='same', return_sequences=False))
model.add(BatchNormalization())


model.add(Conv2D(filters=1, kernel_size=(3, 3),
               activation='sigmoid',
               padding='same', data_format='channels_last', name='conv2d'))


model.compile(loss='binary_crossentropy', optimizer='adadelta')

Training:

data_train_x:(10, 10, 62, 62, 12)
data_train_y:(10, 1, 62, 62, 1)


model.fit(data_train_x, data_train_y, batch_size=10, epochs=1, 
validation_split=0.05)

But I get the following error:

ValueError: Error when checking target: expected conv2d to have 4 dimensions, but got array with shape (10, 1, 62, 62, 1)

And this is the results of 'model.summary()':

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv_lst_m2d_4 (ConvLSTM2D)  (None, None, 62, 62, 128) 645632    
_________________________________________________________________
batch_normalization_3 (Batch (None, None, 62, 62, 128) 512       
_________________________________________________________________
conv_lst_m2d_5 (ConvLSTM2D)  (None, None, 62, 62, 64)  442624    
_________________________________________________________________
batch_normalization_4 (Batch (None, None, 62, 62, 64)  256       
_________________________________________________________________
conv_lst_m2d_6 (ConvLSTM2D)  (None, 62, 62, 64)        295168    
_________________________________________________________________
batch_normalization_5 (Batch (None, 62, 62, 64)        256       
_________________________________________________________________
conv2d (Conv2D)              (None, 62, 62, 1)         577       
=================================================================
Total params: 1,385,025
Trainable params: 1,384,513
Non-trainable params: 512
_________________________________________________________________

This model is a revised version of another model which was compiled without error, what is changed from the previous model is just the last two layers. Previously was like:

model.add(ConvLSTM2D(filters=64, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())


model.add(Conv3D(filters=1, kernel_size=(3, 3, 3),
               activation='sigmoid',
               padding='same', data_format='channels_last', name='conv3d'))

I made this change because I want to get a 4-dimensional output of the form (samples, output_row, output_col, filters)

Upvotes: 1

Views: 1069

Answers (1)

Mitiku
Mitiku

Reputation: 5412

The error message is clear. The model expects the output rank to be four, but you are passing output of rank 5. Squeeze the second dimension of data_train_y before feeding it to the model.

data_train_y = tf.squeeze(data_train_y, axis=1)

Upvotes: 2

Related Questions