matcher.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. from .loss_utils import box_cxcywh_to_xyxy, generalized_box_iou
  6. class HungarianMatcher(nn.Module):
  7. def __init__(self, cost_class=2.0, cost_bbox=5.0, cost_giou=2.0, alpha=0.25, gamma=2.0):
  8. super().__init__()
  9. self.cost_class = cost_class
  10. self.cost_bbox = cost_bbox
  11. self.cost_giou = cost_giou
  12. self.alpha = alpha
  13. self.gamma = gamma
  14. assert self.cost_class != 0 or self.cost_bbox != 0 or self.cost_giou != 0, "all costs cant be 0"
  15. @torch.no_grad()
  16. def forward(self, outputs, targets):
  17. bs, num_queries = outputs["pred_logits"].shape[:2]
  18. # We flatten to compute the cost matrices in a batch
  19. out_prob = F.sigmoid(outputs["pred_logits"].flatten(0, 1))
  20. out_bbox = outputs["pred_boxes"].flatten(0, 1) # [batch_size * num_queries, 4]
  21. # Also concat the target labels and boxes
  22. tgt_ids = torch.cat([v["labels"] for v in targets])
  23. tgt_bbox = torch.cat([v["boxes"] for v in targets])
  24. # Compute the classification cost
  25. out_prob = out_prob[:, tgt_ids]
  26. neg_cost_class = (1 - self.alpha) * (out_prob**self.gamma) * (-(1 - out_prob + 1e-8).log())
  27. pos_cost_class = self.alpha * ((1 - out_prob)**self.gamma) * (-(out_prob + 1e-8).log())
  28. cost_class = pos_cost_class - neg_cost_class
  29. # Compute the L1 cost between boxes
  30. cost_bbox = torch.cdist(out_bbox, tgt_bbox, p=1)
  31. # Compute the giou cost betwen boxes
  32. cost_giou = -generalized_box_iou(box_cxcywh_to_xyxy(out_bbox), box_cxcywh_to_xyxy(tgt_bbox))
  33. # Final cost matrix
  34. C = self.cost_bbox * cost_bbox + self.cost_class * cost_class + self.cost_giou * cost_giou
  35. C = C.view(bs, num_queries, -1).cpu()
  36. sizes = [len(v["boxes"]) for v in targets]
  37. indices = [linear_sum_assignment(c[i]) for i, c in enumerate(C.split(sizes, -1))]
  38. return [(torch.as_tensor(i, dtype=torch.int64), torch.as_tensor(j, dtype=torch.int64)) for i, j in indices]