0
votes

Im trying to run a model with xtrain=(1221,50,50,1) at the time of model.fit its showing this error

ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (1221, 50, 50, 1)

im using these functions:

model.compile(loss=categorical_crossentropy,optimizer=Adadelta(lr=0.8), metrics=['acc'])
model.fit(x=ZZ, y=yyy, batch_size=128, epochs=1, validation_split=0.2)

when i increase the dimensions to (1221,50,50,1,1) using expand_dims im getting this error:

ValueError: Error when checking input: expected input_1 to have shape (16, 50, 50, 1) but got array with shape (1221,50, 50, 1, 1)

Idk where im getting wrong

this is my model

input_layer = Input((16, 50, 50, 1))
## convolutional layers
conv_layer1 = Conv3D(filters=8, kernel_size=(3, 3, 3), activation='relu')(input_layer)
conv_layer2 = Conv3D(filters=16, kernel_size=(3, 3, 3), activation='relu')(conv_layer1)

## add max pooling to obtain the most imformatic features
pooling_layer1 = MaxPool3D(pool_size=(2, 2, 2))(conv_layer2)

conv_layer3 = Conv3D(filters=32, kernel_size=(3, 3, 3), activation='relu')(pooling_layer1)
conv_layer4 = Conv3D(filters=64, kernel_size=(3, 3, 3), activation='relu')(conv_layer3)
pooling_layer2 = MaxPool3D(pool_size=(2, 2, 2))(conv_layer4)

## perform batch normalization on the convolution outputs before feeding it to MLP architecture
pooling_layer2 = BatchNormalization()(pooling_layer2)
flatten_layer = Flatten()(pooling_layer2)

## create an MLP architecture with dense layers : 4096 -> 512 -> 10
## add dropouts to avoid overfitting / perform regularization
dense_layer1 = Dense(units=2048, activation='relu')(flatten_layer)
dense_layer1 = Dropout(0.4)(dense_layer1)
dense_layer2 = Dense(units=512, activation='relu')(dense_layer1)
dense_layer2 = Dropout(0.4)(dense_layer2)
output_layer = Dense(units=6, activation='softmax')(dense_layer2) #Use 5 instead of 1

## define the model with input layer and output layer
model = Model(inputs=input_layer, outputs=output_layer)```
3
please share the code where you defined your model layers. - Rishabh Sahrawat
Can you provide the model definition, the error probably lays there - Orphee Faucoz
Please show the whole model. - MH304
I have shown the model @RishabhSahrawat - Rben10 senpai
model @faucozo ,MH394 - Rben10 senpai

3 Answers

0
votes
input_layer = Input((16, 50, 50, 1))

This line specify that an input has a shape of (16, 50, 50 ,1). You're passing to the network the whole (1221, 50, 50, 1)vector. You need to reshape to (X, 16, 50, 50, 1) so that an element of the vector has the shape desired (16, 50, 50, 1).

0
votes

The problem is in your xtrain, it consists 1221 images of shape (50,50,1) however, the Conv3D is expecting images of shape (16,50,50,1).

In case you are confused, the input_layer to Conv2D doesn't require the total number of images value. It only requires the shape of the image coming to it.

For your help, you can look at this example how the images are being passed and set.

0
votes

I get what mistake I did my x_train should have consist of (X,16,50,50,1) but the 16 was not there. this is due to an error in which the input was taken.

Thank you for your help Rishabh Sahrawat Orphee Faucoz MH304