Reputation: 79
I want to use VGG
model (tensorflow or keras pretrained model) as a feature extractor; I load the 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
last_layer = vgg16(image_batch)
Does any one know to get the middle layers features given input images image_batch? That is I want to extract lower level features of the given images. Thank you very much!
Upvotes: 1
Views: 3348
Reputation: 8699
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()
?
Answer - through 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.
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
Upvotes: 6