|
| 1 | +import unittest, copy |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | +import intel_pytorch_extension as ipex |
| 5 | +from common_utils import TestCase |
| 6 | +import time, sys |
| 7 | +from intel_pytorch_extension import batch_score_nms, parallel_scale_back_batch |
| 8 | +import torch.nn.functional as F |
| 9 | +import os |
| 10 | + |
| 11 | +def get_rand_seed(): |
| 12 | + return int(time.time() * 1000000000) |
| 13 | + |
| 14 | +# This function is from https://github.com/kuangliu/pytorch-ssd. |
| 15 | +def calc_iou_tensor(box1, box2): |
| 16 | + """ Calculation of IoU based on two boxes tensor, |
| 17 | + Reference to https://github.com/kuangliu/pytorch-ssd |
| 18 | + input: |
| 19 | + box1 (N, 4) |
| 20 | + box2 (M, 4) |
| 21 | + output: |
| 22 | + IoU (N, M) |
| 23 | + """ |
| 24 | + N = box1.size(0) |
| 25 | + M = box2.size(0) |
| 26 | + be1 = box1.unsqueeze(1).expand(-1, M, -1) |
| 27 | + be2 = box2.unsqueeze(0).expand(N, -1, -1) |
| 28 | + # Left Top & Right Bottom |
| 29 | + lt = torch.max(be1[:,:,:2], be2[:,:,:2]) |
| 30 | + #mask1 = (be1[:,:, 0] < be2[:,:, 0]) ^ (be1[:,:, 1] < be2[:,:, 1]) |
| 31 | + #mask1 = ~mask1 |
| 32 | + rb = torch.min(be1[:,:,2:], be2[:,:,2:]) |
| 33 | + #mask2 = (be1[:,:, 2] < be2[:,:, 2]) ^ (be1[:,:, 3] < be2[:,:, 3]) |
| 34 | + #mask2 = ~mask2 |
| 35 | + delta = rb - lt |
| 36 | + delta[delta < 0] = 0 |
| 37 | + intersect = delta[:,:,0]*delta[:,:,1] |
| 38 | + #*mask1.float()*mask2.float() |
| 39 | + delta1 = be1[:,:,2:] - be1[:,:,:2] |
| 40 | + area1 = delta1[:,:,0]*delta1[:,:,1] |
| 41 | + delta2 = be2[:,:,2:] - be2[:,:,:2] |
| 42 | + area2 = delta2[:,:,0]*delta2[:,:,1] |
| 43 | + iou = intersect/(area1 + area2 - intersect) |
| 44 | + return iou |
| 45 | + |
| 46 | +class TestScaleBackBatch(TestCase): |
| 47 | + def scale_back_batch(self, bboxes_in, scores_in, dboxes_xywh, scale_xy, scale_wh): |
| 48 | + """ |
| 49 | + Python implementation of Encoder::scale_back_batch, refer to https://github.com/mlcommons/inference/blob/v0.7/others/cloud/single_stage_detector/pytorch/utils.py |
| 50 | + """ |
| 51 | + bboxes_in[:, :, :2] = scale_xy*bboxes_in[:, :, :2] |
| 52 | + bboxes_in[:, :, 2:] = scale_wh*bboxes_in[:, :, 2:] |
| 53 | + bboxes_in[:, :, :2] = bboxes_in[:, :, :2]*dboxes_xywh[:, :, 2:] + dboxes_xywh[:, :, :2] |
| 54 | + bboxes_in[:, :, 2:] = bboxes_in[:, :, 2:].exp()*dboxes_xywh[:, :, 2:] |
| 55 | + # Transform format to ltrb |
| 56 | + l, t, r, b = bboxes_in[:, :, 0] - 0.5*bboxes_in[:, :, 2],\ |
| 57 | + bboxes_in[:, :, 1] - 0.5*bboxes_in[:, :, 3],\ |
| 58 | + bboxes_in[:, :, 0] + 0.5*bboxes_in[:, :, 2],\ |
| 59 | + bboxes_in[:, :, 1] + 0.5*bboxes_in[:, :, 3] |
| 60 | + bboxes_in[:, :, 0] = l |
| 61 | + bboxes_in[:, :, 1] = t |
| 62 | + bboxes_in[:, :, 2] = r |
| 63 | + bboxes_in[:, :, 3] = b |
| 64 | + return bboxes_in, F.softmax(scores_in, dim=-1) |
| 65 | + |
| 66 | + def test_scale_back_batch_result(self): |
| 67 | + batch_size = 16 |
| 68 | + number_boxes = 1024 |
| 69 | + scale_xy = 0.1 |
| 70 | + scale_wh = 0.2 |
| 71 | + predicted_loc = torch.randn((batch_size, number_boxes, 4)).contiguous().to(torch.float32) |
| 72 | + predicted_score = torch.randn((batch_size, number_boxes, 81)).contiguous().to(torch.float32) |
| 73 | + dboxes_xywh = torch.randn((1, number_boxes, 4)).contiguous().to(torch.float64) |
| 74 | + bbox_res1, score_res1 = self.scale_back_batch(predicted_loc.clone(), predicted_score.clone(), dboxes_xywh.clone(), scale_xy, scale_wh) |
| 75 | + bbox_res2, score_res2 = parallel_scale_back_batch(predicted_loc, predicted_score, dboxes_xywh, scale_xy, scale_wh) |
| 76 | + self.assertTrue(torch.allclose(bbox_res1, bbox_res2, rtol=1e-4, atol=1e-4)) |
| 77 | + self.assertTrue(torch.allclose(score_res1, score_res2, rtol=1e-4, atol=1e-4)) |
| 78 | + |
| 79 | +class TestNMS(TestCase): |
| 80 | + def decode_single(self, bboxes_in, scores_in, criteria, max_output, max_num=200): |
| 81 | + """ |
| 82 | + Python implementation of Encoder::decode_single, refer to https://github.com/mlcommons/inference/blob/v0.7/others/cloud/single_stage_detector/pytorch/utils.py |
| 83 | + """ |
| 84 | + # perform non-maximum suppression |
| 85 | + # Reference to https://github.com/amdegroot/ssd.pytorch |
| 86 | + |
| 87 | + bboxes_out = [] |
| 88 | + scores_out = [] |
| 89 | + labels_out = [] |
| 90 | + for i, score in enumerate(scores_in.split(1, 1)): |
| 91 | + # skip background |
| 92 | + # print(score[score>0.90]) |
| 93 | + if i == 0: continue |
| 94 | + score = score.squeeze(1) |
| 95 | + mask = score > 0.05 |
| 96 | + bboxes, score = bboxes_in[mask, :], score[mask] |
| 97 | + if score.size(0) == 0: continue |
| 98 | + score_sorted, score_idx_sorted = score.sort(dim=0) |
| 99 | + # select max_output indices |
| 100 | + score_idx_sorted = score_idx_sorted[-max_num:] |
| 101 | + candidates = [] |
| 102 | + while score_idx_sorted.numel() > 0: |
| 103 | + idx = score_idx_sorted[-1].item() |
| 104 | + bboxes_sorted = bboxes[score_idx_sorted, :] |
| 105 | + bboxes_idx = bboxes[idx, :].unsqueeze(dim=0) |
| 106 | + iou_sorted = calc_iou_tensor(bboxes_sorted, bboxes_idx).squeeze() |
| 107 | + # we only need iou < criteria |
| 108 | + score_idx_sorted = score_idx_sorted[iou_sorted < criteria] |
| 109 | + candidates.append(idx) |
| 110 | + |
| 111 | + bboxes_out.append(bboxes[candidates, :]) |
| 112 | + scores_out.append(score[candidates]) |
| 113 | + labels_out.extend([i]*len(candidates)) |
| 114 | + bboxes_out, labels_out, scores_out = torch.cat(bboxes_out, dim=0), \ |
| 115 | + torch.tensor(labels_out, dtype=torch.long), \ |
| 116 | + torch.cat(scores_out, dim=0) |
| 117 | + _, max_ids = scores_out.sort(dim=0) |
| 118 | + max_ids = max_ids[-max_output:] |
| 119 | + return bboxes_out[max_ids, :], labels_out[max_ids], scores_out[max_ids] |
| 120 | + |
| 121 | + def test_nms_result(self): |
| 122 | + batch_size = 1 |
| 123 | + number_boxes = 15130 |
| 124 | + scale_xy = 0.1 |
| 125 | + scale_wh = 0.2 |
| 126 | + criteria = 0.50 |
| 127 | + max_output = 200 |
| 128 | + predicted_loc = torch.randn((batch_size, number_boxes, 4)).contiguous().to(torch.float32) |
| 129 | + predicted_score = torch.randn((batch_size, number_boxes, 81)).contiguous().to(torch.float32) |
| 130 | + dboxes_xywh = torch.randn((1, number_boxes, 4)).contiguous().to(torch.float64) |
| 131 | + dboxes_xywh = torch.load(os.path.dirname(__file__) + "/data/nms_dboxes_xywh.pt") |
| 132 | + bboxes, probs = parallel_scale_back_batch(predicted_loc, predicted_score, dboxes_xywh, scale_xy, scale_wh) |
| 133 | + bboxes_clone = bboxes.clone() |
| 134 | + probs_clone = probs.clone() |
| 135 | + |
| 136 | + output = [] |
| 137 | + for bbox, prob in zip(bboxes.split(1, 0), probs.split(1, 0)): |
| 138 | + bbox = bbox.squeeze(0) |
| 139 | + prob = prob.squeeze(0) |
| 140 | + output.append(self.decode_single(bbox, prob, criteria, max_output)) |
| 141 | + output2 = batch_score_nms(bboxes_clone, probs_clone, criteria, max_output) |
| 142 | + |
| 143 | + for i in range(batch_size): |
| 144 | + loc, label, prob = [r for r in output[i]] |
| 145 | + loc2, label2, prob2 = [r for r in output2[i]] |
| 146 | + self.assertTrue(torch.allclose(loc, loc2, rtol=1e-4, atol=1e-4)) |
| 147 | + self.assertEqual(label, label2) |
| 148 | + self.assertTrue(torch.allclose(prob, prob2, rtol=1e-4, atol=1e-4)) |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + test = unittest.main() |
0 commit comments