Exemplo n.º 1
0
    def test_compute_intersect_2d(self):
        import time
        from det3.ops import compute_intersect_2d, write_pkl
        from det3.utils.utils import compute_intersec
        for test_dtype in [np.float32, np.float64]:
            # npy
            boxes = np.array([[3, 2, 4, 4.5]])
            boxes = [boxes + np.abs(np.random.randn(4).reshape(1, 4)) for i in range(10)]
            boxes = np.vstack(boxes).astype(test_dtype)
            others = np.array([[6, 3, 2, 5]])
            others = [others + np.abs(np.random.randn(4).reshape(1, 4)) for i in range(21)]
            others = np.vstack(others).astype(test_dtype)
            boxes_gt = [np.array([itm[0], itm[1], 0, itm[2], itm[3], 0, 0]) for itm in boxes]
            boxes_gt = np.vstack(boxes_gt)
            others_gt = [np.array([itm[0], itm[1], 0, itm[2], itm[3], 0, 0]) for itm in others]
            others_gt = np.vstack(others_gt)
            gt = []
            for box_gt in boxes_gt:
                gt.append(compute_intersec(box_gt, others_gt, mode="2d-rot"))
            gt = np.stack(gt, axis=0)
            times = 100

            est = compute_intersect_2d(boxes, others)
            self.assertTrue(est.shape == gt.shape)
            self.assertTrue(np.allclose(est, gt, atol=1e-1))
            self.assertTrue(est.dtype == boxes.dtype)
            t1 = time.time()
            for i in range(times):
                est = compute_intersect_2d(boxes, others)
            t = (time.time()-t1) / times * 1000
            print(f"compute_intersect_2d np: {t:.2f} ms")
            # torch
            boxes_ts = torch.from_numpy(boxes)
            others_ts = torch.from_numpy(others)
            est_ts = compute_intersect_2d(boxes_ts, others_ts)
            self.assertTrue(est_ts.numpy().shape == gt.shape)
            self.assertTrue(np.allclose(est_ts.numpy(), gt, atol=1e-1))
            self.assertTrue(est_ts.dtype == boxes_ts.dtype)
            t1 = time.time()
            for i in range(times):
                est_ts = compute_intersect_2d(boxes_ts, others_ts)
            t = (time.time()-t1) / times * 1000
            print(f"compute_intersect_2d torch: {t:.2f} ms")
            # torch cuda
            boxes_tsgpu = boxes_ts.cuda()
            others_tsgpu = others_ts.cuda()
            est_tsgpu = compute_intersect_2d(boxes_tsgpu, others_tsgpu)
            self.assertTrue(est_tsgpu.cpu().numpy().shape == gt.shape)
            self.assertTrue(np.allclose(est_tsgpu.cpu().numpy(), gt, atol=1e-1))
            self.assertTrue(est_tsgpu.dtype == boxes_tsgpu.dtype)
            torch.cuda.synchronize()
            t1 = time.time()
            for i in range(times):
                est_tsgpu = compute_intersect_2d(boxes_tsgpu, others_tsgpu)
            torch.cuda.synchronize()
            t = (time.time()-t1) / times * 1000
            print(f"compute_intersect_2d torch cuda: {t:.2f} ms")
Exemplo n.º 2
0
 def test_compute_intersect_3drot(self):
     import time
     from det3.ops import compute_intersect_3drot, write_pkl
     from det3.utils.utils import compute_intersec
     for test_dtype in [np.float32, np.float64]:
         # npy
         boxes = np.array([[5, 3.5, 1, 3, 5, 2, 0.05]])
         boxes = [boxes + np.abs(np.random.randn(7).reshape(-1, 7)) for i in range(10)]
         boxes = np.vstack(boxes).astype(test_dtype)
         others = np.array([[6, 3, 1, 2, 5, 2, 0.05]])
         others = [others + np.abs(np.random.randn(7).reshape(-1, 7)) for i in range(21)]
         others = np.vstack(others).astype(test_dtype)
         gt = []
         for box_gt in boxes:
             gt.append(compute_intersec(box_gt, others, mode="3d-rot"))
         gt = np.stack(gt, axis=0)
         times = 100
         est = compute_intersect_3drot(boxes, others)
         self.assertTrue(est.shape == gt.shape)
         self.assertTrue(np.allclose(est, gt, atol=1e-1))
         self.assertTrue(est.dtype == boxes.dtype)
         t1 = time.time()
         for i in range(times):
             est = compute_intersect_3drot(boxes, others)
         t = (time.time()-t1) / times * 1000
         print(f"compute_intersect_3drot np: {t:.2f} ms")
         # torch
         boxes_ts = torch.from_numpy(boxes)
         others_ts = torch.from_numpy(others)
         est_ts = compute_intersect_3drot(boxes_ts, others_ts)
         self.assertTrue(est_ts.numpy().shape == gt.shape)
         self.assertTrue(np.allclose(est_ts.numpy(), gt, atol=1e-1))
         self.assertTrue(est_ts.dtype == boxes_ts.dtype)
         t1 = time.time()
         for i in range(times):
             est_ts = compute_intersect_3drot(boxes_ts, others_ts)
         t = (time.time()-t1) / times * 1000
         print(f"compute_intersect_3drot torch: {t:.2f} ms")
         # # torch cuda
         boxes_tsgpu = torch.from_numpy(boxes).cuda()
         others_tsgpu = torch.from_numpy(others).cuda()
         est_tsgpu = compute_intersect_3drot(boxes_tsgpu, others_tsgpu)
         self.assertTrue(est_tsgpu.cpu().numpy().shape == gt.shape)
         self.assertTrue(np.allclose(est_tsgpu.cpu().numpy(), gt, atol=1e-1))
         self.assertTrue(est_tsgpu.dtype == boxes_tsgpu.dtype)
         torch.cuda.synchronize()
         t1 = time.time()
         for i in range(times):
             est_tsgpu = compute_intersect_3drot(boxes_tsgpu, others_tsgpu)
         torch.cuda.synchronize()
         t = (time.time()-t1) / times * 1000
         print(f"compute_intersect_3drot torch cuda: {t:.2f} ms")
Exemplo n.º 3
0
 def check_overlap(label: KittiLabel) -> bool:
     '''
     check if there is overlap in label.
     inputs:
         label: the result after agmentation
     return:
         bool: True if no overlap exists.
     '''
     assert istype(label, "KittiLabel")
     boxes = []
     for obj in label.data:
         boxes.append([obj.x, obj.z, -obj.y, obj.l, obj.w, obj.h,
                       -obj.ry])  # Fcam to Flidar
     boxes = np.vstack(boxes)
     while boxes.shape[0] > 0:
         box = boxes[0:1, :]
         others = boxes[1:, :]
         inter = compute_intersec(box, others, mode='2d-rot')
         if inter.sum() > 0:
             return False
         boxes = others
     return True
Exemplo n.º 4
0
import torch
import iou_cpp
from det3.utils.utils import compute_intersec

if __name__ == "__main__":
    import numpy as np
    box = np.array([3, 2, 4, 4.5])
    others = np.array([[6, 3, 2, 5], [5, 3, 2, 5]])
    others = [others + np.abs(np.random.randn(8).reshape(2, 4)) for i in range(2)]
    others = np.vstack(others)
    box_ts = torch.from_numpy(box).float()
    others_ts = torch.from_numpy(others).float()
    est_ts = iou_cpp.compute_intersect_2d_cpu(box_ts, others_ts)
    box_gt = np.array([box[0], box[1], 0, box[2], box[3], 0, 0])
    others_gt = [np.array([itm[0], itm[1], 0, itm[2], itm[3], 0, 0]) for itm in others]
    others_gt = np.vstack(others_gt)
    gt = compute_intersec(box_gt, others_gt, mode="2d-rot")
    print(est_ts, gt)