如何使用VGG-16中预先训练的特征作为Keras中GlobalAveragePoolig2D()层的输入

如何使用VGG-16中预先训练的特征作为Keras中GlobalAveragePoolig2D()层的输入,keras,deep-learning,pre-trained-model,vgg-net,Keras,Deep Learning,Pre Trained Model,Vgg Net,是否可以使用VGG-16中预先训练好的模型特征并传递给Keras中其他模型的GlobalAveragePoolig2D()层 用于存储VGG-16网络脱机功能的示例代码: model = applications.VGG16(include_top=False, weights='imagenet') bottleneck_features_train = model.predict(input) 顶级车型的示例代码: model = Sequential() model.add(Global

是否可以使用VGG-16中预先训练好的模型特征并传递给Keras中其他模型的GlobalAveragePoolig2D()层

用于存储VGG-16网络脱机功能的示例代码:

model = applications.VGG16(include_top=False, weights='imagenet')
bottleneck_features_train = model.predict(input)
顶级车型的示例代码:

model = Sequential()
model.add(GlobalAveragePooling2D()) # Here I want to use pre-trained feature from VGG-16 net as input.

我不能使用flatte()层,因为我想用多个类预测多个标签。

当然可以。您有两个选择:

pooling-kwarg
在VGG16构造函数中使用
pooling
kwarg,它用指定的类型替换最后一个池层。i、 e

model_base = keras.applications.vgg16.VGG16(include_top=False, input_shape=(*IMG_SIZE, 3), weights='imagenet', pooling="avg")
向输出中添加层 还可以向预训练模型添加更多层:

from keras.models import Model

model_base = keras.applications.vgg16.VGG16(include_top=False, input_shape=(*IMG_SIZE, 3), weights='imagenet')
output = model_base.output
output = GlobalAveragePooling2D()(output)
# Add any other layers you want to `output` here...
model = Model(model_base.input, output)
for layer in model_base.layers:
    layer.trainable = False
最后一行冻结了预训练的层,以便保留预训练模型的特征,只训练新层

我写了一篇博文,介绍了使用预训练模型的基础知识,并将其扩展到处理各种图像分类问题;它还提供了一些工作代码示例的链接,这些示例可能提供更多上下文: