This module converts the model's output into the format expected by the coco api.
| 294 | |
| 295 | |
| 296 | class PostProcess_aios(nn.Module): |
| 297 | """This module converts the model's output into the format expected by the |
| 298 | coco api.""" |
| 299 | def __init__(self, |
| 300 | num_select=100, |
| 301 | nms_iou_threshold=-1, |
| 302 | num_body_points=17) -> None: |
| 303 | super().__init__() |
| 304 | self.num_select = num_select |
| 305 | self.nms_iou_threshold = nms_iou_threshold |
| 306 | self.num_body_points = num_body_points |
| 307 | |
| 308 | @torch.no_grad() |
| 309 | def forward(self, outputs, target_sizes, not_to_xyxy=False, test=False): |
| 310 | num_select = self.num_select |
| 311 | out_logits, out_bbox, out_keypoints = outputs['pred_logits'], outputs[ |
| 312 | 'pred_boxes'], outputs['pred_keypoints'] |
| 313 | assert len(out_logits) == len(target_sizes) |
| 314 | assert target_sizes.shape[1] == 2 |
| 315 | prob = out_logits.sigmoid() |
| 316 | topk_values, topk_indexes = torch.topk(prob.view( |
| 317 | out_logits.shape[0], -1), |
| 318 | num_select, |
| 319 | dim=1) |
| 320 | scores = topk_values |
| 321 | |
| 322 | # bbox |
| 323 | topk_boxes = topk_indexes // out_logits.shape[2] |
| 324 | labels = topk_indexes % out_logits.shape[2] |
| 325 | if not_to_xyxy: |
| 326 | boxes = out_bbox |
| 327 | else: |
| 328 | boxes = box_ops.box_cxcywh_to_xyxy(out_bbox) |
| 329 | |
| 330 | if test: |
| 331 | assert not not_to_xyxy |
| 332 | boxes[:, :, 2:] = boxes[:, :, 2:] - boxes[:, :, :2] |
| 333 | boxes = torch.gather(boxes, 1, |
| 334 | topk_boxes.unsqueeze(-1).repeat(1, 1, 4)) |
| 335 | |
| 336 | # from relative [0, 1] to absolute [0, height] coordinates |
| 337 | img_h, img_w = target_sizes.unbind(1) |
| 338 | scale_fct = torch.stack([img_w, img_h, img_w, img_h], dim=1) |
| 339 | boxes = boxes * scale_fct[:, None, :] |
| 340 | |
| 341 | # keypoints |
| 342 | topk_keypoints = topk_indexes // out_logits.shape[2] |
| 343 | labels = topk_indexes % out_logits.shape[2] |
| 344 | keypoints = torch.gather( |
| 345 | out_keypoints, 1, |
| 346 | topk_keypoints.unsqueeze(-1).repeat(1, 1, |
| 347 | self.num_body_points * 3)) |
| 348 | |
| 349 | Z_pred = keypoints[:, :, :(self.num_body_points * 2)] |
| 350 | V_pred = keypoints[:, :, (self.num_body_points * 2):] |
| 351 | img_h, img_w = target_sizes.unbind(1) |
| 352 | Z_pred = Z_pred * torch.stack([img_w, img_h], dim=1).repeat( |
| 353 | 1, self.num_body_points)[:, None, :] |
no outgoing calls
no test coverage detected