如何取出预训练的 keras 模型的中间层
Posted
技术标签:
【中文标题】如何取出预训练的 keras 模型的中间层【英文标题】:how to take out the intermediate layer of a a pretrained keras model 【发布时间】:2019-11-28 16:58:48 【问题描述】:我想使用VGG
模型(tensorflow或keras预训练模型)作为特征提取器;我加载了VGG16 model
:
IMG_SHAPE = (224, 224, 3)
vgg16 = tf.keras.applications.VGG16(input_shape = IMG_SHAPE,
include_top=False,
weights='imagenet')
现在如果我有一批图像
image_batch =np.ones((5,224,224,3),np.float32)
我可以通过
得到最后一层VGG16last_layer = vgg16(image_batch)
有没有人知道在给定输入图像 image_batch 的情况下获取中间层特征?那就是我想提取给定图像的较低级别的特征。非常感谢!
【问题讨论】:
你见过:***.com/questions/41711190/… 吗? 感谢您的 cmets。我阅读了您提到的帖子的公认解决方案;但那个无关紧要;但是,第二个很好。谢谢。 【参考方案1】:您可以执行以下操作:
IMG_SHAPE = (224, 224, 3)
model = tf.keras.applications.VGG16(input_shape = IMG_SHAPE,
include_top=False,
weights=None)
pretrain_model_path = "weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"
model.load_weights(pretrain_model_path)
# print(model.summary())
image_batch = np.ones((5,224,224,3),np.float32)
last_layer = tf.keras.models.Model(inputs=model.input, outputs=model.get_layer('block5_pool').output)
res = last_layer.predict(image_batch)
但是,你怎么知道在model.get_layer()
中传递什么?
回答 - 通过model.summary()
如果你打印model.summary()
的输出,你会得到不同的层名,你可以传入model.get_layer()
,得到那个层的输出。
Layer (type) Output Shape Param #
=================================================================
input_17 (InputLayer) (None, 224, 224, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
【讨论】:
以上是关于如何取出预训练的 keras 模型的中间层的主要内容,如果未能解决你的问题,请参考以下文章
如何使用分布式 Dask 和预训练的 Keras 模型进行模型预测?