-
Notifications
You must be signed in to change notification settings - Fork 1
/
detector.py
345 lines (283 loc) · 15 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
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
import logging
import math
from typing import List
import numpy as np
import torch
import torch.distributed as dist
import torch.nn.functional as F
from torch import nn
from detectron2.layers import Conv2d, ShapeSpec, get_norm
from detectron2.modeling import META_ARCH_REGISTRY, detector_postprocess
from detectron2.modeling.roi_heads import build_roi_heads
from detectron2.structures import Boxes, ImageList, Instances
from detectron2.utils.logger import log_first_n
from fvcore.nn import giou_loss, smooth_l1_loss
import fvcore.nn.weight_init as weight_init
from .loss import SetCriterion, HungarianMatcher
from .head import DynamicHead
from .util.box_ops import box_cxcywh_to_xyxy, box_xyxy_to_cxcywh
from .util.misc import (NestedTensor, nested_tensor_from_tensor_list,
accuracy, get_world_size, interpolate,
is_dist_avail_and_initialized)
from .build import build_backbone_gate
import os
import torch.distributed as dist
__all__ = ["SparseRCNN_ROSETTA"]
@META_ARCH_REGISTRY.register()
class SparseRCNN_ROSETTA(nn.Module):
"""
Implement SparseRCNN
"""
def __init__(self, cfg):
super().__init__()
self.device = torch.device(cfg.MODEL.DEVICE)
self.in_features = cfg.MODEL.ROI_HEADS.IN_FEATURES
self.num_classes = cfg.MODEL.SparseRCNN.NUM_CLASSES
self.num_proposals = cfg.MODEL.SparseRCNN.NUM_PROPOSALS
self.hidden_dim = cfg.MODEL.SparseRCNN.HIDDEN_DIM
self.num_heads = cfg.MODEL.SparseRCNN.NUM_HEADS
self.task = cfg.MODEL.TASK
self.fre = cfg.MODEL.BACKBONE_FRE_GATE_INPUT
# Build distillation
self.backbone = build_backbone_gate(cfg)
for i in self.backbone.parameters():
i.requires_grad = False
self.distillation_shape = self.backbone.bottom_up.dis_feature_shape()
self.names = cfg.MODEL.DISTILLATION_FEATURE
if self.task != 1:
self.prototpyes_pre_task = []
self.prototpyes_pre_task_threshold = []
for i in range(self.task-1):
prototpye = torch.load(f'prototypes/task{i + 1}.pt',map_location = f'cuda:{dist.get_rank()}')
self.prototpyes_pre_task.append(prototpye)
matrix = torch.zeros(prototpye.shape[0],prototpye.shape[0])
for j in range(prototpye.shape[0]):
for k in range(prototpye.shape[0]):
matrix[j][k] = (prototpye[j]-prototpye[k]).square().mean()
if j == k :
matrix[j][k] = 1000000
matrix = matrix.min(0)[0]
matrix = matrix.mean()
#matrix = matrix.max(0)[0]
self.prototpyes_pre_task_threshold.append(matrix)
# Build Backbone.
self.backbone_model = build_backbone_gate(cfg, gate=True)
self.size_divisibility = self.backbone.size_divisibility
# Build Proposals.
self.init_proposal_features = nn.Embedding(self.num_proposals, self.hidden_dim)
self.init_proposal_boxes = nn.Embedding(self.num_proposals, 4)
nn.init.constant_(self.init_proposal_boxes.weight[:, :2], 0.5)
nn.init.constant_(self.init_proposal_boxes.weight[:, 2:], 1.0)
# Build Dynamic Head.
self.head = DynamicHead(cfg=cfg, roi_input_shape=self.backbone_model.output_shape())
# Loss parameters:
class_weight = cfg.MODEL.SparseRCNN.CLASS_WEIGHT
distillation_weight = cfg.MODEL.SparseRCNN.DISTILLATION_WEIGHT
diversity_weight = cfg.MODEL.SparseRCNN.DIVERSITY_WEIGHT
gate_weight = cfg.MODEL.SparseRCNN.GATE_WEIGHT
giou_weight = cfg.MODEL.SparseRCNN.GIOU_WEIGHT
l1_weight = cfg.MODEL.SparseRCNN.L1_WEIGHT
no_object_weight = cfg.MODEL.SparseRCNN.NO_OBJECT_WEIGHT
self.deep_supervision = cfg.MODEL.SparseRCNN.DEEP_SUPERVISION
self.use_focal = cfg.MODEL.SparseRCNN.USE_FOCAL
# Build Criterion.
matcher = HungarianMatcher(cfg=cfg,
cost_class=class_weight,
cost_bbox=l1_weight,
cost_giou=giou_weight,
use_focal=self.use_focal)
weight_dict = {"loss_ce": class_weight, "loss_bbox": l1_weight, "loss_giou": giou_weight,
"loss_gate": gate_weight, "loss_distillation": distillation_weight,
"loss_diversity": diversity_weight}
if self.deep_supervision:
aux_weight_dict = {}
for i in range(self.num_heads - 1):
aux_weight_dict.update({k + f"_{i}": v for k, v in weight_dict.items()})
weight_dict.update(aux_weight_dict)
losses = ["labels", "boxes"]
self.criterion = SetCriterion(cfg=cfg,
num_classes=self.num_classes,
matcher=matcher,
weight_dict=weight_dict,
eos_coef=no_object_weight,
losses=losses,
use_focal=self.use_focal)
pixel_mean = torch.Tensor(cfg.MODEL.PIXEL_MEAN).to(self.device).view(3, 1, 1)
pixel_std = torch.Tensor(cfg.MODEL.PIXEL_STD).to(self.device).view(3, 1, 1)
self.normalizer = lambda x: (x - pixel_mean) / pixel_std
self.to(self.device)
def forward(self, batched_inputs):
"""
Args:
batched_inputs: a list, batched outputs of :class:`DatasetMapper` .
Each item in the list contains the inputs for one image.
For now, each item in the list is a dict that contains:
* image: Tensor, image in (C, H, W) format.
* instances: Instances
Other information that's included in the original dicts, such as:
* "height", "width" (int): the output resolution of the model, used in inference.
See :meth:`postprocess` for details.
"""
images, images_whwh = self.preprocess_image(batched_inputs)
if isinstance(images, (list, torch.Tensor)):
images = nested_tensor_from_tensor_list(images)
# Feature Extraction.
src, gate_loss, feature_gate_distillation, gate_list, diversity_loss_total = self.backbone_model(images.tensor)
free_src, free_gate_loss, feature_nogate_distillation, free_gate_list, free_diversity_loss = self.backbone(
images.tensor)
features = list()
for f in self.in_features:
feature = src[f]
features.append(feature)
# Distillation
loss_distillation = 0
for name in self.names:
loss_distillation = loss_distillation + (feature_nogate_distillation[name] - feature_gate_distillation[name]).square().mean()
loss_distillation /= len(self.names)
if self.fre == 1 :
loss_distillation = None
gate_loss = None
# Diversity
diversity_loss_control = None
if os.path.exists(f'prototypes/task{self.task}_0.pt'):
prototpye = torch.load(f'prototypes/task{self.task}_0.pt',map_location = f'cuda:{dist.get_rank()}')
else:
prototpye = None
if (self.task == 1 or self.fre == 1 or prototpye == None):
diversity_loss = None
diversity_loss_control_list= None
else:
diversity_loss = 0
diversity_loss_control_list= []
for prototpye_pre_task, prototpye_pre_task_threshold,diversity_loss_one in zip(
self.prototpyes_pre_task,self.prototpyes_pre_task_threshold,diversity_loss_total):
matrix = torch.zeros(prototpye_pre_task.shape[0], prototpye.shape[0])
for j in range(prototpye_pre_task.shape[0]):
for k in range(prototpye.shape[0]):
matrix[j][k] = (prototpye_pre_task[j] - prototpye[k]).square().mean()
matrix = matrix.min(0)[0]
matrix = matrix.mean()
diversity_loss_control = ((matrix - prototpye_pre_task_threshold)/matrix).detach()
diversity_loss_control_list.append(diversity_loss_control)
if diversity_loss_control<=0:
diversity_loss_control = 0.000000001
diversity_loss += (diversity_loss_control*diversity_loss_one).sum()
diversity_loss /= len(diversity_loss_total)
# Prepare Proposals.
proposal_boxes = self.init_proposal_boxes.weight.clone()
proposal_boxes = box_cxcywh_to_xyxy(proposal_boxes)
proposal_boxes = proposal_boxes[None] * images_whwh[:, None, :]
# Prediction.
outputs_class, outputs_coord, prototypes = self.head(features, proposal_boxes, self.init_proposal_features.weight)
output = {'pred_logits': outputs_class[-1], 'pred_boxes': outputs_coord[-1]}
if self.training:
gt_instances = [x["instances"].to(self.device) for x in batched_inputs]
targets = self.prepare_targets(gt_instances)
if self.deep_supervision:
output['aux_outputs'] = [{'pred_logits': a, 'pred_boxes': b}
for a, b in zip(outputs_class[:-1], outputs_coord[:-1])]
loss_dict = self.criterion(output, targets, gate_loss, loss_distillation, diversity_loss)
weight_dict = self.criterion.weight_dict
for k in loss_dict.keys():
if k in weight_dict:
loss_dict[k] *= weight_dict[k]
return loss_dict
else:
if diversity_loss_control_list != None:
print(diversity_loss_control_list)
box_cls = output["pred_logits"]
box_pred = output["pred_boxes"]
results = self.inference(box_cls, box_pred, images.image_sizes)
prototypes_tensor = torch.zeros(self.num_classes,prototypes.shape[2]).cuda()
prototypes_number = torch.zeros(self.num_classes).cuda()
for i in range(len(results)):
prototype = prototypes[i]
zero = torch.zeros_like(results[i].pred_classes) - 1
cls = torch.where(results[i].scores > 0.5, results[i].pred_classes,zero)
for j in range(cls.shape[0]):
if cls[j] != -1 :
prototypes_tensor[cls[j]] += prototype[j]
prototypes_number[cls[j]] += 1
prototypes_dict = {}
prototypes_dict['prototypes_tensor'] = prototypes_tensor
prototypes_dict['prototypes_number'] = prototypes_number
processed_results = []
for results_per_image, input_per_image, image_size in zip(results, batched_inputs, images.image_sizes):
height = input_per_image.get("height", image_size[0])
width = input_per_image.get("width", image_size[1])
r = detector_postprocess(results_per_image, height, width)
processed_results.append({"instances": r})
return processed_results, gate_list, prototypes_dict
def prepare_targets(self, targets):
new_targets = []
for targets_per_image in targets:
target = {}
h, w = targets_per_image.image_size
image_size_xyxy = torch.as_tensor([w, h, w, h], dtype=torch.float, device=self.device)
gt_classes = targets_per_image.gt_classes
gt_boxes = targets_per_image.gt_boxes.tensor / image_size_xyxy
gt_boxes = box_xyxy_to_cxcywh(gt_boxes)
target["labels"] = gt_classes.to(self.device)
target["boxes"] = gt_boxes.to(self.device)
target["boxes_xyxy"] = targets_per_image.gt_boxes.tensor.to(self.device)
target["image_size_xyxy"] = image_size_xyxy.to(self.device)
image_size_xyxy_tgt = image_size_xyxy.unsqueeze(0).repeat(len(gt_boxes), 1)
target["image_size_xyxy_tgt"] = image_size_xyxy_tgt.to(self.device)
target["area"] = targets_per_image.gt_boxes.area().to(self.device)
new_targets.append(target)
return new_targets
def inference(self, box_cls, box_pred, image_sizes):
"""
Arguments:
box_cls (Tensor): tensor of shape (batch_size, num_proposals, K).
The tensor predicts the classification probability for each proposal.
box_pred (Tensor): tensors of shape (batch_size, num_proposals, 4).
The tensor predicts 4-vector (x,y,w,h) box
regression values for every proposal
image_sizes (List[torch.Size]): the input image sizes
Returns:
results (List[Instances]): a list of #images elements.
"""
assert len(box_cls) == len(image_sizes)
results = []
if self.use_focal:
scores = torch.sigmoid(box_cls)
labels = torch.arange(self.num_classes, device=self.device). \
unsqueeze(0).repeat(self.num_proposals, 1).flatten(0, 1)
for i, (scores_per_image, box_pred_per_image, image_size) in enumerate(zip(
scores, box_pred, image_sizes
)):
result = Instances(image_size)
scores_per_image, topk_indices = scores_per_image.flatten(0, 1).topk(self.num_proposals, sorted=False)
labels_per_image = labels[topk_indices]
box_pred_per_image = box_pred_per_image.view(-1, 1, 4).repeat(1, self.num_classes, 1).view(-1, 4)
box_pred_per_image = box_pred_per_image[topk_indices]
result.pred_boxes = Boxes(box_pred_per_image)
result.scores = scores_per_image
result.pred_classes = labels_per_image
results.append(result)
else:
# For each box we assign the best class or the second best if the best on is `no_object`.
scores, labels = F.softmax(box_cls, dim=-1)[:, :, :-1].max(-1)
for i, (scores_per_image, labels_per_image, box_pred_per_image, image_size) in enumerate(zip(
scores, labels, box_pred, image_sizes
)):
result = Instances(image_size)
result.pred_boxes = Boxes(box_pred_per_image)
result.pred_boxes.scale(scale_x=image_size[1], scale_y=image_size[0])
result.scores = scores_per_image
result.pred_classes = labels_per_image
results.append(result)
return results
def preprocess_image(self, batched_inputs):
"""
Normalize, pad and batch the input images.
"""
images = [self.normalizer(x["image"].to(self.device)) for x in batched_inputs]
images = ImageList.from_tensors(images, self.size_divisibility)
images_whwh = list()
for bi in batched_inputs:
h, w = bi["image"].shape[-2:]
images_whwh.append(torch.tensor([w, h, w, h], dtype=torch.float32, device=self.device))
images_whwh = torch.stack(images_whwh)
return images, images_whwh