简体   繁体   English

添加 GlobalAveragePooling2D(在 ResNet50 之前)

[英]Add GlobalAveragePooling2D (before ResNet50)

I'm trying to do a model using ResNet50 for image classification into 6 classes and I want to reduce the dimension of the images before using them to train the ResNet50 model. To do this I start creating a ResNet50 model using the model in keras:我正在尝试使用 ResNet50 将图像分类为 model,将图像分类为 6 个类别,我想在使用它们训练 ResNet50 model 之前减小图像的维度。为此,我开始使用 model 在 88393938861 中创建一个 ResNet50 model:

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

And then I create a sequential model that includes ResNet50 but adding some final layers for the classification and also the first layer for dimensionality reduction before using ResNet50: (About the input shape: The images I'm using have a dimension of 128x217 and the 3 is for the channel that ResNet needs)然后我创建了一个包含 ResNet50 的顺序 model,但在使用 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'))

But this doesn't work because the dimension after the first global average pooling doesn't fit with the input shape in the Re.net, the error I get is:但这不起作用,因为第一次全局平均池化后的维度不适合 Re.net 中的输入形状,我得到的错误是:

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]

I think I understand what is the problem but I don't know how to fix it since (None, 3) is not a valid input shape for ResNet50.我想我明白问题出在哪里,但我不知道如何解决它,因为 (None, 3) 不是 ResNet50 的有效输入形状。 How can I fix this?我怎样才能解决这个问题? Thank you::)谢谢::)

You should first understand what GlobalAveragePooling actually does.您应该首先了解 GlobalAveragePooling 实际上做了什么。 This layer cannot be apllied right after the input, because it will only give the maximum value of all the images for each channel (in your case 3 values, because you have 3 channels).该层不能在输入后立即应用,因为它只会为每个通道提供所有图像的最大值(在您的情况下为 3 个值,因为您有 3 个通道)。

You have to use another method to reduce the size of the images (eg simple conversion to a smaller size.您必须使用另一种方法来减小图像的大小(例如,简单地转换为更小的尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM