如何取出预训练的keras模型的中间层

时间:2019-07-19 17:04:29

标签: python tensorflow keras deep-learning

我想使用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)

我可以通过以下方式获得VGG16的最后一层

last_layer = vgg16(image_batch)

有人知道在给定输入图像image_batch的情况下获取中间层特征吗?那就是我想提取给定图像的低级特征。非常感谢你!

1 个答案:

答案 0 :(得分: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