VGG Net | Build VGG Net from scratch with Python

Contents

This article was published as part of the Data Science Blogathon

Introduction

VGG- Network is a model of red neuronal convolucional proposed by K. Simonyan y A. Zisserman in the article “Very deep convolutional networks for large-scale image recognition” [1]. This architecture achieved test accuracy between the 5 best of 92,7% on ImageNet, that has more than 14 million images belonging to 1000 lessons.

It is one of the well-known architectures in the field of deep learning. Replace large kernel size filters with 11 Y 5 in the first and second layer, respectively, showed improvement over AlexNet architecture, with multiple kernel size filters from 3 × 3 one after another. It was trained for weeks and was using NVIDIA Titan Black GPU.

341717-6837355
Source: neurohive.io

VGG16 architecture

The input to the red neuronal convolution is a fixed-size RGB image 224 × 224. The only pre-processing it does is subtract the average RGB values, which are computed from the dataset of training, of each pixel.

Later, the image runs through a stack of convolutional layers (Conv.), Where there are filters with a very small receptive field that is 3 × 3, which is the smallest size to capture the notion of left / right, up / down, and central part.

In one of the configurations, also uses convolution filters 1 × 1, which can be observed as a linear transformation of the input channels followed by non-linearity. Convolutional strides are fixed at 1 pixel; the spatial padding of the input of the convolutional cover is such that the resolution spatial dimensions are maintained after convolution, namely, the filling is 1 pixel for 3 × 3 Conv. covers.

Later, spatial clustering is carried out by five layers of maximum clustering, 16 that follow some of the Conv. covers, but not all Conv. layers are followed by maximum grouping. This maximum grouping is done in a window of 2 × 2 pixels, with step 2.

521138-3699573
Source: neurohive.io

The architecture contains a stack of convolutional layers that have a different depth in different architectures that are followed by three Fully-Connected layers (FC): the first two FC have 4096 channels each and the third FC performs a classification of 1000 routes and therefore contains 1000 channels which is one for each class.

The last layer is the soft-max layer. The configuration of the fully connected layers is similar in all networks.

All hidden layers are equipped with rectification (resume) nonlinear. What's more, here one of the networks contains Normalization local response (LRN), such normalization does not improve performance on the trained dataset, but its use leads to higher memory consumption and calculation time.

Architecture Summary:

• The input to the model is a fixed-size RGB image 224 × 224224 × 224

• Preprocessing consists of subtracting the mean of the RGB value of the training set from each pixel

• Convolutional layers 17

– Fixed stride to 1 pixel

– the filling is 1 pixel for 3 × 33 × 3

• Spatial grouping layers

– This layer does not count towards the depth of the web by convention

– Spatial grouping is done using maximum grouping layers

– the window size is 2 × 22 × 2

– Stride set to 2

– Convnets used 5 maximum grouping layers

• Fully connected layers:

• 1º: 4096 (resume).

▪ 2do: 4096 (resume).

▪ 3º: 1000 (Softmax).

Architecture configuration

The following figure contains the VGG network convolutional neural network configuration with

next layers:

• VGG-11

• VGG-11 (LRN)

• VGG-13

• VGG-16 (Conv1)

• VGG-16

• VGG-19

463339-1530029

Source: “Very deep convolutional networks for large-scale image recognition”

Convolutional neural network configurations are mentioned above one per column.

Then, networks are referred to by their names (A - E). All configurations follow the traditional design and differ only in depth: of 11 weight layers in network A which are 8 Conv. Y 3 layers FC a 19 weight layers in the E network which is 16 Conv. Y 3 FC layers. The width of each conv. layer is the number of channels is quite small, that starts from 64 in the first layer and then continues to increase by a factor of 2 after each layer of maximum grouping until reaching 512.

The number of parameters for each configuration is described below. Although it has great depth, the number of weights in the networks is not greater than the number of weights in a shallower network with higher conv. layer widths and receptive fields

3349110-1384232
Source: “Very deep convolutional networks for large-scale image recognition”

Training

• The Loss function is a multinomial logistic regression

• The learning algorithm is gradient descent gradient stochastic (SGD) mini-batch based on momentum backspread.

· The lot size was 256

· The impulse was 0,9

Regularization

· L2 weight decay (the penalty multiplier was 0,0005)

· Dropout for the first two fully connected layers is set to 0.5

• Learning rate

· initial: 0.01

· When the accuracy of the validation set stopped improving, se reduce a 10.

• Although it has a greater number of parameters and also depth compared to Alexnet, CNN required fewer times for the loss function to converge due to

· Small convolutional grains and more regularization due to great depth.

· Pre-initialization of certain layers.

• Training image size

· S is the smallest side of the isotopically rescaled image

· Two approaches to establishing S

▪ Fix S, known as single scale training

▪ Here S = 256 y S = 384

▪ Vary S, known as multi-scale training

▪ S de [Smin, Smax] where Smin = 256, Smax = 512

– Later 224 × 224224 × 224
Image was randomly cropped from SGD iteration rescaled image.

Key features

• VGG16 has a total of 16 layers that have some weights.

• Only convolution and grouping layers are used.

• Always use a core of 3 x 3 for convolution. 20

• Size 2 × 2 maximum pool.

• 138 million parameters.

• Trained in ImageNet data.

• Has an accuracy of 92,7%.

• Another version that is VGG 19, has a total of 19 layers with weights.

• It is a very good deep learning architecture for benchmarking on any particular task.

• Pre-trained networks for VGG are open source, so they can be commonly used for various kinds of applications.

Implementemos VGG Net

First, let's create filter mapping for each version of the VGG network. Please refer to the configuration image above for the number of filters. Namely, create a dictionary for the version with a key named VGG11, VGG13, VGG16, VGG19 and create a list according to the number of filters in each version respectively. Here “M” in the list it is known as Maxpool operation.

import torch
import torch.nn as nn
VGG_types = {
"VGG11": [64, "M", 128, "M", 256, 256, "M", 512, 512, "M", 512, 512, "M"],
"VGG13": [64, 64, "M", 128, 128, "M", 256, 256, "M", 512, 512, "M", 512, 512, "M"],
"VGG16": [64,64,"M",128,128,"M",256,256,256,"M",512,512,512,"M",512,512,512,"M",],
"VGG19": [64,64,"M",128,128,"M",256,256,256,256,"M",512,512,512,512,
          "M",512,512,512,512,"M",],}

Create a variable global to mention the version of the architecture. Then create a class called VGG_net with inputs like in_channels and num_classes. Takes inputs as a number of image channels and the number of output classes.

Initialize sequential layers, namely, in the sequence, Linear layer–> ReLU–> Skip.

Then create a function called create_conv_layers which takes the VGGnet architecture configuration as input, which is the list that we created previously for different versions. When he meets the letter “M” from the list above, perform the MaxPool2d operation.

VGGType = "VGG16"
class VGGnet(nn.Module):
    def __init__(self, in_channels=3, num_classes=1000):
        super(VGGnet, self).__init__()
        self.in_channels = in_channels
        self.conv_layers = self.create_conv_layers(VGG_types[VGGType])
self.fcs = nn.Sequential(
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096, num_classes),
)

def forward(self, x):
x = self.conv_layers(x)
x = x.reshape(x.shape[0], -1)
x = self.fcs(x)
return x

def create_conv_layers(self, architecture):
layers = []
in_channels = self.in_channels

for x in architecture:
if type(x) == int:
out_channels = x

layers += [
nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
padding=(1, 1),
),
nn.BatchNorm2d(x),
nn.ReLU(),
]
in_channels = x
elif x == "M":
layers += [nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))]

return nn.Sequential(*layers)

Once this is done, write a little test code to check if our implementation is working fine.

In the following test code, the number of classes given is 500.

if __name__ == "__main__":
    device = "miracles" if torch.cuda.is_available() else "cpu"
    model = VGGnet(in_channels=3, num_classes=500).to(device)
    # print(model)
    x = torch.randn(1, 3, 224, 224).to(device)
    print(model(x).shape)

The output should be like this:

3456811-4554802

If you want to see the network architecture, you can uncomment the to print (model) above code declaration. You can also try different versions by changing the VGG versions in the VGGType variable.

The full code can be accessed here:

https://github.com/BakingBrains/Deep_Learning_models_implementation_from-scratch_using_pytorch_/blob/main/VGG.py

[1]. K. Simonyan y A. Zisserman: Very deep convolutional networks for large-scale image recognition, april of 2015, DOI: https://arxiv.org/pdf/1409.1556.pdf

Thanks

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker