tal.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import torch
  3. import torch.nn as nn
  4. from .checks import check_version
  5. from .metrics import bbox_iou, probiou, wasserstein_loss
  6. from .ops import xywhr2xyxyxyxy
  7. TORCH_1_10 = check_version(torch.__version__, "1.10.0")
  8. class TaskAlignedAssigner(nn.Module):
  9. """
  10. A task-aligned assigner for object detection.
  11. This class assigns ground-truth (gt) objects to anchors based on the task-aligned metric, which combines both
  12. classification and localization information.
  13. Attributes:
  14. topk (int): The number of top candidates to consider.
  15. num_classes (int): The number of object classes.
  16. alpha (float): The alpha parameter for the classification component of the task-aligned metric.
  17. beta (float): The beta parameter for the localization component of the task-aligned metric.
  18. eps (float): A small value to prevent division by zero.
  19. """
  20. def __init__(self, topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-9):
  21. """Initialize a TaskAlignedAssigner object with customizable hyperparameters."""
  22. super().__init__()
  23. self.topk = topk
  24. self.num_classes = num_classes
  25. self.bg_idx = num_classes
  26. self.alpha = alpha
  27. self.beta = beta
  28. self.eps = eps
  29. @torch.no_grad()
  30. def forward(self, pd_scores, pd_bboxes, anc_points, gt_labels, gt_bboxes, mask_gt):
  31. """
  32. Compute the task-aligned assignment. Reference code is available at
  33. https://github.com/Nioolek/PPYOLOE_pytorch/blob/master/ppyoloe/assigner/tal_assigner.py.
  34. Args:
  35. pd_scores (Tensor): shape(bs, num_total_anchors, num_classes)
  36. pd_bboxes (Tensor): shape(bs, num_total_anchors, 4)
  37. anc_points (Tensor): shape(num_total_anchors, 2)
  38. gt_labels (Tensor): shape(bs, n_max_boxes, 1)
  39. gt_bboxes (Tensor): shape(bs, n_max_boxes, 4)
  40. mask_gt (Tensor): shape(bs, n_max_boxes, 1)
  41. Returns:
  42. target_labels (Tensor): shape(bs, num_total_anchors)
  43. target_bboxes (Tensor): shape(bs, num_total_anchors, 4)
  44. target_scores (Tensor): shape(bs, num_total_anchors, num_classes)
  45. fg_mask (Tensor): shape(bs, num_total_anchors)
  46. target_gt_idx (Tensor): shape(bs, num_total_anchors)
  47. """
  48. self.bs = pd_scores.shape[0]
  49. self.n_max_boxes = gt_bboxes.shape[1]
  50. if self.n_max_boxes == 0:
  51. device = gt_bboxes.device
  52. return (
  53. torch.full_like(pd_scores[..., 0], self.bg_idx).to(device),
  54. torch.zeros_like(pd_bboxes).to(device),
  55. torch.zeros_like(pd_scores).to(device),
  56. torch.zeros_like(pd_scores[..., 0]).to(device),
  57. torch.zeros_like(pd_scores[..., 0]).to(device),
  58. )
  59. mask_pos, align_metric, overlaps = self.get_pos_mask(
  60. pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt
  61. )
  62. target_gt_idx, fg_mask, mask_pos = self.select_highest_overlaps(mask_pos, overlaps, self.n_max_boxes)
  63. # Assigned target
  64. target_labels, target_bboxes, target_scores = self.get_targets(gt_labels, gt_bboxes, target_gt_idx, fg_mask)
  65. # Normalize
  66. align_metric *= mask_pos
  67. pos_align_metrics = align_metric.amax(dim=-1, keepdim=True) # b, max_num_obj
  68. pos_overlaps = (overlaps * mask_pos).amax(dim=-1, keepdim=True) # b, max_num_obj
  69. norm_align_metric = (align_metric * pos_overlaps / (pos_align_metrics + self.eps)).amax(-2).unsqueeze(-1)
  70. target_scores = target_scores * norm_align_metric
  71. return target_labels, target_bboxes, target_scores, fg_mask.bool(), target_gt_idx
  72. def get_pos_mask(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt):
  73. """Get in_gts mask, (b, max_num_obj, h*w)."""
  74. mask_in_gts = self.select_candidates_in_gts(anc_points, gt_bboxes)
  75. # Get anchor_align metric, (b, max_num_obj, h*w)
  76. align_metric, overlaps = self.get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes, mask_in_gts * mask_gt)
  77. # Get topk_metric mask, (b, max_num_obj, h*w)
  78. mask_topk = self.select_topk_candidates(align_metric, topk_mask=mask_gt.expand(-1, -1, self.topk).bool())
  79. # Merge all mask to a final mask, (b, max_num_obj, h*w)
  80. mask_pos = mask_topk * mask_in_gts * mask_gt
  81. return mask_pos, align_metric, overlaps
  82. def get_box_metrics(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, mask_gt):
  83. """Compute alignment metric given predicted and ground truth bounding boxes."""
  84. na = pd_bboxes.shape[-2]
  85. mask_gt = mask_gt.bool() # b, max_num_obj, h*w
  86. overlaps = torch.zeros([self.bs, self.n_max_boxes, na], dtype=pd_bboxes.dtype, device=pd_bboxes.device)
  87. bbox_scores = torch.zeros([self.bs, self.n_max_boxes, na], dtype=pd_scores.dtype, device=pd_scores.device)
  88. ind = torch.zeros([2, self.bs, self.n_max_boxes], dtype=torch.long) # 2, b, max_num_obj
  89. ind[0] = torch.arange(end=self.bs).view(-1, 1).expand(-1, self.n_max_boxes) # b, max_num_obj
  90. ind[1] = gt_labels.squeeze(-1) # b, max_num_obj
  91. # Get the scores of each grid for each gt cls
  92. bbox_scores[mask_gt] = pd_scores[ind[0], :, ind[1]][mask_gt] # b, max_num_obj, h*w
  93. # (b, max_num_obj, 1, 4), (b, 1, h*w, 4)
  94. pd_boxes = pd_bboxes.unsqueeze(1).expand(-1, self.n_max_boxes, -1, -1)[mask_gt]
  95. gt_boxes = gt_bboxes.unsqueeze(2).expand(-1, -1, na, -1)[mask_gt]
  96. overlaps[mask_gt] = self.iou_calculation(gt_boxes, pd_boxes)
  97. align_metric = bbox_scores.pow(self.alpha) * overlaps.pow(self.beta)
  98. return align_metric, overlaps
  99. def iou_calculation(self, gt_bboxes, pd_bboxes):
  100. """Iou calculation for horizontal bounding boxes."""
  101. return bbox_iou(gt_bboxes, pd_bboxes, xywh=False, CIoU=True).squeeze(-1).clamp_(0)
  102. # return wasserstein_loss(gt_bboxes, pd_bboxes).squeeze(-1).clamp_(0)
  103. def select_topk_candidates(self, metrics, largest=True, topk_mask=None):
  104. """
  105. Select the top-k candidates based on the given metrics.
  106. Args:
  107. metrics (Tensor): A tensor of shape (b, max_num_obj, h*w), where b is the batch size,
  108. max_num_obj is the maximum number of objects, and h*w represents the
  109. total number of anchor points.
  110. largest (bool): If True, select the largest values; otherwise, select the smallest values.
  111. topk_mask (Tensor): An optional boolean tensor of shape (b, max_num_obj, topk), where
  112. topk is the number of top candidates to consider. If not provided,
  113. the top-k values are automatically computed based on the given metrics.
  114. Returns:
  115. (Tensor): A tensor of shape (b, max_num_obj, h*w) containing the selected top-k candidates.
  116. """
  117. # (b, max_num_obj, topk)
  118. topk_metrics, topk_idxs = torch.topk(metrics, self.topk, dim=-1, largest=largest)
  119. if topk_mask is None:
  120. topk_mask = (topk_metrics.max(-1, keepdim=True)[0] > self.eps).expand_as(topk_idxs)
  121. # (b, max_num_obj, topk)
  122. topk_idxs.masked_fill_(~topk_mask, 0)
  123. # (b, max_num_obj, topk, h*w) -> (b, max_num_obj, h*w)
  124. count_tensor = torch.zeros(metrics.shape, dtype=torch.int8, device=topk_idxs.device)
  125. ones = torch.ones_like(topk_idxs[:, :, :1], dtype=torch.int8, device=topk_idxs.device)
  126. for k in range(self.topk):
  127. # Expand topk_idxs for each value of k and add 1 at the specified positions
  128. count_tensor.scatter_add_(-1, topk_idxs[:, :, k : k + 1], ones)
  129. # count_tensor.scatter_add_(-1, topk_idxs, torch.ones_like(topk_idxs, dtype=torch.int8, device=topk_idxs.device))
  130. # Filter invalid bboxes
  131. count_tensor.masked_fill_(count_tensor > 1, 0)
  132. return count_tensor.to(metrics.dtype)
  133. def get_targets(self, gt_labels, gt_bboxes, target_gt_idx, fg_mask):
  134. """
  135. Compute target labels, target bounding boxes, and target scores for the positive anchor points.
  136. Args:
  137. gt_labels (Tensor): Ground truth labels of shape (b, max_num_obj, 1), where b is the
  138. batch size and max_num_obj is the maximum number of objects.
  139. gt_bboxes (Tensor): Ground truth bounding boxes of shape (b, max_num_obj, 4).
  140. target_gt_idx (Tensor): Indices of the assigned ground truth objects for positive
  141. anchor points, with shape (b, h*w), where h*w is the total
  142. number of anchor points.
  143. fg_mask (Tensor): A boolean tensor of shape (b, h*w) indicating the positive
  144. (foreground) anchor points.
  145. Returns:
  146. (Tuple[Tensor, Tensor, Tensor]): A tuple containing the following tensors:
  147. - target_labels (Tensor): Shape (b, h*w), containing the target labels for
  148. positive anchor points.
  149. - target_bboxes (Tensor): Shape (b, h*w, 4), containing the target bounding boxes
  150. for positive anchor points.
  151. - target_scores (Tensor): Shape (b, h*w, num_classes), containing the target scores
  152. for positive anchor points, where num_classes is the number
  153. of object classes.
  154. """
  155. # Assigned target labels, (b, 1)
  156. batch_ind = torch.arange(end=self.bs, dtype=torch.int64, device=gt_labels.device)[..., None]
  157. target_gt_idx = target_gt_idx + batch_ind * self.n_max_boxes # (b, h*w)
  158. target_labels = gt_labels.long().flatten()[target_gt_idx] # (b, h*w)
  159. # Assigned target boxes, (b, max_num_obj, 4) -> (b, h*w, 4)
  160. target_bboxes = gt_bboxes.view(-1, gt_bboxes.shape[-1])[target_gt_idx]
  161. # Assigned target scores
  162. target_labels.clamp_(0)
  163. # 10x faster than F.one_hot()
  164. target_scores = torch.zeros(
  165. (target_labels.shape[0], target_labels.shape[1], self.num_classes),
  166. dtype=torch.int64,
  167. device=target_labels.device,
  168. ) # (b, h*w, 80)
  169. target_scores.scatter_(2, target_labels.unsqueeze(-1), 1)
  170. fg_scores_mask = fg_mask[:, :, None].repeat(1, 1, self.num_classes) # (b, h*w, 80)
  171. target_scores = torch.where(fg_scores_mask > 0, target_scores, 0)
  172. return target_labels, target_bboxes, target_scores
  173. @staticmethod
  174. def select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-9):
  175. """
  176. Select the positive anchor center in gt.
  177. Args:
  178. xy_centers (Tensor): shape(h*w, 2)
  179. gt_bboxes (Tensor): shape(b, n_boxes, 4)
  180. Returns:
  181. (Tensor): shape(b, n_boxes, h*w)
  182. """
  183. n_anchors = xy_centers.shape[0]
  184. bs, n_boxes, _ = gt_bboxes.shape
  185. lt, rb = gt_bboxes.view(-1, 1, 4).chunk(2, 2) # left-top, right-bottom
  186. bbox_deltas = torch.cat((xy_centers[None] - lt, rb - xy_centers[None]), dim=2).view(bs, n_boxes, n_anchors, -1)
  187. # return (bbox_deltas.min(3)[0] > eps).to(gt_bboxes.dtype)
  188. return bbox_deltas.amin(3).gt_(eps)
  189. @staticmethod
  190. def select_highest_overlaps(mask_pos, overlaps, n_max_boxes):
  191. """
  192. If an anchor box is assigned to multiple gts, the one with the highest IoU will be selected.
  193. Args:
  194. mask_pos (Tensor): shape(b, n_max_boxes, h*w)
  195. overlaps (Tensor): shape(b, n_max_boxes, h*w)
  196. Returns:
  197. target_gt_idx (Tensor): shape(b, h*w)
  198. fg_mask (Tensor): shape(b, h*w)
  199. mask_pos (Tensor): shape(b, n_max_boxes, h*w)
  200. """
  201. # (b, n_max_boxes, h*w) -> (b, h*w)
  202. fg_mask = mask_pos.sum(-2)
  203. if fg_mask.max() > 1: # one anchor is assigned to multiple gt_bboxes
  204. mask_multi_gts = (fg_mask.unsqueeze(1) > 1).expand(-1, n_max_boxes, -1) # (b, n_max_boxes, h*w)
  205. max_overlaps_idx = overlaps.argmax(1) # (b, h*w)
  206. is_max_overlaps = torch.zeros(mask_pos.shape, dtype=mask_pos.dtype, device=mask_pos.device)
  207. is_max_overlaps.scatter_(1, max_overlaps_idx.unsqueeze(1), 1)
  208. mask_pos = torch.where(mask_multi_gts, is_max_overlaps, mask_pos).float() # (b, n_max_boxes, h*w)
  209. fg_mask = mask_pos.sum(-2)
  210. # Find each grid serve which gt(index)
  211. target_gt_idx = mask_pos.argmax(-2) # (b, h*w)
  212. return target_gt_idx, fg_mask, mask_pos
  213. class RotatedTaskAlignedAssigner(TaskAlignedAssigner):
  214. def iou_calculation(self, gt_bboxes, pd_bboxes):
  215. """IoU calculation for rotated bounding boxes."""
  216. return probiou(gt_bboxes, pd_bboxes).squeeze(-1).clamp_(0)
  217. @staticmethod
  218. def select_candidates_in_gts(xy_centers, gt_bboxes):
  219. """
  220. Select the positive anchor center in gt for rotated bounding boxes.
  221. Args:
  222. xy_centers (Tensor): shape(h*w, 2)
  223. gt_bboxes (Tensor): shape(b, n_boxes, 5)
  224. Returns:
  225. (Tensor): shape(b, n_boxes, h*w)
  226. """
  227. # (b, n_boxes, 5) --> (b, n_boxes, 4, 2)
  228. corners = xywhr2xyxyxyxy(gt_bboxes)
  229. # (b, n_boxes, 1, 2)
  230. a, b, _, d = corners.split(1, dim=-2)
  231. ab = b - a
  232. ad = d - a
  233. # (b, n_boxes, h*w, 2)
  234. ap = xy_centers - a
  235. norm_ab = (ab * ab).sum(dim=-1)
  236. norm_ad = (ad * ad).sum(dim=-1)
  237. ap_dot_ab = (ap * ab).sum(dim=-1)
  238. ap_dot_ad = (ap * ad).sum(dim=-1)
  239. return (ap_dot_ab >= 0) & (ap_dot_ab <= norm_ab) & (ap_dot_ad >= 0) & (ap_dot_ad <= norm_ad) # is_in_box
  240. def make_anchors(feats, strides, grid_cell_offset=0.5):
  241. """Generate anchors from features."""
  242. anchor_points, stride_tensor = [], []
  243. assert feats is not None
  244. dtype, device = feats[0].dtype, feats[0].device
  245. for i, stride in enumerate(strides):
  246. _, _, h, w = feats[i].shape
  247. sx = torch.arange(end=w, device=device, dtype=dtype) + grid_cell_offset # shift x
  248. sy = torch.arange(end=h, device=device, dtype=dtype) + grid_cell_offset # shift y
  249. sy, sx = torch.meshgrid(sy, sx, indexing="ij") if TORCH_1_10 else torch.meshgrid(sy, sx)
  250. anchor_points.append(torch.stack((sx, sy), -1).view(-1, 2))
  251. stride_tensor.append(torch.full((h * w, 1), stride, dtype=dtype, device=device))
  252. return torch.cat(anchor_points), torch.cat(stride_tensor)
  253. def dist2bbox(distance, anchor_points, xywh=True, dim=-1):
  254. """Transform distance(ltrb) to box(xywh or xyxy)."""
  255. lt, rb = distance.chunk(2, dim)
  256. x1y1 = anchor_points - lt
  257. x2y2 = anchor_points + rb
  258. if xywh:
  259. c_xy = (x1y1 + x2y2) / 2
  260. wh = x2y2 - x1y1
  261. return torch.cat((c_xy, wh), dim) # xywh bbox
  262. return torch.cat((x1y1, x2y2), dim) # xyxy bbox
  263. def bbox2dist(anchor_points, bbox, reg_max):
  264. """Transform bbox(xyxy) to dist(ltrb)."""
  265. x1y1, x2y2 = bbox.chunk(2, -1)
  266. return torch.cat((anchor_points - x1y1, x2y2 - anchor_points), -1).clamp_(0, reg_max - 0.01) # dist (lt, rb)
  267. def dist2rbox(pred_dist, pred_angle, anchor_points, dim=-1):
  268. """
  269. Decode predicted object bounding box coordinates from anchor points and distribution.
  270. Args:
  271. pred_dist (torch.Tensor): Predicted rotated distance, (bs, h*w, 4).
  272. pred_angle (torch.Tensor): Predicted angle, (bs, h*w, 1).
  273. anchor_points (torch.Tensor): Anchor points, (h*w, 2).
  274. Returns:
  275. (torch.Tensor): Predicted rotated bounding boxes, (bs, h*w, 4).
  276. """
  277. lt, rb = pred_dist.split(2, dim=dim)
  278. cos, sin = torch.cos(pred_angle), torch.sin(pred_angle)
  279. # (bs, h*w, 1)
  280. xf, yf = ((rb - lt) / 2).split(1, dim=dim)
  281. x, y = xf * cos - yf * sin, xf * sin + yf * cos
  282. xy = torch.cat([x, y], dim=dim) + anchor_points
  283. return torch.cat([xy, lt + rb], dim=dim)