-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgg.py
75 lines (57 loc) · 2.1 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
74
75
from torch import nn
from typing import Literal
cfgs = {
'vgg11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'vgg13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'vgg16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'vgg19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
class VGG(nn.Module):
def __init__(self):
super(VGG, self).__init__()
self.features = None
self.dense = nn.Sequential(
nn.Linear(512, 4096),
nn.ReLU(inplace=True),
nn.Dropout(0.4),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Dropout(0.4),
)
self.classifier = nn.Linear(4096, 10)
def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
out = self.dense(out)
out = self.classifier(out)
return out
@staticmethod
def make_layers(vgg: Literal["vgg11", "vgg13", "vgg16", "vgg19"]):
layers = []
in_channels = 3
for x in cfgs[vgg]:
if x == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
nn.BatchNorm2d(x),
nn.ReLU(inplace=True)]
in_channels = x
layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
return nn.Sequential(*layers)
class VGG11(VGG):
def __init__(self):
super(VGG11, self).__init__()
self.features = VGG.make_layers(vgg="vgg11")
class VGG13(VGG):
def __init__(self):
super(VGG13, self).__init__()
self.features = VGG.make_layers(vgg="vgg13")
class VGG16(VGG):
def __init__(self):
super(VGG16, self).__init__()
self.features = VGG.make_layers(vgg="vgg16")
class VGG19(VGG):
def __init__(self):
super(VGG19, self).__init__()
self.features = VGG.make_layers(vgg="vgg19")