megashigger
megashigger

Reputation: 9043

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (120, 1)

When I print(inp_shape) I get (288, 512, 3). However I still get the

ValueError: Error when checking input: expected conv2d_1_input to have 4 
dimensions, but got array with shape (120, 1)

I don't understand where the shape (120, 1) comes from.

dropout_prob = 0.2
activation_function = 'relu'
loss_function = 'categorical_crossentropy'
verbose_level = 1
convolutional_batches = 32
convolutional_epochs = 3
inp_shape = X_training.shape[1:]
num_classes = 2
opt = SGD()
opt2 = 'adam'

y_train_cat = np_utils.to_categorical(y_training, num_classes) 
y_test_cat = np_utils.to_categorical(y_testing, num_classes)

model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(3, 3), input_shape=inp_shape))
model.add(Conv2D(filters=32, kernel_size=(3, 3)))
#model.add(MaxPooling2D(pool_size = (2,2)))
#model.add(Dropout(rate=dropout_prob))
model.add(Flatten())
model.add(Dense(128,activation=activation_function))
#model.add(Dropout(rate=dropout_prob))
model.add(Dense(64,activation=activation_function))
#model.add(Dropout(rate=dropout_prob))
model.add(Dense(32,activation=activation_function))
model.add(Dense(num_classes,activation='softmax'))
model.summary()
model.compile(loss=loss_function, optimizer=opt, metrics=['accuracy'])
history = model.fit(X_training, y_train_cat, batch_size=convolutional_batches, epochs = convolutional_epochs, verbose = verbose_level, validation_data=(X_testing, y_test_cat))
model.save('../models/neural_net.h5')

Upvotes: 1

Views: 2515

Answers (1)

Mitiku
Mitiku

Reputation: 5412

add this line

X_training= tf.reshape(X_training,[-1,288, 512, 3])

before feeding X_training to the model.fit

Upvotes: 2

Related Questions