-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
260 lines (207 loc) · 7.5 KB
/
model.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import torch
import torch.nn as nn
class DenseLayer(nn.Module):
"""
Dense layer
"""
def __init__(self,
in_channels,
expand_factor=4,
growth_rate=32):
super(DenseLayer, self).__init__()
self.in_channels = in_channels
self.growth_rate = growth_rate
self.bottleneck_size = expand_factor * growth_rate
self.conv1x1 = self.get_conv1x1()
self.conv3x3 = self.get_conv3x3()
def get_conv1x1(self):
"""
returns a stack of Batch Normalization, ReLU, and
1x1 Convolution layers
"""
layers = []
layers.append(nn.BatchNorm2d(num_features=self.in_channels))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(in_channels=self.in_channels,
out_channels=self.bottleneck_size,
kernel_size=1,
stride=1,
bias=False))
return nn.Sequential(*layers)
def get_conv3x3(self):
"""
returns a stack of Batch Normalization, ReLU, and
3x3 Convolutional layers
"""
layers = []
layers.append(nn.BatchNorm2d(num_features=self.bottleneck_size))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(in_channels=self.bottleneck_size,
out_channels=self.growth_rate,
kernel_size=3,
stride=1,
padding=1,
bias=False))
return nn.Sequential(*layers)
def forward(self, x):
"""
feed forward
"""
y = self.conv1x1(x)
y = self.conv3x3(y)
y = torch.cat([x, y], 1)
return y
class DenseBlock(nn.Module):
"""
Dense block
"""
def __init__(self,
in_channels,
num_layers,
expand_factor=4,
growth_rate=32):
super(DenseBlock, self).__init__()
self.in_channels = in_channels
self.num_layers = num_layers
self.expand_factor = expand_factor
self.growth_rate = growth_rate
self.net = self.get_network()
def get_network(self):
"""
return num_layers dense layers
"""
layers = []
for i in range(self.num_layers):
in_channels = self.in_channels + i * self.growth_rate
layers.append(DenseLayer(in_channels=in_channels,
expand_factor=self.expand_factor,
growth_rate=self.growth_rate))
return nn.Sequential(*layers)
def forward(self, x):
"""
feed forward
"""
return self.net(x)
class TransitionBlock(nn.Module):
"""
Transition block
"""
def __init__(self, in_channels, out_channels):
super(TransitionBlock, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.net = self.get_network()
def get_network(self):
"""
returns the structure of the block
"""
layers = []
layers.append(nn.BatchNorm2d(num_features=self.in_channels))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(in_channels=self.in_channels,
out_channels=self.out_channels,
kernel_size=1,
stride=1,
bias=False))
layers.append(nn.AvgPool2d(kernel_size=2,
stride=2))
return nn.Sequential(*layers)
def forward(self, x):
"""
forward pass
"""
return self.net(x)
"""
different configurations of DenseNet
"""
configs = {
'121': [6, 12, 24, 16],
'169': [6, 12, 32, 32],
'201': [6, 12, 48, 32],
'264': [6, 12, 64, 48]
}
class STDN(nn.Module):
"""STDN Architecture"""
def __init__(self,
config,
channels,
class_count,
num_features=64,
compress_factor=2,
expand_factor=4,
growth_rate=32):
super(STDN, self).__init__()
self.config = configs[config]
self.channels = channels
self.class_count = class_count
self.num_features = num_features
self.compress_factor = compress_factor
self.expand_factor = expand_factor
self.growth_rate = growth_rate
self.stem_block = self.get_stem_block()
self.dense_blocks = self.get_dense_blocks()
self.init_weights()
def get_stem_block(self):
"""
returns the stem block of the STDN network
"""
layers = []
layers.append(nn.BatchNorm2d(num_features=self.channels))
layers.append(nn.Conv2d(in_channels=self.channels,
out_channels=self.num_features,
kernel_size=3,
stride=2,
padding=1))
layers.append(nn.BatchNorm2d(num_features=self.num_features))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(in_channels=self.num_features,
out_channels=self.num_features,
kernel_size=3,
stride=1,
padding=1))
layers.append(nn.BatchNorm2d(num_features=self.num_features))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(in_channels=self.num_features,
out_channels=self.num_features,
kernel_size=3,
stride=1,
padding=1))
layers.append(nn.AvgPool2d(kernel_size=2, stride=2))
return nn.Sequential(*layers)
def get_dense_blocks(self):
"""
returns the convolutional layers of the network
"""
layers = []
for i, num_layers in enumerate(self.config):
layers.append(DenseBlock(in_channels=self.num_features,
num_layers=num_layers,
expand_factor=self.expand_factor,
growth_rate=self.growth_rate))
self.num_features += num_layers * self.growth_rate
if i != len(self.config) - 1:
out_channels = self.num_features // self.compress_factor
layers.append(TransitionBlock(in_channels=self.num_features,
out_channels=out_channels))
self.num_features = out_channels
return nn.Sequential(*layers)
def init_weights(self):
"""
initializes weights for each layer
"""
for module in self.modules():
if isinstance(module, nn.Conv2d):
nn.init.kaiming_normal_(module.weight)
elif isinstance(module, nn.BatchNorm2d):
nn.init.constant_(module.weight, 1)
nn.init.constant_(module.bias, 0)
elif isinstance(module, nn.Linear):
nn.init.constant_(module.bias, 0)
def forward(self, x):
"""
feed forward
"""
y = self.conv_net(x)
y = y.view(-1, y.size(1) * y.size(2) * y.size(3))
y = self.fc_net(y)
return y