-
Notifications
You must be signed in to change notification settings - Fork 32
/
hrda_head.py
269 lines (235 loc) · 9.81 KB
/
hrda_head.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
# ---------------------------------------------------------------
# Copyright (c) 2022 ETH Zurich, Lukas Hoyer. All rights reserved.
# Licensed under the Apache License, Version 2.0
# ---------------------------------------------------------------
from copy import deepcopy
import torch
from torch.nn import functional as F
from ...core import add_prefix
from ...ops import resize as _resize
from .. import builder
from ..builder import HEADS
from ..segmentors.hrda_encoder_decoder import crop
from .decode_head import BaseDecodeHead
def scale_box(box, scale):
y1, y2, x1, x2 = box
# assert y1 % scale == 0
# assert y2 % scale == 0
# assert x1 % scale == 0
# assert x2 % scale == 0
y1 = int(y1 / scale)
y2 = int(y2 / scale)
x1 = int(x1 / scale)
x2 = int(x2 / scale)
return y1, y2, x1, x2
@HEADS.register_module()
class HRDAHead(BaseDecodeHead):
def __init__(self,
single_scale_head,
lr_loss_weight=0,
hr_loss_weight=0,
scales=[1],
attention_embed_dim=256,
attention_classwise=True,
enable_hr_crop=False,
hr_slide_inference=True,
fixed_attention=None,
debug_output_attention=False,
**kwargs):
head_cfg = deepcopy(kwargs)
attn_cfg = deepcopy(kwargs)
if single_scale_head == 'DAFormerHead':
attn_cfg['channels'] = attention_embed_dim
attn_cfg['decoder_params']['embed_dims'] = attention_embed_dim
if attn_cfg['decoder_params']['fusion_cfg']['type'] == 'aspp':
attn_cfg['decoder_params']['fusion_cfg'] = dict(
type='conv',
kernel_size=1,
act_cfg=dict(type='ReLU'),
norm_cfg=attn_cfg['decoder_params']['fusion_cfg']
['norm_cfg'])
kwargs['init_cfg'] = None
kwargs['input_transform'] = 'multiple_select'
self.os = 4
elif single_scale_head == 'DLV2Head':
kwargs['init_cfg'] = None
kwargs.pop('dilations')
kwargs['channels'] = 1
self.os = 8
else:
raise NotImplementedError(single_scale_head)
super(HRDAHead, self).__init__(**kwargs)
del self.conv_seg
del self.dropout
head_cfg['type'] = single_scale_head
self.head = builder.build_head(head_cfg)
attn_cfg['type'] = single_scale_head
if not attention_classwise:
attn_cfg['num_classes'] = 1
if fixed_attention is None:
self.scale_attention = builder.build_head(attn_cfg)
else:
self.scale_attention = None
self.fixed_attention = fixed_attention
self.lr_loss_weight = lr_loss_weight
self.hr_loss_weight = hr_loss_weight
self.scales = scales
self.enable_hr_crop = enable_hr_crop
self.hr_crop_box = None
self.hr_slide_inference = hr_slide_inference
self.debug_output_attention = debug_output_attention
def set_hr_crop_box(self, boxes):
self.hr_crop_box = boxes
def hr_crop_slice(self, scale):
crop_y1, crop_y2, crop_x1, crop_x2 = scale_box(self.hr_crop_box, scale)
return slice(crop_y1, crop_y2), slice(crop_x1, crop_x2)
def resize(self, input, scale_factor):
return _resize(
input=input,
scale_factor=scale_factor,
mode='bilinear',
align_corners=self.align_corners)
def decode_hr(self, inp, bs):
if isinstance(inp, dict) and 'boxes' in inp.keys():
features = inp['features'] # level, crop * bs, c, h, w
boxes = inp['boxes']
dev = features[0][0].device
h_img, w_img = 0, 0
for i in range(len(boxes)):
boxes[i] = scale_box(boxes[i], self.os)
y1, y2, x1, x2 = boxes[i]
if h_img < y2:
h_img = y2
if w_img < x2:
w_img = x2
preds = torch.zeros((bs, self.num_classes, h_img, w_img),
device=dev)
count_mat = torch.zeros((bs, 1, h_img, w_img), device=dev)
crop_seg_logits = self.head(features)
for i in range(len(boxes)):
y1, y2, x1, x2 = boxes[i]
crop_seg_logit = crop_seg_logits[i * bs:(i + 1) * bs]
preds += F.pad(crop_seg_logit,
(int(x1), int(preds.shape[3] - x2), int(y1),
int(preds.shape[2] - y2)))
count_mat[:, :, y1:y2, x1:x2] += 1
assert (count_mat == 0).sum() == 0
preds = preds / count_mat
return preds
else:
return self.head(inp)
def get_scale_attention(self, inp):
if self.scale_attention is not None:
att = torch.sigmoid(self.scale_attention(inp))
else:
att = self.fixed_attention
return att
def forward(self, inputs):
assert len(inputs) == 2
hr_inp = inputs[1]
hr_scale = self.scales[1]
lr_inp = inputs[0]
lr_sc_att_inp = inputs[0] # separate var necessary for stack hr_fusion
lr_scale = self.scales[0]
batch_size = lr_inp[0].shape[0]
assert lr_scale <= hr_scale
has_crop = self.hr_crop_box is not None
if has_crop:
crop_y1, crop_y2, crop_x1, crop_x2 = self.hr_crop_box
# print_log(f'lr_inp {[f.shape for f in lr_inp]}', 'mmseg')
lr_seg = self.head(lr_inp)
# print_log(f'lr_seg {lr_seg.shape}', 'mmseg')
hr_seg = self.decode_hr(hr_inp, batch_size)
att = self.get_scale_attention(lr_sc_att_inp)
if has_crop:
mask = lr_seg.new_zeros([lr_seg.shape[0], 1, *lr_seg.shape[2:]])
sc_os = self.os / lr_scale
slc = self.hr_crop_slice(sc_os)
mask[:, :, slc[0], slc[1]] = 1
att = att * mask
# print_log(f'att {att.shape}', 'mmseg')
lr_seg = (1 - att) * lr_seg
# print_log(f'scaled lr_seg {lr_seg.shape}', 'mmseg')
up_lr_seg = self.resize(lr_seg, hr_scale / lr_scale)
if torch.is_tensor(att):
att = self.resize(att, hr_scale / lr_scale)
if has_crop:
hr_seg_inserted = torch.zeros_like(up_lr_seg)
slc = self.hr_crop_slice(self.os)
hr_seg_inserted[:, :, slc[0], slc[1]] = hr_seg
else:
hr_seg_inserted = hr_seg
fused_seg = att * hr_seg_inserted + up_lr_seg
if self.debug_output_attention:
att = torch.sum(
att * torch.softmax(fused_seg, dim=1), dim=1, keepdim=True)
return att, None, None
if self.debug:
self.debug_output.update({
'High Res':
torch.max(hr_seg, dim=1)[1].detach().cpu().numpy(),
'High Res Inserted':
torch.max(hr_seg_inserted, dim=1)[1].detach().cpu().numpy(),
'Low Res':
torch.max(lr_seg, dim=1)[1].detach().cpu().numpy(),
'Fused':
torch.max(fused_seg, dim=1)[1].detach().cpu().numpy(),
})
if torch.is_tensor(att):
self.debug_output['Attention'] = torch.sum(
att * torch.softmax(fused_seg, dim=1), dim=1,
keepdim=True).detach().cpu().numpy()
return fused_seg, lr_seg, hr_seg
def reset_crop(self):
del self.hr_crop_box
self.hr_crop_box = None
def forward_train(self,
inputs,
img_metas,
gt_semantic_seg,
train_cfg,
seg_weight=None):
"""Forward function for training."""
if self.enable_hr_crop:
assert self.hr_crop_box is not None
seg_logits = self.forward(inputs)
losses = self.losses(seg_logits, gt_semantic_seg, seg_weight)
self.reset_crop()
return losses
def forward_test(self, inputs, img_metas, test_cfg):
"""Forward function for testing, only ``fused_seg`` is used."""
return self.forward(inputs)[0]
def losses(self, seg_logit, seg_label, seg_weight=None):
"""Compute losses."""
fused_seg, lr_seg, hr_seg = seg_logit
loss = super(HRDAHead, self).losses(fused_seg, seg_label, seg_weight)
if self.hr_loss_weight == 0 and self.lr_loss_weight == 0:
return loss
if self.lr_loss_weight > 0:
loss.update(
add_prefix(
super(HRDAHead, self).losses(lr_seg, seg_label,
seg_weight), 'lr'))
if self.hr_loss_weight > 0 and self.enable_hr_crop:
cropped_seg_label = crop(seg_label, self.hr_crop_box)
if seg_weight is not None:
cropped_seg_weight = crop(seg_weight, self.hr_crop_box)
else:
cropped_seg_weight = seg_weight
self.debug_output['Cropped GT'] = \
cropped_seg_label.squeeze(1).detach().cpu().numpy()
loss.update(
add_prefix(
super(HRDAHead, self).losses(hr_seg, cropped_seg_label,
cropped_seg_weight), 'hr'))
elif self.hr_loss_weight > 0:
loss.update(
add_prefix(
super(HRDAHead, self).losses(hr_seg, seg_label,
seg_weight), 'hr'))
loss['loss_seg'] *= (1 - self.lr_loss_weight - self.hr_loss_weight)
if self.lr_loss_weight > 0:
loss['lr.loss_seg'] *= self.lr_loss_weight
if self.hr_loss_weight > 0:
loss['hr.loss_seg'] *= self.hr_loss_weight
return loss