Creating VGG from Scratch using Tensorflow | by Arjun Sarkar | Towards Data Science

LeNet-5 was one of the oldest convolutional neural network architectures, designed by Yann LeCun in 1998, which was used to recognize handwritten digits. It used 5x5 filters, average pooling, and no padding. But by modern standards, this was a very small neural network and had only 60 thousand parameters. Nowadays, we see networks that have a range of 10 million to a few billion parameters. The next big Convolutional neural network that revolutionized the use of a convolutional network was AlexNet which had approximately 60 million parameters. The first layer of AlexNet uses 96 filters with kernel size 11x11, with strides of 4. The next layer uses 3x3 filters, and so on. Also, AlexNet uses Max Pooling and padding, which were not used in LeNet-5. AlexNet was very similar to LeNet-5, but it was much bigger. Also, AlexNet uses the ReLU activation function, while LeNet-5 mainly used the Sigmoid activation. What these networks had in common is that, as we go deeper into the network, the size of the tensor kept on decreasing, while the number of channels kept on increasing. Also, another trend that is still used nowadays while creating neural network architectures is the use of Convolutional layers (one or multiple) followed by some Pooling layers, and in the end, some fully connected layers.
The next big convolutional neural network was the VGG network. The remarkable thing about VGG was that, instead of having so many hyperparameters, the authors used a much simpler network, where the focus was on using convolutional layers with small sizes of 3x3 filters, with a stride of 1 and using the ‘same’ padding, and make all the MaxPooling layers 2x2 with a stride of 2. VGG greatly simplified the previously made neural network architectures.
VGG paper link — https://arxiv.org/abs/1409.1556
VGG 16 architecture and implementation using Tensorflow:
Figure 2 shows all the VGG architectures. The architecture of VGG 16 is highlighted in red. A simpler version of the architecture is presented in Figure 1.
VGG network uses Max Pooling and ReLU activation function. All the hidden layers use ReLU activation and the last Dense layer uses Softmax activation. MaxPooling is performed over a 2x2 pixel window with a stride of 2.
VGG 16 has 5 convolutional blocks and 3 fully connected layers. Each block consists of 2 or more Convolutional layers and a Max Pool layer.
Algorithm:
import all the necessary layersWrite code for the convolutional blocksWrite code for the Dense layersBuild the model
import all the necessary layers
Write code for the convolutional blocks
Write code for the Dense layers
Build the model
Importing the libraries:
# import necessary layersfrom tensorflow.keras.layers import Input, Conv2Dfrom tensorflow.keras.layers import MaxPool2D, Flatten, Densefrom tensorflow.keras import Model
Input:
# input  input = Input(shape =(224,224,3))
Input is a 224x224 RGB image, so 3 channels.
Conv Block 1:
It has two Conv layers with 64 filters each, followed by Max Pooling.
# 1st Conv Blockx = Conv2D (filters =64, kernel_size =3, padding ='same', activation='relu')(input)x = Conv2D (filters =64, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)
Conv Block 2:
It has two Conv layers with 128 filters followed by Max Pooling.
# 2nd Conv Blockx = Conv2D (filters =128, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =128, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)
Conv Block 3:
It has three Conv layers with 256 filters followed by Max Pooling.
# 3rd Conv block  x = Conv2D (filters =256, kernel_size =3, padding ='same', activation='relu')(x) x = Conv2D (filters =256, kernel_size =3, padding ='same', activation='relu')(x) x = Conv2D (filters =256, kernel_size =3, padding ='same', activation='relu')(x) x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)
Conv Block 4 and 5:
Both Conv blocks 4 and 5 have 3 Conv layers with 512 filters followed by Max Pooling.
# 4th Conv blockx = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)# 5th Conv blockx = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)
Dense layers:
There are 3 fully connected layers, the first two layers with 4096 hidden units and ReLU activation and the last output layer with 1000 hidden units and Softmax activation.
# Fully connected layers  x = Flatten()(x) x = Dense(units = 4096, activation ='relu')(x) x = Dense(units = 4096, activation ='relu')(x) output = Dense(units = 1000, activation ='softmax')(x)
Creating the Model:
# creating the modelmodel = Model (inputs=input, outputs =output)model.summary()
Output:
Plotting the Model:
# plotting the modelfrom tensorflow.python.keras.utils.vis_utils import model_to_dotfrom IPython.display import SVGimport pydotimport graphvizSVG(model_to_dot(model, show_shapes=True, show_layer_names=True, rankdir='TB',expand_nested=False, dpi=60, subgraph=False).create(prog='dot',format='svg'))
Output Snippet:
# import necessary layers  from tensorflow.keras.layers import Input, Conv2D from tensorflow.keras.layers import MaxPool2D, Flatten, Dense from tensorflow.keras import Model# inputinput = Input(shape =(224,224,3))# 1st Conv Blockx = Conv2D (filters =64, kernel_size =3, padding ='same', activation='relu')(input)x = Conv2D (filters =64, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)# 2nd Conv Blockx = Conv2D (filters =128, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =128, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)# 3rd Conv blockx = Conv2D (filters =256, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =256, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =256, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)# 4th Conv blockx = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)# 5th Conv blockx = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = Conv2D (filters =512, kernel_size =3, padding ='same', activation='relu')(x)x = MaxPool2D(pool_size =2, strides =2, padding ='same')(x)# Fully connected layersx = Flatten()(x)x = Dense(units = 4096, activation ='relu')(x)x = Dense(units = 4096, activation ='relu')(x)output = Dense(units = 1000, activation ='softmax')(x)# creating the modelmodel = Model (inputs=input, outputs =output)model.summary()
Conclusion:
The VGG network is a very simple Convolutional Neural Network, and due to its simplicity is very easy to implement using Tensorflow. It has only Conv2D, MaxPooling, and Dense layers. VGG 16 has a total of 138 million trainable parameters.
VGG was the deepest CNN model architecture during its publication with a maximum of 19 weight layers. It achieved state-of-the-art performance in the ImageNet challenge and showed that deeper networks are beneficial for better classification accuracy.
References:
Karen Simonyan and Andrew Zisserman, Very Deep Convolutional Networks for Large-Scale Image Recognition, arXiv:1409.1556v6 [cs.CV], 2015.
Karen Simonyan and Andrew Zisserman, Very Deep Convolutional Networks for Large-Scale Image Recognition, arXiv:1409.1556v6 [cs.CV], 2015.