Reputation: 768
What is the difference of calling the VGG16 model with or without including top layers of the model? I wonder, why the input parameters to the layers are not shown in the model summary when the model is called without including the top layers. I used the VGG16 model in the following two ways:
from keras.applications import vgg16
model = vgg16.VGG16(weights='imagenet', include_top=False)
print(model.summary)
The shape of the layers in the model does not show any inputs i.e.(None, None, None,64), please see below
Layer (type) Output Shape Param
===================================================================
block1_conv1 (Conv2D) (None, None, None, 64) 1792
block1_conv2 (Conv2D) (None, None, None, 64) 36928
block1_pool (MaxPooling2D) (None, None, None, 64) 0
However, the following code returns the input parameters
from keras.applications import vgg16
model = vgg16.VGG16()
print(model.summary)
The shape of the layers, in this case, return the input parameters
Layer (type) Output Shape Param
==================================================================
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
I seek to understand why it is like this, Please comment
Upvotes: 2
Views: 2603
Reputation: 7129
The top layers of VGG are fully-connected layers which are connected to the output of the convolutional base. These contain a fixed number of nodes with the option to instantiate them with weights pretrained on imagenet. When instantiating a VGG model with the top layers included, the size of the architecture is therefore fixed, and the model will only accept images with a fixed input size of (224,224,3). Feeding the network with images of other sizes would change the amount of weights in the dense classification layers.
When you leave out the top classifier however, you'll be able to feed images of varying size to the network, and the output of the convolutional stack will change accordingly. In this way, you can apply the VGG architecture to images of your size of choice, and paste your own densely connected classifier on top of it. In contrast with the dense layers, the number of weights in the convolutional layers stay the same, only the shape of their output changes.
You will notice all this when you instantiate a VGG model without the top layer, but with a specific input shape:
from keras.applications import vgg16
model = vgg16.VGG16(include_top=False, input_shape=(100,100,3))
model.summary()
Will produce:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) (None, 100, 100, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 100, 100, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 100, 100, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 50, 50, 64) 0
_____________________________________________________________
etc.
It's interesting to see how the output shape of the convolutional layers change as you call the architecture with different input shapes. For the above examples, we get:
block5_conv3 (Conv2D) (None, 6, 6, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 3, 3, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
While if you would instantiate the architecture with images of shape (400,400,3), you would get this output:
_________________________________________________________________
block5_conv3 (Conv2D) (None, 25, 25, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 12, 12, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
Note how the number of weights remains the same in both cases.
Upvotes: 6