简体   繁体   English

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

[英]how to take out the intermediate layer of a a pretrained keras model

I want to use VGG model (tensorflow or keras pretrained model) as a feature extractor; 我想使用VGG模型(tensorflow或keras预训练模型)作为特征提取器; I load the VGG16 model : 我加载了VGG16 model

IMG_SHAPE = (224, 224, 3)
vgg16 = tf.keras.applications.VGG16(input_shape = IMG_SHAPE,
                                    include_top=False,
                                    weights='imagenet')

Now if I have a batch of image 现在如果我有一批图像

image_batch  =np.ones((5,224,224,3),np.float32)

I can get the last layer of VGG16 by 我可以通过以下方式获得VGG16的最后一层

last_layer = vgg16(image_batch)

Does any one know to get the middle layers features given input images image_batch? 有谁知道在给定输入图像image_batch的情况下获取中间层要素吗? That is I want to extract lower level features of the given images. 那就是我想提取给定图像的低级特征。 Thank you very much! 非常感谢你!

You can do something like below: 您可以执行以下操作:

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)

But, how do you know what to pass in model.get_layer() ? 但是,您如何知道要在model.get_layer()传递什么?

Answer - through model.summary() 答案-通过model.summary()

If you print the output of model.summary() , you will get different layer names that you can pass in the model.get_layer() and get the output of that layer. 如果打印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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM