Error when checking model input: expected convolution2d_input_1 to have 4 dimensions, but got array with shape (32, 32, 3)

4,070 views
Skip to first unread message

Ralph Fehrer

unread,
Jan 10, 2017, 3:08:21 AM1/10/17
to Keras-users

I want to train a deep network starting with the following layer:


model = Sequential()
model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3)))


using


history = model.fit_generator(get_training_data(),
                samples_per_epoch=1, nb_epoch=1,nb_val_samples=5,
                verbose=1,validation_data=get_validation_data()


with the following generator:


def get_training_data(self):
     while 1:
        for i in range(1,5):
            image = self.X_train[i]
            label = self.Y_train[i]
            yield (image,label)


(validation generator looks similar).


During training, I get the error:


Error when checking model input: expected convolution2d_input_1 to have 4 
dimensions, but got array with shape (32, 32, 3)


How can that be, with a first layer


 model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3)))


?

Benjamin Striner

unread,
Jan 17, 2017, 2:02:51 AM1/17/17
to Keras-users
input_shape=(32, 32, 3) means the input shape should be (batch_size, 32, 32, 3). Your generator should yield batches. So if you have 300 examples, make your generator return items in shape (30,32,32,3) and it will run 10 batches of 30 items.

Cheers
Reply all
Reply to author
Forward
0 new messages