Error with the input_shape expected to have 4 dimensions, but got array with shape (73257, 32, 32)

0

I have greyscale images of this shape: x_train_grey.shape = (73257, 32, 32)

I specify the first layer like this:

Flatten(input_shape=(32,32,1)' Because I don't pass the batch_size and the greyscale images have only 1 channel. But I get this error:

ValueError: Error when checking input: expected flatten_1_input to have 4 dimensions, but got an array with shape (73257, 32, 32)

I don't understand what is wrong, please help. I understand this has been asked many times, but I cannot find a solution.

Cheers!

2

1

The problem probably lies in the way you are passing your data to your model. If your input shape is (batch_size, 32, 32) then try something like this:

import tensorflow as tf

grey_scale_images = tf.random.normal((64, 32, 32))

model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(32,32,1)))

print(model(grey_scale_images).shape)
# (64, 1024)

Update: Both input_shape=(32,32,1) and input_shape=(32,32) will work. It depends how you are feeding your data to your model:

import tensorflow as tf

grey_scale_images = tf.random.normal((64, 32, 32))
Y = tf.random.normal((64, 1024))
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(32, 32, 1)))
model.compile(loss='MSE')
model.fit(grey_scale_images, Y)
2021-11-24 08:40:01

Actually, simply passing input_shape=(32,32) has worked, thanks!
mankojag

I think you are mistaken...Check my updated answer.
AloneTogether

Thanks but I am not quite sure this is right. I tried input_shape=(32,32,1) but it's not working. What do you suggest needs to be done before passing that instead of input_shape=(32,32)? Is there some rule that says (32,32,1) should be used over (32,32) or does it not matter, as you say both options work?
mankojag

Check the bottom of my answer. As I said both work but it depends on what your data looks like
AloneTogether
-1

I'm still learning this stuff too but I would guess that "1" as a dimension's number of entries isn't possible. Even if it is possible, it's a start. "1" as a size of an axis doesn't make sense to me. Anyone else?

2021-11-24 07:34:08

Thanks, indeed input_shape=(32,32) seems to be working fine
mankojag

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................