如何使用 VGG-16 中的预训练特征作为 Keras 中 GlobalAveragePooling2D() 层的输入
How to use pre-trained features from VGG-16 as input to GlobalAveragePooling2D() layer in Keras
是否可以使用 VGG-16 中的预训练模型特征并传递给 Keras 中其他模型的 GlobalAveragePooling2D() 层?
存储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.
我不能使用 Flatten() 层,因为我想用 multi-类.
预测多标签
当然,你绝对可以。您有几个选择:
pooling kwarg
在 VGG16 构造函数中使用 pooling
kwarg,它用指定的类型替换最后一个池化层。即
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
最后一行冻结了预训练层,以便您保留预训练模型的特征并只训练新层。
我写了一篇博客 post,介绍了使用预训练模型的基础知识并将它们扩展到处理各种图像分类问题;它还有一些可能提供更多上下文的工作代码示例的 link:http://innolitics.com/10x/pretrained-models-with-keras/
是否可以使用 VGG-16 中的预训练模型特征并传递给 Keras 中其他模型的 GlobalAveragePooling2D() 层?
存储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.
我不能使用 Flatten() 层,因为我想用 multi-类.
预测多标签当然,你绝对可以。您有几个选择:
pooling kwarg
在 VGG16 构造函数中使用 pooling
kwarg,它用指定的类型替换最后一个池化层。即
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
最后一行冻结了预训练层,以便您保留预训练模型的特征并只训练新层。
我写了一篇博客 post,介绍了使用预训练模型的基础知识并将它们扩展到处理各种图像分类问题;它还有一些可能提供更多上下文的工作代码示例的 link:http://innolitics.com/10x/pretrained-models-with-keras/