VGG-16 Architecture Explained .
Paper : Very Deep Convolutional Networks for Large-Scale Image Recognition
Authors : Karen Simonyan, Andrew Zisserman. University of Oxford, UK.
Published in : 2014 .
Model Architecture :
keras :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D , MaxPool2D , Dense , Dropout , Flatten
from tensorflow.keras.optimizers import Adam
def VGG16() :
model = Sequential()
model.add(Conv2D(filters=64 , kernel_size=(3,3) , strides=1 , input_shape=(224,224,3) , activation='relu'))
model.add(Conv2D(filters=64 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(MaxPool2D(pool_size=(2,2) , strides=2))
model.add(Conv2D(filters=128 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(Conv2D(filters=128 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(MaxPool2D(pool_size=(2,2) , strides=2))
model.add(Conv2D(filters=256 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(Conv2D(filters=256 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(Conv2D(filters=256 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(MaxPool2D(pool_size=(2,2) , strides=2))
model.add(Conv2D(filters=512 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(Conv2D(filters=512 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(Conv2D(filters=512 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(MaxPool2D(pool_size=(2,2) , strides=2))
model.add(Conv2D(filters=512 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(Conv2D(filters=512 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(Conv2D(filters=512 , kernel_size=(3,3) , strides=1 , activation='relu'))
model.add(MaxPool2D(pool_size=(2,2) , strides=2))
model.add(Dense(units=4096 , activation='relu'))
model.add(Dense(units=4096 , activation='relu'))
model.add(Dense(units=1000 , activation='softmax'))
return model
pyTorch :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import torch.nn as nn
import torch.nn.functional as F
class VGG16(nn.Module):
def __init__(self):
super(VGG16 , self).__init__()
self.conv1 = nn.Conv2d(in_channels=3 , out_channels= 64 , kernel_size=(3,3) , stride=(1,1) , padding=(1,1))
self.conv2 = nn.Conv2d(in_channels=64 , out_channels= 64 , kernel_size=(3,3) , stride=(1,1), padding=(1,1))
self.conv3 = nn.Conv2d(in_channels=64 , out_channels= 128 , kernel_size=(3,3) , stride=(1,1), padding=(1,1))
self.conv4 = nn.Conv2d(in_channels=128 , out_channels= 128 , kernel_size=(3,3) , stride=(1,1), padding=(1,1))
self.conv5 = nn.Conv2d(in_channels=128 , out_channels= 256 , kernel_size=(3,3) , stride=(1,1), padding=(1,1))
self.conv6 = nn.Conv2d(in_channels=256 , out_channels= 256 , kernel_size=(3,3) , stride=(1,1), padding=(1,1))
self.conv7 = nn.Conv2d(in_channels=256 , out_channels= 512 , kernel_size=(3,3) , stride=(1,1), padding=(1,1))
self.conv8 = nn.Conv2d(in_channels=512 , out_channels= 512 , kernel_size=(3,3) , stride=(1,1), padding=(1,1))
self.maxPool = nn.MaxPool2d(kernel_size=(2,2) , stride=(2,2))
self.fc1 = nn.Linear(in_features=25088 , out_features=4096)
self.fc2 = nn.Linear(in_features=4096 , out_features=4096)
self.fc3 = nn.Linear(in_features=4096 , out_features=1000)
def forward(self, x):
# 2 Conv Layer with 64 kernels of size 3*3 and stride = 1
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
#Max Pooling Layer with Kernel size 2*2 and stride = 2
x = self.maxPool(x)
# 2 Conv Layer with 128 kernels of size 3*3 and stride = 1
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
#Max Pooling Layer with Kernel size 2*2 and stride = 2
x = self.maxPool(x)
# 3 Conv Layer with 256 kernels of size 3*3 and stride = 1
x = self.conv5(x)
x = F.relu(x)
x = self.conv6(x)
x = F.relu(x)
x = self.conv6(x)
x = F.relu(x)
#Max Pooling Layer with Kernel size 2*2 and stride = 2
x = self.maxPool(x)
# 3 Conv Layer with 512 kernels of size 3*3 and stride = 1
x = self.conv7(x)
x = F.relu(x)
x = self.conv8(x)
x = F.relu(x)
x = self.conv8(x)
x = F.relu(x)
#Max Pooling Layer with Kernel size 2*2 and stride = 2
x = self.maxPool(x)
# 3 Conv Layer with 512 kernels of size 3*3 and stride = 1
x = self.conv8(x)
x = F.relu(x)
x = self.conv8(x)
x = F.relu(x)
x = self.conv8(x)
x = F.relu(x)
#Max Pooling Layer with Kernel size 2*2 and stride = 2
x = self.maxPool(x)
# Convert Matrix of features to Vector
x = x.reshape(x.shape[0] , -1)
# Fully Connected Layer With 4096 Unit
x = self.fc1(x)
x = F.relu(x)
# Fully Connected Layer With 4096 Unit
x = self.fc2(x)
x = F.relu(x)
# Fully Connected Layer With 1000 Unit
x = self.fc3(x)
x = F.softmax(x)
return x