/
vgg.py
155 lines (130 loc) · 4.68 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import torch
import torchvision.models as models
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
from utils import *
class VGG16_conv(torch.nn.Module):
def __init__(self, n_classes, args):
super(VGG16_conv, self).__init__()
self.std = args.std
self.factor = args.std_factor
self.epoch = args.epoch
self.kernel_size = args.kernel_size
self.conv1 = torch.nn.Sequential(
torch.nn.Conv2d(3, 64, 3, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(64, 64, 3, padding=1),
)
self.post1 = torch.nn.Sequential(
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, stride=2)
)
self.conv2 = torch.nn.Sequential(
torch.nn.Conv2d(64, 128, 3, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(128, 128, 3, padding=1),
)
self.post2 = torch.nn.Sequential(
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, stride=2)
)
self.conv3 = torch.nn.Sequential(
torch.nn.Conv2d(128, 256, 3, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(256, 256, 3, padding=1),
)
self.post3 = torch.nn.Sequential(
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, stride=2)
)
self.conv4 = torch.nn.Sequential(
torch.nn.Conv2d(256, 512, 3, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(512, 512, 3, padding=1),
)
self.post4 = torch.nn.Sequential(
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, stride=2)
)
self.conv5 = torch.nn.Sequential(
torch.nn.Conv2d(512, 512, 3, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(512, 512, 3, padding=1),
)
self.post5 = torch.nn.Sequential(
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, stride=2)
)
self.classifier = torch.nn.Sequential(
torch.nn.Linear(512, 4096),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(4096, 4096),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(4096, n_classes)
)
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.constant_(m.bias, 0)
def get_new_kernels(self, epoch_count):
if epoch_count % self.epoch == 0 and epoch_count is not 0:
self.std *= 0.9
self.kernel1 = get_gaussian_filter(
kernel_size=self.kernel_size,
sigma=self.std,
channels=64
)
self.kernel2= get_gaussian_filter(
kernel_size=self.kernel_size,
sigma=self.std,
channels=128
)
self.kernel3 = get_gaussian_filter(
kernel_size=self.kernel_size,
sigma=self.std,
channels=256
)
self.kernel4 = get_gaussian_filter(
kernel_size=self.kernel_size,
sigma=self.std,
channels=512
)
self.kernel5 = get_gaussian_filter(
kernel_size=self.kernel_size,
sigma=self.std,
channels=512
)
def forward(self, x, return_intermediate=False):
output = self.conv1(x)
output = self.kernel1(output)
output = self.post1(output)
output = self.conv2(output)
output = self.kernel2(output)
output = self.post2(output)
output = self.conv3(output)
output = self.kernel3(output)
output = self.post3(output)
output = self.conv4(output)
output = self.kernel4(output)
output = self.post4(output)
output = self.conv5(output)
output = self.kernel5(output)
if return_intermediate:
output = output.view(output.size(0), -1)
return output
output = self.post5(output)
output = output.view(output.size(0), -1)
output = self.classifier(output)
return output