Hakan Akgün
Hakan Akgün

Reputation: 927

My Input shape is correct but I stil l get the following err.: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4,

This is my model:

def make_model():
model = Sequential()

model.add(Conv2D(kernel_size=(3, 3), filters=16, input_shape=(32, 32,1), padding='same'))
model.add(LeakyReLU(0.1))

model.add(Conv2D(kernel_size=(3, 3), filters=32, padding='same'))
model.add(LeakyReLU(0.1))

model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(kernel_size=(3, 3), filters=32, padding='same'))
model.add(LeakyReLU(0.1))

model.add(Conv2D(kernel_size=(3, 3), filters=64, padding='same'))
model.add(LeakyReLU(0.1))

model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(256))
model.add(LeakyReLU(0.1))
model.add(Dropout(0.5))

model.add(Dense(10))
model.add(Activation('softmax'))
return model

Compile part:

INIT_LR = 5e-3  # initial learning rate
BATCH_SIZE = 32
EPOCHS = 10
from tensorflow.keras import backend as K
K.clear_session()
model = make_model()
model.compile(
loss='categorical_crossentropy',  # we train 10-way classification
optimizer=tf.keras.optimizers.Adamax(lr=INIT_LR),  # for SGD
metrics=['accuracy']  # report accuracy during training
)

def lr_scheduler(epoch):
    return INIT_LR * 0.9 ** epoch

# callback for printing of actual learning rate used by optimizer
class LrHistory(keras.callbacks.Callback):
    def on_epoch_begin(self, epoch, logs={}):
        print("Learning rate:", K.get_value(model.optimizer.lr))

Fitting:

 model.fit(
X_train.reshape(-1, 32, 32, 1), y_train,  # prepared data
batch_size=BATCH_SIZE,
epochs=EPOCHS,
callbacks=[keras.callbacks.LearningRateScheduler(lr_scheduler), 
           LrHistory(), 
           tfa.callbacks.TQDMProgressBar() ],
validation_data=(X_test, y_test),
shuffle=True,
verbose=0,
initial_epoch=None or 0

)

My Data_trainX shape: enter image description here

My Data_trainy shape enter image description here

My input shape is compatible with models Conv2D layer's input shape. I've looked at other questions about this applied those solutions, but it didn't work. It seems everything correct to me. Where am I doing wrong?

Upvotes: 0

Views: 47

Answers (1)

afsharov
afsharov

Reputation: 5174

While you are reshaping the training data X_train to fit the model specifications, you are not doing this with the validation data X_test.

Reshape X_test as well and it should work fine:

model.fit(
    X_train.reshape(-1, 32, 32, 1), y_train,
    ...
    validation_data=(X_test.reshape(-1, 32, 32, 1), y_test),  # <-- apply changes here
    ...
)

Upvotes: 2

Related Questions