VGG Network
Overview
The VGG network, developed by the Visual Geometry Group, is a deep convolutional neural network architecture that emphasizes simplicity and depth. It is characterized by its use of small 3x3 convolution filters and deep architectures with 16-19 layers. Unlike some other architectures, VGG removes local response normalization layers, focusing instead on achieving high accuracy in image classification tasks through its straightforward design.
Architecture
The VGG network is known for its deep structure, utilizing more convolution layers with smaller 3x3 filters. This design choice provides more expressive power and reduces the number of parameters compared to using larger filters. The architecture of VGG-11, a variant of the VGG network, is depicted in Figure 11.5. The diagram illustrates the input and output tensor shapes for each layer, with all shapes following the format N × C × H × W, where N is the batch size, C is the number of channels, H is the height, and W is the width.
Figure 11.5 VGG-11 architecture diagram. All shapes are of the form N × C × H × W, where N is the batch size, C is the number of channels, H is the height, and W is the width.
Implementation
PyTorch Code for VGG Network
The VGG network can be implemented in PyTorch as shown in Listing 11.4. The network consists of a convolutional backbone and a classifier. The classifier is composed of three fully connected (FC) layers, with the first two layers followed by ReLU nonlinearity and dropout for regularization.
Listing 11.4 PyTorch code for the VGG network
class VGG(nn.Module):
def __init__(self, conv_backbone, num_classes):
super(VGG, self).__init__()
self.conv_backbone = conv_backbone #1
self.classifier = nn.Sequential(
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096), #2
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, num_classes)
)
def forward(self, x):
conv_features = self.conv_backbone(x)
logits = self.classifier(
conv_features.view(
conv_features.shape[0], -1)) #3
return logits
#1 Backbone network defined in listing <a href="#code-vgg-backbone" style="text-decoration: none; color: #4080C0;">11.3</a>
#2 The classifier is made up of three linear Layers. The first two are followed by ReLU nonlinearity.
#3 Flattens the conv features before passing it to the classifier
Instantiating a VGG-11 Network
A VGG-11 network can be instantiated using a specific configuration, as shown in Listing 11.5. The configuration defines the number of layers and their respective parameters, which are then used to create the convolutional backbone and the complete VGG network.
Listing 11.5 PyTorch code instantiating a VGG network from a specific config
vgg11_cfg = [ #1
(3, 1, 64),
(64, 1, 128),
(128, 2, 256),
(256, 2, 512),
(512, 2, 512)
]
vgg11_backbone = ConvBackbone(vgg11_cfg) #1
num_classes = 1000
vgg11 = VGG(vgg11_backbone, num_classes) #1
#1 Creates the cfg for VGG-11
#2 Instantiates the conv backbone
#3 Instantiates the VGG network
Practical Considerations
While the VGG network can be implemented from scratch in PyTorch, it is often more practical to use the pre-implemented version available in the torchvision package. This package includes the VGG network along with several other popular deep networks, providing a reliable and efficient implementation for practical use.