Generalized IoU from https://giou.stanford.edu/ The boxes should be in [x0, y0, x1, y1] format Returns a [N, M] pairwise matrix, where N = len(boxes1) and M = len(boxes2)
(boxes1, boxes2, data_batch=None)
| 32 | |
| 33 | |
| 34 | def generalized_box_iou(boxes1, boxes2, data_batch=None): |
| 35 | """Generalized IoU from https://giou.stanford.edu/ |
| 36 | |
| 37 | The boxes should be in [x0, y0, x1, y1] format |
| 38 | |
| 39 | Returns a [N, M] pairwise matrix, where N = len(boxes1) |
| 40 | and M = len(boxes2) |
| 41 | """ |
| 42 | if not (boxes1[:, 2:] >= boxes1[:, :2]).all(): |
| 43 | import mmcv |
| 44 | import cv2 |
| 45 | import numpy as np |
| 46 | bs = len(data_batch['img']) |
| 47 | boxes_pred = boxes1.reshape(bs, 100, 4) |
| 48 | for i in range(bs): |
| 49 | import torch.distributed as dist |
| 50 | dist.barrier() |
| 51 | idx = data_batch['idx'] |
| 52 | img = mmcv.imdenormalize( |
| 53 | img=(data_batch['img'][i].cpu().numpy()).transpose(1, 2, 0), |
| 54 | mean=np.array([123.675, 116.28, 103.53]), |
| 55 | std=np.array([58.395, 57.12, 57.375]), |
| 56 | to_bgr=True).astype(np.uint8) |
| 57 | img_wh = data_batch['img_shape'][i] |
| 58 | lhand_bbox = data_batch['lhand_bbox'][i] |
| 59 | lhand_bbox = (lhand_bbox.reshape(-1,2).cpu().numpy()*img_wh.cpu().numpy()[::-1]).reshape(-1, 4) |
| 60 | rhand_bbox = data_batch['rhand_bbox'][i] |
| 61 | rhand_bbox = (rhand_bbox.reshape(-1,2).cpu().numpy()*img_wh.cpu().numpy()[::-1]).reshape(-1, 4) |
| 62 | face_bbox = data_batch['face_bbox'][i] |
| 63 | face_bbox = (face_bbox.reshape(-1,2).cpu().numpy()*img_wh.cpu().numpy()[::-1]).reshape(-1, 4) |
| 64 | body_bbox = data_batch['body_bbox'][i] |
| 65 | body_bbox = (body_bbox.reshape(-1,2).cpu().numpy()*img_wh.cpu().numpy()[::-1]).reshape(-1, 4) |
| 66 | img = mmcv.imshow_bboxes(img, body_bbox, show=False, colors='green') |
| 67 | img = mmcv.imshow_bboxes(img, lhand_bbox, show=False, colors='blue') |
| 68 | img = mmcv.imshow_bboxes(img, rhand_bbox, show=False, colors='yellow') |
| 69 | img = mmcv.imshow_bboxes(img, face_bbox, show=False, colors='red') |
| 70 | cv2.imwrite(f'error_gt_img_{idx[i]}.jpg',img) |
| 71 | |
| 72 | img = mmcv.imdenormalize( |
| 73 | img=(data_batch['img'][i].cpu().numpy()).transpose(1, 2, 0), |
| 74 | mean=np.array([123.675, 116.28, 103.53]), |
| 75 | std=np.array([58.395, 57.12, 57.375]), |
| 76 | to_bgr=True).astype(np.uint8) |
| 77 | boxes_pred_ = (boxes_pred[i].reshape(-1,2).detach().cpu().numpy()*img_wh.cpu().numpy()[::-1]).reshape(-1, 4) |
| 78 | img = mmcv.imshow_bboxes(img.copy(), boxes_pred_, show=False) |
| 79 | cv2.imwrite(f'error_pred_img_{idx[i]}.jpg',img) |
| 80 | |
| 81 | # assert (boxes1[:, 2:] >= boxes1[:, :2]).all() |
| 82 | # assert (boxes2[:, 2:] >= boxes2[:, :2]).all() |
| 83 | iou, union = box_iou(boxes1, boxes2) |
| 84 | |
| 85 | lt = torch.min(boxes1[:, None, :2], boxes2[:, :2]) |
| 86 | rb = torch.max(boxes1[:, None, 2:], boxes2[:, 2:]) |
| 87 | |
| 88 | wh = (rb - lt).clamp(min=0) # [N,M,2] |
| 89 | area = wh[:, :, 0] * wh[:, :, 1] |
| 90 | |
| 91 | return iou - (area - union) / (area + 1e-6) |