def test_pairwise_iou_many_boxes_cuda(self): device = torch.device("cuda") num_boxes1 = 100 num_boxes2 = 200 boxes1 = torch.stack( [ torch.tensor( [5 + 20 * i, 5 + 20 * i, 10, 10, 0], dtype=torch.float32, device=device ) for i in range(num_boxes1) ] ) boxes2 = torch.stack( [ torch.tensor( [5 + 20 * i, 5 + 20 * i, 10, 1 + 9 * i / num_boxes2, 0], dtype=torch.float32, device=device, ) for i in range(num_boxes2) ] ) expected_ious = torch.zeros(num_boxes1, num_boxes2, dtype=torch.float32, device=device) for i in range(min(num_boxes1, num_boxes2)): expected_ious[i][i] = (1 + 9 * i / num_boxes2) / 10.0 ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_0_degree_cuda(self): device = torch.device("cuda") boxes1 = torch.tensor( [[0.5, 0.5, 1.0, 1.0, 0.0], [0.5, 0.5, 1.0, 1.0, 0.0]], dtype=torch.float32, device=device, ) boxes2 = torch.tensor( [ [0.5, 0.5, 1.0, 1.0, 0.0], [0.25, 0.5, 0.5, 1.0, 0.0], [0.5, 0.25, 1.0, 0.5, 0.0], [0.25, 0.25, 0.5, 0.5, 0.0], [0.75, 0.75, 0.5, 0.5, 0.0], [1.0, 1.0, 1.0, 1.0, 0.0], ], dtype=torch.float32, device=device, ) expected_ious = torch.tensor( [ [1.0, 0.5, 0.5, 0.25, 0.25, 0.25 / (2 - 0.25)], [1.0, 0.5, 0.5, 0.25, 0.25, 0.25 / (2 - 0.25)], ], dtype=torch.float32, device=device, ) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_0_degree(self): for device in ["cpu" ] + (["cuda"] if torch.cuda.is_available() else []): boxes1 = torch.tensor( [[0.5, 0.5, 1.0, 1.0, 0.0], [0.5, 0.5, 1.0, 1.0, 0.0]], dtype=torch.float32, device=device, ) boxes2 = torch.tensor( [ [0.5, 0.5, 1.0, 1.0, 0.0], [0.25, 0.5, 0.5, 1.0, 0.0], [0.5, 0.25, 1.0, 0.5, 0.0], [0.25, 0.25, 0.5, 0.5, 0.0], [0.75, 0.75, 0.5, 0.5, 0.0], [1.0, 1.0, 1.0, 1.0, 0.0], ], dtype=torch.float32, device=device, ) expected_ious = torch.tensor( [ [1.0, 0.5, 0.5, 0.25, 0.25, 0.25 / (2 - 0.25)], [1.0, 0.5, 0.5, 0.25, 0.25, 0.25 / (2 - 0.25)], ], dtype=torch.float32, device=device, ) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_many_boxes(self): for device in ["cpu" ] + (["cuda"] if torch.cuda.is_available() else []): num_boxes1 = 100 num_boxes2 = 200 boxes1 = torch.stack([ torch.tensor( [5 + 20 * i, 5 + 20 * i, 10, 10, 0], dtype=torch.float32, device=device, ) for i in range(num_boxes1) ]) boxes2 = torch.stack([ torch.tensor( [5 + 20 * i, 5 + 20 * i, 10, 1 + 9 * i / num_boxes2, 0], dtype=torch.float32, device=device, ) for i in range(num_boxes2) ]) expected_ious = torch.zeros(num_boxes1, num_boxes2, dtype=torch.float32, device=device) for i in range(min(num_boxes1, num_boxes2)): expected_ious[i][i] = (1 + 9 * i / num_boxes2) / 10.0 ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_orthogonal_cuda(self): device = torch.device("cuda") boxes1 = torch.tensor([[5, 5, 10, 6, 55]], dtype=torch.float32, device=device) boxes2 = torch.tensor([[5, 5, 10, 6, -35]], dtype=torch.float32, device=device) iou = (6.0 * 6.0) / (6.0 * 6.0 + 4.0 * 6.0 + 4.0 * 6.0) expected_ious = torch.tensor([[iou]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_issue1207_simplified(self): for device in ["cpu"] + ["cuda"] if torch.cuda.is_available() else []: # Simplified test case of D2-issue-1207 boxes1 = torch.tensor([[3, 3, 8, 2, -45.0]], device=device) boxes2 = torch.tensor([[6, 0, 8, 2, -45.0]], device=device) iou = 0.0 expected_ious = torch.tensor([[iou]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_issue1207_simplified_cuda(self): device = torch.device("cuda") # Simplified test case of D2-issue-1207 boxes1 = torch.tensor([[3, 3, 8, 2, -45.0]], device=device) boxes2 = torch.tensor([[6, 0, 8, 2, -45.0]], device=device) iou = 0.0 expected_ious = torch.tensor([[iou]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_45_degrees_cuda(self): device = torch.device("cuda") boxes1 = torch.tensor( [ [1, 1, math.sqrt(2), math.sqrt(2), 45], [1, 1, 2 * math.sqrt(2), 2 * math.sqrt(2), -45], ], dtype=torch.float32, device=device, ) boxes2 = torch.tensor([[1, 1, 2, 2, 0]], dtype=torch.float32, device=device) expected_ious = torch.tensor([[0.5], [0.5]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_orthogonal(self): for device in ["cpu"] + ["cuda"] if torch.cuda.is_available() else []: boxes1 = torch.tensor([[5, 5, 10, 6, 55]], dtype=torch.float32, device=device) boxes2 = torch.tensor([[5, 5, 10, 6, -35]], dtype=torch.float32, device=device) iou = (6.0 * 6.0) / (6.0 * 6.0 + 4.0 * 6.0 + 4.0 * 6.0) expected_ious = torch.tensor([[iou]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_issue1207(self): for device in ["cpu"] + ["cuda"] if torch.cuda.is_available() else []: # The original test case in D2-issue-1207 boxes1 = torch.tensor([[160.0, 153.0, 230.0, 23.0, -37.0]], device=device) boxes2 = torch.tensor([[190.0, 127.0, 80.0, 21.0, -46.0]], device=device) iou = 0.0 expected_ious = torch.tensor([[iou]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_large_close_boxes_cuda(self): device = torch.device("cuda") boxes1 = torch.tensor( [[299.500000, 417.370422, 600.000000, 364.259186, 27.1828]], dtype=torch.float32, device=device, ) boxes2 = torch.tensor( [[299.500000, 417.370422, 600.000000, 364.259155, 27.1828]], dtype=torch.float32, device=device, ) iou = 364.259155 / 364.259186 expected_ious = torch.tensor([[iou]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_issue1207_cuda(self): device = torch.device("cuda") # The original test case in D2-issue-1207 boxes1 = torch.tensor([[160.0, 153.0, 230.0, 23.0, -37.0]], device=device) boxes2 = torch.tensor([[190.0, 127.0, 80.0, 21.0, -46.0]], device=device) iou = 0.0 expected_ious = torch.tensor([[iou]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_large_close_boxes(self): for device in ["cpu"] + ["cuda"] if torch.cuda.is_available() else []: boxes1 = torch.tensor( [[299.500000, 417.370422, 600.000000, 364.259186, 27.1828]], dtype=torch.float32, device=device, ) boxes2 = torch.tensor( [[299.500000, 417.370422, 600.000000, 364.259155, 27.1828]], dtype=torch.float32, device=device, ) iou = 364.259155 / 364.259186 expected_ious = torch.tensor([[iou]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))
def test_pairwise_iou_45_degrees(self): for device in ["cpu"] + ["cuda"] if torch.cuda.is_available() else []: boxes1 = torch.tensor( [ [1, 1, math.sqrt(2), math.sqrt(2), 45], [1, 1, 2 * math.sqrt(2), 2 * math.sqrt(2), -45], ], dtype=torch.float32, device=device, ) boxes2 = torch.tensor([[1, 1, 2, 2, 0]], dtype=torch.float32, device=device) expected_ious = torch.tensor([[0.5], [0.5]], dtype=torch.float32, device=device) ious = pairwise_iou(RotatedBoxes(boxes1), RotatedBoxes(boxes2)) self.assertTrue(torch.allclose(ious, expected_ious))