【问题标题】:CNN data augmentation error when adding width, height添加宽度、高度时的 CNN 数据增强错误
【发布时间】:2021-06-03 18:07:30
【问题描述】:

我正在尝试按照 tensorflow 文档中提到的以下方式对二值图像分类问题应用数据增强:https://www.tensorflow.org/tutorials/images/classification#data_augmentation

我的模型是这样的:

Sequential([
  data_augmentation,
  layers.experimental.preprocessing.Rescaling(1./255),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Dropout(0.2),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Dropout(0.2),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dropout(0.5),
  layers.Dense(1, activation='sigmoid')
])

当我的数据增强层是这样的时候,模型编译没有错误:

data_augmentation = keras.Sequential(
  [
    layers.experimental.preprocessing.RandomFlip("horizontal", 
                                                 input_shape=(150, 
                                                              150,
                                                              3)),
    layers.experimental.preprocessing.RandomRotation(0.2),
    layers.experimental.preprocessing.RandomZoom(0.2)
  ]
)

如果我尝试在我的扩充层中引入RandomHeight() 和/或RandomWidth(),我会在创建模型时收到以下错误:

ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.

知道为什么会发生这种情况以及如何解决吗?

【问题讨论】:

    标签: python tensorflow keras artificial-intelligence


    【解决方案1】:

    您可以检查 RandomWidth-Height 输出的形状。Source code of RandomWidth class:

     return tensor_shape.TensorShape(
            [input_shape[0], None, input_shape[2], input_shape[3]])
    

    假设我使用RandomHeight 作为第一层,input_shape 作为 150 x 150 RGB 图像。我们可以通过以下方式确认输出形状:

    data_augmentation.summary()
    Layer (type)                 Output Shape              Param #   
    =================================================================
    random_height_2 (RandomHeigh (None, None, 150, 3)      0         
    _________________________________________________________________
    random_flip_2 (RandomFlip)   (None, None, 150, 3)      0         
    _________________________________________________________________
    random_rotation_2 (RandomRot (None, None, 150, 3)      0         
    _________________________________________________________________
    random_zoom_2 (RandomZoom)   (None, None, 150, 3)      0         
    

    当你像这样使用它并且如果你在没有密集层的情况下编译你的模型,你会在模型摘要中看到:

    dropout_6 (Dropout)          (None, None, 18, 64)      0         
    _________________________________________________________________
    flatten_6 (Flatten)          (None, None)              0         
    

    (None,None) 导致此处出现错误。您可以使用tf.keras.layers.GlobalMaxPooling2D() 而不是Flatten() 来解决它

    虽然这解决了由Flatten() 层引起的维度问题,但GlobalMaxPooling2D 的行为略有不同。

    You can check this question 表示差异。

    【讨论】:

    • 虽然它实际上解决了维度问题,但GlobalMaxPooling2D 的行为与Flatten 完全不同,您可能至少需要链接到该问题:stackoverflow.com/questions/49295311/…
    • 是的,我知道,我应该提到它,谢谢@Lescurel
    • 那么如果我在模型中包含高度/宽度增强,就没有办法保留Flatten 了吗?通过ImageDataGenerator 进行增强有点令人失望,模型架构对模型架构的挑剔程度要低得多,猜想这就是为什么这些增强仍在 tensorflow 中的experimental 库下的原因。
    • 我猜不,展平会导致尺寸错误。但也许您可以使用子类化创建您自己的增强层和 tf.image。
    猜你喜欢
    • 2016-07-12
    • 2012-06-15
    • 1970-01-01
    • 2019-03-05
    • 2015-01-26
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多