hugo
hugo

Reputation: 475

Stack CNN with GRU for image

I am trying to stack CNN 2D with GRU. I have success to get a way to stack the CNN but I have an error GRU.

Here is my code :

model = Sequential()


#model.add(Dropout(0.25))


model.add(Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', 
                 activation ='relu', input_shape = (35,152,1)))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', 
                 activation ='relu'))


model.add(GRU(50,return_sequences=True))
model.add(layers.Dense(nb_labels))

model.summary()

model.compile(optimizer=RMSprop(), loss='mae')

history = model.fit(x_train_pad, y_train_pad,
          batch_size=batch_size,
          epochs=100,
          shuffle=True,
          verbose=2,
          validation_data=(x_test_pad, y_test_pad), callbacks=[TQDMNotebookCallback(leave_inner=True, leave_outer=True)]) #callbacks=[TQDMNotebookCallback()]

Here is my error:

Anaconda3\lib\site-packages\keras\engine\base_layer.py in assert_input_compatibility(self, inputs)
    309                                      self.name + ': expected ndim=' +
    310                                      str(spec.ndim) + ', found ndim=' +
--> 311                                      str(K.ndim(x)))
    312             if spec.max_ndim is not None:
    313                 ndim = K.ndim(x)

ValueError: Input 0 is incompatible with layer gru_16: expected ndim=3, found ndim=4

Thanks in advance for your helps

Upvotes: 1

Views: 482

Answers (1)

Baptiste Pouthier
Baptiste Pouthier

Reputation: 572

As the ValueError explains, the GRU layer is waiting for a 3-dimensional input but the output of your last Conv2D is of dimension 4 (In your case the dimension is:(None, 17, 76, 64)). Depending on what you need to do, you need to reduce the dimension shape before feeding the GRU layer. For example you can use pooling techniques.

Upvotes: 1

Related Questions