def test_get_boxes_shape(self, device, dtype): box = Boxes(torch.tensor([[[1.0, 1.0], [3.0, 2.0], [1.0, 2.0], [3.0, 1.0]]], device=device, dtype=dtype)) t_boxes = torch.tensor( [[[1.0, 1.0], [3.0, 1.0], [1.0, 2.0], [3.0, 2.0]], [[5.0, 4.0], [2.0, 2.0], [5.0, 2.0], [2.0, 4.0]]], device=device, dtype=dtype, ) # (2, 4, 2) boxes = Boxes(t_boxes) boxes_batch = Boxes(t_boxes[None]) # (1, 2, 4, 2) # Single box h, w = box.get_boxes_shape() assert (h.item(), w.item()) == (2, 3) # Boxes h, w = boxes.get_boxes_shape() assert h.ndim == 1 and w.ndim == 1 assert len(h) == 2 and len(w) == 2 assert (h == torch.as_tensor([2.0, 3.0], device=device)).all() and ( w == torch.as_tensor([3.0, 4.0], device=device) ).all() # Box batch h, w = boxes_batch.get_boxes_shape() assert h.ndim == 2 and w.ndim == 2 assert h.shape == (1, 2) and w.shape == (1, 2) assert (h == torch.as_tensor([[2.0, 3.0]], device=device)).all() and ( w == torch.as_tensor([[3.0, 4.0]], device=device) ).all()
def test_smoke(self, device, dtype): def _create_tensor_box(): # Sample two points of the rectangle points = torch.rand(1, 4, device=device, dtype=dtype) # Fill according missing points tensor_boxes = torch.zeros(1, 4, 2, device=device, dtype=dtype) tensor_boxes[0, 0] = points[0][:2] tensor_boxes[0, 1, 0] = points[0][2] tensor_boxes[0, 1, 1] = points[0][1] tensor_boxes[0, 2] = points[0][2:] tensor_boxes[0, 3, 0] = points[0][0] tensor_boxes[0, 3, 1] = points[0][3] return tensor_boxes # Validate assert Boxes(_create_tensor_box()) # Validate 1 box # 2 boxes without batching (N, 4, 2) where N=2 two_boxes = torch.cat([_create_tensor_box(), _create_tensor_box()]) assert Boxes(two_boxes) # 2 boxes in batch (B, 1, 4, 2) where B=2 batched_bbox = torch.stack([_create_tensor_box(), _create_tensor_box()]) assert Boxes(batched_bbox)
def test_boxes_to_tensor(self, shape: Tuple[int], device, dtype): # quadrilateral with randomized vertices to reflect possible transforms. box = Boxes(torch.as_tensor([[[2, 2], [2, 3], [1, 3], [1, 2]]], device=device, dtype=dtype).view(*shape, 2)) expected_box_xyxy = torch.as_tensor([[1, 2, 3, 4]], device=device, dtype=dtype).view(*shape) expected_box_xyxy_plus = torch.as_tensor([[1, 2, 2, 3]], device=device, dtype=dtype).view(*shape) expected_box_xywh = torch.as_tensor([[1, 2, 2, 2]], device=device, dtype=dtype).view(*shape) expected_vertices = torch.as_tensor([[[1, 2], [3, 2], [3, 4], [1, 4]]], device=device, dtype=dtype).view( *shape, 2 ) expected_vertices_plus = torch.as_tensor([[[1, 2], [2, 2], [2, 3], [1, 3]]], device=device, dtype=dtype).view( *shape, 2 ) boxes_xyxy = box.to_tensor(mode='xyxy') boxes_xyxy_plus = box.to_tensor(mode='xyxy_plus') boxes_xywh = box.to_tensor(mode='xywh') boxes_vertices = box.to_tensor(mode='vertices') boxes_vertices_plus = box.to_tensor(mode='vertices_plus') assert boxes_xyxy.shape == expected_box_xyxy.shape # type: ignore assert_allclose(boxes_xyxy, expected_box_xyxy) assert boxes_xyxy_plus.shape == expected_box_xyxy_plus.shape # type: ignore assert_allclose(boxes_xyxy_plus, expected_box_xyxy_plus) assert boxes_xywh.shape == expected_box_xywh.shape # type: ignore assert_allclose(boxes_xywh, expected_box_xywh) assert boxes_vertices.shape == expected_vertices.shape # type: ignore assert_allclose(boxes_vertices, expected_vertices) assert boxes_vertices_plus.shape == expected_vertices_plus.shape # type: ignore assert_allclose(boxes_vertices_plus, expected_vertices_plus)
def test_get_boxes_shape_batch(self, device, dtype): t_box1 = torch.tensor([[[1.0, 1.0], [3.0, 2.0], [3.0, 1.0], [1.0, 2.0]]], device=device, dtype=dtype) t_box2 = torch.tensor([[[5.0, 2.0], [2.0, 2.0], [5.0, 4.0], [2.0, 4.0]]], device=device, dtype=dtype) batched_boxes = Boxes(torch.stack([t_box1, t_box2])) h, w = batched_boxes.get_boxes_shape() assert h.ndim == 2 and w.ndim == 2 assert h.shape == (2, 1) and w.shape == (2, 1) assert (h == torch.as_tensor([[2], [3]], device=device)).all() and ( w == torch.as_tensor([[3], [4]], device=device) ).all()
def _wrapper_transform_boxes(quadrilaterals, M): boxes = Boxes(quadrilaterals) boxes = boxes.transform_boxes(M) return boxes.data
def apply_boxes_method(tensor: torch.Tensor, method: str, **kwargs): boxes = Boxes(tensor) result = getattr(boxes, method)(**kwargs) return result.data if isinstance(result, Boxes) else result
def test_boxes_to_mask(self, device, dtype): t_box1 = torch.tensor( [[[1.0, 1.0], [3.0, 1.0], [3.0, 2.0], [1.0, 2.0]]], device=device, dtype=dtype ) # (1, 4, 2) t_box2 = torch.tensor( [[[2.0, 2.0], [4.0, 2.0], [4.0, 5.0], [2.0, 4.0]]], device=device, dtype=dtype ) # (1, 4, 2) box1, box2 = Boxes(t_box1), Boxes(t_box2) two_boxes = Boxes(torch.cat([t_box1, t_box2])) # (2, 4, 2) batched_boxes = Boxes(torch.stack([t_box1, t_box2])) # (2, 1, 4, 2) height, width = 7, 5 expected_mask1 = torch.tensor( [ [ [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], ] ], device=device, dtype=dtype, ) expected_mask2 = torch.tensor( [ [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 1, 1], [0, 0, 1, 1, 1], [0, 0, 1, 1, 1], [0, 0, 1, 1, 1], [0, 0, 0, 0, 0], ] ], device=device, dtype=dtype, ) expected_two_masks = torch.cat([expected_mask1, expected_mask2]) expected_batched_masks = torch.stack([expected_mask1, expected_mask2]) mask1 = box1.to_mask(height, width) mask2 = box2.to_mask(height, width) two_masks = two_boxes.to_mask(height, width) batched_masks = batched_boxes.to_mask(height, width) assert mask1.shape == expected_mask1.shape assert_allclose(mask1, expected_mask1) assert mask2.shape == expected_mask2.shape assert_allclose(mask2, expected_mask2) assert two_masks.shape == expected_two_masks.shape assert_allclose(two_masks, expected_two_masks) assert batched_masks.shape == expected_batched_masks.shape assert_allclose(batched_masks, expected_batched_masks)