Spaces:
Runtime error
Runtime error
File size: 20,503 Bytes
b34d1d6 |
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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
import copy
from typing import Optional, Tuple
import torch
import torch.nn.functional as F
from mmdet.models import DetDataPreprocessor
from mmdet.registry import MODELS
from kornia.contrib import distance_transform
from mmengine.structures import InstanceData
from seg.models.data_preprocessor import VideoSegDataPreprocessor
def get_center_coords(gt_instances, rescale_shape=None, device='cpu'):
if rescale_shape is not None:
masks = gt_instances.masks
masks = masks.rescale(rescale_shape)
else:
masks = gt_instances.masks
masks = masks.to_tensor(dtype=torch.bool, device=device)[:, None]
point_coords = []
for mask in masks:
mask = mask[None]
n, _, h, w = mask.shape
mask_dt = (
distance_transform(
(~F.pad(mask, pad=(1, 1, 1, 1), mode='constant', value=0)).float()
)[:, :, 1:-1, 1:-1]
)
selected_point = torch.tensor([mask_dt.argmax() / w, mask_dt.argmax() % w]).long().flip(0).to(
device)
point_coords.append(selected_point)
if len(point_coords) > 0:
point_coords = torch.stack(point_coords)[:, None]
else:
point_coords = torch.empty((0, 1, 2), dtype=torch.int32).to(device=device)
return point_coords
def get_random_points(gt_instances, device='cpu'):
point_coords = []
for instance_idx in range(len(gt_instances)):
mask = gt_instances.masks.masks[instance_idx]
candidate_indices = torch.tensor(mask, device=device).nonzero()
assert len(candidate_indices) > 0
selected_point = candidate_indices[torch.randperm(
len(candidate_indices), dtype=torch.int32, device=device)[0]].flip(0)
point_coords.append(selected_point)
if len(point_coords) > 0:
point_coords = torch.stack(point_coords)[:, None]
else:
point_coords = torch.empty((0, 1, 2), dtype=torch.int32).to(device=device)
return point_coords
@MODELS.register_module()
class OVSAMDataPreprocessor(DetDataPreprocessor):
def __init__(self, *args,
use_det: bool = False,
use_point: bool = False,
use_center_point: bool = False,
use_point_det: bool = False,
use_center_point_det: bool = False,
use_point_pseudo_box: bool = False,
use_img_center: bool = False,
use_custom_bbox: Optional[Tuple] = None,
use_custom_point: Optional[Tuple] = None,
num_proposals: int = 60,
default_mode: str = 'sam',
**kwargs):
super().__init__(*args, **kwargs)
self.num_proposals = num_proposals
self.use_det = use_det
self.use_point = use_point
self.use_center_point = use_center_point
self.use_point_det = use_point_det
self.use_center_point_det = use_center_point_det
self.use_point_pseudo_box = use_point_pseudo_box
self.use_img_center = use_img_center
self.use_custom_bbox = use_custom_bbox
self.use_custom_point = use_custom_point
self.default_mode = default_mode
def forward(self, data: dict, training: bool = False) -> dict:
data = super().forward(data, training=training)
inputs, data_samples = data['inputs'], data['data_samples']
if 'data_tag' in data_samples[0]:
data_tag = data_samples[0].data_tag
for i in range(1, len(data_samples)):
assert data_samples[i].data_tag == data_tag
else:
data_tag = self.default_mode
for i in range(0, len(data_samples)):
data_samples[i].data_tag = data_tag
device = inputs.device
if data_tag == 'sam_mul':
for data_sample in data_samples:
gt_instances_collected = data_sample.gt_instances_collected
gt_instances = data_sample.gt_instances
masks_list = []
for idx in range(len(gt_instances_collected)):
gt_ids = gt_instances_collected.sub_instances[idx]
masks_list.append(gt_instances.masks[gt_ids])
gt_instances = InstanceData(
labels=torch.zeros_like(gt_instances_collected.idx),
masks=masks_list,
point_coords=gt_instances_collected.point_coords,
bp=torch.zeros_like(gt_instances_collected.idx), # all box
)
# all points
data_sample.gt_instances = gt_instances
del data_sample.gt_instances_collected
elif data_tag == 'sam':
num_proposals = self.num_proposals if training else 10000000
if self.use_custom_bbox:
for data_sample in data_samples:
img_shape = data_sample.img_shape
data_sample.gt_instances = InstanceData(
bboxes=inputs.new_tensor([[img_shape[1] * self.use_custom_bbox[0],
img_shape[0] * self.use_custom_bbox[1],
img_shape[1] * self.use_custom_bbox[2],
img_shape[0] * self.use_custom_bbox[3]]])
)
elif self.use_img_center:
for data_sample in data_samples:
data_sample.gt_instances = InstanceData(
point_coords=inputs.new_tensor([[[data_sample.img_shape[1] / 2, data_sample.img_shape[0] / 2]]])
)
elif self.use_custom_point:
for data_sample in data_samples:
data_sample.gt_instances = InstanceData(
point_coords=inputs.new_tensor([[[self.use_custom_point[0], self.use_custom_point[1]]]])
)
elif self.use_det:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
gt_instances = gt_instances[:num_proposals]
if not training:
bboxes = gt_instances.bboxes
scale_factor = bboxes.new_tensor(data_sample.scale_factor).repeat(2)
bboxes = bboxes * scale_factor
gt_instances.bboxes = bboxes
num_ins = len(gt_instances)
bp_indicator = torch.zeros((num_ins,))
gt_instances.bp = bp_indicator.to(device=device)
data_sample.gt_instances = gt_instances
elif self.use_point_det:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
if len(gt_instances) < num_proposals:
num_copy = num_proposals // len(gt_instances) + 1
gt_instances = InstanceData.cat([copy.deepcopy(gt_instances) for _ in range(num_copy)])
gt_instances = gt_instances[:num_proposals]
if training:
gt_instances.point_coords = get_random_points(gt_instances, device=device)
else:
raise NotImplementedError
num_ins = len(gt_instances)
bp_indicator = torch.arange(2).repeat_interleave((num_ins // 2) + 1)[:num_ins]
gt_instances = gt_instances[torch.randperm(num_ins, device=device)]
gt_instances.bp = bp_indicator.to(device=device)
data_sample.gt_instances = gt_instances
elif self.use_center_point_det:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
gt_instances = gt_instances[:num_proposals]
if training:
gt_instances.point_coords = get_center_coords(gt_instances, device=device)
else:
gt_instances.point_coords = get_center_coords(
gt_instances, rescale_shape=data_sample.img_shape, device=device
)
bboxes = gt_instances.bboxes
scale_factor = bboxes.new_tensor(data_sample.scale_factor).repeat(2)
bboxes = bboxes * scale_factor
gt_instances.bboxes = bboxes
data_sample.gt_instances = gt_instances
elif self.use_point:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
gt_instances = gt_instances[:num_proposals]
if training:
gt_instances.point_coords = get_random_points(gt_instances, device=device)
else:
raise NotImplementedError
data_sample.gt_instances = gt_instances
elif self.use_center_point:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
gt_instances = gt_instances[:num_proposals]
if training:
gt_instances.point_coords = get_center_coords(gt_instances, device=device)
else:
gt_instances.point_coords = get_center_coords(
gt_instances, rescale_shape=data_sample.img_shape, device=device
)
data_sample.gt_instances = gt_instances
elif self.use_point_pseudo_box:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
if training:
if len(gt_instances) < num_proposals:
num_copy = num_proposals // len(gt_instances) + 1
gt_instances = InstanceData.cat([copy.deepcopy(gt_instances) for _ in range(num_copy)])
gt_instances = gt_instances[:num_proposals]
points = get_random_points(gt_instances, device=device)
else:
points = get_center_coords(
gt_instances, rescale_shape=data_sample.img_shape, device=device
)
points = points.squeeze(1)
gt_instances.point_coords = torch.cat([points - 3, points + 3], 1)
gt_instances.bp = torch.zeros_like(gt_instances.labels) # bug to match sam_mul
data_sample.gt_instances = gt_instances
else:
raise NotImplementedError
elif data_tag == 'coco':
pass
elif data_tag == 'img':
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
h, w = data_sample.img_shape
gt_instances.bboxes = torch.tensor(
[[0., 0., h, w]], dtype=torch.float32, device=gt_instances.labels.device
)
gt_instances.bp = torch.zeros((1,), dtype=torch.int32, device=gt_instances.labels.device)
elif data_tag == 'mosaic_img':
b, three, h, w = inputs.shape
num_img_per_batch = 4 * 4
assert b % num_img_per_batch == 0
target_h, target_w = h * 4, w * 4
new_b = b // num_img_per_batch
result_input = inputs.new_empty(b // num_img_per_batch, three, target_h, target_w)
cnt = 0
result_data_samples = []
for id_b in range(new_b):
cur_data_sample = data_samples[cnt]
cur_gt_instances = []
for id_x in range(4):
for id_y in range(4):
result_input[id_b, :, id_x * h: (id_x + 1) * h, id_y * w: (id_y + 1) * w] = inputs[cnt]
img_gt_instances = data_samples[cnt].gt_instances
img_gt_instances.bboxes += img_gt_instances.bboxes.new_tensor([
id_x * h, id_y * w, id_x * h, id_y * w
])
cur_gt_instances.append(img_gt_instances)
cnt += 1
cur_gt_instances = InstanceData.cat(cur_gt_instances)
cur_data_sample.gt_instances = cur_gt_instances
result_data_samples.append(cur_data_sample)
inputs = result_input
data_samples = result_data_samples
else:
raise NotImplementedError
return dict(inputs=inputs, data_samples=data_samples)
@MODELS.register_module()
class OVSAMVideoSegDataPreprocessor(VideoSegDataPreprocessor):
def __init__(self, *args,
use_det: bool = False,
use_point: bool = False,
use_center_point: bool = False,
use_point_det: bool = False,
use_center_point_det: bool = False,
use_point_pseudo_box: bool = False,
num_proposals: int = 60,
**kwargs):
super().__init__(*args, **kwargs)
self.num_proposals = num_proposals
self.use_det = use_det
self.use_point = use_point
self.use_center_point = use_center_point
self.use_point_det = use_point_det
self.use_center_point_det = use_center_point_det
self.use_point_pseudo_box = use_point_pseudo_box
def forward(self, data: dict, training: bool = False) -> dict:
data = super().forward(data, training=training)
inputs, data_samples = data['inputs'], data['data_samples']
if 'data_tag' in data_samples[0]:
data_tag = data_samples[0].data_tag
for i in range(1, len(data_samples)):
assert data_samples[i].data_tag == data_tag
else:
data_tag = 'sam'
for i in range(0, len(data_samples)):
data_samples[i].data_tag = data_tag
device = inputs.device
if data_tag == 'sam_mul':
for data_sample in data_samples:
gt_instances_collected = data_sample.gt_instances_collected
gt_instances = data_sample.gt_instances
masks_list = []
for idx in range(len(gt_instances_collected)):
gt_ids = gt_instances_collected.sub_instances[idx]
masks_list.append(gt_instances.masks[gt_ids])
gt_instances = InstanceData(
labels=torch.zeros_like(gt_instances_collected.idx),
masks=masks_list,
point_coords=gt_instances_collected.point_coords,
bp=torch.zeros_like(gt_instances_collected.idx), # all box
)
# all points
data_sample.gt_instances = gt_instances
del data_sample.gt_instances_collected
elif data_tag == 'sam':
num_proposals = self.num_proposals if training else 10000000
if self.use_det:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
gt_instances = gt_instances[:num_proposals]
if not training:
bboxes = gt_instances.bboxes
scale_factor = bboxes.new_tensor(data_sample.scale_factor).repeat(2)
bboxes = bboxes * scale_factor
gt_instances.bboxes = bboxes
data_sample.gt_instances = gt_instances
elif self.use_point_det:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
if len(gt_instances) < num_proposals:
num_copy = num_proposals // len(gt_instances) + 1
gt_instances = InstanceData.cat([copy.deepcopy(gt_instances) for _ in range(num_copy)])
gt_instances = gt_instances[:num_proposals]
if training:
gt_instances.point_coords = get_random_points(gt_instances, device=device)
else:
raise NotImplementedError
num_ins = len(gt_instances)
bp_indicator = torch.arange(2).repeat_interleave((num_ins // 2) + 1)[:num_ins]
gt_instances = gt_instances[torch.randperm(num_ins, device=device)]
gt_instances.bp = bp_indicator.to(device=device)
data_sample.gt_instances = gt_instances
elif self.use_center_point_det:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
gt_instances = gt_instances[:num_proposals]
if training:
gt_instances.point_coords = get_center_coords(gt_instances, device=device)
else:
gt_instances.point_coords = get_center_coords(
gt_instances, rescale_shape=data_sample.img_shape, device=device
)
bboxes = gt_instances.bboxes
scale_factor = bboxes.new_tensor(data_sample.scale_factor).repeat(2)
bboxes = bboxes * scale_factor
gt_instances.bboxes = bboxes
data_sample.gt_instances = gt_instances
elif self.use_point:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
gt_instances = gt_instances[:num_proposals]
if training:
gt_instances.point_coords = get_random_points(gt_instances, device=device)
else:
raise NotImplementedError
data_sample.gt_instances = gt_instances
elif self.use_center_point:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
gt_instances = gt_instances[:num_proposals]
if training:
gt_instances.point_coords = get_center_coords(gt_instances, device=device)
else:
gt_instances.point_coords = get_center_coords(
gt_instances, rescale_shape=data_sample.img_shape, device=device
)
data_sample.gt_instances = gt_instances
elif self.use_point_pseudo_box:
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
if training:
if len(gt_instances) < num_proposals:
num_copy = num_proposals // len(gt_instances) + 1
gt_instances = InstanceData.cat([copy.deepcopy(gt_instances) for _ in range(num_copy)])
gt_instances = gt_instances[:num_proposals]
points = get_random_points(gt_instances, device=device)
else:
points = get_center_coords(
gt_instances, rescale_shape=data_sample.img_shape, device=device
)
points = points.squeeze(1)
gt_instances.point_coords = torch.cat([points - 3, points + 3], 1)
gt_instances.bp = torch.zeros_like(gt_instances.labels) # bug to match sam_mul
data_sample.gt_instances = gt_instances
else:
raise NotImplementedError
elif data_tag == 'coco':
pass
elif data_tag == 'img':
for data_sample in data_samples:
gt_instances = data_sample.gt_instances
h, w = data_sample.img_shape
gt_instances.bboxes = torch.tensor(
[[0., 0., h, w]], dtype=torch.float32, device=gt_instances.labels.device
)
gt_instances.bp = torch.zeros((1,), dtype=torch.int32, device=gt_instances.labels.device)
else:
raise NotImplementedError
return dict(inputs=inputs, data_samples=data_samples)
|