-
Notifications
You must be signed in to change notification settings - Fork 1
/
5_vgg_pytorch.py
93 lines (79 loc) · 3.62 KB
/
5_vgg_pytorch.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
import torch
import torch.nn as nn
from torchvision.models import vgg16_bn, VGG16_BN_Weights
# Direct Implementation
class VGG16(nn.Module):
def __init__(self):
super(VGG16, self).__init__()
self.features = nn.Sequential(
# first conv block --------------------------------------------------------
nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
# -------------------------------------------------------------------------
# second conv block -------------------------------------------------------
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
# -------------------------------------------------------------------------
# third conv block --------------------------------------------------------
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
# --------------------------------------------------------------------------
# fourth conv block --------------------------------------------------------
nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
# --------------------------------------------------------------------------
# fifth conv block ---------------------------------------------------------
nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
# --------------------------------------------------------------------------
)
# fully connected layers -------------------------------------------------------
self.classifier = nn.Sequential(
nn.Linear(7*7*512, 4096),
nn.ReLU(),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Linear(4096, 1000),
)
# ------------------------------------------------------------------------------
def forward(self, x):
batch_size = x.size(0)
x = self.features(x)
x = x.view(batch_size, -1)
x = self.classifier(x)
return x
# Load Pretrained Model in PyTorch
class VGG16(nn.Module):
def __init__(self, num_classes=1000):
super(VGG16, self).__init__()
model = vgg16_bn(weights=VGG16_BN_Weights.IMAGENET1K_V1)
self.features = model.features
self.avgpool = model.avgpool
self.classifier = model.classifier
self.classifier[6] = nn.Linear(4096, num_classes)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = self.classifier(x)
return x