Returns a list of APs for this image, with each element being for a class
(ap_data, dets, img, gt, gt_masks, h, w, num_crowd, image_id, detections:Detections=None)
| 384 | return ret.cpu() |
| 385 | |
| 386 | def prep_metrics(ap_data, dets, img, gt, gt_masks, h, w, num_crowd, image_id, detections:Detections=None): |
| 387 | """ Returns a list of APs for this image, with each element being for a class """ |
| 388 | if not args.output_coco_json: |
| 389 | with timer.env('Prepare gt'): |
| 390 | gt_boxes = torch.Tensor(gt[:, :4]) |
| 391 | gt_boxes[:, [0, 2]] *= w |
| 392 | gt_boxes[:, [1, 3]] *= h |
| 393 | gt_classes = list(gt[:, 4].astype(int)) |
| 394 | gt_masks = torch.Tensor(gt_masks).view(-1, h*w) |
| 395 | |
| 396 | if num_crowd > 0: |
| 397 | split = lambda x: (x[-num_crowd:], x[:-num_crowd]) |
| 398 | crowd_boxes , gt_boxes = split(gt_boxes) |
| 399 | crowd_masks , gt_masks = split(gt_masks) |
| 400 | crowd_classes, gt_classes = split(gt_classes) |
| 401 | |
| 402 | with timer.env('Postprocess'): |
| 403 | classes, scores, boxes, masks = postprocess(dets, w, h, crop_masks=args.crop, score_threshold=args.score_threshold) |
| 404 | |
| 405 | if classes.size(0) == 0: |
| 406 | return |
| 407 | |
| 408 | classes = list(classes.cpu().numpy().astype(int)) |
| 409 | if isinstance(scores, list): |
| 410 | box_scores = list(scores[0].cpu().numpy().astype(float)) |
| 411 | mask_scores = list(scores[1].cpu().numpy().astype(float)) |
| 412 | else: |
| 413 | scores = list(scores.cpu().numpy().astype(float)) |
| 414 | box_scores = scores |
| 415 | mask_scores = scores |
| 416 | masks = masks.view(-1, h*w).cuda() |
| 417 | boxes = boxes.cuda() |
| 418 | |
| 419 | |
| 420 | if args.output_coco_json: |
| 421 | with timer.env('JSON Output'): |
| 422 | boxes = boxes.cpu().numpy() |
| 423 | masks = masks.view(-1, h, w).cpu().numpy() |
| 424 | for i in range(masks.shape[0]): |
| 425 | # Make sure that the bounding box actually makes sense and a mask was produced |
| 426 | if (boxes[i, 3] - boxes[i, 1]) * (boxes[i, 2] - boxes[i, 0]) > 0: |
| 427 | detections.add_bbox(image_id, classes[i], boxes[i,:], box_scores[i]) |
| 428 | detections.add_mask(image_id, classes[i], masks[i,:,:], mask_scores[i]) |
| 429 | return |
| 430 | |
| 431 | with timer.env('Eval Setup'): |
| 432 | num_pred = len(classes) |
| 433 | num_gt = len(gt_classes) |
| 434 | |
| 435 | mask_iou_cache = _mask_iou(masks, gt_masks) |
| 436 | bbox_iou_cache = _bbox_iou(boxes.float(), gt_boxes.float()) |
| 437 | |
| 438 | if num_crowd > 0: |
| 439 | crowd_mask_iou_cache = _mask_iou(masks, crowd_masks, iscrowd=True) |
| 440 | crowd_bbox_iou_cache = _bbox_iou(boxes.float(), crowd_boxes.float(), iscrowd=True) |
| 441 | else: |
| 442 | crowd_mask_iou_cache = None |
| 443 | crowd_bbox_iou_cache = None |
no test coverage detected