|
|
@@ -3,14 +3,15 @@ import torch.nn as nn
|
|
|
from utils.box_ops import bbox_iou
|
|
|
|
|
|
|
|
|
-# -------------------------- Task Aligned Assigner --------------------------
|
|
|
+# ------------------ Task Aligned Assigner ------------------
|
|
|
class TaskAlignedAssigner(nn.Module):
|
|
|
def __init__(self,
|
|
|
num_classes = 80,
|
|
|
topk_candidates = 10,
|
|
|
alpha = 0.5,
|
|
|
beta = 6.0,
|
|
|
- eps = 1e-9):
|
|
|
+ eps = 1e-9,
|
|
|
+ ):
|
|
|
super(TaskAlignedAssigner, self).__init__()
|
|
|
self.topk_candidates = topk_candidates
|
|
|
self.num_classes = num_classes
|
|
|
@@ -32,7 +33,7 @@ class TaskAlignedAssigner(nn.Module):
|
|
|
mask_pos, align_metric, overlaps = self.get_pos_mask(
|
|
|
pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points)
|
|
|
|
|
|
- target_gt_idx, fg_mask, mask_pos = select_highest_overlaps(
|
|
|
+ target_gt_idx, fg_mask, mask_pos = self.select_highest_overlaps(
|
|
|
mask_pos, overlaps, self.n_max_boxes)
|
|
|
|
|
|
# Assigned target
|
|
|
@@ -48,9 +49,55 @@ class TaskAlignedAssigner(nn.Module):
|
|
|
|
|
|
return target_labels, target_bboxes, target_scores, fg_mask.bool(), target_gt_idx
|
|
|
|
|
|
+ def select_highest_overlaps(self, mask_pos, overlaps, n_max_boxes):
|
|
|
+ """if an anchor box is assigned to multiple gts,
|
|
|
+ the one with the highest iou will be selected.
|
|
|
+ Args:
|
|
|
+ mask_pos (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
+ overlaps (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
+ Return:
|
|
|
+ target_gt_idx (Tensor): shape(bs, num_total_anchors)
|
|
|
+ fg_mask (Tensor): shape(bs, num_total_anchors)
|
|
|
+ mask_pos (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
+ """
|
|
|
+ fg_mask = mask_pos.sum(-2)
|
|
|
+ if fg_mask.max() > 1: # one anchor is assigned to multiple gt_bboxes
|
|
|
+ mask_multi_gts = (fg_mask.unsqueeze(1) > 1).expand(-1, n_max_boxes, -1) # (b, n_max_boxes, h*w)
|
|
|
+ max_overlaps_idx = overlaps.argmax(1) # (b, h*w)
|
|
|
+
|
|
|
+ is_max_overlaps = torch.zeros(mask_pos.shape, dtype=mask_pos.dtype, device=mask_pos.device)
|
|
|
+ is_max_overlaps.scatter_(1, max_overlaps_idx.unsqueeze(1), 1)
|
|
|
+
|
|
|
+ mask_pos = torch.where(mask_multi_gts, is_max_overlaps, mask_pos).float() # (b, n_max_boxes, h*w)
|
|
|
+ fg_mask = mask_pos.sum(-2)
|
|
|
+ # Find each grid serve which gt(index)
|
|
|
+ target_gt_idx = mask_pos.argmax(-2) # (b, h*w)
|
|
|
+
|
|
|
+ return target_gt_idx, fg_mask, mask_pos
|
|
|
+
|
|
|
+ def select_candidates_in_gts(self, xy_centers, gt_bboxes, eps=1e-9):
|
|
|
+ """select the positive anchors's center in gt
|
|
|
+ Args:
|
|
|
+ xy_centers (Tensor): shape(bs*n_max_boxes, num_total_anchors, 4)
|
|
|
+ gt_bboxes (Tensor): shape(bs, n_max_boxes, 4)
|
|
|
+ Return:
|
|
|
+ (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
+ """
|
|
|
+ n_anchors = xy_centers.size(0)
|
|
|
+ bs, n_max_boxes, _ = gt_bboxes.size()
|
|
|
+ _gt_bboxes = gt_bboxes.reshape([-1, 4])
|
|
|
+ xy_centers = xy_centers.unsqueeze(0).repeat(bs * n_max_boxes, 1, 1)
|
|
|
+ gt_bboxes_lt = _gt_bboxes[:, 0:2].unsqueeze(1).repeat(1, n_anchors, 1)
|
|
|
+ gt_bboxes_rb = _gt_bboxes[:, 2:4].unsqueeze(1).repeat(1, n_anchors, 1)
|
|
|
+ b_lt = xy_centers - gt_bboxes_lt
|
|
|
+ b_rb = gt_bboxes_rb - xy_centers
|
|
|
+ bbox_deltas = torch.cat([b_lt, b_rb], dim=-1)
|
|
|
+ bbox_deltas = bbox_deltas.reshape([bs, n_max_boxes, n_anchors, -1])
|
|
|
+ return (bbox_deltas.min(axis=-1)[0] > eps).to(gt_bboxes.dtype)
|
|
|
+
|
|
|
def get_pos_mask(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points):
|
|
|
# get in_gts mask, (b, max_num_obj, h*w)
|
|
|
- mask_in_gts = select_candidates_in_gts(anc_points, gt_bboxes)
|
|
|
+ mask_in_gts = self.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)
|
|
|
# get topk_metric mask, (b, max_num_obj, h*w)
|
|
|
@@ -127,72 +174,3 @@ class TaskAlignedAssigner(nn.Module):
|
|
|
target_scores = torch.where(fg_scores_mask > 0, target_scores, 0)
|
|
|
|
|
|
return target_labels, target_bboxes, target_scores
|
|
|
-
|
|
|
-
|
|
|
-# -------------------------- Basic Functions --------------------------
|
|
|
-def select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-9):
|
|
|
- """select the positive anchors's center in gt
|
|
|
- Args:
|
|
|
- xy_centers (Tensor): shape(bs*n_max_boxes, num_total_anchors, 4)
|
|
|
- gt_bboxes (Tensor): shape(bs, n_max_boxes, 4)
|
|
|
- Return:
|
|
|
- (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
- """
|
|
|
- n_anchors = xy_centers.size(0)
|
|
|
- bs, n_max_boxes, _ = gt_bboxes.size()
|
|
|
- _gt_bboxes = gt_bboxes.reshape([-1, 4])
|
|
|
- xy_centers = xy_centers.unsqueeze(0).repeat(bs * n_max_boxes, 1, 1)
|
|
|
- gt_bboxes_lt = _gt_bboxes[:, 0:2].unsqueeze(1).repeat(1, n_anchors, 1)
|
|
|
- gt_bboxes_rb = _gt_bboxes[:, 2:4].unsqueeze(1).repeat(1, n_anchors, 1)
|
|
|
- b_lt = xy_centers - gt_bboxes_lt
|
|
|
- b_rb = gt_bboxes_rb - xy_centers
|
|
|
- bbox_deltas = torch.cat([b_lt, b_rb], dim=-1)
|
|
|
- bbox_deltas = bbox_deltas.reshape([bs, n_max_boxes, n_anchors, -1])
|
|
|
- return (bbox_deltas.min(axis=-1)[0] > eps).to(gt_bboxes.dtype)
|
|
|
-
|
|
|
-def select_highest_overlaps(mask_pos, overlaps, n_max_boxes):
|
|
|
- """if an anchor box is assigned to multiple gts,
|
|
|
- the one with the highest iou will be selected.
|
|
|
- Args:
|
|
|
- mask_pos (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
- overlaps (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
- Return:
|
|
|
- target_gt_idx (Tensor): shape(bs, num_total_anchors)
|
|
|
- fg_mask (Tensor): shape(bs, num_total_anchors)
|
|
|
- mask_pos (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
- """
|
|
|
- fg_mask = mask_pos.sum(-2)
|
|
|
- if fg_mask.max() > 1: # one anchor is assigned to multiple gt_bboxes
|
|
|
- mask_multi_gts = (fg_mask.unsqueeze(1) > 1).expand(-1, n_max_boxes, -1) # (b, n_max_boxes, h*w)
|
|
|
- max_overlaps_idx = overlaps.argmax(1) # (b, h*w)
|
|
|
-
|
|
|
- is_max_overlaps = torch.zeros(mask_pos.shape, dtype=mask_pos.dtype, device=mask_pos.device)
|
|
|
- is_max_overlaps.scatter_(1, max_overlaps_idx.unsqueeze(1), 1)
|
|
|
-
|
|
|
- mask_pos = torch.where(mask_multi_gts, is_max_overlaps, mask_pos).float() # (b, n_max_boxes, h*w)
|
|
|
- fg_mask = mask_pos.sum(-2)
|
|
|
- # Find each grid serve which gt(index)
|
|
|
- target_gt_idx = mask_pos.argmax(-2) # (b, h*w)
|
|
|
-
|
|
|
- return target_gt_idx, fg_mask, mask_pos
|
|
|
-
|
|
|
-def iou_calculator(box1, box2, eps=1e-9):
|
|
|
- """Calculate iou for batch
|
|
|
- Args:
|
|
|
- box1 (Tensor): shape(bs, n_max_boxes, 1, 4)
|
|
|
- box2 (Tensor): shape(bs, 1, num_total_anchors, 4)
|
|
|
- Return:
|
|
|
- (Tensor): shape(bs, n_max_boxes, num_total_anchors)
|
|
|
- """
|
|
|
- box1 = box1.unsqueeze(2) # [N, M1, 4] -> [N, M1, 1, 4]
|
|
|
- box2 = box2.unsqueeze(1) # [N, M2, 4] -> [N, 1, M2, 4]
|
|
|
- px1y1, px2y2 = box1[:, :, :, 0:2], box1[:, :, :, 2:4]
|
|
|
- gx1y1, gx2y2 = box2[:, :, :, 0:2], box2[:, :, :, 2:4]
|
|
|
- x1y1 = torch.maximum(px1y1, gx1y1)
|
|
|
- x2y2 = torch.minimum(px2y2, gx2y2)
|
|
|
- overlap = (x2y2 - x1y1).clip(0).prod(-1)
|
|
|
- area1 = (px2y2 - px1y1).clip(0).prod(-1)
|
|
|
- area2 = (gx2y2 - gx1y1).clip(0).prod(-1)
|
|
|
- union = area1 + area2 - overlap + eps
|
|
|
-
|
|
|
- return overlap / union
|