-
Notifications
You must be signed in to change notification settings - Fork 0
/
denseconv.py
executable file
·261 lines (243 loc) · 7.31 KB
/
denseconv.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
261
# from https://github.com/gpleiss/efficient_densenet_pytorch/blob/master/models/densenet.py
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.checkpoint as cp
import config
from baseconv import LayerBase
def _bn_function_factory(norm:nn.Module, relu:nn.Module, conv:nn.Module):
"""
@brief: returns a function which concatenates the inputs and applies BN, RELU as well as CONV on it
"""
def bn_function(*inputs):
concated_features = torch.cat(inputs, 1)
bottleneck_output = conv(
relu(
norm(
concated_features
)
)
)
return bottleneck_output
return bn_function
class DenseLayer(LayerBase):
"""
@brief: concatenates the inputs of previous layers, applies batch normalization, activation function as well as a conv layer with kernel size 1x1. Afterwards, the outputs are again batch noramlized, a activation function is applied and a conv layer with kernel size 3x3 and stride 1x1 is utilized.
"""
def __init__(self, in_channels, growth_rate, bn_size, efficient=False):
super(DenseLayer, self).__init__()
self.add_module(
"norm1",
nn.BatchNorm2d(
in_channels
)
)
self.add_module(
"relu1",
nn.ReLU(
inplace=True
)
)
self.add_module(
"conv1",
nn.Conv2d(
in_channels,
bn_size * growth_rate,
kernel_size=1,
stride=1,
bias=False
)
)
self._initialize(self.conv1)
self.add_module(
"norm2",
nn.BatchNorm2d(
bn_size * growth_rate
)
)
self.add_module(
"relu2",
nn.ReLU(
inplace=True
)
)
self.add_module(
"conv2",
nn.Conv2d(
bn_size * growth_rate,
growth_rate,
kernel_size=3,
stride=1,
padding=1,
bias=False
)
)
self._initialize(self.conv2)
self.drop_rate = config.dropout_rate
self.efficient = efficient
def forward(self, *prev_features):
bn_function = _bn_function_factory(
self.norm1,
self.relu1,
self.conv1
)
if self.efficient and any(prev_feature.requires_grad for prev_feature in prev_features):
bottleneck_output = cp.checkpoint(
bn_function,
*prev_features
)
else:
bottleneck_output = bn_function(
*prev_features
)
new_features = self.conv2(
self.relu2(
self.norm2(
bottleneck_output
)
)
)
if config.employ_dropout_conv:
new_features = F.dropout(
new_features,
p=self.drop_rate,
training=self.training
)
return new_features
class Transition(nn.Sequential):
"""
@brief: reduces the output size of Denseblock. Consists of batchnormalization, activation function, convolutional layer and an average pooling layer.
"""
def __init__(
self,
in_channels,
out_channles
):
super(Transition, self).__init__()
self.add_module(
"norm",
nn.BatchNorm2d(
in_channels
)
)
self.add_module(
"relu",
nn.ReLU(
inplace=True
)
)
self.add_module(
"conv",
nn.Conv2d(
in_channels,
out_channles,
kernel_size=1,
stride=1,
bias=False
)
)
self.add_module(
"pool",
nn.AvgPool2d(
kernel_size=2,
stride=2
)
)
class DenseBlock(nn.Module):
r"""DenseBlock-BC model class, based on
`"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
Args:
num_layers (int) - determines, how many layers, the denseblock contains.
in_channles (int) - the number of filters to learn in the first convolution layer
bn_size (int) - multiplicative factor for number of bottle neck layers
(i.e. bn_size * k features in the bottleneck layer)
growth_rate (int) - how many filters to add each layer (`k` in paper)
drop_rate (float) - dropout rate after each dense layer
efficient (bool) - set to True to use checkpointing. Much more memory efficient, but slower.
compression (float) - About which fraction, the number of output filters should be reduced.
"""
def __init__(
self,
num_layers,
in_channels,
bn_size=4,
growth_rate=12,
efficient=True,
compression=0.5
):
super(DenseBlock, self).__init__()
for i in range(num_layers):
layer = DenseLayer(
in_channels + i * growth_rate,
growth_rate=growth_rate,
bn_size=bn_size,
efficient=efficient,
)
self.add_module(
"denselayer%d" % (i + 1),
layer
)
out_channels = in_channels+num_layers * growth_rate
trans = Transition(
in_channels=out_channels,
out_channles=int(
out_channels * compression
)
)
self.add_module(
"transition%d" % (i + 1),
trans
)
self.out_channels = int(
out_channels * compression
)
def forward(self, init_features):
features = [init_features]
for name, layer in self.named_children():
if "transition" not in name:
new_features = layer(*features)
features.append(new_features)
else:
features = layer(
torch.cat(
features,
1
)
)
return features
class SampleDenseNet(nn.Module):
"""
@brief: Sample Network demonstrating the utilization of a DenseBlock.
"""
def __init__(self,in_channels,num_layers=16):
super(SampleDenseNet, self).__init__()
self.add_module(
"db",
DenseBlock(
num_layers=num_layers,in_channels=in_channels
)
)
self.add_module(
"flatten",
nn.Flatten()
)
self.add_module(
"gap",
nn.AdaptiveAvgPool2d(
(1,1)
)
)
self.add_module(
"fc",
nn.Linear(
self.db.out_channels,
10
)
)
def forward(self,x):
x = self.db(x)
x = self.gap(x)
x = self.flatten(x)
x = self.fc(x)
output = F.log_softmax(x, dim=1)
return output