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?