| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import torch
- import torch.nn as nn
- import torch.nn.functional as F
- class TaskAlignedAssigner(nn.Module):
- def __init__(self,
- topk=10,
- num_classes=80,
- alpha=0.5,
- beta=6.0,
- eps=1e-9):
- super(TaskAlignedAssigner, self).__init__()
- self.topk = topk
- self.num_classes = num_classes
- self.bg_idx = num_classes
- self.alpha = alpha
- self.beta = beta
- self.eps = eps
- @torch.no_grad()
- def forward(self,
- pd_scores,
- pd_bboxes,
- anc_points,
- gt_labels,
- gt_bboxes):
- """This code referenced to
- https://github.com/Nioolek/PPYOLOE_pytorch/blob/master/ppyoloe/assigner/tal_assigner.py
- Args:
- pd_scores (Tensor): shape(bs, num_total_anchors, num_classes)
- pd_bboxes (Tensor): shape(bs, num_total_anchors, 4)
- anc_points (Tensor): shape(num_total_anchors, 2)
- gt_labels (Tensor): shape(bs, n_max_boxes, 1)
- gt_bboxes (Tensor): shape(bs, n_max_boxes, 4)
- Returns:
- target_labels (Tensor): shape(bs, num_total_anchors)
- target_bboxes (Tensor): shape(bs, num_total_anchors, 4)
- target_scores (Tensor): shape(bs, num_total_anchors, num_classes)
- fg_mask (Tensor): shape(bs, num_total_anchors)
- """
- self.bs = pd_scores.size(0)
- self.n_max_boxes = gt_bboxes.size(1)
- 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(
- mask_pos, overlaps, self.n_max_boxes)
- # assigned target
- target_labels, target_bboxes, target_scores = self.get_targets(
- gt_labels, gt_bboxes, target_gt_idx, fg_mask)
- # normalize
- align_metric *= mask_pos
- pos_align_metrics = align_metric.max(axis=-1, keepdim=True)[0]
- pos_overlaps = (overlaps * mask_pos).max(axis=-1, keepdim=True)[0]
- norm_align_metric = (align_metric * pos_overlaps / (pos_align_metrics + self.eps)).max(-2)[0].unsqueeze(-1)
- target_scores = target_scores * norm_align_metric
- return target_labels, target_bboxes, target_scores, fg_mask.bool()
- def get_pos_mask(self,
- pd_scores,
- pd_bboxes,
- gt_labels,
- gt_bboxes,
- anc_points):
- # get anchor_align metric
- align_metric, overlaps = self.get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes)
- # get in_gts mask
- mask_in_gts = select_candidates_in_gts(anc_points, gt_bboxes)
- # get topk_metric mask
- mask_topk = self.select_topk_candidates(align_metric * mask_in_gts)
- # merge all mask to a final mask
- mask_pos = mask_topk * mask_in_gts
- return mask_pos, align_metric, overlaps
- def get_box_metrics(self,
- pd_scores,
- pd_bboxes,
- gt_labels,
- gt_bboxes):
- pd_scores = pd_scores.permute(0, 2, 1)
- gt_labels = gt_labels.long()
- ind = torch.zeros([2, self.bs, self.n_max_boxes], dtype=torch.long)
- ind[0] = torch.arange(end=self.bs).view(-1, 1).repeat(1, self.n_max_boxes)
- ind[1] = gt_labels.squeeze(-1)
- bbox_scores = pd_scores[ind[0], ind[1]]
- overlaps = iou_calculator(gt_bboxes, pd_bboxes)
- align_metric = bbox_scores.pow(self.alpha) * overlaps.pow(self.beta)
- return align_metric, overlaps
- def select_topk_candidates(self, metrics, largest=True):
- num_anchors = metrics.shape[-1]
- topk_metrics, topk_idxs = torch.topk(
- metrics, self.topk, axis=-1, largest=largest)
- topk_mask = (topk_metrics.max(axis=-1, keepdim=True)[0] > self.eps).tile(
- [1, 1, self.topk])
- topk_idxs = torch.where(topk_mask, topk_idxs, torch.zeros_like(topk_idxs))
- is_in_topk = F.one_hot(topk_idxs, num_anchors).sum(axis=-2)
- is_in_topk = torch.where(is_in_topk > 1,
- torch.zeros_like(is_in_topk), is_in_topk)
- return is_in_topk.to(metrics.dtype)
- def get_targets(self,
- gt_labels,
- gt_bboxes,
- target_gt_idx,
- fg_mask):
- # assigned target labels
- batch_ind = torch.arange(end=self.bs, dtype=torch.int64, device=gt_labels.device)[...,None]
- target_gt_idx = target_gt_idx + batch_ind * self.n_max_boxes
- target_labels = gt_labels.long().flatten()[target_gt_idx]
- # assigned target boxes
- target_bboxes = gt_bboxes.reshape([-1, 4])[target_gt_idx]
- # assigned target scores
- target_labels[target_labels<0] = 0
- target_scores = F.one_hot(target_labels, self.num_classes)
- fg_scores_mask = fg_mask[:, :, None].repeat(1, 1, self.num_classes)
- target_scores = torch.where(fg_scores_mask > 0, target_scores,
- torch.full_like(target_scores, 0))
- return target_labels, target_bboxes, target_scores
-
- 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(axis=-2)
- if fg_mask.max() > 1:
- mask_multi_gts = (fg_mask.unsqueeze(1) > 1).repeat([1, n_max_boxes, 1])
- max_overlaps_idx = overlaps.argmax(axis=1)
- is_max_overlaps = F.one_hot(max_overlaps_idx, n_max_boxes)
- is_max_overlaps = is_max_overlaps.permute(0, 2, 1).to(overlaps.dtype)
- mask_pos = torch.where(mask_multi_gts, is_max_overlaps, mask_pos)
- fg_mask = mask_pos.sum(axis=-2)
- target_gt_idx = mask_pos.argmax(axis=-2)
- 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
|