(self, outputs, targets)
| 191 | |
| 192 | @torch.no_grad() |
| 193 | def forward(self, outputs, targets): |
| 194 | bs, num_queries = outputs['pred_logits'].shape[:2] |
| 195 | out_prob = outputs['pred_logits'].flatten(0, 1).sigmoid() |
| 196 | out_bbox = outputs['pred_boxes'].flatten(0, 1) |
| 197 | |
| 198 | |
| 199 | # Also concat the target labels and boxes |
| 200 | tgt_ids = torch.cat([v['labels'] for v in targets]) |
| 201 | tgt_bbox = torch.cat([v['boxes'] for v in targets]) |
| 202 | |
| 203 | # Compute the classification cost. |
| 204 | alpha = self.focal_alpha |
| 205 | gamma = 2.0 |
| 206 | neg_cost_class = (1 - alpha) * (out_prob** |
| 207 | gamma) * (-(1 - out_prob + 1e-8).log()) |
| 208 | pos_cost_class = alpha * ( |
| 209 | (1 - out_prob)**gamma) * (-(out_prob + 1e-8).log()) |
| 210 | cost_class = pos_cost_class[:, tgt_ids] - neg_cost_class[:, tgt_ids] |
| 211 | |
| 212 | # Compute the L1 cost between boxes |
| 213 | cost_bbox = torch.cdist(out_bbox, tgt_bbox, p=1) |
| 214 | |
| 215 | # Compute the giou cost betwen boxes |
| 216 | cost_giou = -generalized_box_iou(box_cxcywh_to_xyxy(out_bbox), |
| 217 | box_cxcywh_to_xyxy(tgt_bbox)) |
| 218 | |
| 219 | |
| 220 | cost_oks = torch.zeros_like(cost_bbox) |
| 221 | cost_keypoints = torch.zeros_like(cost_bbox) |
| 222 | C = self.cost_bbox * cost_bbox + self.cost_class * cost_class + self.cost_giou * cost_giou |
| 223 | C = C.view(bs, num_queries, -1).cpu() |
| 224 | |
| 225 | sizes = [len(v['boxes']) for v in targets] |
| 226 | indices = [ |
| 227 | linear_sum_assignment(c[i]) |
| 228 | for i, c in enumerate(C.split(sizes, -1)) |
| 229 | ] |
| 230 | |
| 231 | if tgt_ids.shape[0] > 0: |
| 232 | cost_mean_dict = { |
| 233 | 'class': cost_class.mean(), |
| 234 | 'bbox': cost_bbox.mean(), |
| 235 | 'giou': cost_giou.mean(), |
| 236 | } |
| 237 | else: |
| 238 | cost_mean_dict = { |
| 239 | 'class': torch.zeros_like(cost_class.mean()), |
| 240 | 'bbox': torch.zeros_like(cost_bbox.mean()), |
| 241 | 'giou': torch.zeros_like(cost_giou.mean()), |
| 242 | } |
| 243 | |
| 244 | return [(torch.as_tensor(i, dtype=torch.int64), |
| 245 | torch.as_tensor(j, dtype=torch.int64)) |
| 246 | for i, j in indices], cost_mean_dict |
nothing calls this directly
no test coverage detected