forked from LeftAttention/PI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPI_utils.py
315 lines (248 loc) · 11.8 KB
/
PI_utils.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#########################################################################
##
## Some utility for training, data processing, and network.
##
#########################################################################
import torch
import torch.nn as nn
from parameters import Parameters
p = Parameters()
def backward_hook(self, grad_input, grad_output):
print('grad_input norm:', grad_input[0].data.norm())
######################################################################
##
## Convolution layer modules
##
######################################################################
class Conv2D_BatchNorm_Relu(nn.Module):
def __init__(self, in_channels, n_filters, k_size, padding, stride, bias=True, acti=True, dilation=1):
super(Conv2D_BatchNorm_Relu, self).__init__()
if acti:
self.cbr_unit = nn.Sequential(nn.Conv2d(in_channels, n_filters, k_size,
padding=padding, stride=stride, bias=bias, dilation=dilation),
nn.BatchNorm2d(n_filters),
#nn.ReLU(inplace=True),)
nn.PReLU(),)
else:
self.cbr_unit = nn.Conv2d(in_channels, n_filters, k_size, padding=padding, stride=stride, bias=bias, dilation=dilation)
def forward(self, inputs):
outputs = self.cbr_unit(inputs)
return outputs
class bottleneck(nn.Module):
def __init__(self, in_channels, out_channels, acti=True):
super(bottleneck, self).__init__()
self.acti = acti
temp_channels = in_channels//4
if in_channels < 4:
temp_channels = in_channels
self.conv1 = Conv2D_BatchNorm_Relu(in_channels, temp_channels, 1, 0, 1)
self.conv2 = Conv2D_BatchNorm_Relu(temp_channels, temp_channels, 3, 1, 1)
self.conv3 = Conv2D_BatchNorm_Relu(temp_channels, out_channels, 1, 0, 1, acti = self.acti)
self.residual = Conv2D_BatchNorm_Relu(in_channels, out_channels, 1, 0, 1)
def forward(self, x):
re = x
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
if not self.acti:
return out
re = self.residual(x)
out = out + re
return out
class bottleneck_down(nn.Module):
def __init__(self, in_channels, out_channels):
super(bottleneck_down, self).__init__()
temp_channels = in_channels//4
if in_channels < 4:
temp_channels = in_channels
self.conv1 = Conv2D_BatchNorm_Relu(in_channels, temp_channels, 3, 1, 2)
self.conv2 = Conv2D_BatchNorm_Relu(temp_channels, temp_channels, 3, 1, 1, dilation=1)
#self.conv3 = Conv2D_BatchNorm_Relu(temp_channels, out_channels, 1, 0, 1)
self.conv3 = nn.Conv2d(temp_channels, out_channels, 1, padding=0, stride=1, bias=True)
#self.residual = Conv2D_BatchNorm_Relu(in_channels, out_channels, 3, 1, 2, acti=False)
self.residual = nn.MaxPool2d(2, 2)
self.dropout = nn.Dropout2d(p=0.1)
self.prelu = nn.PReLU()
def forward(self, x, residual=False):
re = x
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
#out = self.dropout(out)
#re = self.residual(x)
#out = out + re
if residual:
return out
else:
out = self.prelu(out)
return out
class bottleneck_up(nn.Module):
def __init__(self, in_channels, out_channels):
super(bottleneck_up, self).__init__()
temp_channels = in_channels//4
if in_channels < 4:
temp_channels = in_channels
self.conv1 = nn.Sequential( nn.ConvTranspose2d(in_channels, temp_channels, 3, 2, 1, 1),
nn.BatchNorm2d(temp_channels),
nn.PReLU() )
self.conv2 = Conv2D_BatchNorm_Relu(temp_channels, temp_channels, 3, 1, 1, dilation=1)
#self.conv3 = Conv2D_BatchNorm_Relu(temp_channels, out_channels, 1, 0, 1)
self.conv3 = nn.Conv2d(temp_channels, out_channels, 1, padding=0, stride=1, bias=True)
#self.residual = nn.ConvTranspose2d(in_channels, out_channels, 3, 2, 1, 1)
#self.residual = nn.Sequential( nn.ConvTranspose2d(in_channels, out_channels, 3, 2, 1, 1),
# nn.BatchNorm2d(out_channels),
# nn.ReLU() )
self.residual = nn.Upsample(size=None, scale_factor=2, mode='bilinear')
self.dropout = nn.Dropout2d(p=0.1)
self.prelu = nn.PReLU()
def forward(self, x):
re = x
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
#out = self.dropout(out)
#re = self.residual(re)
#out = out + re
#out = self.prelu(out)
return out
class bottleneck_dilation(nn.Module):
def __init__(self, in_channels, out_channels):
super(bottleneck_dilation, self).__init__()
temp_channels = in_channels//4
if in_channels < 4:
temp_channels = in_channels
self.conv1 = Conv2D_BatchNorm_Relu(in_channels, temp_channels, 1, 0, 1)
self.conv2 = Conv2D_BatchNorm_Relu(temp_channels, temp_channels, 3, 1, 1, dilation=1)
self.conv3 = nn.Conv2d(temp_channels, out_channels, 1, padding=0, stride=1, bias=True)
#self.residual = Conv2D_BatchNorm_Relu(in_channels, out_channels, 1, 0, 1)
self.dropout = nn.Dropout2d(p=0.1)
self.prelu = nn.PReLU()
def forward(self, x, residual=False):
re = x
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
#out = self.dropout(out)
#re = self.residual(x)
#out = out + re
if residual:
return out
else:
out = self.prelu(out)
return out
class Output(nn.Module):
def __init__(self, in_size, out_size):
super(Output, self).__init__()
self.conv1 = Conv2D_BatchNorm_Relu(in_size, in_size//2, 3, 1, 1, dilation=1)
self.conv2 = Conv2D_BatchNorm_Relu(in_size//2, in_size//4, 3, 1, 1, dilation=1)
self.conv3 = Conv2D_BatchNorm_Relu(in_size//4, out_size, 1, 0, 1, acti = False)
def forward(self, inputs):
outputs = self.conv1(inputs)
outputs = self.conv2(outputs)
outputs = self.conv3(outputs)
return outputs
class hourglass_same(nn.Module):
def __init__(self, in_channels, out_channels):
super(hourglass_same, self).__init__()
self.down1 = bottleneck_down(in_channels, out_channels)
self.down2 = bottleneck_down(out_channels, out_channels)
self.down3 = bottleneck_down(out_channels, out_channels)
self.down4 = bottleneck_down(out_channels, out_channels)
self.same1 = bottleneck_dilation(out_channels, out_channels)
self.same2 = bottleneck_dilation(out_channels, out_channels)
self.same3 = bottleneck_dilation(out_channels, out_channels)
self.same4 = bottleneck_dilation(out_channels, out_channels)
self.up1 = bottleneck_up(out_channels, out_channels)
self.up2 = bottleneck_up(out_channels, out_channels)
self.up3 = bottleneck_up(out_channels, out_channels)
self.up4 = bottleneck_up(out_channels, out_channels)
self.residual1 = bottleneck_down(out_channels, out_channels)
self.residual2 = bottleneck_down(out_channels, out_channels)
self.residual3 = bottleneck_down(out_channels, out_channels)
self.residual4 = bottleneck_down(in_channels, out_channels)
#self.residual = nn.MaxPool2d(2, 2)
self.bn = nn.BatchNorm2d(out_channels)
self.bn1 = nn.BatchNorm2d(out_channels)
self.bn2 = nn.BatchNorm2d(out_channels)
self.bn3 = nn.BatchNorm2d(out_channels)
self.bn4 = nn.BatchNorm2d(out_channels)
self.prelu = nn.PReLU()
def forward(self, inputs):
outputs1 = self.down1(inputs) # 64*32 -> 32*16
outputs2 = self.down2(outputs1) # 32*16 -> 16*8
outputs3 = self.down3(outputs2) # 16*8 -> 8*4
outputs4 = self.down4(outputs3) # 8*4 -> 4*2
outputs = self.same1(outputs4) # 4*2 -> 4*2
feature = self.same2(outputs, True) # 4*2 -> 4*2
outputs = self.same3(self.prelu(self.bn(feature))) # 4*2 -> 4*2
outputs = self.same4(outputs, True) # 4*2 -> 4*2
outputs = self.up1( self.prelu(self.bn1(outputs + self.residual1(outputs3, True))) )
outputs = self.up2( self.prelu(self.bn2(outputs + self.residual2(outputs2, True))) )
outputs = self.up3( self.prelu(self.bn3(outputs + self.residual3(outputs1, True))) )
outputs = self.up4( self.prelu(self.bn4(outputs + self.residual4(inputs, True))) )
#outputs = self.up3( self.prelu(self.bn3(outputs)) )
#outputs = self.up4( self.prelu(self.bn4(outputs)) )
#outputs = self.prelu(outputs)
return outputs, feature
class resize_layer(nn.Module):
def __init__(self, in_channels, out_channels, acti = True):
super(resize_layer, self).__init__()
self.conv1 = Conv2D_BatchNorm_Relu(in_channels, out_channels//4, 3, 1, 2, dilation=1, acti = False)
self.conv2 = Conv2D_BatchNorm_Relu(out_channels//4, out_channels//2, 3, 1, 2, dilation=1, acti = False)
self.conv3 = Conv2D_BatchNorm_Relu(out_channels//2, out_channels//1, 3, 1, 2, dilation=1, acti = False)
self.maxpool = nn.MaxPool2d(2, 2)
self.bn1 = nn.BatchNorm2d(out_channels//4)
self.bn2 = nn.BatchNorm2d(out_channels//2)
self.bn3 = nn.BatchNorm2d(out_channels//1)
self.prelu = nn.PReLU()
def forward(self, inputs):
#re = self.maxpool(inputs)
outputs = self.conv1(inputs)
outputs = self.bn1(outputs)
#outputs = torch.cat((outputs, re),1)
outputs = self.prelu(outputs)
#re = self.maxpool(outputs)
outputs = self.conv2(outputs)
outputs = self.bn2(outputs)
#outputs = torch.cat((outputs, re),1)
outputs = self.prelu(outputs)
#re = self.maxpool(outputs)
outputs = self.conv3(outputs)
#outputs = self.bn3(outputs)
#outputs = torch.cat((outputs, re),1)
# #outputs = self.prelu(outputs)
return outputs
class hourglass_block(nn.Module):
def __init__(self, in_channels, out_channels, acti = True, input_re=True):
super(hourglass_block, self).__init__()
self.layer1 = hourglass_same(in_channels, out_channels)
self.re1 = bottleneck_dilation(out_channels, out_channels)
self.re2 = nn.Conv2d(out_channels, out_channels, 1, padding=0, stride=1, bias=True, dilation=1)
self.re3 = nn.Conv2d(1, out_channels, 1, padding=0, stride=1, bias=True, dilation=1)
self.out_confidence = Output(out_channels, 1)
self.out_offset = Output(out_channels, 2)
self.out_instance = Output(out_channels, p.feature_size)
self.bn1 = nn.BatchNorm2d(out_channels)
self.bn2 = nn.BatchNorm2d(out_channels)
self.bn3 = nn.BatchNorm2d(1)
self.input_re = input_re
self.prelu = nn.PReLU()
self.dropout = nn.Dropout2d(p=0.1)
def forward(self, inputs):
inputs_a = self.prelu(self.bn1(inputs))
outputs, feature = self.layer1(inputs_a)
outputs_a = self.bn2(outputs)
outputs_a = self.prelu(outputs_a)
outputs_a = self.re1(outputs_a)
outputs = self.re2(outputs_a)
out_confidence = self.out_confidence(outputs_a)
out_offset = self.out_offset(outputs_a)
out_instance = self.out_instance(outputs_a)
out = self.prelu( self.bn3(out_confidence) )
out = self.re3(out)
#out = self.dropout(out)
if self.input_re:
outputs = outputs + out + inputs
else:
outputs = outputs + out
return [out_confidence, out_offset, out_instance], outputs, feature