forked from Kyle1993/Amazon-Kaggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyramidnet.py
297 lines (229 loc) · 10.3 KB
/
pyramidnet.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
# [1] "Feature Pyramid Networks for Object Detection" - Tsung-Yi Lin, Piotr Dollár,
# Ross Girshick, Kaiming He, Bharath Hariharan, Serge Belongie, arxiv 2016
# https://arxiv.org/abs/1612.03144
#
# [2] "DSSD : Deconvolutional Single Shot Detector" - Cheng-Yang Fu, Wei Liu, Ananth Ranga,
# Ambrish Tyagi, Alexander C. Berg, arxiv 2017
#
# [3] "Aggregated Residual Transformations for Deep Neural Networks" - Saining Xie,
# Ross Girshick, Piotr Dollár, Zhuowen Tu, Kaiming He, arxiv 2016
# https://github.com/D-X-Y/ResNeXt/blob/master/models/resnext.py
#
# [4] "Is object localization for free? – Weakly-supervised learning with convolutional neural networks" -
# Maxime Oquab, Léon Bottou, Ivan Laptev, Josef Sivic, cvpr 2015
#
#
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
# from net.common import *
# from net.utility.tool import *
#----- helper functions --------
def make_linear_bn_prelu(in_channels, out_channels):
return [
nn.Linear(in_channels, out_channels, bias=False),
nn.BatchNorm1d(out_channels),
nn.PReLU(out_channels),
]
def make_conv_bn_relu(in_channels, out_channels, kernel_size=3, stride=1, padding=1, groups=1):
return [
nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=groups, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
]
def make_linear_bn_relu(in_channels, out_channels):
return [
nn.Linear(in_channels, out_channels, bias=False),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True),
]
def make_max_flat(out):
flat = F.adaptive_max_pool2d(out,output_size=1) ##nn.AdaptiveMaxPool2d(1)(out)
flat = flat.view(flat.size(0), -1)
return flat
def make_avg_flat(out):
flat = F.adaptive_avg_pool2d(out,output_size=1)
flat = flat.view(flat.size(0), -1)
return flat
def make_shortcut(out, modifier):
if modifier is None:
return out
else:
return modifier(out)
def make_flat(out):
#flat = F.adaptive_avg_pool2d(out,output_size=4)
out = F.avg_pool2d(out,kernel_size=4, stride=2, padding=0)
out = F.adaptive_max_pool2d(out,output_size=1)
flat = out.view(out.size(0), -1)
return flat
#############################################################################3
class PyNet_10(nn.Module):
def __init__(self, in_shape, num_classes):
super(PyNet_10, self).__init__()
in_channels, height, width = in_shape
self.preprocess = nn.Sequential(
*make_conv_bn_relu(in_channels, 16, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(16, 16, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(16, 16, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(16, 16, kernel_size=1, stride=1, padding=0 ),
) # 128
self.conv1d = nn.Sequential(
*make_conv_bn_relu(16,32, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(32,32, kernel_size=3, stride=1, padding=1 ),
*make_conv_bn_relu(32,64, kernel_size=1, stride=1, padding=0 ),
) # 128
self.shortld = nn.Conv2d(16, 64, kernel_size=1, stride=1, padding=0, bias=False)
self.conv2d = nn.Sequential(
*make_conv_bn_relu(64,64, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(64,64, kernel_size=3, stride=1, padding=1 ),
*make_conv_bn_relu(64,128, kernel_size=1, stride=1, padding=0 ),
) # 64
self.short2d = nn.Conv2d(64, 128, kernel_size=1, stride=1, padding=0, bias=False)
self.conv3d = nn.Sequential(
*make_conv_bn_relu(128,128, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(128,128, kernel_size=3, stride=1, padding=1, groups=16 ),
*make_conv_bn_relu(128,256, kernel_size=1, stride=1, padding=0 ),
) # 32
self.short3d = nn.Conv2d(128, 256, kernel_size=1, stride=1, padding=0, bias=False)
self.conv4d = nn.Sequential(
*make_conv_bn_relu(256,256, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(256,256, kernel_size=3, stride=1, padding=1, groups=16 ),
*make_conv_bn_relu(256,256, kernel_size=1, stride=1, padding=0 ),
) # 16
self.short4d = None #nn.Identity()
self.conv5d = nn.Sequential(
*make_conv_bn_relu(256,256, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(256,256, kernel_size=3, stride=1, padding=1, groups=16 ),
*make_conv_bn_relu(256,256, kernel_size=1, stride=1, padding=0 ),
) # 8
self.short5d = None # nn.Identity()
self.conv4u = nn.Sequential(
*make_conv_bn_relu(256,256, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(256,256, kernel_size=3, stride=1, padding=1, groups=16 ),
*make_conv_bn_relu(256,256, kernel_size=1, stride=1, padding=0 ),
) # 16
self.conv3u = nn.Sequential(
*make_conv_bn_relu(256,128, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(128,128, kernel_size=3, stride=1, padding=1, groups=16 ),
*make_conv_bn_relu(128,128, kernel_size=1, stride=1, padding=0 ),
) # 32
self.conv2u = nn.Sequential(
*make_conv_bn_relu(128,64, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu( 64,64, kernel_size=3, stride=1, padding=1 ),
*make_conv_bn_relu( 64,64, kernel_size=1, stride=1, padding=0 ),
) # 64
self.conv1u = nn.Sequential(
*make_conv_bn_relu(64,64, kernel_size=1, stride=1, padding=0 ),
*make_conv_bn_relu(64,64, kernel_size=3, stride=1, padding=1 ),
*make_conv_bn_relu(64,64, kernel_size=1, stride=1, padding=0 ),
) # 128
self.cls2d = nn.Sequential(
*make_linear_bn_relu(128, 512),
*make_linear_bn_relu(512, 512),
nn.Linear(512, num_classes)
)
self.cls3d = nn.Sequential(
*make_linear_bn_relu(256, 512),
*make_linear_bn_relu(512, 512),
nn.Linear(512, num_classes)
)
self.cls4d = nn.Sequential(
*make_linear_bn_relu(256, 512),
*make_linear_bn_relu(512, 512),
nn.Linear(512, num_classes)
)
self.cls5d = nn.Sequential(
*make_linear_bn_relu(256, 512),
*make_linear_bn_relu(512, 512),
nn.Linear(512, num_classes)
)
self.cls1u = nn.Sequential(
*make_linear_bn_relu(64, 512),
*make_linear_bn_relu(512, 512),
nn.Linear(512, num_classes)
)
self.cls2u = nn.Sequential(
*make_linear_bn_relu( 64, 512),
*make_linear_bn_relu(512, 512),
nn.Linear(512, num_classes)
)
self.cls3u = nn.Sequential(
*make_linear_bn_relu(128, 512),
*make_linear_bn_relu(512, 512),
nn.Linear(512, num_classes)
)
self.cls4u = nn.Sequential(
*make_linear_bn_relu(256, 512),
*make_linear_bn_relu(512, 512),
nn.Linear(512, num_classes)
)
def forward(self, x):
out = self.preprocess(x) #128
conv1d = self.conv1d(out) #128
out = F.max_pool2d(conv1d, kernel_size=2, stride=2) # 64
conv2d = self.conv2d(out) + make_shortcut(out, self.short2d) # 64
out = F.max_pool2d(conv2d, kernel_size=2, stride=2) # 32
flat2d = make_max_flat(out)
conv3d = self.conv3d(out) + make_shortcut(out, self.short3d) # 32
out = F.max_pool2d(conv3d, kernel_size=2, stride=2) # 16
flat3d = make_max_flat(out)
conv4d = self.conv4d(out) + make_shortcut(out, self.short4d) # 16
out = F.max_pool2d(conv4d, kernel_size=2, stride=2) # 8
flat4d = make_max_flat(out)
conv5d = self.conv5d(out) + make_shortcut(out, self.short5d) # 8
out = conv5d # 4
flat5d = make_max_flat(out)
out = F.upsample_bilinear(out,scale_factor=2) # 16
out = out + conv4d
out = self.conv4u(out)
flat4u = make_max_flat(out)
out = F.upsample_bilinear(out,scale_factor=2) # 32
out = out + conv3d
out = self.conv3u(out)
flat3u = make_max_flat(out)
out = F.upsample_bilinear(out,scale_factor=2) # 64
out = out + conv2d
out = self.conv2u(out)
flat2u = make_max_flat(out)
out = F.upsample_bilinear(out,scale_factor=2) #128
out = out + conv1d
out = self.conv1u(out)
flat1u = make_max_flat(out)
logit2d = self.cls2d(flat2d).unsqueeze(2)
logit3d = self.cls3d(flat3d).unsqueeze(2)
logit4d = self.cls4d(flat4d).unsqueeze(2)
logit5d = self.cls5d(flat5d).unsqueeze(2)
logit1u = self.cls1u(flat1u).unsqueeze(2)
logit2u = self.cls2u(flat2u).unsqueeze(2)
logit3u = self.cls3u(flat3u).unsqueeze(2)
logit4u = self.cls4u(flat4u).unsqueeze(2)
logit = torch.cat((logit2d,logit3d,logit4d,logit5d,logit1u,logit2u,logit3u,logit4u),2)
logit = F.dropout(logit, p=0.15,training=self.training)
logit = logit.sum(2)
logit = logit.view(logit.size(0),logit.size(1)) #unsqueeze(2)
prob = F.sigmoid(logit)
return prob
def getname(self):
return 'pynet10'
# main #################################################################
if __name__ == '__main__':
print( '%s: calling main function ... ' % os.path.basename(__file__))
# https://discuss.pytorch.org/t/print-autograd-graph/692/8
#inputs = torch.randn(96,3,128,128)
inputs = torch.randn(1,3,112,112)
#inputs = torch.randn(96,3,96,96)
in_shape = inputs.size()[1:]
num_classes = 17
if 1:
net = PyNet_10(in_shape,num_classes).cuda().train()
x = Variable(inputs).cuda()
start = timer()
logit,prob = net.forward(x)
end = timer()
print ('cuda(): end-start=%0.0f ms'%((end - start)*1000))
#dot = make_dot(y)
#dot.view()
print(type(net))
print(net)
print(prob)