matcher.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from scipy.optimize import linear_sum_assignment
  5. try:
  6. from .loss_utils import box_cxcywh_to_xyxy, box_xyxy_to_cxcywh, generalized_box_iou
  7. except:
  8. from loss_utils import box_cxcywh_to_xyxy, box_xyxy_to_cxcywh, generalized_box_iou
  9. class HungarianMatcher(nn.Module):
  10. def __init__(self, cost_class, cost_bbox, cost_giou, alpha=0.25, gamma=2.0):
  11. super().__init__()
  12. self.cost_class = cost_class
  13. self.cost_bbox = cost_bbox
  14. self.cost_giou = cost_giou
  15. self.alpha = alpha
  16. self.gamma = gamma
  17. @torch.no_grad()
  18. def forward(self, pred_boxes, pred_logits, gt_boxes, gt_labels):
  19. bs, num_queries = pred_logits.shape[:2]
  20. # [B, Nq, C] -> [BNq, C]
  21. out_prob = pred_logits.flatten(0, 1).sigmoid()
  22. out_bbox = pred_boxes.flatten(0, 1)
  23. # List[B, M, C] -> [BM, C]
  24. tgt_ids = torch.cat(gt_labels).long()
  25. tgt_bbox = torch.cat(gt_boxes).float()
  26. # -------------------- Classification cost --------------------
  27. neg_cost_class = (1 - self.alpha) * (out_prob ** self.gamma) * (-(1 - out_prob + 1e-8).log())
  28. pos_cost_class = self.alpha * ((1 - out_prob) ** self.gamma) * (-(out_prob + 1e-8).log())
  29. cost_class = pos_cost_class[:, tgt_ids] - neg_cost_class[:, tgt_ids]
  30. # -------------------- Regression cost --------------------
  31. ## L1 cost: [Nq, M]
  32. cost_bbox = torch.cdist(out_bbox, tgt_bbox.to(out_bbox.device), p=1)
  33. ## GIoU cost: Nq, M]
  34. cost_giou = -generalized_box_iou(box_cxcywh_to_xyxy(out_bbox),
  35. box_cxcywh_to_xyxy(tgt_bbox).to(out_bbox.device))
  36. # Final cost: [B, Nq, M]
  37. C = self.cost_bbox * cost_bbox + self.cost_class * cost_class + self.cost_giou * cost_giou
  38. C = C.view(bs, num_queries, -1).cpu()
  39. # Label assignment
  40. sizes = [len(t) for t in gt_boxes]
  41. indices = [linear_sum_assignment(c[i]) for i, c in enumerate(C.split(sizes, -1))]
  42. return [(torch.as_tensor(i, dtype=torch.int64), torch.as_tensor(j, dtype=torch.int64)) for i, j in indices]