0
votes
  re_size = [cv2.resize(img, (50,50), interpolation=cv2.INTER_LINEAR) for img in 
  read_images]

  X = np.array(read_images)
  df = pd.read_csv('pth to csv file ')
  y = df['label']   

  X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)

  model = Sequential()

  model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=(897,50,50)))

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


  model.add(Dense(64, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(10, activation='softmax'))

  model.add(Flatten())

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

  model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test)) 

`

This is my error

'at this line'---> 15 model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test)) Error when checking input: expected conv2d_10_input to have 4 dimensions, but got array with shape (817, 450, 600)

What does it mean and how can I fix it

1

1 Answers

0
votes

Per your code, the model required input will be (32, 897,50,50) where 32 is the default batch_size. But training input is (817, 450, 600) per the error massage.

There are 2 problems here :

  • you set X with the original images instead of the resized ones. And I guess you have totally 897 images and split by 8:2 which is why the error message saying the actually input is (817, 450, 600) where 817=897*0.8, 450,600 is the original image size.

    X = np.array(read_images)
  • your image seems only have 1 channel, so the input shape should actually be (50,50) instead of (897,50,50) as 897 is your total training image number, not the channel of each image.

    model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=(50,50)))

As a summary, you should use the resized images as training data and the input shape should be the shape of each training sample, no need to add the number of total training samples.