-
Notifications
You must be signed in to change notification settings - Fork 88
/
hrnet.py
executable file
·365 lines (289 loc) · 16.2 KB
/
hrnet.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
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: RainbowSecret
## Microsoft Research
## yuyua@microsoft.com
## Copyright (c) 2018
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import os
import pdb
import torch
import torch.nn as nn
import torch.nn.functional as F
from lib.models.backbones.backbone_selector import BackboneSelector
from lib.models.tools.module_helper import ModuleHelper
from lib.models.modules.projection import ProjectionHead
from lib.utils.tools.logger import Logger as Log
from lib.models.modules.hanet_attention import HANet_Conv
class HRNet_W48(nn.Module):
"""
deep high-resolution representation learning for human pose estimation, CVPR2019
"""
def __init__(self, configer):
super(HRNet_W48, self).__init__()
self.configer = configer
self.num_classes = self.configer.get('data', 'num_classes')
self.backbone = BackboneSelector(configer).get_backbone()
# extra added layers
in_channels = 720 # 48 + 96 + 192 + 384
self.cls_head = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(in_channels, bn_type=self.configer.get('network', 'bn_type')),
nn.Dropout2d(0.10),
nn.Conv2d(in_channels, self.num_classes, kernel_size=1, stride=1, padding=0, bias=False)
)
def forward(self, x_):
x = self.backbone(x_)
_, _, h, w = x[0].size()
feat1 = x[0]
feat2 = F.interpolate(x[1], size=(h, w), mode="bilinear", align_corners=True)
feat3 = F.interpolate(x[2], size=(h, w), mode="bilinear", align_corners=True)
feat4 = F.interpolate(x[3], size=(h, w), mode="bilinear", align_corners=True)
feats = torch.cat([feat1, feat2, feat3, feat4], 1)
out = self.cls_head(feats)
out = F.interpolate(out, size=(x_.size(2), x_.size(3)), mode="bilinear", align_corners=True)
return out
class HRNet_W48_CONTRAST(nn.Module):
"""
deep high-resolution representation learning for human pose estimation, CVPR2019
"""
def __init__(self, configer):
super(HRNet_W48_CONTRAST, self).__init__()
self.configer = configer
self.num_classes = self.configer.get('data', 'num_classes')
self.backbone = BackboneSelector(configer).get_backbone()
self.proj_dim = self.configer.get('contrast', 'proj_dim')
# extra added layers
in_channels = 720 # 48 + 96 + 192 + 384
self.cls_head = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(in_channels, bn_type=self.configer.get('network', 'bn_type')),
nn.Dropout2d(0.10),
nn.Conv2d(in_channels, self.num_classes, kernel_size=1, stride=1, padding=0, bias=False)
)
self.proj_head = ProjectionHead(dim_in=in_channels, proj_dim=self.proj_dim)
def forward(self, x_, with_embed=False, is_eval=False):
x = self.backbone(x_)
_, _, h, w = x[0].size()
feat1 = x[0]
feat2 = F.interpolate(x[1], size=(h, w), mode="bilinear", align_corners=True)
feat3 = F.interpolate(x[2], size=(h, w), mode="bilinear", align_corners=True)
feat4 = F.interpolate(x[3], size=(h, w), mode="bilinear", align_corners=True)
feats = torch.cat([feat1, feat2, feat3, feat4], 1)
out = self.cls_head(feats)
emb = self.proj_head(feats)
return {'seg': out, 'embed': emb}
class HRNet_W48_OCR_CONTRAST(nn.Module):
def __init__(self, configer):
super(HRNet_W48_OCR_CONTRAST, self).__init__()
self.configer = configer
self.num_classes = self.configer.get('data', 'num_classes')
self.backbone = BackboneSelector(configer).get_backbone()
self.proj_dim = self.configer.get('contrast', 'proj_dim')
in_channels = 720
self.conv3x3 = nn.Sequential(
nn.Conv2d(in_channels, 512, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(512, bn_type=self.configer.get('network', 'bn_type')),
)
from lib.models.modules.spatial_ocr_block import SpatialGather_Module
self.ocr_gather_head = SpatialGather_Module(self.num_classes)
from lib.models.modules.spatial_ocr_block import SpatialOCR_Module
self.ocr_distri_head = SpatialOCR_Module(in_channels=512,
key_channels=256,
out_channels=512,
scale=1,
dropout=0.05,
bn_type=self.configer.get('network', 'bn_type'))
self.cls_head = nn.Conv2d(512, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)
self.aux_head = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(in_channels, bn_type=self.configer.get('network', 'bn_type')),
nn.Conv2d(in_channels, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)
)
self.proj_head = ProjectionHead(dim_in=in_channels, proj_dim=self.proj_dim)
def forward(self, x_, with_embed=False, is_eval=False):
x = self.backbone(x_)
_, _, h, w = x[0].size()
feat1 = x[0]
feat2 = F.interpolate(x[1], size=(h, w), mode="bilinear", align_corners=True)
feat3 = F.interpolate(x[2], size=(h, w), mode="bilinear", align_corners=True)
feat4 = F.interpolate(x[3], size=(h, w), mode="bilinear", align_corners=True)
feats = torch.cat([feat1, feat2, feat3, feat4], 1)
out_aux = self.aux_head(feats)
emb = self.proj_head(feats)
feats = self.conv3x3(feats)
context = self.ocr_gather_head(feats, out_aux)
feats = self.ocr_distri_head(feats, context)
out = self.cls_head(feats)
return {'seg': out, 'seg_aux': out_aux, 'embed': emb}
class HRNet_W48_MEM(nn.Module):
def __init__(self, configer, dim=256, m=0.999, with_masked_ppm=False):
super(HRNet_W48_MEM, self).__init__()
self.configer = configer
self.m = m
self.r = self.configer.get('contrast', 'memory_size')
self.with_masked_ppm = with_masked_ppm
num_classes = self.configer.get('data', 'num_classes')
self.encoder_q = HRNet_W48_CONTRAST(configer)
self.register_buffer("segment_queue", torch.randn(num_classes, self.r, dim))
self.segment_queue = nn.functional.normalize(self.segment_queue, p=2, dim=2)
self.register_buffer("segment_queue_ptr", torch.zeros(num_classes, dtype=torch.long))
self.register_buffer("pixel_queue", torch.randn(num_classes, self.r, dim))
self.pixel_queue = nn.functional.normalize(self.pixel_queue, p=2, dim=2)
self.register_buffer("pixel_queue_ptr", torch.zeros(num_classes, dtype=torch.long))
@torch.no_grad()
def _momentum_update_key_encoder(self):
for param_q, param_k in zip(self.encoder_q.parameters(), self.encoder_k.parameters()):
param_k.data = param_k.data * self.m + param_q.data * (1. - self.m)
def forward(self, im_q, lb_q=None, with_embed=True, is_eval=False):
if is_eval is True or lb_q is None:
ret = self.encoder_q(im_q, with_embed=with_embed)
return ret
ret = self.encoder_q(im_q)
q = ret['embed']
out = ret['seg']
return {'seg': out, 'embed': q, 'key': q.detach(), 'lb_key': lb_q.detach()}
class HRNet_W48_OCR(nn.Module):
def __init__(self, configer):
super(HRNet_W48_OCR, self).__init__()
self.configer = configer
self.num_classes = self.configer.get('data', 'num_classes')
self.backbone = BackboneSelector(configer).get_backbone()
in_channels = 720
self.conv3x3 = nn.Sequential(
nn.Conv2d(in_channels, 512, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(512, bn_type=self.configer.get('network', 'bn_type')),
)
from lib.models.modules.spatial_ocr_block import SpatialGather_Module
self.ocr_gather_head = SpatialGather_Module(self.num_classes)
from lib.models.modules.spatial_ocr_block import SpatialOCR_Module
self.ocr_distri_head = SpatialOCR_Module(in_channels=512,
key_channels=256,
out_channels=512,
scale=1,
dropout=0.05,
bn_type=self.configer.get('network', 'bn_type'))
self.cls_head = nn.Conv2d(512, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)
self.aux_head = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(in_channels, bn_type=self.configer.get('network', 'bn_type')),
nn.Conv2d(in_channels, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)
)
def forward(self, x_):
x = self.backbone(x_)
_, _, h, w = x[0].size()
feat1 = x[0]
feat2 = F.interpolate(x[1], size=(h, w), mode="bilinear", align_corners=True)
feat3 = F.interpolate(x[2], size=(h, w), mode="bilinear", align_corners=True)
feat4 = F.interpolate(x[3], size=(h, w), mode="bilinear", align_corners=True)
feats = torch.cat([feat1, feat2, feat3, feat4], 1)
out_aux = self.aux_head(feats)
feats = self.conv3x3(feats)
context = self.ocr_gather_head(feats, out_aux)
feats = self.ocr_distri_head(feats, context)
out = self.cls_head(feats)
out_aux = F.interpolate(out_aux, size=(x_.size(2), x_.size(3)), mode="bilinear", align_corners=True)
out = F.interpolate(out, size=(x_.size(2), x_.size(3)), mode="bilinear", align_corners=True)
return out_aux, out
class HRNet_W48_OCR_B(nn.Module):
"""
Considering that the 3x3 convolution on the 4x resolution feature map is expensive,
we can decrease the intermediate channels from 512 to 256 w/o performance loss.
"""
def __init__(self, configer):
super(HRNet_W48_OCR_B, self).__init__()
self.configer = configer
self.num_classes = self.configer.get('data', 'num_classes')
self.backbone = BackboneSelector(configer).get_backbone()
in_channels = 720 # 48 + 96 + 192 + 384
self.conv3x3 = nn.Sequential(
nn.Conv2d(in_channels, 256, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(256, bn_type=self.configer.get('network', 'bn_type')),
)
from lib.models.modules.spatial_ocr_block import SpatialGather_Module
self.ocr_gather_head = SpatialGather_Module(self.num_classes)
from lib.models.modules.spatial_ocr_block import SpatialOCR_Module
self.ocr_distri_head = SpatialOCR_Module(in_channels=256,
key_channels=128,
out_channels=256,
scale=1,
dropout=0.05,
bn_type=self.configer.get('network', 'bn_type'))
self.cls_head = nn.Conv2d(256, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)
self.aux_head = nn.Sequential(
nn.Conv2d(in_channels, 256, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(256, bn_type=self.configer.get('network', 'bn_type')),
nn.Conv2d(256, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)
)
def forward(self, x_):
x = self.backbone(x_)
_, _, h, w = x[0].size()
feat1 = x[0]
feat2 = F.interpolate(x[1], size=(h, w), mode="bilinear", align_corners=True)
feat3 = F.interpolate(x[2], size=(h, w), mode="bilinear", align_corners=True)
feat4 = F.interpolate(x[3], size=(h, w), mode="bilinear", align_corners=True)
feats = torch.cat([feat1, feat2, feat3, feat4], 1)
out_aux = self.aux_head(feats)
feats = self.conv3x3(feats)
context = self.ocr_gather_head(feats, out_aux)
feats = self.ocr_distri_head(feats, context)
out = self.cls_head(feats)
out_aux = F.interpolate(out_aux, size=(x_.size(2), x_.size(3)), mode="bilinear", align_corners=True)
out = F.interpolate(out, size=(x_.size(2), x_.size(3)), mode="bilinear", align_corners=True)
return out_aux, out
class HRNet_W48_OCR_B_HA(nn.Module):
"""
Considering that the 3x3 convolution on the 4x resolution feature map is expensive,
we can decrease the intermediate channels from 512 to 256 w/o performance loss.
"""
def __init__(self, configer):
super(HRNet_W48_OCR_B_HA, self).__init__()
self.configer = configer
self.num_classes = self.configer.get('data', 'num_classes')
self.backbone = BackboneSelector(configer).get_backbone()
in_channels = 720 # 48 + 96 + 192 + 384
self.conv3x3 = nn.Sequential(
nn.Conv2d(in_channels, 256, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(256, bn_type=self.configer.get('network', 'bn_type')),
)
from lib.models.modules.spatial_ocr_block import SpatialGather_Module
self.ocr_gather_head = SpatialGather_Module(self.num_classes)
from lib.models.modules.spatial_ocr_block import SpatialOCR_Module
self.ocr_distri_head = SpatialOCR_Module(in_channels=256,
key_channels=128,
out_channels=256,
scale=1,
dropout=0.05,
bn_type=self.configer.get('network', 'bn_type'))
self.cls_head = nn.Conv2d(256, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)
self.aux_head = nn.Sequential(
nn.Conv2d(in_channels, 256, kernel_size=3, stride=1, padding=1),
ModuleHelper.BNReLU(256, bn_type=self.configer.get('network', 'bn_type')),
nn.Conv2d(256, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)
)
self.ha1 = HANet_Conv(384, 384, bn_type=self.configer.get('network', 'bn_type'))
self.ha2 = HANet_Conv(192, 192, bn_type=self.configer.get('network', 'bn_type'))
self.ha3 = HANet_Conv(96, 96, bn_type=self.configer.get('network', 'bn_type'))
self.ha4 = HANet_Conv(48, 48, bn_type=self.configer.get('network', 'bn_type'))
def forward(self, x_):
x = self.backbone(x_)
_, _, h, w = x[0].size()
x[0] = x[0] + self.ha1(x[0])
x[1] = x[1] + self.ha1(x[1])
x[2] = x[2] + self.ha1(x[2])
x[3] = x[3] + self.ha1(x[3])
feat1 = x[0]
feat2 = F.interpolate(x[1], size=(h, w), mode="bilinear", align_corners=True)
feat3 = F.interpolate(x[2], size=(h, w), mode="bilinear", align_corners=True)
feat4 = F.interpolate(x[3], size=(h, w), mode="bilinear", align_corners=True)
feats = torch.cat([feat1, feat2, feat3, feat4], 1)
out_aux = self.aux_head(feats)
feats = self.conv3x3(feats)
context = self.ocr_gather_head(feats, out_aux)
feats = self.ocr_distri_head(feats, context)
out = self.cls_head(feats)
out_aux = F.interpolate(out_aux, size=(x_.size(2), x_.size(3)), mode="bilinear", align_corners=True)
out = F.interpolate(out, size=(x_.size(2), x_.size(3)), mode="bilinear", align_corners=True)
return out_aux, out