|
from unittest import TestCase |
|
|
|
import numpy as np |
|
import torch |
|
from mmengine.testing import assert_allclose |
|
|
|
from .utils import ToyBaseBoxes |
|
|
|
|
|
class TestBaseBoxes(TestCase): |
|
|
|
def test_init(self): |
|
box_tensor = torch.rand((3, 4, 4)) |
|
boxes = ToyBaseBoxes(box_tensor) |
|
|
|
boxes = ToyBaseBoxes(box_tensor, dtype=torch.float64) |
|
self.assertEqual(boxes.tensor.dtype, torch.float64) |
|
|
|
if torch.cuda.is_available(): |
|
boxes = ToyBaseBoxes(box_tensor, device='cuda') |
|
self.assertTrue(boxes.tensor.is_cuda) |
|
|
|
with self.assertRaises(AssertionError): |
|
box_tensor = torch.rand((4, )) |
|
boxes = ToyBaseBoxes(box_tensor) |
|
|
|
with self.assertRaises(AssertionError): |
|
box_tensor = torch.rand((3, 4, 3)) |
|
boxes = ToyBaseBoxes(box_tensor) |
|
|
|
def test_getitem(self): |
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
|
|
|
|
|
|
new_boxes = boxes[0] |
|
self.assertIsInstance(new_boxes, ToyBaseBoxes) |
|
self.assertEqual(new_boxes.tensor.shape, (4, 4)) |
|
|
|
new_boxes = boxes[[0, 2]] |
|
self.assertIsInstance(new_boxes, ToyBaseBoxes) |
|
self.assertEqual(new_boxes.tensor.shape, (2, 4, 4)) |
|
|
|
new_boxes = boxes[0:2] |
|
self.assertIsInstance(new_boxes, ToyBaseBoxes) |
|
self.assertEqual(new_boxes.tensor.shape, (2, 4, 4)) |
|
|
|
new_boxes = boxes[torch.LongTensor([0, 1])] |
|
self.assertIsInstance(new_boxes, ToyBaseBoxes) |
|
self.assertEqual(new_boxes.tensor.shape, (2, 4, 4)) |
|
|
|
new_boxes = boxes[torch.BoolTensor([True, False, True])] |
|
self.assertIsInstance(new_boxes, ToyBaseBoxes) |
|
self.assertEqual(new_boxes.tensor.shape, (2, 4, 4)) |
|
with self.assertRaises(AssertionError): |
|
index = torch.rand((2, 4, 4)) > 0 |
|
new_boxes = boxes[index] |
|
|
|
|
|
|
|
new_boxes = boxes[1, 2] |
|
self.assertIsInstance(new_boxes, ToyBaseBoxes) |
|
self.assertEqual(new_boxes.tensor.shape, (1, 4)) |
|
|
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes[1, 2, 1] |
|
|
|
new_boxes = boxes[None, ...] |
|
self.assertIsInstance(new_boxes, ToyBaseBoxes) |
|
self.assertEqual(new_boxes.tensor.shape, (1, 3, 4, 4)) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes[..., None] |
|
|
|
def test_setitem(self): |
|
values = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
tensor = torch.rand(3, 4, 4) |
|
|
|
|
|
with self.assertRaises(AssertionError): |
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
boxes[0:2] = tensor[0:2] |
|
|
|
|
|
|
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
boxes[1] = values[1] |
|
assert_allclose(boxes.tensor[1], values.tensor[1]) |
|
|
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
boxes[[1, 2]] = values[[1, 2]] |
|
assert_allclose(boxes.tensor[[1, 2]], values.tensor[[1, 2]]) |
|
|
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
boxes[0:2] = values[0:2] |
|
assert_allclose(boxes.tensor[0:2], values.tensor[0:2]) |
|
|
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
index = torch.rand(3, 4) > 0.5 |
|
boxes[index] = values[index] |
|
assert_allclose(boxes.tensor[index], values.tensor[index]) |
|
|
|
|
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
boxes[0:2, 0:2] = values[0:2, 0:2] |
|
assert_allclose(boxes.tensor[0:2, 0:2], values.tensor[0:2, 0:2]) |
|
|
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
boxes[1, 1] = values[1, 1] |
|
assert_allclose(boxes.tensor[1, 1], values.tensor[1, 1]) |
|
|
|
with self.assertRaises(AssertionError): |
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
boxes[1, 1, 1] = values[1, 1, 1] |
|
|
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
boxes[0:2, ...] = values[0:2, ...] |
|
assert_allclose(boxes.tensor[0:2, ...], values.tensor[0:2, ...]) |
|
|
|
def test_tensor_like_functions(self): |
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
|
|
boxes.new_tensor([1, 2, 3]) |
|
|
|
boxes.new_full((3, 4), 0) |
|
|
|
boxes.new_empty((3, 4)) |
|
|
|
boxes.new_ones((3, 4)) |
|
|
|
boxes.new_zeros((3, 4)) |
|
|
|
self.assertEqual(boxes.size(0), 3) |
|
self.assertEqual(tuple(boxes.size()), (3, 4, 4)) |
|
|
|
self.assertEqual(boxes.dim(), 3) |
|
|
|
self.assertIsInstance(boxes.device, torch.device) |
|
|
|
self.assertIsInstance(boxes.dtype, torch.dtype) |
|
|
|
np_boxes = boxes.numpy() |
|
self.assertIsInstance(np_boxes, np.ndarray) |
|
self.assertTrue((np_boxes == np_boxes).all()) |
|
|
|
new_boxes = boxes.to(torch.uint8) |
|
self.assertEqual(new_boxes.tensor.dtype, torch.uint8) |
|
if torch.cuda.is_available(): |
|
new_boxes = boxes.to(device='cuda') |
|
self.assertTrue(new_boxes.tensor.is_cuda) |
|
|
|
if torch.cuda.is_available(): |
|
new_boxes = boxes.to(device='cuda') |
|
new_boxes = new_boxes.cpu() |
|
self.assertFalse(new_boxes.tensor.is_cuda) |
|
|
|
if torch.cuda.is_available(): |
|
new_boxes = boxes.cuda() |
|
self.assertTrue(new_boxes.tensor.is_cuda) |
|
|
|
boxes.clone() |
|
|
|
boxes.detach() |
|
|
|
new_boxes = boxes.view(12, 4) |
|
self.assertEqual(tuple(new_boxes.size()), (12, 4)) |
|
new_boxes = boxes.view(-1, 4) |
|
self.assertEqual(tuple(new_boxes.size()), (12, 4)) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes.view(-1) |
|
|
|
new_boxes = boxes.reshape(12, 4) |
|
self.assertEqual(tuple(new_boxes.size()), (12, 4)) |
|
new_boxes = boxes.reshape(-1, 4) |
|
self.assertEqual(tuple(new_boxes.size()), (12, 4)) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes.reshape(-1) |
|
|
|
new_boxes = boxes[None, ...].expand(4, -1, -1, -1) |
|
self.assertEqual(tuple(new_boxes.size()), (4, 3, 4, 4)) |
|
|
|
new_boxes = boxes.repeat(2, 2, 1) |
|
self.assertEqual(tuple(new_boxes.size()), (6, 8, 4)) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes.repeat(2, 2, 2) |
|
|
|
new_boxes = boxes.transpose(0, 1) |
|
self.assertEqual(tuple(new_boxes.size()), (4, 3, 4)) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes.transpose(1, 2) |
|
|
|
new_boxes = boxes.permute(1, 0, 2) |
|
self.assertEqual(tuple(new_boxes.size()), (4, 3, 4)) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes.permute(2, 1, 0) |
|
|
|
boxes_list = boxes.split(1, dim=0) |
|
for box in boxes_list: |
|
self.assertIsInstance(box, ToyBaseBoxes) |
|
self.assertEqual(tuple(box.size()), (1, 4, 4)) |
|
boxes_list = boxes.split([1, 2], dim=0) |
|
with self.assertRaises(AssertionError): |
|
boxes_list = boxes.split(1, dim=2) |
|
|
|
boxes_list = boxes.split(3, dim=1) |
|
self.assertEqual(len(boxes_list), 2) |
|
for box in boxes_list: |
|
self.assertIsInstance(box, ToyBaseBoxes) |
|
with self.assertRaises(AssertionError): |
|
boxes_list = boxes.split(3, dim=2) |
|
|
|
boxes_list = boxes.unbind(dim=1) |
|
self.assertEqual(len(boxes_list), 4) |
|
for box in boxes_list: |
|
self.assertIsInstance(box, ToyBaseBoxes) |
|
self.assertEqual(tuple(box.size()), (3, 4)) |
|
with self.assertRaises(AssertionError): |
|
boxes_list = boxes.unbind(dim=2) |
|
|
|
new_boxes = boxes.flatten() |
|
self.assertEqual(tuple(new_boxes.size()), (12, 4)) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes.flatten(end_dim=2) |
|
|
|
boxes = ToyBaseBoxes(torch.rand(1, 3, 1, 4, 4)) |
|
new_boxes = boxes.squeeze() |
|
self.assertEqual(tuple(new_boxes.size()), (3, 4, 4)) |
|
new_boxes = boxes.squeeze(dim=2) |
|
self.assertEqual(tuple(new_boxes.size()), (1, 3, 4, 4)) |
|
|
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
new_boxes = boxes.unsqueeze(0) |
|
self.assertEqual(tuple(new_boxes.size()), (1, 3, 4, 4)) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes.unsqueeze(3) |
|
|
|
with self.assertRaises(ValueError): |
|
ToyBaseBoxes.cat([]) |
|
box_list = [] |
|
box_list.append(ToyBaseBoxes(torch.rand(3, 4, 4))) |
|
box_list.append(ToyBaseBoxes(torch.rand(1, 4, 4))) |
|
with self.assertRaises(AssertionError): |
|
ToyBaseBoxes.cat(box_list, dim=2) |
|
cat_boxes = ToyBaseBoxes.cat(box_list, dim=0) |
|
self.assertIsInstance(cat_boxes, ToyBaseBoxes) |
|
self.assertEqual((cat_boxes.size()), (4, 4, 4)) |
|
|
|
with self.assertRaises(ValueError): |
|
ToyBaseBoxes.stack([]) |
|
box_list = [] |
|
box_list.append(ToyBaseBoxes(torch.rand(3, 4, 4))) |
|
box_list.append(ToyBaseBoxes(torch.rand(3, 4, 4))) |
|
with self.assertRaises(AssertionError): |
|
ToyBaseBoxes.stack(box_list, dim=3) |
|
stack_boxes = ToyBaseBoxes.stack(box_list, dim=1) |
|
self.assertIsInstance(stack_boxes, ToyBaseBoxes) |
|
self.assertEqual((stack_boxes.size()), (3, 2, 4, 4)) |
|
|
|
def test_misc(self): |
|
boxes = ToyBaseBoxes(torch.rand(3, 4, 4)) |
|
|
|
self.assertEqual(len(boxes), 3) |
|
|
|
repr(boxes) |
|
|
|
new_boxes = boxes.fake_boxes((3, 4, 4), 1) |
|
self.assertEqual(tuple(new_boxes.size()), (3, 4, 4)) |
|
self.assertEqual(boxes.dtype, new_boxes.dtype) |
|
self.assertEqual(boxes.device, new_boxes.device) |
|
self.assertTrue((new_boxes.tensor == 1).all()) |
|
with self.assertRaises(AssertionError): |
|
new_boxes = boxes.fake_boxes((3, 4, 1)) |
|
new_boxes = boxes.fake_boxes((3, 4, 4), dtype=torch.uint8) |
|
self.assertEqual(new_boxes.dtype, torch.uint8) |
|
if torch.cuda.is_available(): |
|
new_boxes = boxes.fake_boxes((3, 4, 4), device='cuda') |
|
self.assertTrue(new_boxes.tensor.is_cuda) |
|
|