-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgg16.py
114 lines (95 loc) · 3.9 KB
/
vgg16.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import torch
import torch.nn as nn
import torch.nn.functional as F
class VGG16(nn.Module):
def __init__(self):
super(VGG16, self).__init__()
# 3 * 32 * 32
self.conv1_1 = nn.Conv2d(3, 64, 3) # 64 * 30 * 30
self.bn1_1 = nn.BatchNorm2d(64)
self.conv1_2 = nn.Conv2d(64, 64, 3, padding=(1, 1)) # 64 * 30* 30
self.bn1_2 = nn.BatchNorm2d(64)
# self.maxpool1 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 64 * 112 * 112
self.conv2_1 = nn.Conv2d(64, 128, 3) # 128 * 28 * 28
self.bn2_1 = nn.BatchNorm2d(128)
# self.conv2_2 = nn.Conv2d(128, 128, 3, padding=(1, 1)) # 128 * 28 * 28
# self.maxpool2 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 128 * 15 * 15
self.conv3_1 = nn.Conv2d(128, 256, 3) # 256 * 13 * 13
self.bn3_1 = nn.BatchNorm2d(256)
self.conv3_2 = nn.Conv2d(256, 256, 3, padding=(1, 1)) # 256 * 13 * 13
self.bn3_2 = nn.BatchNorm2d(256)
# self.conv3_3 = nn.Conv2d(256, 256, 3, padding=(1, 1)) # 256 * 26 * 26
# self.maxpool3 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 256 * 28 * 28
self.conv4_1 = nn.Conv2d(256, 512, 3) # 512 * 11 * 11
self.bn4_1 = nn.BatchNorm2d(512)
# self.conv4_2 = nn.Conv2d(512, 512, 3, padding=(1, 1)) # 512 * 24 * 24
self.conv4_3 = nn.Conv2d(512, 512, 3, padding=(1, 1)) # 512 * 11 * 11
self.bn4_3 = nn.BatchNorm2d(512)
self.maxpool4 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 512 * 6 * 13
self.conv5_1 = nn.Conv2d(512, 512, 3) # 512 * 11 * 11
self.bn5_1 = nn.BatchNorm2d(512)
# self.conv5_2 = nn.Conv2d(512, 512, 3, padding=(1, 1)) # 512 * 11 * 11
self.conv5_3 = nn.Conv2d(512, 512, 3, padding=(1, 1)) # 512 * 11 * 11
self.bn5_3 = nn.BatchNorm2d(512)
self.maxpool5 = nn.MaxPool2d((2, 2), padding=(1, 1)) # pooling 512 * 6 * 6
# view
self.fc1 = nn.Linear(512 * 6 * 6, 360)
self.drop1 = nn.Dropout2d()
self.fc2 = nn.Linear(360, 360)
self.drop2 = nn.Dropout2d()
self.fc3 = nn.Linear(360, 10)
# softmax 1 * 1 * 1000
def save_weights(self, path):
torch.save(self.state_dict(), path)
def forward(self, x):
# x.size(0)即为batch_size
in_size = x.size(0)
out = self.conv1_1(x) # 222
out = self.bn1_1(out)
out = F.relu(out)
out = self.conv1_2(out) # 222
out = self.bn1_2(out)
out = F.relu(out)
# out = self.maxpool1(out) # 112
out = self.conv2_1(out) # 110
out = self.bn2_1(out)
out = F.relu(out)
#out = self.conv2_2(out) # 110
#out = F.relu(out)
# out = self.maxpool2(out) # 56
out = self.conv3_1(out) # 54
out = self.bn3_1(out)
out = F.relu(out)
out = self.conv3_2(out) # 54
out = self.bn3_2(out)
out = F.relu(out)
#out = self.conv3_3(out) # 54
#out = F.relu(out)
# out = self.maxpool3(out) # 28
out = self.conv4_1(out) # 26
out = self.bn4_1(out)
out = F.relu(out)
# out = self.conv4_2(out) # 26
# out = F.relu(out)
out = self.conv4_3(out) # 26
out = self.bn4_3(out)
out = F.relu(out)
out = self.maxpool4(out) # 14
out = self.conv5_1(out) # 12
out = F.relu(out)
#out = self.conv5_2(out) # 12
#out = F.relu(out)
out = self.conv5_3(out) # 12
out = F.relu(out)
out = self.maxpool5(out) # 7
# 展平
out = out.view(in_size, -1)
out = self.fc1(out)
out = F.relu(out)
out = self.drop1(out)
out = self.fc2(out)
out = F.relu(out)
out = self.drop2(out)
out = self.fc3(out)
# out = F.log_softmax(out, dim=1)
return out