include_top属性无法获取的ResNet50获取模型

Obtaining model of ResNet50 which cannot be obtained with the include_top attribute

我正在使用 ResNet50 模型进行特征提取。我有两个模型按以下方式初始化:

from tensorflow.keras.applications import ResNet50

model1=ResNet50(weights="imagenet", include_top=False) 
model2=ResNet50(weights="imagenet", include_top=True)`

现在,当我绘制模型架构时,我得到了这个:(我只展示了架构的结尾)

它们都不以 avg_pool: GlobalAveragePooling2D 结尾,即我希望模型的输出为 (?, 2048)。是否有可能获得架构?获得具有 imagenet 权重的此类架构将简化特征提取。

非常感谢您的帮助。

您可以从 ResNet 输入和全局平均池 (GAP) 层的输出生成新的 keras 模型。为了获得 GAP 层的输出,您需要使用 get_layer 方法提取它。您可以通过使用 model.summary()

查找图层名称来为任何图层执行此操作
model1=ResNet50(weights='imagenet', include_top=True)
GAP_output = model1.get_layer('avg_pool').output
new_model = tf.keras.Model(model1.input, GAP_output)
new_model.summary()