# Error in Convolutional Neural network for input shape

I have 1000, 28*28 resolution images. I converted those 1000 images into numpy array and formed a new array with size of (1000,28,28). So, while creating convolution layer using Keras, input shape(X value) is specified as (1000,28,28) and output shape(Y value) as (1000,10). Because I have 1000 examples as inputs and 10 categories of output.

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=(1000,28,28)))
.
.
.
model.fit(train_x,train_y,batch_size=32,epochs=10,verbose=1)


So, while using fit function, it shows ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1000, 28, 28) as an error. How can I provide proper input and output dimension for CNN.

Code:

 model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=(4132,28,28)))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Dropout(0.4))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adam(),metrics=['accuracy'])
model.summary()

train_x  = numpy.array([train_x])

model.fit(train_x,train_y,batch_size=32,epochs=10,verbose=1)


## 1 Answer

The convolution operation used in CNNs are batch operations. The customary inputs for these networks, at least for Keras and Tensorflow, are channels last and channels first. The first dimension specifies the number of data in your batch, the second and third can be height and width if you choose channels-last and the last one will be the number of channels of your input, for instance the number of channels for grey-scaled images is one whilst for coloured images is three. If you use channels first, the first dimension would be the number of data in your batch, the second would be the number of channels and finally, the third and fourth dimensions would specify the number of height and width respectively.

For your case, it seems you are using MNIST data-set. What you need to do is just reshaping your input images to one of the standard mentioned manners. You can resize your input to (1000,28,28, 1).