-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgg19.py
More file actions
60 lines (59 loc) · 2.55 KB
/
vgg19.py
File metadata and controls
60 lines (59 loc) · 2.55 KB
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
import torch
class vgg19(torch.nn.Module):
def __init__(self, outputs):
super(vgg19, self).__init__()
self.features = torch.nn.Sequential(
# first block
torch.nn.Conv2d(3, 64, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(64, 64, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# second block
torch.nn.Conv2d(64, 128, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(128, 128, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# third block
torch.nn.Conv2d(128, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(256, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(256, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(256, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# fourth block
torch.nn.Conv2d(256, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# fifth block
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# tail
torch.nn.AdaptiveAvgPool2d((7, 7)))
self.classifier = torch.nn.Sequential(
torch.nn.Linear(512*7*7, 4096),
torch.nn.ReLU(True),
torch.nn.Dropout(0.5),
torch.nn.Linear(4096, 4096),
torch.nn.ReLU(True),
torch.nn.Dropout(0.5),
torch.nn.Linear(4096, outputs))
def forward(self, x):
return self.classifier(torch.flatten(self.features(x), 0))