【问题标题】:how to change input_shape to one channel like (64,64,1) from my code?如何从我的代码中将 input_shape 更改为一个通道,例如 (64,64,1)?
【发布时间】:2020-01-02 05:58:09
【问题描述】:

所以我有一个灰度图像形式的数据集,这意味着一个通道对吗?

我从这段代码中将 input_shape 更改为 64.64.1

classifier.add(ZeroPadding2D(padding=(2,2), input_shape = (64, 64, 1)))

但我有错误

Error when checking input: expected zero_padding2d_1_input to have shape (64, 64, 1) but got array with shape (64, 64, 3)

这有什么问题,如何使用一个通道 input_shape (64,64,1) 进行训练?

你可以在Why my acc always higher but my val_acc is very small?上看到完整的代码

【问题讨论】:

  • 在原始代码的第一层,您的 input_shape 为 (64,64,3) 。如果您想将其训练为 (64,64,1) 将您的图像转换为灰度并变为 (64,64) 然后在 -1 处扩展暗淡,因此变为 (64,64,1)
  • 谢谢回复,能否提供将文件夹中所有图像更改为灰度的代码?我已经搜索过,但它只能转换一张图像。我真的需要这个
  • 你可以试试 imagemagick - stackoverflow.com/questions/53262968/…
  • 我试试这个代码(结束页)stackoverflow.com/questions/47087528/…。它可以将我所有的图像转换为灰度。但是当我尝试再次运行我的 cnn 模型时,它仍然给出错误 ValueError: Error when checks input: expected zero_padding2d_1_input to have shape (64, 64, 1) but got array with shape (64, 64, 3)
  • 太好了,非常感谢兄弟 :)

标签: python machine-learning keras deep-learning


【解决方案1】:

主要原因是您所有的图像都是 RGB 并且默认情况下被 keras flow 从目录读取为 RGB 图像。所以设置 flow_from_directory 来读取你的图像为灰度,并将第一层的 input_shape 设置为 (64,64,1)

flow_from_directory 的代码示例

 train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary',
                                                 color_mode = "grayscale")

)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2020-09-13
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多