-
Notifications
You must be signed in to change notification settings - Fork 17
/
detector.py
242 lines (192 loc) · 8.83 KB
/
detector.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
import cv2
import torch
import torch.nn.functional as F
from math import log
from torch import nn
from hisup.backbones import build_backbone
from hisup.utils.polygon import generate_polygon
from hisup.utils.polygon import get_pred_junctions
from skimage.measure import label, regionprops
def cross_entropy_loss_for_junction(logits, positive):
nlogp = -F.log_softmax(logits, dim=1)
loss = (positive * nlogp[:, None, 1] + (1 - positive) * nlogp[:, None, 0])
return loss.mean()
def sigmoid_l1_loss(logits, targets, offset = 0.0, mask=None):
logp = torch.sigmoid(logits) + offset
loss = torch.abs(logp-targets)
if mask is not None:
t = ((mask == 1) | (mask == 2)).float()
w = t.mean(3, True).mean(2,True)
w[w==0] = 1
loss = loss*(t/w)
return loss.mean()
# Copyright (c) 2019 BangguWu, Qilong Wang
# Modified by Bowen Xu, Jiakun Xu, Nan Xue and Gui-song Xia
class ECA(nn.Module):
def __init__(self, channel, gamma=2, b=1):
super(ECA, self).__init__()
C = channel
t = int(abs((log(C, 2) + b) / gamma))
k = t if t % 2 else t + 1
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv = nn.Conv1d(1, 1, kernel_size=k, padding=int(k/2), bias=False)
self.sigmoid = nn.Sigmoid()
self.out_conv = nn.Sequential(
nn.Conv2d(channel, channel, kernel_size=1, padding=0, bias=False),
nn.BatchNorm2d(channel),
nn.ReLU(inplace=True)
)
def forward(self, x1, x2):
y = self.avg_pool(x1 + x2)
y = self.conv(y.squeeze(-1).transpose(-1, -2))
y = y.transpose(-1 ,-2).unsqueeze(-1)
y = self.sigmoid(y)
out = self.out_conv(x2 * y.expand_as(x2))
return out
class BuildingDetector(nn.Module):
def __init__(self, cfg, test=False):
super(BuildingDetector, self).__init__()
self.backbone = build_backbone(cfg)
self.backbone_name = cfg.MODEL.NAME
self.junc_loss = nn.CrossEntropyLoss()
self.test_inria = 'inria' in cfg.DATASETS.TEST[0]
if not test:
from hisup.encoder import Encoder
self.encoder = Encoder(cfg)
self.pred_height = cfg.DATASETS.TARGET.HEIGHT
self.pred_width = cfg.DATASETS.TARGET.WIDTH
self.origin_height = cfg.DATASETS.ORIGIN.HEIGHT
self.origin_width = cfg.DATASETS.ORIGIN.WIDTH
dim_in = cfg.MODEL.OUT_FEATURE_CHANNELS
self.mask_head = self._make_conv(dim_in, dim_in, dim_in)
self.jloc_head = self._make_conv(dim_in, dim_in, dim_in)
self.afm_head = self._make_conv(dim_in, dim_in, dim_in)
self.a2m_att = ECA(dim_in)
self.a2j_att = ECA(dim_in)
self.mask_predictor = self._make_predictor(dim_in, 2)
self.jloc_predictor = self._make_predictor(dim_in, 3)
self.afm_predictor = self._make_predictor(dim_in, 2)
self.refuse_conv = self._make_conv(2, dim_in//2, dim_in)
self.final_conv = self._make_conv(dim_in*2, dim_in, 2)
self.train_step = 0
def forward(self, images, annotations = None):
if self.training:
return self.forward_train(images, annotations=annotations)
else:
return self.forward_test(images, annotations=annotations)
def forward_test(self, images, annotations = None):
device = images.device
outputs, features = self.backbone(images)
mask_feature = self.mask_head(features)
jloc_feature = self.jloc_head(features)
afm_feature = self.afm_head(features)
mask_att_feature = self.a2m_att(afm_feature, mask_feature)
jloc_att_feature = self.a2j_att(afm_feature, jloc_feature)
mask_pred = self.mask_predictor(mask_feature + mask_att_feature)
jloc_pred = self.jloc_predictor(jloc_feature + jloc_att_feature)
afm_pred = self.afm_predictor(afm_feature)
afm_conv = self.refuse_conv(afm_pred)
remask_pred = self.final_conv(torch.cat((features, afm_conv), dim=1))
joff_pred = outputs[:, :].sigmoid() - 0.5
mask_pred = mask_pred.softmax(1)[:,1:]
jloc_convex_pred = jloc_pred.softmax(1)[:, 2:3]
jloc_concave_pred = jloc_pred.softmax(1)[:, 1:2]
remask_pred = remask_pred.softmax(1)[:, 1:]
scale_y = self.origin_height / self.pred_height
scale_x = self.origin_width / self.pred_width
batch_polygons = []
batch_masks = []
batch_scores = []
batch_juncs = []
for b in range(remask_pred.size(0)):
mask_pred_per_im = cv2.resize(remask_pred[b][0].cpu().numpy(), (self.origin_width, self.origin_height))
juncs_pred = get_pred_junctions(jloc_concave_pred[b], jloc_convex_pred[b], joff_pred[b])
juncs_pred[:,0] *= scale_x
juncs_pred[:,1] *= scale_y
if not self.test_inria:
polys, scores = [], []
props = regionprops(label(mask_pred_per_im > 0.5))
for prop in props:
poly, juncs_sa, edges_sa, score, juncs_index = generate_polygon(prop, mask_pred_per_im, \
juncs_pred, 0, self.test_inria)
if juncs_sa.shape[0] == 0:
continue
polys.append(poly)
scores.append(score)
batch_scores.append(scores)
batch_polygons.append(polys)
batch_masks.append(mask_pred_per_im)
batch_juncs.append(juncs_pred)
extra_info = {}
output = {
'polys_pred': batch_polygons,
'mask_pred': batch_masks,
'scores': batch_scores,
'juncs_pred': batch_juncs
}
return output, extra_info
def forward_train(self, images, annotations = None):
self.train_step += 1
device = images.device
targets, metas = self.encoder(annotations)
outputs, features = self.backbone(images)
loss_dict = {
'loss_jloc': 0.0,
'loss_joff': 0.0,
'loss_mask': 0.0,
'loss_afm' : 0.0,
'loss_remask': 0.0
}
mask_feature = self.mask_head(features)
jloc_feature = self.jloc_head(features)
afm_feature = self.afm_head(features)
mask_att_feature = self.a2m_att(afm_feature, mask_feature)
jloc_att_feature = self.a2j_att(afm_feature, jloc_feature)
mask_pred = self.mask_predictor(mask_feature + mask_att_feature)
jloc_pred = self.jloc_predictor(jloc_feature + jloc_att_feature)
afm_pred = self.afm_predictor(afm_feature)
afm_conv = self.refuse_conv(afm_pred)
remask_pred = self.final_conv(torch.cat((features, afm_conv), dim=1))
if targets is not None:
loss_dict['loss_jloc'] += self.junc_loss(jloc_pred, targets['jloc'].squeeze(dim=1))
loss_dict['loss_joff'] += sigmoid_l1_loss(outputs[:, :], targets['joff'], -0.5, targets['jloc'])
loss_dict['loss_mask'] += F.cross_entropy(mask_pred, targets['mask'].squeeze(dim=1).long())
loss_dict['loss_afm'] += F.l1_loss(afm_pred, targets['afmap'])
loss_dict['loss_remask'] += F.cross_entropy(remask_pred, targets['mask'].squeeze(dim=1).long())
extra_info = {}
return loss_dict, extra_info
def _make_conv(self, dim_in, dim_hid, dim_out):
layer = nn.Sequential(
nn.Conv2d(dim_in, dim_hid, kernel_size=3, padding=1),
nn.BatchNorm2d(dim_hid),
nn.ReLU(inplace=True),
nn.Conv2d(dim_hid, dim_hid, kernel_size=3, padding=1),
nn.BatchNorm2d(dim_hid),
nn.ReLU(inplace=True),
nn.Conv2d(dim_hid, dim_out, kernel_size=3, padding=1),
nn.BatchNorm2d(dim_out),
nn.ReLU(inplace=True),
)
return layer
def _make_predictor(self, dim_in, dim_out):
m = int(dim_in / 4)
layer = nn.Sequential(
nn.Conv2d(dim_in, m, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(m, dim_out, kernel_size=1),
)
return layer
def get_pretrained_model(cfg, dataset, device, pretrained=True):
PRETRAINED = {
'crowdai': 'https://github.com/XJKunnn/pretrained_model/releases/download/pretrained_model/crowdai_hrnet48_e100.pth',
'inria': 'https://github.com/XJKunnn/pretrained_model/releases/download/pretrained_model/inria_hrnet48_e5.pth'
}
model = BuildingDetector(cfg, test=True)
if pretrained:
url = PRETRAINED[dataset]
state_dict = torch.hub.load_state_dict_from_url(url, map_location=device, progress=True)
state_dict = {k[7:]:v for k,v in state_dict['model'].items() if k[0:7] == 'module.'}
model.load_state_dict(state_dict)
model = model.eval()
return model
return model