Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 36864, but received input with shape (190,1536)

I am trying to create a model for image classification for grayscale images, and I got the error: ValueError: Exception encountered when calling Sequential.call().

Input 0 of layer “dense” is incompatible with the layer: expected axis -1 of input shape to have value 36864, but received input with shape (190, 1536) My model:

model = Sequential([
    layers.Rescaling(1./255, input_shape=(190, 190, 1),
    layers.Conv2D(16, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Conv2D(32, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Conv2D(64, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes)
    ])

    model.compile(optimizer='adam',
                loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                metrics=['accuracy'])

I load the numpy array of grayscale images in the .npy files and turn it into train_ds to pass to the model when training with this code:

     train_img = train_img[:, :, :, np.newaxis]
     train_labels = np.full(train_img.shape[0], i+1)
     train_ds = tf.data.Dataset.from_tensor_slices((train_img, train_labels))

]
When I do train_img.shape, the shape of the input is (5055, 190, 190, 1)

Do model.summary() shows me this:

I tried to remove 1 pair of “layers.Conv2D(64, (3, 3), padding=‘same’, activation=‘relu’), layers.MaxPooling2D((2,2), padding=‘same’)”, and still got the same wrong input of shape (190, 1536) (full message: ValueError: Exception encountered when calling Sequential.call(). Input 0 of layer “dense” is incompatible with the layer: expected axis -1 of input shape to have value 73728, but received input with shape (190, 1536)) which to me is strange because at least the wrong input shape should be different if I remove those layers.
I am sure that the input shape is correct.

Can anyone please help me how to fix this?

1 Like

Which OS, Python, Tensorflow and Keras versions are you using?

For example, this runs fine in collab:

!pip install keras==3.4.1
from keras.models import Sequential
from keras import layers
import numpy as np

samples = 5055
num_classes=26

imgs = np.random.randint(low=0, high=255, size=(samples, 190, 190, 1))
labels = np.random.randint(low=0, high=25, size=(samples,1))

model = Sequential([
    layers.Rescaling(1./255, input_shape=(190, 190, 1)),
    layers.Conv2D(16, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Conv2D(32, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Conv2D(64, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes)
    ])
model.summary()
model.compile(optimizer="adam",loss="categorical_crossentropy")
model.fit(x=imgs, y=labels)

I am using Windows 11, the latest version of TensorFlow and Keras, and using python version 3.9. I tried to run your code on Google Collab but got the error: ValueError: Arguments target and output must have the same shape. Received: target.shape=(None, 1), output.shape=(None, 26). I am new to google collab.
I installed tensorflow by doing pip install tensorflow
I am really lost.

@wiln

Don’t worry, it’s difficult at first. That’s why I suggested a collab so that it’s a live example to build on.

Here is a working version:

!pip install keras==3.4.1
from keras.models import Sequential
from keras import layers
import numpy as np

samples = 5055
num_classes=26

imgs = np.random.randint(low=0, high=255, size=(samples, 190, 190, 1))
labels = np.random.randint(low=0, high=25, size=(samples,1))

model = Sequential([
    layers.Input((190, 190, 1)),
    layers.Rescaling(1./255),
    layers.Conv2D(16, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Conv2D(32, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Conv2D(64, 3, padding='same', activation='relu'),
    layers.MaxPooling2D((2,2), padding='same'),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes, activation='softmax')
    ])
model.summary()
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy")
model.fit(x=imgs, y=labels, epochs=10