0

I know that similar questions were asked before, but the solutions didn't helped me.

I have the following model:

        model = Sequential()

        # CNN
        model.add(Conv2D(filters=16, kernel_size=2, input_shape=(40, 2000, 1), activation='relu'))
        model.add(MaxPooling2D(pool_size=2))
        model.add(Dropout(0.2))

        # CNN
        model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
        model.add(MaxPooling2D(pool_size=2))
        model.add(Dropout(0.2))

        # CNN
        model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
        model.add(MaxPooling2D(pool_size=2))
        model.add(Dropout(0.2))

        # CNN
        model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
        model.add(MaxPooling2D(pool_size=2))
        model.add(Dropout(0.2))
        model.add(GlobalAveragePooling2D())
        model.add(Dense(num_labels, activation='softmax'))

        optimizer = optimizers.SGD(lr=0.002, decay=1e-6, momentum=0.9, nesterov=True)
        model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

I'm trying to fit the model:

model.fit(X_train, y_train_hot, batch_size=10, epochs=50,
                            validation_data=(X_test, y_test_hot))

where

X_train.shape = {tuple:3} (246, 40, 2000)

from other post I read (Keras input_shape for conv2d and manually loaded images) it seems that my input is right.

But I'm getting the following error:

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 40, 2000]

What am I missing ? and how can I fix it ?

Boom
  • 1,145
  • 18
  • 44

2 Answers2

0

As you saw from the link you posted you must reshape your data to be 4dim.

X_train = X_train.reshape(246, 40, 2000, 1)

or

X_train = X_train.reshape(-1, 40, 2000, 1)

4D: [batch_size, img_height, img_width, number_of_channels]

George
  • 5,808
  • 15
  • 83
  • 160
0

The error is that you did not include the 4-th axis(i.e. axis=3 or axis -1 for that matter).

Here, you can see the following:

expected min_ndim=4, found ndim=3. Full shape received: [None, 40, 2000]

The None translates to the batch size, which of course is variable and set to None.

Then, you have 40, 2000, which should correspond to height and width respectively.

At the same time, remember that you wrote in your code that the input shape your network expects is input_shape=(40, 2000, 1) not (40,2000).

You need to explicitly add the "color"/"number of channels" axis, the 3-rd channel in this case, so you need to either use reshape or expand_dims to achieve that.

For demonstrative purposes, suppose that X is of shape (40,2000), then reshape X to X = X.reshape(40,2000,1)

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59