forked from ElvishElvis/68-Retinaface-Pytorch-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
462 lines (363 loc) · 16.7 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import torch.nn as nn
import torch
import math
import time
import torch.utils.model_zoo as model_zoo
from utils import BasicBlock, Bottleneck, RegressionTransform
from anchors import Anchors
import losses
model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}
class PyramidFeatures(nn.Module):
def __init__(self, C2_size, C3_size, C4_size, C5_size, feature_size=256):
super(PyramidFeatures, self).__init__()
# upsample C5 to get P5 from the FPN paper
self.P5_1 = nn.Conv2d(C5_size, feature_size, kernel_size=1, stride=1, padding=0)
self.P5_upsampled = nn.Upsample(scale_factor=2, mode='nearest')
self.P5_2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, stride=1, padding=1)
# add P5 elementwise to C4
self.P4_1 = nn.Conv2d(C4_size, feature_size, kernel_size=1, stride=1, padding=0)
self.P4_upsampled = nn.Upsample(scale_factor=2, mode='nearest')
self.P4_2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, stride=1, padding=1)
# add P4 elementwise to C3
self.P3_1 = nn.Conv2d(C3_size, feature_size, kernel_size=1, stride=1, padding=0)
self.P3_upsampled = nn.Upsample(scale_factor=2, mode='nearest')
self.P3_2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, stride=1, padding=1)
# "P6 is obtained via a 3x3 stride-2 conv on C5"
self.P6 = nn.Conv2d(C5_size, feature_size, kernel_size=3, stride=2, padding=1)
# "P7 is computed by applying ReLU followed by a 3x3 stride-2 conv on P6"
# Retinaface does not need P7
# self.P7_1 = nn.ReLU()
# self.P7_2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, stride=2, padding=1)
# solve C2
self.P2_1 = nn.Conv2d(C2_size, feature_size, kernel_size=1, stride=1, padding=0)
self.P2_2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, stride=1, padding=1)
def forward(self, inputs):
C2, C3, C4, C5 = inputs
P5_x = self.P5_1(C5)
P5_upsampled_x = self.P5_upsampled(P5_x)
P5_x = self.P5_2(P5_x)
P4_x = self.P4_1(C4)
P4_x = P5_upsampled_x + P4_x
P4_upsampled_x = self.P4_upsampled(P4_x)
P4_x = self.P4_2(P4_x)
P3_x = self.P3_1(C3)
P3_x = P3_x + P4_upsampled_x
P3_upsampled_x = self.P3_upsampled(P3_x)
P3_x = self.P3_2(P3_x)
P2_x = self.P2_1(C2)
P2_x = P2_x + P3_upsampled_x
P2_x = self.P2_2(P2_x)
P6_x = self.P6(C5)
return [P2_x, P3_x, P4_x, P5_x, P6_x]
class ClassHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(ClassHead,self).__init__()
self.num_anchors = num_anchors
self.conv1x1 = nn.Conv2d(inchannels,self.num_anchors*2,kernel_size=(1,1),stride=1)
# if use focal loss instead of OHEM
#self.output_act = nn.Sigmoid()
# if use OHEM
self.output_act = nn.LogSoftmax(dim=-1)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1)
b, h, w, c = out.shape
out = out.view(b, h, w, self.num_anchors, 2)
#out = out.permute(0,2,3,1).contiguous().view(out.shape[0], -1, 2)
out = self.output_act(out)
return out.contiguous().view(out.shape[0], -1, 2)
class BboxHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(BboxHead,self).__init__()
self.conv1x1 = nn.Conv2d(inchannels,num_anchors*4,kernel_size=(1,1),stride=1)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1)
return out.contiguous().view(out.shape[0], -1, 4)
class LandmarkHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(LandmarkHead,self).__init__()
self.conv1x1 = nn.Conv2d(inchannels,num_anchors*10,kernel_size=(1,1),stride=1)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1)
return out.contiguous().view(out.shape[0], -1, 10)
class ClassHead_(nn.Module):
def __init__(self,inchannels=256,num_anchors=3):
super(ClassHead_,self).__init__()
self.num_anchors = num_anchors
self.feature_head = self._make_head(self.num_anchors*2)
self.output_act = nn.LogSoftmax(dim=-1)
def _make_head(self,out_size):
layers = []
for _ in range(4):
layers += [nn.Conv2d(256, 256, 3, padding=1), nn.ReLU(inplace=True)]
layers += [nn.Conv2d(256, out_size, 3, padding=1)]
return nn.Sequential(*layers)
def forward(self,x):
out = self.feature_head(x)
out = out.permute(0,2,3,1)
b, h, w, c = out.shape
out = out.view(b, h, w, self.num_anchors, 2)
#out = out.permute(0,2,3,1).contiguous().view(out.shape[0], -1, 2)
out = self.output_act(out)
return out.contiguous().view(out.shape[0], -1, 2)
class BboxHead_(nn.Module):
def __init__(self,inchannels=256,num_anchors=3):
super(BboxHead_,self).__init__()
self.num_anchors = num_anchors
self.feature_head = self._make_head(self.num_anchors*4)
def _make_head(self,out_size):
layers = []
for _ in range(4):
layers += [nn.Conv2d(256, 256, 3, padding=1), nn.ReLU(inplace=True)]
layers += [nn.Conv2d(256, out_size, 3, padding=1)]
return nn.Sequential(*layers)
def forward(self,x):
out = self.feature_head(x)
out = out.permute(0,2,3,1)
return out.contiguous().view(out.shape[0], -1, 4)
class LandmarkHead_(nn.Module):
def __init__(self,inchannels=256,num_anchors=3):
super(LandmarkHead_,self).__init__()
self.num_anchors = num_anchors
self.feature_head = self._make_head(self.num_anchors*10)
def _make_head(self,out_size):
layers = []
for _ in range(4):
layers += [nn.Conv2d(256, 256, 3, padding=1), nn.ReLU(inplace=True)]
layers += [nn.Conv2d(256, out_size, 3, padding=1)]
return nn.Sequential(*layers)
def forward(self,x):
out = self.feature_head(x)
out = out.permute(0,2,3,1)
return out.contiguous().view(out.shape[0], -1, 10)
class CBR(nn.Module):
def __init__(self,inchannels,outchannels):
super(CBR,self).__init__()
self.conv3x3 = nn.Conv2d(inchannels,outchannels,kernel_size=3,stride=1,padding=1,bias=False)
self.bn = nn.BatchNorm2d(outchannels)
self.relu = nn.ReLU(inplace=True)
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
#nn.init.normal_(m.weight, std=0.01)
def forward(self,x):
x = self.conv3x3(x)
x = self.bn(x)
x = self.relu(x)
return x
class CB(nn.Module):
def __init__(self,inchannels):
super(CB,self).__init__()
self.conv3x3 = nn.Conv2d(inchannels,inchannels,kernel_size=3,stride=1,padding=1,bias=False)
self.bn = nn.BatchNorm2d(inchannels)
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
#nn.init.normal_(m.weight, std=0.01)
def forward(self,x):
x = self.conv3x3(x)
x = self.bn(x)
return x
class Concat(nn.Module):
def forward(self,*feature):
out = torch.cat(feature,dim=1)
return out
class Context(nn.Module):
def __init__(self,inchannels=256):
super(Context,self).__init__()
self.context_plain = inchannels//2
self.conv1 = CB(inchannels)
self.conv2 = CBR(inchannels,self.context_plain)
self.conv2_1 = CB(self.context_plain)
self.conv2_2_1 = CBR(self.context_plain,self.context_plain)
self.conv2_2_2 = CB(self.context_plain)
self.concat = Concat()
self.relu = nn.ReLU(inplace=True)
def forward(self,x):
f1 = self.conv1(x)
f2_ = self.conv2(x)
f2 = self.conv2_1(f2_)
f3 = self.conv2_2_1(f2_)
f3 = self.conv2_2_2(f3)
#out = torch.cat([f1,f2,f3],dim=1)
out = self.concat(f1,f2,f3)
out = self.relu(out)
return out
def initialize_layer(layer):
if isinstance(layer, nn.Conv2d):
nn.init.normal_(layer.weight, std=0.01)
if layer.bias is not None:
nn.init.constant_(layer.bias, val=0)
class ResNet(nn.Module):
def __init__(self, num_classes, block, layers, num_anchors=3):
self.inplanes = 64
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
if block == BasicBlock:
fpn_sizes = [self.layer1[layers[0]-1].conv2.out_channels, self.layer2[layers[1]-1].conv2.out_channels,
self.layer3[layers[2]-1].conv2.out_channels, self.layer4[layers[3]-1].conv2.out_channels]
elif block == Bottleneck:
fpn_sizes = [self.layer1[layers[0]-1].conv3.out_channels, self.layer2[layers[1]-1].conv3.out_channels,
self.layer3[layers[2]-1].conv3.out_channels, self.layer4[layers[3]-1].conv3.out_channels]
self.fpn = PyramidFeatures(fpn_sizes[0], fpn_sizes[1], fpn_sizes[2],fpn_sizes[3])
self.context = self._make_contextlayer()
self.clsHead = ClassHead_()
self.bboxHead = BboxHead_()
self.ldmHead = LandmarkHead_()
# self.clsHead = self._make_class_head()
# self.bboxHead = self._make_bbox_head()
# self.ldmHead = self._make_landmark_head()
self.anchors = Anchors()
self.regressBoxes = RegressionTransform()
self.losslayer = losses.LossLayer()
self.freeze_bn()
# initialize head
# self.clsHead.apply(initialize_layer)
# self.bboxHead.apply(initialize_layer)
# self.ldmHead.apply(initialize_layer)
# initialize context
for layer in self.context:
for m in layer.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight, std=0.01)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
if isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def _make_contextlayer(self,fpn_num=5,inchannels=256):
context = nn.ModuleList()
for i in range(fpn_num):
context.append(Context())
return context
def _make_class_head(self,fpn_num=5,inchannels=512,anchor_num=3):
classhead = nn.ModuleList()
for i in range(fpn_num):
classhead.append(ClassHead(inchannels,anchor_num))
return classhead
def _make_bbox_head(self,fpn_num=5,inchannels=512,anchor_num=3):
bboxhead = nn.ModuleList()
for i in range(fpn_num):
bboxhead.append(BboxHead(inchannels,anchor_num))
return bboxhead
def _make_landmark_head(self,fpn_num=5,inchannels=512,anchor_num=3):
landmarkhead = nn.ModuleList()
for i in range(fpn_num):
landmarkhead.append(LandmarkHead(inchannels,anchor_num))
return landmarkhead
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def freeze_bn(self):
'''Freeze BatchNorm layers.'''
for layer in self.modules():
if isinstance(layer, nn.BatchNorm2d):
layer.eval()
def freeze_first_layer(self):
'''Freeze First layer'''
for param in self.conv1.parameters():
param.requires_grad = False
def forward(self, inputs):
if self.training:
img_batch, annotations = inputs
else:
img_batch = inputs
x = self.conv1(img_batch)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x1 = self.layer1(x)
x2 = self.layer2(x1)
x3 = self.layer3(x2)
x4 = self.layer4(x3)
features = self.fpn([x1, x2, x3, x4])
#context_features = [self.context[i](feature) for i,feature in enumerate(features)]
# bbox_regressions = torch.cat([self.bboxHead[i](feature) for i,feature in enumerate(context_features)], dim=1)
# ldm_regressions = torch.cat([self.ldmHead[i](feature) for i,feature in enumerate(context_features)], dim=1)
# classifications = torch.cat([self.clsHead[i](feature) for i,feature in enumerate(context_features)],dim=1)
bbox_regressions = torch.cat([self.bboxHead(feature) for feature in features], dim=1)
ldm_regressions = torch.cat([self.ldmHead(feature) for feature in features], dim=1)
classifications = torch.cat([self.clsHead(feature) for feature in features],dim=1)
anchors = self.anchors(img_batch)
if self.training:
return self.losslayer(classifications, bbox_regressions,ldm_regressions, anchors, annotations)
else:
bboxes, landmarks = self.regressBoxes(anchors, bbox_regressions, ldm_regressions, img_batch)
return classifications, bboxes, landmarks
def resnet18(num_classes, pretrained=False, **kwargs):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(num_classes, BasicBlock, [2, 2, 2, 2], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet18'], model_dir='.'), strict=False)
return model
def resnet34(num_classes, pretrained=False, **kwargs):
"""Constructs a ResNet-34 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(num_classes, BasicBlock, [3, 4, 6, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet34'], model_dir='.'), strict=False)
return model
def resnet50(num_classes, pretrained=False, **kwargs):
"""Constructs a ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(num_classes, Bottleneck, [3, 4, 6, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet50'], model_dir='.'), strict=False)
return model
def resnet101(num_classes, pretrained=False, **kwargs):
"""Constructs a ResNet-101 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(num_classes, Bottleneck, [3, 4, 23, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet101'], model_dir='.'), strict=False)
return model
def resnet152(num_classes, pretrained=False, **kwargs):
"""Constructs a ResNet-152 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(num_classes, Bottleneck, [3, 8, 36, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet152'], model_dir='.'), strict=False)
return model