2

I don't have much experience in posting questions to Stack Overflow. Pardon my mistakes. I will try to be thorough.

I have two numpy arrays :

  1. X with shape (78300, 90, 90).
  2. y with shape (78300, 29)
  • X is an array of black and white images with height and width (90, 90).
  • y is the corresponding encoded class labels for X. (encoded as in y = tensorflow.keras.utils.to_categorical(labels) )

I am trying to train the following CNN on this data.

from tensorflow.keras import utils
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
from tensorflow.keras.callbacks import ModelCheckpoint

model = Sequential()

model.add(Conv2D(64, (3, 3), input_shape=(90, 90, 1), activation='relu'))
model.add(MaxPooling2D((2, 2)))

model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))

model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))

model.add(BatchNormalization())

model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(1024, activation='sigmoid'))
model.add(Dense(29, activation='softmax'))

I am receiving the following error on running

n_classes = 29
batch = 64
epochs = 5
learning_rate = 0.001

adam = Adam(lr=learning_rate)

model.compile(optimizer=adam,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

cp_callback = ModelCheckpoint(filepath=os.path.join("/output_dir", "result_folder"),
                              save_weights_only=True,
                              verbose=1)

history = model.fit(x,
                    y,
                    batch_size=batch,
                    epochs=epochs,
                    validation_split=0.1,
                    shuffle=True,verbose=1,
                   callbacks=[cp_callback])

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

I cannot figure out what is going wrong. Please help me. Also, if you could link a documentation/article/blog post/video that will help me understand the nuances of the input shape in layers, that will be very helpful.

the_it_weirdo
  • 23
  • 1
  • 4

1 Answers1

0

Shape should be (78300, 90, 90, 1).

Reshape x (add a dimension):

x = x[..., tf.newaxis]
Andrey
  • 5,932
  • 3
  • 17
  • 35