MCPcopy Create free account
hub / github.com/FoundationVision/ByteTrack / loss_boxes

Method loss_boxes

tutorials/motr/motr_det.py:107–136  ·  view source on GitHub ↗

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, h, w), normalized by the image size

(self, outputs, gt_instances: List[Instances], indices: List[tuple], num_boxes)

Source from the content-addressed store, hash-verified

105 return loss_map[loss](outputs, gt_instances, indices, num_boxes, **kwargs)
106
107 def loss_boxes(self, outputs, gt_instances: List[Instances], indices: List[tuple], num_boxes):
108 """Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss
109 targets dicts must contain the key "boxes" containing a tensor of dim [nb_target_boxes, 4]
110 The target boxes are expected in format (center_x, center_y, h, w), normalized by the image size.
111 """
112 # We ignore the regression loss of the track-disappear slots.
113 #TODO: Make this filter process more elegant.
114 filtered_idx = []
115 for src_per_img, tgt_per_img in indices:
116 keep = tgt_per_img != -1
117 filtered_idx.append((src_per_img[keep], tgt_per_img[keep]))
118 indices = filtered_idx
119 idx = self._get_src_permutation_idx(indices)
120 src_boxes = outputs['pred_boxes'][idx]
121 target_boxes = torch.cat([gt_per_img.boxes[i] for gt_per_img, (_, i) in zip(gt_instances, indices)], dim=0)
122
123 # for pad target, don't calculate regression loss, judged by whether obj_id=-1
124 target_obj_ids = torch.cat([gt_per_img.obj_ids[i] for gt_per_img, (_, i) in zip(gt_instances, indices)], dim=0) # size(16)
125 mask = (target_obj_ids != -1)
126
127 loss_bbox = F.l1_loss(src_boxes[mask], target_boxes[mask], reduction='none')
128 loss_giou = 1 - torch.diag(box_ops.generalized_box_iou(
129 box_ops.box_cxcywh_to_xyxy(src_boxes[mask]),
130 box_ops.box_cxcywh_to_xyxy(target_boxes[mask])))
131
132 losses = {}
133 losses['loss_bbox'] = loss_bbox.sum() / num_boxes
134 losses['loss_giou'] = loss_giou.sum() / num_boxes
135
136 return losses
137
138 def loss_labels(self, outputs, gt_instances: List[Instances], indices, num_boxes, log=False):
139 """Classification loss (NLL)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected