-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
executable file
·358 lines (300 loc) · 13.3 KB
/
models.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
import torch
import torch.nn as nn
from torch.nn import functional as F
from typing import Optional, List, Dict, Tuple
import functools, pickle, logging
import cv2 as cv
import numpy as np
from segmentation_models_pytorch.encoders import get_encoder
from segmentation_models_pytorch.encoders._preprocessing import preprocess_input
from segmentation_models_pytorch.decoders.deeplabv3.decoder import SeparableConv2d, ASPPConv, ASPPSeparableConv
from segmentation_models_pytorch.base import initialization as init
from segmentation_models_pytorch.base import SegmentationHead
from segmentation_models_pytorch.losses import TverskyLoss
from detectron2.modeling import ROI_MASK_HEAD_REGISTRY, BaseMaskRCNNHead, ROI_HEADS_REGISTRY, StandardROIHeads
from detectron2.modeling.meta_arch import META_ARCH_REGISTRY, GeneralizedRCNN
from detectron2.modeling.roi_heads.mask_head import mask_rcnn_inference
from detectron2.structures import ImageList, Instances, Boxes, PolygonMasks
from detectron2.layers import ShapeSpec, cat
from detectron2.config import global_cfg
from detectron2.utils.events import get_event_storage
from detectron2.utils.logger import setup_logger
setup_logger()
logger = logging.getLogger("detectron2")
def mask_rcnn_loss(pred_mask_logits: torch.Tensor, instances: List[Instances], vis_period: int = 0):
cls_agnostic_mask = pred_mask_logits.size(1) == 1
total_num_masks = pred_mask_logits.size(0)
mask_side_len = pred_mask_logits.size(2)
assert pred_mask_logits.size(2) == pred_mask_logits.size(3), "Mask prediction must be square!"
gt_classes = []
gt_masks = []
for instances_per_image in instances:
if len(instances_per_image) == 0:
continue
if not cls_agnostic_mask:
gt_classes_per_image = instances_per_image.gt_classes.to(dtype=torch.int64)
gt_classes.append(gt_classes_per_image)
try:
gt_masks_per_image = instances_per_image.gt_masks.crop_and_resize(
instances_per_image.proposal_boxes.tensor, mask_side_len
).to(device=pred_mask_logits.device)
except:
gt_masks_per_image = torch.zeros((len(instances_per_image), mask_side_len, mask_side_len),
dtype=torch.bool, device=pred_mask_logits.device)
gt_masks.append(gt_masks_per_image)
if len(gt_masks) == 0:
return {"loss_mask_bce" : pred_mask_logits.sum() * 0, "loss_mask_dice" : pred_mask_logits.sum() * 0}
gt_masks = cat(gt_masks, dim=0)
if cls_agnostic_mask:
pred_mask_logits = pred_mask_logits[:, 0]
else:
indices = torch.arange(total_num_masks)
gt_classes = cat(gt_classes, dim=0)
pred_mask_logits = pred_mask_logits[indices, gt_classes]
if gt_masks.dtype == torch.bool:
gt_masks_bool = gt_masks
else:
gt_masks_bool = gt_masks > 0.5
gt_masks = gt_masks.to(dtype=torch.float32)
weights = []
pred_masks_bool = torch.sigmoid(pred_mask_logits) > 0.5
for enum, (gtm, pm) in enumerate(zip(gt_masks_bool, pred_masks_bool)):
if gtm.sum() > 0:
pwt = mask_side_len**2 / gtm.sum()
weights.append(torch.where(gtm == True, pwt, torch.ones(1).to(device=pred_mask_logits.device)))
else:
weights.append(torch.ones_like(pred_mask_logits[0]))
weights = torch.stack(weights, dim=0).to(device=pred_mask_logits.device)
bce_loss = F.binary_cross_entropy_with_logits(pred_mask_logits,
gt_masks,
reduction="mean",
weight=weights)
dice_loss = TverskyLoss(mode="binary", alpha=0.5, beta=0.5)(pred_mask_logits, gt_masks)
return {"loss_mask_bce" : bce_loss, "loss_mask_dice" : dice_loss}
class ASPP(nn.Module):
def __init__(self, in_channels, out_channels, atrous_rates, separable=False):
super(ASPP, self).__init__()
modules = []
modules.append(
nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
)
rate1, rate2, rate3 = tuple(atrous_rates)
ASPPConvModule = ASPPConv if not separable else ASPPSeparableConv
modules.append(ASPPConvModule(in_channels, out_channels, rate1))
modules.append(ASPPConvModule(in_channels, out_channels, rate2))
modules.append(ASPPConvModule(in_channels, out_channels, rate3))
modules.append(ASPPPooling(in_channels, out_channels))
self.convs = nn.ModuleList(modules)
self.project = nn.Sequential(
nn.Conv2d(5 * out_channels, out_channels, kernel_size=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
nn.Dropout(0.5),
)
def forward(self, x):
res = []
for conv in self.convs:
res.append(conv(x))
res = torch.cat(res, dim=1)
return self.project(res)
class ASPPPooling(nn.Sequential):
def __init__(self, in_channels, out_channels):
super().__init__(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False),
nn.ReLU(),
)
def forward(self, x):
size = x.shape[-2:]
for mod in self:
x = mod(x)
return F.interpolate(x, size=size, mode='bilinear', align_corners=False)
class DeepLabV3PlusDecoder(nn.Module):
def __init__(
self,
encoder_channels,
out_channels=256,
atrous_rates=(4, 8, 12),
output_stride=16,
):
super().__init__()
if output_stride not in {8, 16}:
raise ValueError("Output stride should be 8 or 16, got {}.".format(output_stride))
self.out_channels = out_channels
self.output_stride = output_stride
self.aspp = nn.Sequential(
ASPP(encoder_channels[-1], out_channels, atrous_rates, separable=True),
SeparableConv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
scale_factor = 8
self.up = nn.UpsamplingBilinear2d(scale_factor=scale_factor)
highres_in_channels = 256
highres_out_channels = 48
self.block1 = nn.Sequential(
nn.Conv2d(highres_in_channels, highres_out_channels, kernel_size=1, bias=False),
nn.BatchNorm2d(highres_out_channels),
nn.ReLU(),
)
self.block2 = nn.Sequential(
SeparableConv2d(
highres_out_channels + out_channels,
out_channels,
kernel_size=3,
padding=1,
bias=False,
),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
def forward(self, *features):
aspp_features = self.aspp(features[-1])
aspp_features = self.up(aspp_features)
high_res_features = self.block1(features[0])
concat_features = torch.cat([aspp_features, high_res_features], dim=1)
fused_features = self.block2(concat_features)
return fused_features
@ROI_MASK_HEAD_REGISTRY.register()
class DeepLabV3Plus(BaseMaskRCNNHead):
def __init__(self, cfg, input_shape: ShapeSpec):
super(DeepLabV3Plus, self).__init__()
self.encoder = get_encoder(
name = "resnet18",
in_channels = input_shape.channels,
depth= 3 ,
weights = "imagenet",
)
self.decoder = DeepLabV3PlusDecoder(
encoder_channels = self.encoder.out_channels,
out_channels = 256,
atrous_rates = (12, 24, 36),
output_stride = 16,
)
self.segmentation_head = SegmentationHead(
in_channels = self.decoder.out_channels,
out_channels = cfg.MODEL.ROI_HEADS.NUM_CLASSES,
activation = None,
kernel_size = 1,
upsampling = 2,
)
init.initialize_decoder(self.decoder)
init.initialize_head(self.segmentation_head)
def layers(self, x):
feats = self.encoder(x)
x = self.decoder(*feats)
x = self.segmentation_head(x)
return x
def forward(self, x, instances: List[Instances]):
x = self.layers(x)
if self.training:
return mask_rcnn_loss(x, instances)
else:
mask_rcnn_inference(x, instances)
return instances
@ROI_HEADS_REGISTRY.register()
class ModROIHeads(StandardROIHeads):
def forward(
self,
images: ImageList,
features: Dict[str, torch.Tensor],
proposals: List[Instances],
targets: Optional[List[Instances]] = None,
) -> Tuple[List[Instances], Dict[str, torch.Tensor]]:
del images
if self.training:
assert targets, "'targets' argument is required during training"
proposals = self.label_and_sample_proposals(proposals, targets)
del targets
if self.training:
losses, proposals = self._forward_box(features, proposals)
if global_cfg.GLOBAL.TRAIN_MASK:
losses.update(self._forward_mask(features, proposals))
storage = get_event_storage()
if storage.iter % global_cfg.GLOBAL.PRINT_FREQ == 0:
logger.info("Cumulative negative count: {}".format(global_cfg.GLOBAL.HN_COUNT))
logger.info("Cumulative positive count: {}".format(global_cfg.GLOBAL.HP_COUNT))
logger.info("Threshold: {}".format(global_cfg.GLOBAL.THRESHOLD))
global_cfg.GLOBAL.HN_COUNT = [0] * 5
global_cfg.GLOBAL.HP_COUNT = [0] * 5
return proposals, losses
else:
pred_instances = self._forward_box(features, proposals)
pred_instances = self.forward_with_given_boxes(features, pred_instances)
return pred_instances, {}
def _forward_mask(self, features: Dict[str, torch.Tensor], instances: List[Instances]):
if not self.mask_on:
return {} if self.training else instances
if self.mask_pooler is not None:
features = [features[f] for f in self.mask_in_features]
boxes = [x.proposal_boxes if self.training else x.pred_boxes for x in instances]
features = self.mask_pooler(features, boxes)
else:
features = {f: features[f] for f in self.mask_in_features}
return self.mask_head(features, instances)
def _forward_box(self, features: Dict[str, torch.Tensor], proposals: List[Instances]):
features = [features[f] for f in self.box_in_features]
box_features = self.box_pooler(features, [x.proposal_boxes for x in proposals])
box_features = self.box_head(box_features)
predictions = self.box_predictor(box_features)
del box_features
if self.training:
losses = self.box_predictor.losses(predictions, proposals) if not global_cfg.GLOBAL.TRAIN_MASK else {}
if self.train_on_pred_boxes:
with torch.no_grad():
pred_boxes = self.box_predictor.predict_boxes_for_gt_classes(
predictions, proposals
)
for proposals_per_image, pred_boxes_per_image in zip(proposals, pred_boxes):
proposals_per_image.proposal_boxes = Boxes(pred_boxes_per_image)
pred_probs = self.box_predictor.predict_probs(predictions, proposals)
filtered_proposals = []
for enum, (proposal_per_img, pred_probs_per_img) in enumerate(zip(proposals, pred_probs)):
gt_classes = proposal_per_img.gt_classes.clone()
pr_classes = torch.argmax(pred_probs_per_img[:, :-1], 1)
pr_scores = pred_probs_per_img[:, :-1][torch.arange(len(pred_probs_per_img)), pr_classes]
if len(torch.unique(gt_classes)) == 1 and torch.unique(gt_classes)[0] == 5:
selection_idxs = torch.argwhere(pr_scores > global_cfg.GLOBAL.THRESHOLD).squeeze(1)
for si in selection_idxs:
global_cfg.GLOBAL.HN_COUNT[pr_classes[si.item()]] += 1
else:
selection_idxs = torch.argwhere(gt_classes != 5).squeeze(1)
for si in selection_idxs:
global_cfg.GLOBAL.HP_COUNT[pr_classes[si.item()]] += 1
proposal_per_img.gt_classes = torch.where(gt_classes == 5, pr_classes, gt_classes)
filtered_proposals.append(proposal_per_img[selection_idxs])
return losses, filtered_proposals
else:
pred_instances, _ = self.box_predictor.inference(predictions, proposals)
return pred_instances
@META_ARCH_REGISTRY.register()
class ModGeneralizedRCNN(GeneralizedRCNN):
def forward(self, batched_inputs: List[Dict[str, torch.Tensor]]):
if not self.training:
return self.inference(batched_inputs)
images = self.preprocess_image(batched_inputs)
if "instances" in batched_inputs[0]:
gt_instances = [x["instances"].to(self.device) for x in batched_inputs]
else:
gt_instances = None
features = self.backbone(images.tensor)
if self.proposal_generator is not None:
proposals, proposal_losses = self.proposal_generator(images, features, gt_instances)
else:
assert "proposals" in batched_inputs[0]
proposals = [x["proposals"].to(self.device) for x in batched_inputs]
proposal_losses = {}
_, detector_losses = self.roi_heads(images, features, proposals, gt_instances)
if self.vis_period > 0:
storage = get_event_storage()
if storage.iter % self.vis_period == 0:
self.visualize_training(batched_inputs, proposals)
losses = {}
losses.update(detector_losses)
if not global_cfg.GLOBAL.TRAIN_MASK:
losses.update(proposal_losses)
return losses