-
Notifications
You must be signed in to change notification settings - Fork 0
/
VGG.py
73 lines (60 loc) · 2.6 KB
/
VGG.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
import torch.nn as nn
import torch.nn.functional as F
class VGGBlock(nn.Module):
def __init__(self, in_channels, out_channels, batch_norm=False): # 输入输出通道数,是否使用批量归一化
super().__init__()
conv2_params = {'kernel_size': (3, 3),
'stride' : (1, 1),
'padding' : 1}
noop = lambda x : x
self._batch_norm = batch_norm
# 卷积层
self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels , **conv2_params)
self.bn1 = nn.BatchNorm2d(out_channels) if batch_norm else noop
self.conv2 = nn.Conv2d(in_channels=out_channels, out_channels=out_channels, **conv2_params)
self.bn2 = nn.BatchNorm2d(out_channels) if batch_norm else noop
# 最大池化层
self.max_pooling = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
@property
def batch_norm(self):
return self._batch_norm
def forward(self,x):
# 依次经过conv1、conv2,使用ReLU激活函数,最后通过max_pooling层减小特征图的大小神经网络模型构建
x = self.conv1(x)
x = self.bn1(x)
x = F.relu(x)
x = self.conv2(x)
x = self.bn2(x)
x = F.relu(x)
x = self.max_pooling(x)
return x
class VGG16(nn.Module):
def __init__(self, input_size, num_classes=10, batch_norm=False): # 类别数(num_classes)
super(VGG16, self).__init__()
self.in_channels, self.in_width, self.in_height = input_size
# VGG网络的四个卷积块
self.block_1 = VGGBlock(self.in_channels, 64, batch_norm=batch_norm)
self.block_2 = VGGBlock(64, 128, batch_norm=batch_norm)
self.block_3 = VGGBlock(128, 256, batch_norm=batch_norm)
self.block_4 = VGGBlock(256,512, batch_norm=batch_norm)
# 全连接层
self.classifier = nn.Sequential(
nn.Linear(2048, 4096),
nn.ReLU(True),
nn.Dropout(p=0.65),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(p=0.65),
nn.Linear(4096, num_classes)
)
@property
def input_size(self):
return self.in_channels, self.in_width, self.in_height
def forward(self, x): # 将输入图像x传递给VGGBlock对象,然后将输出特征展平,最后通过全连接层计算类别概率
x = self.block_1(x)
x = self.block_2(x)
x = self.block_3(x)
x = self.block_4(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x