# TF Implementing a VGG-19 network in TensorFlow 2.0

# TF Implementing a VGG-19 network in TensorFlow 2.0

Highlights: In this post we will show how to implement a fundamental Convolutional Neural Network like \(VGG-19\) in TensorFlow. The VGG-19 architecture was design by Visual Geometry Group, Department of Engineering Science, University of Oxford. It competed in the ImageNet Large Scale Visual Recognition Challenge in 2014.

Tutorial Overview:

  1. Theory recapitulation
  2. Implementation in TensorFlow

1. Theory recapitulation

With ConvNets becoming more of a popular in the computer vision field, a number of attempts have been made to improve the original AlexNet architecture. One important aspect of ConvNet architecture design is it’s depth.

Remarkable thing about the \(VGG-19 \) is that instead of having so many hyper parameters it is a much simpler network. It has \(conv \) layers that are just \(3\times3\) filters with a stride of \(1 \), and with the same padding. In all \(Max\enspace pooling \) layers \(2 \times 2\) filters are used with a stride of \(2 \).

For classification, three fully connected layers are used, two with \(4096\) neurons, and last one with \(1000\) neurons.

In all layers except the last one, \(ReLU\) activation function is used, while in the last one \(Softmax\) is used for probability distribution between classes.

\(VGG-19\) is trained on more than a million images from the ImageNet database. The network is 19 layers deep and can classify images into 1000 object categories.

Let’s see in details how this architecture looks like.

Layer TypeFeature MapSizeKernel SizeStrideActivation
Image1224×224
Convolution64224×2243×31ReLU
Convolution64224×2243×31ReLU
Max Pooling64112×1122×22
Convolution128112×1123×31ReLU
Convolution128112×1123×31ReLU
Max Pooling12856×562×22
Convolution25656×563×31ReLU
Convolution25656×563×31ReLU
Convolution25656×563×31ReLU
Convolution25656×563×31ReLU
Max Pooling25628×282×22
Convolution51228×283×31ReLU
Convolution51228×283×31ReLU
Convolution51228×283×31ReLU
Convolution51228×283×31ReLU
Max Pooling51214×142×22
Convolution51214×143×31ReLU
Convolution51214×143×31ReLU
Convolution51214×143×31ReLU
Convolution51214×143×31ReLU
Max Pooling5127×72×22
Fully Connected4096ReLU
Fully Connected4096ReLU
Fully Connected1000Softmax

2. Implementation in TensorFlow

The interactive Colab notebook can be found at the following link   

Let’s start with importing all necessary libraries.

After imports, we can create a network. We will create a network by using Sequential API.

Training a network with \(140000000\) parameters will take to long, so here we will just load weights from a pre-trained model. This is done by using the load_weights() function. Weights can be found here.

Our model will predict numerical classes, so in order to make them readable to humans, we need to create a dictionary with their names.

Now we can test our model. Let’s first write a function for this, and then use it to make predictions. We can use TensorFlow function predict() and then use np.argmax() to find the predicted class, or predict_classes() to make it simpler.

Summary

In this post we talked about VGG-19 network and how to implement it in TensorFlow. We made use of available weights in order not to train our network again. In the next post we will show how to implement YOLO v3 object detection algorithm.