|
|
|
@ -10,7 +10,7 @@ from .metrics import bbox_iou
|
|
|
|
|
TORCH_1_10 = check_version(torch.__version__, '1.10.0')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-9, roll_out=False):
|
|
|
|
|
def select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-9):
|
|
|
|
|
"""select the positive anchor center in gt
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
@ -21,18 +21,10 @@ def select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-9, roll_out=False):
|
|
|
|
|
"""
|
|
|
|
|
n_anchors = xy_centers.shape[0]
|
|
|
|
|
bs, n_boxes, _ = gt_bboxes.shape
|
|
|
|
|
if roll_out:
|
|
|
|
|
bbox_deltas = torch.empty((bs, n_boxes, n_anchors), device=gt_bboxes.device)
|
|
|
|
|
for b in range(bs):
|
|
|
|
|
lt, rb = gt_bboxes[b].view(-1, 1, 4).chunk(2, 2) # left-top, right-bottom
|
|
|
|
|
bbox_deltas[b] = torch.cat((xy_centers[None] - lt, rb - xy_centers[None]),
|
|
|
|
|
dim=2).view(n_boxes, n_anchors, -1).amin(2).gt_(eps)
|
|
|
|
|
return bbox_deltas
|
|
|
|
|
else:
|
|
|
|
|
lt, rb = gt_bboxes.view(-1, 1, 4).chunk(2, 2) # left-top, right-bottom
|
|
|
|
|
bbox_deltas = torch.cat((xy_centers[None] - lt, rb - xy_centers[None]), dim=2).view(bs, n_boxes, n_anchors, -1)
|
|
|
|
|
# return (bbox_deltas.min(3)[0] > eps).to(gt_bboxes.dtype)
|
|
|
|
|
return bbox_deltas.amin(3).gt_(eps)
|
|
|
|
|
lt, rb = gt_bboxes.view(-1, 1, 4).chunk(2, 2) # left-top, right-bottom
|
|
|
|
|
bbox_deltas = torch.cat((xy_centers[None] - lt, rb - xy_centers[None]), dim=2).view(bs, n_boxes, n_anchors, -1)
|
|
|
|
|
# return (bbox_deltas.min(3)[0] > eps).to(gt_bboxes.dtype)
|
|
|
|
|
return bbox_deltas.amin(3).gt_(eps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def select_highest_overlaps(mask_pos, overlaps, n_max_boxes):
|
|
|
|
@ -63,7 +55,7 @@ def select_highest_overlaps(mask_pos, overlaps, n_max_boxes):
|
|
|
|
|
|
|
|
|
|
class TaskAlignedAssigner(nn.Module):
|
|
|
|
|
|
|
|
|
|
def __init__(self, topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-9, roll_out_thr=0):
|
|
|
|
|
def __init__(self, topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-9):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.topk = topk
|
|
|
|
|
self.num_classes = num_classes
|
|
|
|
@ -71,7 +63,6 @@ class TaskAlignedAssigner(nn.Module):
|
|
|
|
|
self.alpha = alpha
|
|
|
|
|
self.beta = beta
|
|
|
|
|
self.eps = eps
|
|
|
|
|
self.roll_out_thr = roll_out_thr
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
def forward(self, pd_scores, pd_bboxes, anc_points, gt_labels, gt_bboxes, mask_gt):
|
|
|
|
@ -93,7 +84,6 @@ class TaskAlignedAssigner(nn.Module):
|
|
|
|
|
"""
|
|
|
|
|
self.bs = pd_scores.size(0)
|
|
|
|
|
self.n_max_boxes = gt_bboxes.size(1)
|
|
|
|
|
self.roll_out = self.n_max_boxes > self.roll_out_thr if self.roll_out_thr else False
|
|
|
|
|
|
|
|
|
|
if self.n_max_boxes == 0:
|
|
|
|
|
device = gt_bboxes.device
|
|
|
|
@ -119,40 +109,35 @@ class TaskAlignedAssigner(nn.Module):
|
|
|
|
|
return target_labels, target_bboxes, target_scores, fg_mask.bool(), target_gt_idx
|
|
|
|
|
|
|
|
|
|
def get_pos_mask(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt):
|
|
|
|
|
# get anchor_align metric, (b, max_num_obj, h*w)
|
|
|
|
|
align_metric, overlaps = self.get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes)
|
|
|
|
|
# get in_gts mask, (b, max_num_obj, h*w)
|
|
|
|
|
mask_in_gts = select_candidates_in_gts(anc_points, gt_bboxes, roll_out=self.roll_out)
|
|
|
|
|
mask_in_gts = select_candidates_in_gts(anc_points, gt_bboxes)
|
|
|
|
|
# get anchor_align metric, (b, max_num_obj, h*w)
|
|
|
|
|
align_metric, overlaps = self.get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes, mask_in_gts * mask_gt)
|
|
|
|
|
# get topk_metric mask, (b, max_num_obj, h*w)
|
|
|
|
|
mask_topk = self.select_topk_candidates(align_metric * mask_in_gts,
|
|
|
|
|
topk_mask=mask_gt.repeat([1, 1, self.topk]).bool())
|
|
|
|
|
mask_topk = self.select_topk_candidates(align_metric, topk_mask=mask_gt.repeat([1, 1, self.topk]).bool())
|
|
|
|
|
# merge all mask to a final mask, (b, max_num_obj, h*w)
|
|
|
|
|
mask_pos = mask_topk * mask_in_gts * mask_gt
|
|
|
|
|
|
|
|
|
|
return mask_pos, align_metric, overlaps
|
|
|
|
|
|
|
|
|
|
def get_box_metrics(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes):
|
|
|
|
|
if self.roll_out:
|
|
|
|
|
align_metric = torch.empty((self.bs, self.n_max_boxes, pd_scores.shape[1]), device=pd_scores.device)
|
|
|
|
|
overlaps = torch.empty((self.bs, self.n_max_boxes, pd_scores.shape[1]), device=pd_scores.device)
|
|
|
|
|
ind_0 = torch.empty(self.n_max_boxes, dtype=torch.long)
|
|
|
|
|
for b in range(self.bs):
|
|
|
|
|
ind_0[:], ind_2 = b, gt_labels[b].squeeze(-1).long()
|
|
|
|
|
# get the scores of each grid for each gt cls
|
|
|
|
|
bbox_scores = pd_scores[ind_0, :, ind_2] # b, max_num_obj, h*w
|
|
|
|
|
overlaps[b] = bbox_iou(gt_bboxes[b].unsqueeze(1), pd_bboxes[b].unsqueeze(0), xywh=False,
|
|
|
|
|
CIoU=True).squeeze(2).clamp(0)
|
|
|
|
|
align_metric[b] = bbox_scores.pow(self.alpha) * overlaps[b].pow(self.beta)
|
|
|
|
|
else:
|
|
|
|
|
ind = torch.zeros([2, self.bs, self.n_max_boxes], dtype=torch.long) # 2, b, max_num_obj
|
|
|
|
|
ind[0] = torch.arange(end=self.bs).view(-1, 1).repeat(1, self.n_max_boxes) # b, max_num_obj
|
|
|
|
|
ind[1] = gt_labels.long().squeeze(-1) # b, max_num_obj
|
|
|
|
|
# get the scores of each grid for each gt cls
|
|
|
|
|
bbox_scores = pd_scores[ind[0], :, ind[1]] # b, max_num_obj, h*w
|
|
|
|
|
|
|
|
|
|
overlaps = bbox_iou(gt_bboxes.unsqueeze(2), pd_bboxes.unsqueeze(1), xywh=False,
|
|
|
|
|
CIoU=True).squeeze(3).clamp(0)
|
|
|
|
|
align_metric = bbox_scores.pow(self.alpha) * overlaps.pow(self.beta)
|
|
|
|
|
def get_box_metrics(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, mask_gt):
|
|
|
|
|
na = pd_bboxes.shape[-2]
|
|
|
|
|
mask_gt = mask_gt.bool() # b, max_num_obj, h*w
|
|
|
|
|
overlaps = torch.zeros([self.bs, self.n_max_boxes, na], dtype=pd_bboxes.dtype, device=pd_bboxes.device)
|
|
|
|
|
bbox_scores = torch.zeros([self.bs, self.n_max_boxes, na], dtype=pd_scores.dtype, device=pd_scores.device)
|
|
|
|
|
|
|
|
|
|
ind = torch.zeros([2, self.bs, self.n_max_boxes], dtype=torch.long) # 2, b, max_num_obj
|
|
|
|
|
ind[0] = torch.arange(end=self.bs).view(-1, 1).repeat(1, self.n_max_boxes) # b, max_num_obj
|
|
|
|
|
ind[1] = gt_labels.long().squeeze(-1) # b, max_num_obj
|
|
|
|
|
# get the scores of each grid for each gt cls
|
|
|
|
|
bbox_scores[mask_gt] = pd_scores[ind[0], :, ind[1]][mask_gt] # b, max_num_obj, h*w
|
|
|
|
|
|
|
|
|
|
# (b, max_num_obj, 1, 4), (b, 1, h*w, 4)
|
|
|
|
|
pd_boxes = pd_bboxes.unsqueeze(1).repeat(1, self.n_max_boxes, 1, 1)[mask_gt]
|
|
|
|
|
gt_boxes = gt_bboxes.unsqueeze(2).repeat(1, 1, na, 1)[mask_gt]
|
|
|
|
|
overlaps[mask_gt] = bbox_iou(gt_boxes, pd_boxes, xywh=False, CIoU=True).squeeze(-1).clamp(0)
|
|
|
|
|
|
|
|
|
|
align_metric = bbox_scores.pow(self.alpha) * overlaps.pow(self.beta)
|
|
|
|
|
return align_metric, overlaps
|
|
|
|
|
|
|
|
|
|
def select_topk_candidates(self, metrics, largest=True, topk_mask=None):
|
|
|
|
@ -170,12 +155,10 @@ class TaskAlignedAssigner(nn.Module):
|
|
|
|
|
# (b, max_num_obj, topk)
|
|
|
|
|
topk_idxs[~topk_mask] = 0
|
|
|
|
|
# (b, max_num_obj, topk, h*w) -> (b, max_num_obj, h*w)
|
|
|
|
|
if self.roll_out:
|
|
|
|
|
is_in_topk = torch.empty(metrics.shape, dtype=torch.long, device=metrics.device)
|
|
|
|
|
for b in range(len(topk_idxs)):
|
|
|
|
|
is_in_topk[b] = F.one_hot(topk_idxs[b], num_anchors).sum(-2)
|
|
|
|
|
else:
|
|
|
|
|
is_in_topk = F.one_hot(topk_idxs, num_anchors).sum(-2)
|
|
|
|
|
is_in_topk = torch.zeros(metrics.shape, dtype=torch.long, device=metrics.device)
|
|
|
|
|
for it in range(self.topk):
|
|
|
|
|
is_in_topk += F.one_hot(topk_idxs[:, :, it], num_anchors)
|
|
|
|
|
# is_in_topk = F.one_hot(topk_idxs, num_anchors).sum(-2)
|
|
|
|
|
# filter invalid bboxes
|
|
|
|
|
is_in_topk = torch.where(is_in_topk > 1, 0, is_in_topk)
|
|
|
|
|
return is_in_topk.to(metrics.dtype)
|
|
|
|
|