Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FaceMaskOverlay/lib/models/hrnet.py
3443 views
1
# ------------------------------------------------------------------------------
2
# Copyright (c) Microsoft
3
# Licensed under the MIT License.
4
# Create by Bin Xiao ([email protected])
5
# Modified by Tianheng Cheng([email protected]), Yang Zhao
6
# ------------------------------------------------------------------------------
7
8
from __future__ import absolute_import
9
from __future__ import division
10
from __future__ import print_function
11
12
import os
13
import logging
14
15
import torch
16
import torch.nn as nn
17
import torch.nn.functional as F
18
19
20
BatchNorm2d = nn.BatchNorm2d
21
BN_MOMENTUM = 0.01
22
logger = logging.getLogger(__name__)
23
24
25
def conv3x3(in_planes, out_planes, stride=1):
26
"""3x3 convolution with padding"""
27
return nn.Conv2d(in_planes, out_planes, kernel_size=3,
28
stride=stride, padding=1, bias=False)
29
30
31
class BasicBlock(nn.Module):
32
expansion = 1
33
34
def __init__(self, inplanes, planes, stride=1, downsample=None):
35
super(BasicBlock, self).__init__()
36
self.conv1 = conv3x3(inplanes, planes, stride)
37
self.bn1 = BatchNorm2d(planes, momentum=BN_MOMENTUM)
38
self.relu = nn.ReLU(inplace=True)
39
self.conv2 = conv3x3(planes, planes)
40
self.bn2 = BatchNorm2d(planes, momentum=BN_MOMENTUM)
41
self.downsample = downsample
42
self.stride = stride
43
44
def forward(self, x):
45
residual = x
46
47
out = self.conv1(x)
48
out = self.bn1(out)
49
out = self.relu(out)
50
51
out = self.conv2(out)
52
out = self.bn2(out)
53
54
if self.downsample is not None:
55
residual = self.downsample(x)
56
57
out += residual
58
out = self.relu(out)
59
60
return out
61
62
63
class Bottleneck(nn.Module):
64
expansion = 4
65
66
def __init__(self, inplanes, planes, stride=1, downsample=None):
67
super(Bottleneck, self).__init__()
68
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
69
self.bn1 = BatchNorm2d(planes, momentum=BN_MOMENTUM)
70
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
71
padding=1, bias=False)
72
self.bn2 = BatchNorm2d(planes, momentum=BN_MOMENTUM)
73
self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1,
74
bias=False)
75
self.bn3 = BatchNorm2d(planes * self.expansion,
76
momentum=BN_MOMENTUM)
77
self.relu = nn.ReLU(inplace=True)
78
self.downsample = downsample
79
self.stride = stride
80
81
def forward(self, x):
82
residual = x
83
84
out = self.conv1(x)
85
out = self.bn1(out)
86
out = self.relu(out)
87
88
out = self.conv2(out)
89
out = self.bn2(out)
90
out = self.relu(out)
91
92
out = self.conv3(out)
93
out = self.bn3(out)
94
95
if self.downsample is not None:
96
residual = self.downsample(x)
97
98
out += residual
99
out = self.relu(out)
100
101
return out
102
103
104
class HighResolutionModule(nn.Module):
105
def __init__(self, num_branches, blocks, num_blocks, num_inchannels,
106
num_channels, fuse_method, multi_scale_output=True):
107
super(HighResolutionModule, self).__init__()
108
self._check_branches(
109
num_branches, blocks, num_blocks, num_inchannels, num_channels)
110
111
self.num_inchannels = num_inchannels
112
self.fuse_method = fuse_method
113
self.num_branches = num_branches
114
115
self.multi_scale_output = multi_scale_output
116
117
self.branches = self._make_branches(
118
num_branches, blocks, num_blocks, num_channels)
119
self.fuse_layers = self._make_fuse_layers()
120
self.relu = nn.ReLU(inplace=True)
121
122
def _check_branches(self, num_branches, blocks, num_blocks,
123
num_inchannels, num_channels):
124
if num_branches != len(num_blocks):
125
error_msg = 'NUM_BRANCHES({}) <> NUM_BLOCKS({})'.format(
126
num_branches, len(num_blocks))
127
logger.error(error_msg)
128
raise ValueError(error_msg)
129
130
if num_branches != len(num_channels):
131
error_msg = 'NUM_BRANCHES({}) <> NUM_CHANNELS({})'.format(
132
num_branches, len(num_channels))
133
logger.error(error_msg)
134
raise ValueError(error_msg)
135
136
if num_branches != len(num_inchannels):
137
error_msg = 'NUM_BRANCHES({}) <> NUM_INCHANNELS({})'.format(
138
num_branches, len(num_inchannels))
139
logger.error(error_msg)
140
raise ValueError(error_msg)
141
142
def _make_one_branch(self, branch_index, block, num_blocks, num_channels,
143
stride=1):
144
downsample = None
145
if stride != 1 or \
146
self.num_inchannels[branch_index] != num_channels[branch_index] * block.expansion:
147
downsample = nn.Sequential(
148
nn.Conv2d(self.num_inchannels[branch_index],
149
num_channels[branch_index] * block.expansion,
150
kernel_size=1, stride=stride, bias=False),
151
BatchNorm2d(num_channels[branch_index] * block.expansion,
152
momentum=BN_MOMENTUM),
153
)
154
155
layers = []
156
layers.append(block(self.num_inchannels[branch_index],
157
num_channels[branch_index], stride, downsample))
158
self.num_inchannels[branch_index] = \
159
num_channels[branch_index] * block.expansion
160
for i in range(1, num_blocks[branch_index]):
161
layers.append(block(self.num_inchannels[branch_index],
162
num_channels[branch_index]))
163
164
return nn.Sequential(*layers)
165
166
def _make_branches(self, num_branches, block, num_blocks, num_channels):
167
branches = []
168
169
for i in range(num_branches):
170
branches.append(
171
self._make_one_branch(i, block, num_blocks, num_channels))
172
173
return nn.ModuleList(branches)
174
175
def _make_fuse_layers(self):
176
if self.num_branches == 1:
177
return None
178
179
num_branches = self.num_branches
180
num_inchannels = self.num_inchannels
181
fuse_layers = []
182
for i in range(num_branches if self.multi_scale_output else 1):
183
fuse_layer = []
184
for j in range(num_branches):
185
if j > i:
186
fuse_layer.append(nn.Sequential(
187
nn.Conv2d(num_inchannels[j],
188
num_inchannels[i],
189
1,
190
1,
191
0,
192
bias=False),
193
BatchNorm2d(num_inchannels[i], momentum=BN_MOMENTUM)))
194
# nn.Upsample(scale_factor=2**(j-i), mode='nearest')))
195
elif j == i:
196
fuse_layer.append(None)
197
else:
198
conv3x3s = []
199
for k in range(i - j):
200
if k == i - j - 1:
201
num_outchannels_conv3x3 = num_inchannels[i]
202
conv3x3s.append(nn.Sequential(
203
nn.Conv2d(num_inchannels[j],
204
num_outchannels_conv3x3,
205
3, 2, 1, bias=False),
206
BatchNorm2d(num_outchannels_conv3x3, momentum=BN_MOMENTUM)))
207
else:
208
num_outchannels_conv3x3 = num_inchannels[j]
209
conv3x3s.append(nn.Sequential(
210
nn.Conv2d(num_inchannels[j],
211
num_outchannels_conv3x3,
212
3, 2, 1, bias=False),
213
BatchNorm2d(num_outchannels_conv3x3,
214
momentum=BN_MOMENTUM),
215
nn.ReLU(inplace=True)))
216
fuse_layer.append(nn.Sequential(*conv3x3s))
217
fuse_layers.append(nn.ModuleList(fuse_layer))
218
219
return nn.ModuleList(fuse_layers)
220
221
def get_num_inchannels(self):
222
return self.num_inchannels
223
224
def forward(self, x):
225
if self.num_branches == 1:
226
return [self.branches[0](x[0])]
227
228
for i in range(self.num_branches):
229
x[i] = self.branches[i](x[i])
230
231
x_fuse = []
232
for i in range(len(self.fuse_layers)):
233
y = x[0] if i == 0 else self.fuse_layers[i][0](x[0])
234
for j in range(1, self.num_branches):
235
if i == j:
236
y = y + x[j]
237
elif j > i:
238
y = y + F.interpolate(
239
self.fuse_layers[i][j](x[j]),
240
size=[x[i].shape[2], x[i].shape[3]],
241
mode='bilinear')
242
else:
243
y = y + self.fuse_layers[i][j](x[j])
244
x_fuse.append(self.relu(y))
245
246
return x_fuse
247
248
249
blocks_dict = {
250
'BASIC': BasicBlock,
251
'BOTTLENECK': Bottleneck
252
}
253
254
255
class HighResolutionNet(nn.Module):
256
257
def __init__(self, config, **kwargs):
258
self.inplanes = 64
259
extra = config.MODEL.EXTRA
260
super(HighResolutionNet, self).__init__()
261
262
# stem net
263
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1,
264
bias=False)
265
self.bn1 = BatchNorm2d(64, momentum=BN_MOMENTUM)
266
self.conv2 = nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1,
267
bias=False)
268
self.bn2 = BatchNorm2d(64, momentum=BN_MOMENTUM)
269
self.relu = nn.ReLU(inplace=True)
270
self.sf = nn.Softmax(dim=1)
271
self.layer1 = self._make_layer(Bottleneck, 64, 64, 4)
272
273
self.stage2_cfg = extra['STAGE2']
274
num_channels = self.stage2_cfg['NUM_CHANNELS']
275
block = blocks_dict[self.stage2_cfg['BLOCK']]
276
num_channels = [
277
num_channels[i] * block.expansion for i in range(len(num_channels))]
278
self.transition1 = self._make_transition_layer(
279
[256], num_channels)
280
self.stage2, pre_stage_channels = self._make_stage(
281
self.stage2_cfg, num_channels)
282
283
self.stage3_cfg = extra['STAGE3']
284
num_channels = self.stage3_cfg['NUM_CHANNELS']
285
block = blocks_dict[self.stage3_cfg['BLOCK']]
286
num_channels = [
287
num_channels[i] * block.expansion for i in range(len(num_channels))]
288
self.transition2 = self._make_transition_layer(
289
pre_stage_channels, num_channels)
290
self.stage3, pre_stage_channels = self._make_stage(
291
self.stage3_cfg, num_channels)
292
293
self.stage4_cfg = extra['STAGE4']
294
num_channels = self.stage4_cfg['NUM_CHANNELS']
295
block = blocks_dict[self.stage4_cfg['BLOCK']]
296
num_channels = [
297
num_channels[i] * block.expansion for i in range(len(num_channels))]
298
self.transition3 = self._make_transition_layer(
299
pre_stage_channels, num_channels)
300
self.stage4, pre_stage_channels = self._make_stage(
301
self.stage4_cfg, num_channels, multi_scale_output=True)
302
303
final_inp_channels = sum(pre_stage_channels)
304
305
self.head = nn.Sequential(
306
nn.Conv2d(
307
in_channels=final_inp_channels,
308
out_channels=final_inp_channels,
309
kernel_size=1,
310
stride=1,
311
padding=1 if extra.FINAL_CONV_KERNEL == 3 else 0),
312
BatchNorm2d(final_inp_channels, momentum=BN_MOMENTUM),
313
nn.ReLU(inplace=True),
314
nn.Conv2d(
315
in_channels=final_inp_channels,
316
out_channels=config.MODEL.NUM_JOINTS,
317
kernel_size=extra.FINAL_CONV_KERNEL,
318
stride=1,
319
padding=1 if extra.FINAL_CONV_KERNEL == 3 else 0)
320
)
321
322
def _make_transition_layer(
323
self, num_channels_pre_layer, num_channels_cur_layer):
324
num_branches_cur = len(num_channels_cur_layer)
325
num_branches_pre = len(num_channels_pre_layer)
326
327
transition_layers = []
328
for i in range(num_branches_cur):
329
if i < num_branches_pre:
330
if num_channels_cur_layer[i] != num_channels_pre_layer[i]:
331
transition_layers.append(nn.Sequential(
332
nn.Conv2d(num_channels_pre_layer[i],
333
num_channels_cur_layer[i],
334
3,
335
1,
336
1,
337
bias=False),
338
BatchNorm2d(
339
num_channels_cur_layer[i], momentum=BN_MOMENTUM),
340
nn.ReLU(inplace=True)))
341
else:
342
transition_layers.append(None)
343
else:
344
conv3x3s = []
345
for j in range(i + 1 - num_branches_pre):
346
inchannels = num_channels_pre_layer[-1]
347
outchannels = num_channels_cur_layer[i] \
348
if j == i - num_branches_pre else inchannels
349
conv3x3s.append(nn.Sequential(
350
nn.Conv2d(
351
inchannels, outchannels, 3, 2, 1, bias=False),
352
BatchNorm2d(outchannels, momentum=BN_MOMENTUM),
353
nn.ReLU(inplace=True)))
354
transition_layers.append(nn.Sequential(*conv3x3s))
355
356
return nn.ModuleList(transition_layers)
357
358
def _make_layer(self, block, inplanes, planes, blocks, stride=1):
359
downsample = None
360
if stride != 1 or inplanes != planes * block.expansion:
361
downsample = nn.Sequential(
362
nn.Conv2d(inplanes, planes * block.expansion,
363
kernel_size=1, stride=stride, bias=False),
364
BatchNorm2d(planes * block.expansion, momentum=BN_MOMENTUM),
365
)
366
367
layers = []
368
layers.append(block(inplanes, planes, stride, downsample))
369
inplanes = planes * block.expansion
370
for i in range(1, blocks):
371
layers.append(block(inplanes, planes))
372
373
return nn.Sequential(*layers)
374
375
def _make_stage(self, layer_config, num_inchannels,
376
multi_scale_output=True):
377
num_modules = layer_config['NUM_MODULES']
378
num_branches = layer_config['NUM_BRANCHES']
379
num_blocks = layer_config['NUM_BLOCKS']
380
num_channels = layer_config['NUM_CHANNELS']
381
block = blocks_dict[layer_config['BLOCK']]
382
fuse_method = layer_config['FUSE_METHOD']
383
384
modules = []
385
for i in range(num_modules):
386
# multi_scale_output is only used last module
387
if not multi_scale_output and i == num_modules - 1:
388
reset_multi_scale_output = False
389
else:
390
reset_multi_scale_output = True
391
modules.append(
392
HighResolutionModule(num_branches,
393
block,
394
num_blocks,
395
num_inchannels,
396
num_channels,
397
fuse_method,
398
reset_multi_scale_output)
399
)
400
num_inchannels = modules[-1].get_num_inchannels()
401
402
return nn.Sequential(*modules), num_inchannels
403
404
def forward(self, x):
405
# h, w = x.size(2), x.size(3)
406
x = self.conv1(x)
407
x = self.bn1(x)
408
x = self.relu(x)
409
x = self.conv2(x)
410
x = self.bn2(x)
411
x = self.relu(x)
412
x = self.layer1(x)
413
414
x_list = []
415
for i in range(self.stage2_cfg['NUM_BRANCHES']):
416
if self.transition1[i] is not None:
417
x_list.append(self.transition1[i](x))
418
else:
419
x_list.append(x)
420
y_list = self.stage2(x_list)
421
422
x_list = []
423
for i in range(self.stage3_cfg['NUM_BRANCHES']):
424
if self.transition2[i] is not None:
425
x_list.append(self.transition2[i](y_list[-1]))
426
else:
427
x_list.append(y_list[i])
428
y_list = self.stage3(x_list)
429
430
x_list = []
431
for i in range(self.stage4_cfg['NUM_BRANCHES']):
432
if self.transition3[i] is not None:
433
x_list.append(self.transition3[i](y_list[-1]))
434
else:
435
x_list.append(y_list[i])
436
x = self.stage4(x_list)
437
438
# Head Part
439
height, width = x[0].size(2), x[0].size(3)
440
x1 = F.interpolate(x[1], size=(height, width), mode='bilinear', align_corners=False)
441
x2 = F.interpolate(x[2], size=(height, width), mode='bilinear', align_corners=False)
442
x3 = F.interpolate(x[3], size=(height, width), mode='bilinear', align_corners=False)
443
x = torch.cat([x[0], x1, x2, x3], 1)
444
x = self.head(x)
445
446
return x
447
448
def init_weights(self, pretrained=''):
449
logger.info('=> init weights from normal distribution')
450
for m in self.modules():
451
if isinstance(m, nn.Conv2d):
452
# nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
453
nn.init.normal_(m.weight, std=0.001)
454
# nn.init.constant_(m.bias, 0)
455
elif isinstance(m, nn.BatchNorm2d):
456
nn.init.constant_(m.weight, 1)
457
nn.init.constant_(m.bias, 0)
458
if os.path.isfile(pretrained):
459
pretrained_dict = torch.load(pretrained)
460
logger.info('=> loading pretrained model {}'.format(pretrained))
461
model_dict = self.state_dict()
462
pretrained_dict = {k: v for k, v in pretrained_dict.items()
463
if k in model_dict.keys()}
464
for k, _ in pretrained_dict.items():
465
logger.info(
466
'=> loading {} pretrained model {}'.format(k, pretrained))
467
model_dict.update(pretrained_dict)
468
self.load_state_dict(model_dict)
469
470
471
def get_face_alignment_net(config, **kwargs):
472
473
model = HighResolutionNet(config, **kwargs)
474
pretrained = config.MODEL.PRETRAINED if config.MODEL.INIT_WEIGHTS else ''
475
model.init_weights(pretrained=pretrained)
476
477
return model
478
479
480