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

Method loss_labels

tutorials/motr/motr.py:138–173  ·  view source on GitHub ↗

Classification loss (NLL) targets dicts must contain the key "labels" containing a tensor of dim [nb_target_boxes]

(self, outputs, gt_instances: List[Instances], indices, num_boxes, log=False)

Source from the content-addressed store, hash-verified

136 return losses
137
138 def loss_labels(self, outputs, gt_instances: List[Instances], indices, num_boxes, log=False):
139 """Classification loss (NLL)
140 targets dicts must contain the key "labels" containing a tensor of dim [nb_target_boxes]
141 """
142 src_logits = outputs['pred_logits']
143 idx = self._get_src_permutation_idx(indices)
144 target_classes = torch.full(src_logits.shape[:2], self.num_classes,
145 dtype=torch.int64, device=src_logits.device)
146 # The matched gt for disappear track query is set -1.
147 labels = []
148 for gt_per_img, (_, J) in zip(gt_instances, indices):
149 labels_per_img = torch.ones_like(J)
150 # set labels of track-appear slots to 0.
151 if len(gt_per_img) > 0:
152 labels_per_img[J != -1] = gt_per_img.labels[J[J != -1]]
153 labels.append(labels_per_img)
154 target_classes_o = torch.cat(labels)
155 target_classes[idx] = target_classes_o
156 if self.focal_loss:
157 gt_labels_target = F.one_hot(target_classes, num_classes=self.num_classes + 1)[:, :, :-1] # no loss for the last (background) class
158 gt_labels_target = gt_labels_target.to(src_logits)
159 loss_ce = sigmoid_focal_loss(src_logits.flatten(1),
160 gt_labels_target.flatten(1),
161 alpha=0.25,
162 gamma=2,
163 num_boxes=num_boxes, mean_in_dim1=False)
164 loss_ce = loss_ce.sum()
165 else:
166 loss_ce = F.cross_entropy(src_logits.transpose(1, 2), target_classes, self.empty_weight)
167 losses = {'loss_ce': loss_ce}
168
169 if log:
170 # TODO this should probably be a separate loss, not hacked in this one here
171 losses['class_error'] = 100 - accuracy(src_logits[idx], target_classes_o)[0]
172
173 return losses
174
175 def match_for_single_frame(self, outputs: dict):
176 outputs_without_aux = {k: v for k, v in outputs.items() if k != 'aux_outputs'}

Callers

nothing calls this directly

Calls 1

sigmoid_focal_lossFunction · 0.85

Tested by

no test coverage detected