【问题标题】:Add GlobalAveragePooling2D (before ResNet50)添加 GlobalAveragePooling2D(在 ResNet50 之前)
【发布时间】:2020-12-02 10:31:37
【问题描述】:

我正在尝试使用 ResNet50 将图像分类为 6 个类别的模型,并且我想在使用它们训练 ResNet50 模型之前减少图像的维度。为此,我开始使用 keras 中的模型创建 ResNet50 模型:

ResNet = ResNet50(
    include_top= None, weights='imagenet', input_tensor=None, input_shape=([64, 109, 3]),
    pooling=None, classes=6)

然后我创建了一个包含 ResNet50 的顺序模型,但在使用 ResNet50 之前添加了一些用于分类的最终层以及用于降维的第一层: (关于输入形状:我使用的图像尺寸为 128x217,3 用于 ResNet 需要的通道)

model = models.Sequential()
model.add(GlobalAveragePooling2D(input_shape = ([128, 217, 3])))
model.add(ResNet)
model.add(GlobalAveragePooling2D())
model.add(Dense(units=512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=6, activation='softmax'))

但这不起作用,因为第一次全局平均池化后的维度与 Resnet 中的输入形状不匹配,我得到的错误是:

WARNING:tensorflow:Model was constructed with shape (None, 64, 109, 3) for input Tensor("input_6:0", shape=(None, 64, 109, 3), dtype=float32), but it was called on an input with incompatible shape (None, 3).
ValueError: Input 0 of layer conv1_pad is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 3]

我想我明白问题出在哪里,但我不知道如何解决它,因为 (None, 3) 不是 ResNet50 的有效输入形状。我怎样才能解决这个问题?谢谢!:)

【问题讨论】:

    标签: python tensorflow keras neural-network conv-neural-network


    【解决方案1】:

    您应该首先了解 GlobalAveragePooling 的实际作用。该层不能在输入后立即应用,因为它只会给出每个通道的所有图像的最大值(在您的情况下为 3 个值,因为您有 3 个通道)。

    您必须使用另一种方法来减小图像的大小(例如简单地转换为更小的尺寸。

    【讨论】:

    • 感谢您的回答!我没有意识到 GlobalAveragePooling 实际上在做什么,我使用 AveragePooling(不是全局的)解决了它。
    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 2021-01-22
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    相关资源
    最近更新 更多