def loss_boxes(self, outputs, targets, indices, num_boxes): """ Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss targets dicts must contain the key "boxes" containing a tensor of dim [nb_target_boxes, 4] The target boxes are expected in format (center_x, center_y, w, h), normalized by the image size. """ # assert 'pred_boxes' in outputs idx = self._get_src_permutation_idx(indices) src_boxes = outputs["pred_boxes"][idx] target_boxes = torch.cat( [t["boxes"][i] for t, (_, i) in zip(targets, indices)], dim=0) loss_bbox = F.l1_loss(src_boxes, target_boxes, reduction="none") losses = {} losses["loss_bbox"] = loss_bbox.sum() / num_boxes # loss_giou = 1 - torch.diag(generalized_box_iou(box_cxcywh_to_xyxy(src_boxes), # box_cxcywh_to_xyxy(target_boxes))) loss_giou = 1 - torch.diag( generalized_box_iou( box_convert(src_boxes, in_fmt="cxcywh", out_fmt="xyxy"), box_convert(target_boxes, in_fmt="cxcywh", out_fmt="xyxy"))) losses["loss_giou"] = loss_giou.sum() / num_boxes return losses
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) """ 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).softmax(-1) ) # [batch_size * num_queries, num_classes] out_bbox = outputs["pred_boxes"].flatten( 0, 1) # [batch_size * num_queries, 4] # Also concat the target labels and boxes tgt_ids = torch.cat([v["labels"] for v in targets]) tgt_bbox = torch.cat([v["boxes"] for v in targets]) # Compute the classification cost. Contrary to the loss, we don't use the NLL, # but approximate it in 1 - proba[target class]. # The 1 is a constant that doesn't change the matching, it can be ommitted. cost_class = -out_prob[:, tgt_ids] # Compute the L1 cost between boxes cost_bbox = torch.cdist(out_bbox, tgt_bbox, p=1) # Compute the giou cost betwen boxes # cost_giou = -generalized_box_iou(box_cxcywh_to_xyxy(out_bbox), box_cxcywh_to_xyxy(tgt_bbox)) cost_giou = -generalized_box_iou( box_convert(out_bbox, in_fmt="cxcywh", out_fmt="xyxy"), box_convert(tgt_bbox, in_fmt="cxcywh", out_fmt="xyxy")) # 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), torch.as_tensor(j, dtype=torch.int64), ) for i, j in indices]
def _evaluate_giou(target, pred): """ Evaluate generalized intersection over union (gIOU) for target from dataset and output prediction from model. """ if pred["boxes"].shape[0] == 0: # no box detected, 0 IOU return torch.tensor(0.0, device=pred["boxes"].device) return generalized_box_iou(target["boxes"], pred["boxes"]).diag().mean()
def test_gen_iou(self): # Test Generalized IoU boxes1 = torch.tensor([[0, 0, 100, 100], [0, 0, 50, 50], [200, 200, 300, 300]], dtype=torch.float) boxes2 = torch.tensor([[0, 0, 100, 100], [0, 0, 50, 50], [200, 200, 300, 300]], dtype=torch.float) # Expected gIoU matrix for these boxes expected = torch.tensor([[1.0, 0.25, -0.7778], [0.25, 1.0, -0.8611], [-0.7778, -0.8611, 1.0]]) out = ops.generalized_box_iou(boxes1, boxes2) # Check if all elements of tensor are as expected. assert out.size() == torch.Size([3, 3]) tolerance = 1e-4 assert ((out - expected).abs().max() < tolerance).item() is True
def gen_iou_check(box, expected, tolerance=1e-4): out = ops.generalized_box_iou(box, box) assert out.size() == expected.size() assert ((out - expected).abs().max() < tolerance).item()
def gen_iou_check(box, expected, tolerance=1e-4): out = ops.generalized_box_iou(box, box) torch.testing.assert_close(out, expected, rtol=0.0, check_dtype=False, atol=tolerance)
def forward(self, inputs: Tensor, target: Tensor) -> Tensor: return 1.0 - generalized_box_iou(inputs, target).diagonal()