Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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))) 

?

like image 298
user1934212 Avatar asked Jan 10 '17 07:01

user1934212


1 Answers

The input shape you have defined is the shape of a single sample. The model itself expects some array of samples as input (even if its an array of length 1).

Your output really should be 4-d, with the 1st dimension to enumerate the samples. i.e. for a single image you should return a shape of (1, 32, 32, 3).

You can find more information here under "Convolution2D"/"Input shape"

Edit: Based on Danny's comment below, if you want a batch size of 1, you can add the missing dimension using this:

image = np.expand_dims(image, axis=0) 
like image 153
ginge Avatar answered Sep 21 '22 15:09

ginge