【问题标题】:ValueError: Input to `.fit()` should have rank 4. Got array with shape in CNN?ValueError: `.fit()` 的输入应该有 4 级。在 CNN 中有形状的数组吗?
【发布时间】:2020-07-09 05:48:39
【问题描述】:

我致力于在我的数据集中实现 CNN。

这是我的代码,通过重塑过程获得 x 训练和 y 训练

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)

在我完成一些程序和重塑过程之后,我将 X_train 和 Y_train 分开。这是下面显示的代码。

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)

结果定义如下。

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

然后我创建 CNN 模型。

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"))

然后我使用 ImageGenerator 来使用数据增强

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)

然后它会抛出如下所示的错误。

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

我该如何解决?

【问题讨论】:

    标签: python pandas conv-neural-network


    【解决方案1】:

    我认为问题在于ImageDataGenerator 期望图像具有宽度、高度和颜色通道(最常见的是红色、绿色和蓝色的 3 个通道)。由于还有一个批量大小,它期望的整体形状是(batch size, width, height, channels)。您的张量是 260x260,但没有颜色通道。它们是灰度图像吗?

    the documentation:

    x:样本数据。应该有等级 4。在灰度数据的情况下, 通道轴的值应为 1

    所以我认为你只需要重塑你的输入,在最后添加一个额外的维度。

    【讨论】:

    • 我编辑了我的帖子。我怎样才能在最后添加额外的维度?
    • 在您的代码中,您调用的是.reshape(-1, 260, 260)。只需添加尺寸为 1 的最终尺寸:.reshape(-1, 260, 260, 1)
    • 在定义 datagen.fit 之前,我写了这段代码X_train = np.pad(X_train, ((0,0), (0, (67600-X_train.shape[1]))), 'constant').reshape(-1, 260, 260, 1),但它不起作用。
    • 我需要更多信息。错误信息是否相同?您在reshape 调用或ImageDataGenerator.fit 调用时是否收到错误?是否有可能你重塑了你的训练而不是你的验证数据并且错误出现在验证数据上?
    • 这篇文章有点混乱。看来您现在已经填充了 X_train 两次。你做了一次,然后reshaped(-1, 260, 260) 但看起来你第二次填充和重塑它。我也不知道哪一行给了你这个错误。是来自fit 电话、reshape 电话还是完全其他的电话?
    猜你喜欢
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2023-02-01
    • 2021-07-05
    • 1970-01-01
    相关资源
    最近更新 更多