添加宽度,高度时的CNN数据扩充错误

CNN data augmentation error when adding width, height

我正在尝试按照 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() and/or RandomWidth(),我在创建模型时收到以下错误:

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

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

您可以检查 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 的行为有点不同。

差异。