Using keras to construct CNN to do the problem of multi-classification of pictures, on the dimension of label vectors

The structure of the

network is as follows:

-sharp1st layer
model.add(Conv2D(20, 5, 5, activation="sigmoid", input_shape=(32, 32, 1)))
model.add(MaxPool2D(pool_size=(2, 2)))
-sharp2nd layer
model.add(Conv2D(30, 5, 5, activation="sigmoid"))
model.add(MaxPool2D(pool_size=(2, 2)))
-sharpfully connected layer
model.add(Flatten())
model.add(Dense(500, init="normal", activation="sigmoid"))
-sharp model.add(Dropout(0.5))
-sharp model.add(Dense(10, activation="sigmoid"))
-sharpmodel.add(Reshape((-1, 5)))
model.add(Dense(5, init="normal", activation="softmax"))

when calling the network:

model.fit(x=traindata, y=trainlabel, batch_size=16, nb_epoch=15, shuffle=True, verbose=1, validation_data=(testdata, testlabel))

this is the error report:
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (500,5)

then the x vector is 4-dimensional picture information, and the y vector is the label of the picture. Their shape is (500,32,32,1), (500,5), respectively. I just don"t understand why the y direction also needs to be 4-dimensional

.
Mar.26,2021

The

subject actually wants to ask "Why is the x vector also 4-dimensional" instead of y ? If I understand it correctly, then:

the problem lies in your traindata 's shape . You can check your traindata.shape () . The correct dimension should be (500 code 32) . If the entry is (500 quotation 32) , the error of the main description will be reported, and the input dimension does not match the definition dimension of the number of layers.

you can try to reproduce the error and randomly generate different shape matrices to try. traindata = numpy.random.rand (500,32,32) and traindata = numpy.random.rand (500,32,32,1) . You will find that the former will report an error.

in fact, every two-dimensional image has not only two dimensions, but also a color channel . The color picture has three rgb channels, so it is 3, and the black-and-white picture is 1. Your traindata should be (number of datasets, picture length pixels, picture width pixels, picture color channels) .

The

solution is to add a fourth dimension with a value of 1 for all traindata (or 3 for color images).

Menu