-
Notifications
You must be signed in to change notification settings - Fork 1
/
VGGNet.py
89 lines (80 loc) · 3.38 KB
/
VGGNet.py
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
import torch.nn as nn
class VGGNet(nn.Module):
def __init__(self, layer_num=16, label_num = 10, dropout=0):
super(VGGNet, self).__init__()
self.layer_num = layer_num
self.conv_pool_1 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=4, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=4, out_channels=4, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=1, padding=1)
)
self.conv_pool_2 = nn.Sequential(
nn.Conv2d(in_channels=4, out_channels=8, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=1, padding=1)
)
self.conv_pool_3 = nn.Sequential(
nn.Conv2d(in_channels=8, out_channels=16, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.conv_pool_4 = nn.Sequential(
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.conv_pool_5 = nn.Sequential(
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
if self.layer_num == 19:
self.conv_pool_6 = nn.Sequential(
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=1, padding=1)
)
self.fc = nn.Sequential(
nn.Flatten(),
nn.Dropout(dropout),
nn.Linear(32 * 3 * 3, 256),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(256, 256),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(256, 256),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(256, label_num)
)
def forward(self, x):
x = self.conv_pool_1(x)
x = self.conv_pool_2(x)
x = self.conv_pool_3(x)
x = self.conv_pool_4(x)
x = self.conv_pool_5(x)
if self.layer_num == 19:
x = self.conv_pool_6(x)
x = self.fc(x)
return x