提问者:小点点

添加GlobalAveragePooling2D(ResNet50之前)


我试图做一个模型使用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的有效输入形状。我怎样才能解决这个问题?谢谢!:)


共1个答案

匿名用户

首先,您应该了解GlobalAverage池化实际上是做什么的。这个层不能在输入后立即被激活,因为它只会给出每个通道的所有图像的最大值(在你的情况下是3个值,因为你有3个通道)。

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