-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvgg.py
64 lines (60 loc) · 2.22 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
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
class VGG_F(nn.Module):
"""
Main Class
"""
def __init__(self):
"""
Constructor
"""
super().__init__()
self.block_size = [2, 2, 3, 3, 3]
self.conv1_1 = nn.Conv2d(3, 64, 3, stride=1, padding=1)
self.conv1_2 = nn.Conv2d(64, 64, 3, stride=1, padding=1)
self.conv2_1 = nn.Conv2d(64, 128, 3, stride=1, padding=1)
self.conv2_2 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
self.conv3_1 = nn.Conv2d(128, 256, 3, stride=1, padding=1)
self.conv3_2 = nn.Conv2d(256, 256, 3, stride=1, padding=1)
self.conv3_3 = nn.Conv2d(256, 256, 3, stride=1, padding=1)
self.conv4_1 = nn.Conv2d(256, 512, 3, stride=1, padding=1)
self.conv4_2 = nn.Conv2d(512, 512, 3, stride=1, padding=1)
self.conv4_3 = nn.Conv2d(512, 512, 3, stride=1, padding=1)
self.conv5_1 = nn.Conv2d(512, 512, 3, stride=1, padding=1)
self.conv5_2 = nn.Conv2d(512, 512, 3, stride=1, padding=1)
self.conv5_3 = nn.Conv2d(512, 512, 3, stride=1, padding=1)
self.fc6 = nn.Linear(512 * 7 * 7, 4096)
self.fc7 = nn.Linear(4096, 4096)
self.fc8 = nn.Linear(4096, 2622)
def forward(self, x):
""" Pytorch forward
Args:
x: input image (224x224)
Returns: class logits
"""
x = F.relu(self.conv1_1(x))
x = F.relu(self.conv1_2(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2_1(x))
x = F.relu(self.conv2_2(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv3_1(x))
x = F.relu(self.conv3_2(x))
x = F.relu(self.conv3_3(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv4_1(x))
x = F.relu(self.conv4_2(x))
x = F.relu(self.conv4_3(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv5_1(x))
x = F.relu(self.conv5_2(x))
x = F.relu(self.conv5_3(x))
x = F.max_pool2d(x, 2, 2)
x = x.view(x.size(0), -1)
x = F.relu(self.fc6(x))
x = F.dropout(x, 0.5, self.training)
x = F.relu(self.fc7(x))
x = F.dropout(x, 0.5, self.training)
return self.fc8(x)