| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- # ------------------------------------------------------------------------
- # Plain-DETR
- # Copyright (c) 2023 Xi'an Jiaotong University & Microsoft Research Asia.
- # Licensed under The MIT License [see LICENSE for details]
- # ------------------------------------------------------------------------
- # Deformable DETR
- # Copyright (c) 2020 SenseTime. All Rights Reserved.
- # Licensed under the Apache License, Version 2.0 [see LICENSE for details]
- # ------------------------------------------------------------------------
- # Modified from DETR (https://github.com/facebookresearch/detr)
- # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
- # ------------------------------------------------------------------------
- """
- Modules to compute the matching cost and solve the corresponding LSAP.
- """
- import torch
- from scipy.optimize import linear_sum_assignment
- from torch import nn
- from utils.box_ops import box_cxcywh_to_xyxy, generalized_box_iou, bbox2delta
- class HungarianMatcher(nn.Module):
- def __init__(self,
- cost_class: float = 1,
- cost_bbox: float = 1,
- cost_giou: float = 1,
- ):
- super().__init__()
- self.cost_class = cost_class
- self.cost_bbox = cost_bbox
- self.cost_giou = cost_giou
- assert (
- cost_class != 0 or cost_bbox != 0 or cost_giou != 0
- ), "all costs cant be 0"
- def forward(self, outputs, targets):
- """ Performs the matching
- Params:
- outputs: This is a dict that contains at least these entries:
- "pred_logits": Tensor of dim [batch_size, num_queries, num_classes] with the classification logits
- "pred_boxes": Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates
- targets: This is a list of targets (len(targets) = batch_size), where each target is a dict containing:
- "labels": Tensor of dim [num_target_boxes] (where num_target_boxes is the number of ground-truth
- objects in the target) containing the class labels
- "boxes": Tensor of dim [num_target_boxes, 4] containing the target box coordinates
- Returns:
- A list of size batch_size, containing tuples of (index_i, index_j) where:
- - index_i is the indices of the selected predictions (in order)
- - index_j is the indices of the corresponding selected targets (in order)
- For each batch element, it holds:
- len(index_i) = len(index_j) = min(num_queries, num_target_boxes)
- """
- with torch.no_grad():
- bs, num_queries = outputs["pred_logits"].shape[:2]
- # We flatten to compute the cost matrices in a batch
- out_prob = outputs["pred_logits"].flatten(0, 1).sigmoid()
- out_bbox = outputs["pred_boxes"].flatten(0, 1)
- # Also concat the target labels and boxes
- tgt_ids = torch.cat([v["labels"] for v in targets]).to(out_prob.device)
- tgt_bbox = torch.cat([v["boxes"] for v in targets]).to(out_prob.device)
- # Compute the classification cost.
- alpha = 0.25
- gamma = 2.0
- neg_cost_class = (1 - alpha) * (out_prob ** gamma) * (-(1 - out_prob + 1e-8).log())
- pos_cost_class = alpha * ((1 - out_prob) ** gamma) * (-(out_prob + 1e-8).log())
- cost_class = pos_cost_class[:, tgt_ids] - neg_cost_class[:, tgt_ids]
- # Compute the L1 cost between boxes
- out_delta = outputs["pred_deltas"].flatten(0, 1)
- out_bbox_old = outputs["pred_boxes_old"].flatten(0, 1)
- tgt_delta = bbox2delta(out_bbox_old, tgt_bbox)
- cost_bbox = torch.cdist(out_delta[:, None], tgt_delta, p=1).squeeze(1)
- # Compute the giou cost betwen boxes
- cost_giou = -generalized_box_iou(box_cxcywh_to_xyxy(out_bbox),
- box_cxcywh_to_xyxy(tgt_bbox)
- )
- # Final cost matrix
- C = self.cost_bbox * cost_bbox + \
- self.cost_class * cost_class + \
- self.cost_giou * cost_giou
- C = C.view(bs, num_queries, -1).cpu()
- sizes = [len(v["boxes"]) for v in targets]
- indices = [linear_sum_assignment(c[i]) for i, c in enumerate(C.split(sizes, -1))]
-
- return [(torch.as_tensor(i, dtype=torch.int64), # batch index
- torch.as_tensor(j, dtype=torch.int64)) # query index
- for i, j in indices]
-
|