ValueError: Input to `.fit()` should have rank 4. Got array with shape in CNN?

I engaged in implementing CNN in my dataset.

Here is my code getting x train and y train with reshaping process

Y_train = train["Label"]
X_train = train.drop(labels = ["Label"],axis = 1) 
X_train.shape -> /*(230, 67500)*/
X_train = np.pad(X_train, ((0,0), (0, (67600-X_train.shape[1]))), 'constant').reshape(-1, 260, 260)
Y_train = to_categorical(Y_train, num_classes = 10)

After I have done some procedure and reshape process, I split X_train and Y_train. Here is the code shown below.

X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=42)
print("x_train shape",X_train.shape)
print("x_test shape",X_val.shape)
print("y_train shape",Y_train.shape)
print("y_test shape",Y_val.shape)

The result is defined below.

x_train shape (207, 260, 260)
x_test shape (23, 260, 260)
y_train shape (207, 10)
y_test shape (23, 10)

Then I create CNN Model.

model = Sequential()

#
model.add(Conv2D(filters = 8, kernel_size = (5,5),padding = 'Same', 
                 activation ='relu', input_shape = (260, 260)))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

#
model.add(Conv2D(filters = 16, kernel_size = (3,3),padding = 'Same', 
                 activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.25))

# fully connected
model.add(Flatten())
model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))

Then I use ImageGenerator to use data augumentation

datagen = ImageDataGenerator(
        featurewise_center=False,  # set input mean to 0 over the dataset
        samplewise_center=False,  # set each sample mean to 0
        featurewise_std_normalization=False,  # divide inputs by std of the dataset
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # dimesion reduction
        rotation_range=0.5,  # randomly rotate images in the range 5 degrees
        zoom_range = 0.5, # Randomly zoom image 5%
        width_shift_range=0.5,  # randomly shift images horizontally 5%
        height_shift_range=0.5,  # randomly shift images vertically 5%
        horizontal_flip=False,  # randomly flip images
        vertical_flip=False)  # randomly flip images

X_train = np.pad(X_train, ((0,0), (0, (67600-X_train.shape[1]))), 'constant').reshape(-1, 260, 260, 1)

datagen.fit(X_train)

Then it throws an error shown below.

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (3,2)

How can I fix it ?

Upvotes: 0

Views: 936

Answers (1)

Oliver Dain
Oliver Dain

Reputation: 9953

I think the issue is that ImageDataGenerator expects an image with has a width, height, and the color channels (the most common being 3 channels for red, green, and blue). Since there's also a batch size the overall shape it expects is (batch size, width, height, channels). Your tensors are 260x260 but don't have the color channels. Are they grayscale images?

Per the documentation:

x: Sample data. Should have rank 4. In case of grayscale data, the channels axis should have value 1

So I think you just need to reshape your input adding an extra dimension at the end.

Upvotes: 1

Related Questions