-
Notifications
You must be signed in to change notification settings - Fork 27
/
san.py
277 lines (258 loc) · 10.7 KB
/
san.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
from typing import List
import open_clip
import torch
from detectron2.config import configurable
from detectron2.modeling import META_ARCH_REGISTRY
from detectron2.modeling.postprocessing import sem_seg_postprocess
from detectron2.structures import ImageList
from detectron2.utils.memory import retry_if_cuda_oom
from torch import nn
from torch.nn import functional as F
from .clip_utils import (
FeatureExtractor,
LearnableBgOvClassifier,
PredefinedOvClassifier,
RecWithAttnbiasHead,
get_predefined_templates,
)
from .criterion import SetCriterion
from .matcher import HungarianMatcher
from .side_adapter import build_side_adapter_network
@META_ARCH_REGISTRY.register()
class SAN(nn.Module):
@configurable
def __init__(
self,
*,
clip_visual_extractor: nn.Module,
clip_rec_head: nn.Module,
side_adapter_network: nn.Module,
ov_classifier: PredefinedOvClassifier,
criterion: SetCriterion,
size_divisibility: int,
asymetric_input: bool = True,
clip_resolution: float = 0.5,
pixel_mean: List[float] = [0.48145466, 0.4578275, 0.40821073],
pixel_std: List[float] = [0.26862954, 0.26130258, 0.27577711],
sem_seg_postprocess_before_inference: bool = False,
):
super().__init__()
self.asymetric_input = asymetric_input
self.clip_resolution = clip_resolution
self.sem_seg_postprocess_before_inference = sem_seg_postprocess_before_inference
self.size_divisibility = size_divisibility
self.criterion = criterion
self.side_adapter_network = side_adapter_network
self.clip_visual_extractor = clip_visual_extractor
self.clip_rec_head = clip_rec_head
self.ov_classifier = ov_classifier
self.register_buffer(
"pixel_mean", torch.Tensor(pixel_mean).view(-1, 1, 1), False
)
self.register_buffer("pixel_std", torch.Tensor(pixel_std).view(-1, 1, 1), False)
@classmethod
def from_config(cls, cfg):
## copied from maskformer2
# Loss parameters
no_object_weight = cfg.MODEL.SAN.NO_OBJECT_WEIGHT
# loss weights
class_weight = cfg.MODEL.SAN.CLASS_WEIGHT
dice_weight = cfg.MODEL.SAN.DICE_WEIGHT
mask_weight = cfg.MODEL.SAN.MASK_WEIGHT
# building criterion
matcher = HungarianMatcher(
cost_class=class_weight,
cost_mask=mask_weight,
cost_dice=dice_weight,
num_points=cfg.MODEL.SAN.TRAIN_NUM_POINTS,
)
weight_dict = {
"loss_ce": class_weight,
"loss_mask": mask_weight,
"loss_dice": dice_weight,
}
aux_weight_dict = {}
for i in range(len(cfg.MODEL.SIDE_ADAPTER.DEEP_SUPERVISION_IDXS) - 1):
aux_weight_dict.update({k + f"_{i}": v for k, v in weight_dict.items()})
weight_dict.update(aux_weight_dict)
losses = ["labels", "masks"]
criterion = SetCriterion(
num_classes=cfg.MODEL.SAN.NUM_CLASSES,
matcher=matcher,
weight_dict=weight_dict,
eos_coef=no_object_weight,
losses=losses,
num_points=cfg.MODEL.SAN.TRAIN_NUM_POINTS,
oversample_ratio=cfg.MODEL.SAN.OVERSAMPLE_RATIO,
importance_sample_ratio=cfg.MODEL.SAN.IMPORTANCE_SAMPLE_RATIO,
)
## end of copy
model, _, preprocess = open_clip.create_model_and_transforms(
cfg.MODEL.SAN.CLIP_MODEL_NAME,
pretrained=cfg.MODEL.SAN.CLIP_PRETRAINED_NAME,
)
ov_classifier = LearnableBgOvClassifier(
model, templates=get_predefined_templates(cfg.MODEL.SAN.CLIP_TEMPLATE_SET)
)
clip_visual_extractor = FeatureExtractor(
model.visual,
last_layer_idx=cfg.MODEL.SAN.FEATURE_LAST_LAYER_IDX,
frozen_exclude=cfg.MODEL.SAN.CLIP_FROZEN_EXCLUDE,
)
clip_rec_head = RecWithAttnbiasHead(
model.visual,
first_layer_idx=cfg.MODEL.SAN.FEATURE_LAST_LAYER_IDX,
frozen_exclude=cfg.MODEL.SAN.CLIP_DEEPER_FROZEN_EXCLUDE,
cross_attn=cfg.MODEL.SAN.REC_CROSS_ATTN,
sos_token_format=cfg.MODEL.SAN.SOS_TOKEN_FORMAT,
sos_token_num=cfg.MODEL.SIDE_ADAPTER.NUM_QUERIES,
downsample_method=cfg.MODEL.SAN.REC_DOWNSAMPLE_METHOD,
)
pixel_mean, pixel_std = (
preprocess.transforms[-1].mean,
preprocess.transforms[-1].std,
)
pixel_mean = [255.0 * x for x in pixel_mean]
pixel_std = [255.0 * x for x in pixel_std]
return {
"clip_visual_extractor": clip_visual_extractor,
"clip_rec_head": clip_rec_head,
"side_adapter_network": build_side_adapter_network(
cfg, clip_visual_extractor.output_shapes
),
"ov_classifier": ov_classifier,
"criterion": criterion,
"size_divisibility": cfg.MODEL.SAN.SIZE_DIVISIBILITY,
"asymetric_input": cfg.MODEL.SAN.ASYMETRIC_INPUT,
"clip_resolution": cfg.MODEL.SAN.CLIP_RESOLUTION,
"sem_seg_postprocess_before_inference": cfg.MODEL.SAN.SEM_SEG_POSTPROCESS_BEFORE_INFERENCE,
"pixel_mean": pixel_mean,
"pixel_std": pixel_std,
}
def forward(self, batched_inputs):
# get classifier weight for each dataset
# !! Could be computed once and saved. It will run only once per dataset.
if "vocabulary" in batched_inputs[0]:
ov_classifier_weight = (
self.ov_classifier.logit_scale.exp()
* self.ov_classifier.get_classifier_by_vocabulary(
batched_inputs[0]["vocabulary"]
)
)
else:
dataset_names = [x["meta"]["dataset_name"] for x in batched_inputs]
assert (
len(list(set(dataset_names))) == 1
), "All images in a batch must be from the same dataset."
ov_classifier_weight = (
self.ov_classifier.logit_scale.exp()
* self.ov_classifier.get_classifier_by_dataset_name(dataset_names[0])
) # C+1,ndim
images = [x["image"].to(self.device) for x in batched_inputs]
images = [(x - self.pixel_mean) / self.pixel_std for x in images]
images = ImageList.from_tensors(images, self.size_divisibility)
clip_input = images.tensor
if self.asymetric_input:
clip_input = F.interpolate(
clip_input, scale_factor=self.clip_resolution, mode="bilinear"
)
clip_image_features = self.clip_visual_extractor(clip_input)
mask_preds, attn_biases = self.side_adapter_network(
images.tensor, clip_image_features
)
# !! Could be optimized to run in parallel.
mask_embs = [
self.clip_rec_head(clip_image_features, attn_bias, normalize=True)
for attn_bias in attn_biases
] # [B,N,C]
mask_logits = [
torch.einsum("bqc,nc->bqn", mask_emb, ov_classifier_weight)
for mask_emb in mask_embs
]
if self.training:
if "instances" in batched_inputs[0]:
gt_instances = [x["instances"].to(self.device) for x in batched_inputs]
targets = self.prepare_targets(gt_instances, images)
else:
targets = None
outputs = {
"pred_logits": mask_logits[-1],
"pred_masks": mask_preds[-1],
"aux_outputs": [
{
"pred_logits": aux_pred_logits,
"pred_masks": aux_pred_masks,
}
for aux_pred_logits, aux_pred_masks in zip(
mask_logits[:-1], mask_preds[:-1]
)
],
}
# bipartite matching-based loss
losses = self.criterion(outputs, targets)
for k in list(losses.keys()):
if k in self.criterion.weight_dict:
losses[k] *= self.criterion.weight_dict[k]
else:
# remove this loss if not specified in `weight_dict`
losses.pop(k)
return losses
else:
mask_preds = mask_preds[-1]
mask_logits = mask_logits[-1]
# torch.cuda.empty_cache()
# Inference
mask_preds = F.interpolate(
mask_preds,
size=(images.tensor.shape[-2], images.tensor.shape[-1]),
mode="bilinear",
align_corners=False,
)
processed_results = []
for mask_cls_result, mask_pred_result, input_per_image, image_size in zip(
mask_logits, mask_preds, batched_inputs, images.image_sizes
):
height = input_per_image.get("height", image_size[0])
width = input_per_image.get("width", image_size[1])
processed_results.append({})
if self.sem_seg_postprocess_before_inference:
mask_pred_result = retry_if_cuda_oom(sem_seg_postprocess)(
mask_pred_result, image_size, height, width
)
mask_cls_result = mask_cls_result.to(mask_pred_result)
r = retry_if_cuda_oom(self.semantic_inference)(
mask_cls_result, mask_pred_result
)
if not self.sem_seg_postprocess_before_inference:
r = retry_if_cuda_oom(sem_seg_postprocess)(
r, image_size, height, width
)
processed_results[-1]["sem_seg"] = r
return processed_results
def prepare_targets(self, targets, images):
h_pad, w_pad = images.tensor.shape[-2:]
new_targets = []
for targets_per_image in targets:
# pad gt
gt_masks = targets_per_image.gt_masks
padded_masks = torch.zeros(
(gt_masks.shape[0], h_pad, w_pad),
dtype=gt_masks.dtype,
device=gt_masks.device,
)
padded_masks[:, : gt_masks.shape[1], : gt_masks.shape[2]] = gt_masks
new_targets.append(
{
"labels": targets_per_image.gt_classes,
"masks": padded_masks,
}
)
return new_targets
def semantic_inference(self, mask_cls, mask_pred):
mask_cls = F.softmax(mask_cls, dim=-1)[..., :-1]
mask_pred = mask_pred.sigmoid()
semseg = torch.einsum("qc,qhw->chw", mask_cls, mask_pred)
return semseg
@property
def device(self):
return self.pixel_mean.device