Skip to content

Commit 3f437a6

Browse files
Add rotated bounding box formats
Test Plan: Run unit tests: `pytest test/test_ops.py -vvv -k TestBoxConvert`
1 parent 4249b61 commit 3f437a6

File tree

4 files changed

+549
-28
lines changed

4 files changed

+549
-28
lines changed

test/test_ops.py

+180-2
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,38 @@ def test_bbox_same(self):
12881288
assert_equal(ops.box_convert(box_tensor, in_fmt="xywh", out_fmt="xywh"), exp_xyxy)
12891289
assert_equal(ops.box_convert(box_tensor, in_fmt="cxcywh", out_fmt="cxcywh"), exp_xyxy)
12901290

1291+
def test_rotated_bbox_same(self):
1292+
box_tensor = torch.tensor(
1293+
[
1294+
[0, 0, 100, 100, 0],
1295+
[0, 0, 0, 0, 0],
1296+
[10, 15, 30, 35, 0],
1297+
[23, 35, 93, 95, 0],
1298+
],
1299+
dtype=torch.float,
1300+
)
1301+
1302+
exp_xyxyr = torch.tensor(
1303+
[
1304+
[0, 0, 100, 100, 0],
1305+
[0, 0, 0, 0, 0],
1306+
[10, 15, 30, 35, 0],
1307+
[23, 35, 93, 95, 0],
1308+
],
1309+
dtype=torch.float,
1310+
)
1311+
1312+
assert exp_xyxyr.size() == torch.Size([4, 5])
1313+
assert_equal(
1314+
ops.box_convert(box_tensor, in_fmt="xyxyr", out_fmt="xyxyr"), exp_xyxyr
1315+
)
1316+
assert_equal(
1317+
ops.box_convert(box_tensor, in_fmt="xywhr", out_fmt="xywhr"), exp_xyxyr
1318+
)
1319+
assert_equal(
1320+
ops.box_convert(box_tensor, in_fmt="cxcywhr", out_fmt="cxcywhr"), exp_xyxyr
1321+
)
1322+
12911323
def test_bbox_xyxy_xywh(self):
12921324
# Simple test convert boxes to xywh and back. Make sure they are same.
12931325
# box_tensor is in x1 y1 x2 y2 format.
@@ -1339,8 +1371,154 @@ def test_bbox_xywh_cxcywh(self):
13391371
box_xywh = ops.box_convert(box_cxcywh, in_fmt="cxcywh", out_fmt="xywh")
13401372
assert_equal(box_xywh, box_tensor)
13411373

1342-
@pytest.mark.parametrize("inv_infmt", ["xwyh", "cxwyh"])
1343-
@pytest.mark.parametrize("inv_outfmt", ["xwcx", "xhwcy"])
1374+
def test_bbox_xyxy_to_cxcywhr(self):
1375+
box_tensor = torch.tensor(
1376+
[[0, 0, 100, 100], [0, 0, 0, 0], [10, 15, 30, 35], [23, 35, 93, 95]],
1377+
dtype=torch.float,
1378+
)
1379+
exp_cxcywhr = torch.tensor(
1380+
[
1381+
[50, 50, 100, 100, 0],
1382+
[0, 0, 0, 0, 0],
1383+
[20, 25, 20, 20, 0],
1384+
[58, 65, 70, 60, 0],
1385+
],
1386+
dtype=torch.float,
1387+
)
1388+
1389+
assert exp_cxcywhr.size() == torch.Size([4, 5])
1390+
box_cxcywhr = ops.box_convert(box_tensor, in_fmt="xyxy", out_fmt="cxcywhr")
1391+
assert_equal(box_cxcywhr, exp_cxcywhr)
1392+
1393+
def test_bbox_xyxyr_xywhr(self):
1394+
# Simple test convert boxes to xywh and back. Make sure they are same.
1395+
# box_tensor is in x1 y1 x2 y2 format.
1396+
box_tensor = torch.tensor(
1397+
[
1398+
[0, 0, 100, 100, 0],
1399+
[0, 0, 0, 0, 0],
1400+
[10, 15, 30, 35, 0],
1401+
[23, 35, 93, 95, 0],
1402+
[3, 2, 7, 4, 0],
1403+
[3, 2, 5, -2, 90],
1404+
],
1405+
dtype=torch.float,
1406+
)
1407+
exp_xywhr = torch.tensor(
1408+
[
1409+
[0, 0, 100, 100, 0],
1410+
[0, 0, 0, 0, 0],
1411+
[10, 15, 20, 20, 0],
1412+
[23, 35, 70, 60, 0],
1413+
[3, 2, 4, 2, 0],
1414+
[3, 2, 4, 2, 90],
1415+
],
1416+
dtype=torch.float,
1417+
)
1418+
1419+
assert exp_xywhr.size() == torch.Size([6, 5])
1420+
box_xywhr = ops.box_convert(box_tensor, in_fmt="xyxyr", out_fmt="xywhr")
1421+
assert torch.allclose(box_xywhr, exp_xywhr)
1422+
1423+
# Reverse conversion
1424+
box_xyxyr = ops.box_convert(box_xywhr, in_fmt="xywhr", out_fmt="xyxyr")
1425+
assert torch.allclose(box_xyxyr, box_tensor)
1426+
1427+
def test_bbox_xyxyr_cxcywhr(self):
1428+
# Simple test convert boxes to cxcywh and back. Make sure they are same.
1429+
# box_tensor is in x1 y1 x2 y2 format.
1430+
box_tensor = torch.tensor(
1431+
[
1432+
[0, 0, 100, 100, 0],
1433+
[0, 0, 0, 0, 0],
1434+
[10, 15, 30, 35, 0],
1435+
[23, 35, 93, 95, 0],
1436+
[3, 2, 7, 4, 0],
1437+
],
1438+
dtype=torch.float,
1439+
)
1440+
exp_cxcywhr = torch.tensor(
1441+
[
1442+
[50, 50, 100, 100, 0],
1443+
[0, 0, 0, 0, 0],
1444+
[20, 25, 20, 20, 0],
1445+
[58, 65, 70, 60, 0],
1446+
[5, 3, 4, 2, 0],
1447+
],
1448+
dtype=torch.float,
1449+
)
1450+
1451+
assert exp_cxcywhr.size() == torch.Size([5, 5])
1452+
box_cxcywhr = ops.box_convert(box_tensor, in_fmt="xyxyr", out_fmt="cxcywhr")
1453+
assert torch.allclose(box_cxcywhr, exp_cxcywhr)
1454+
1455+
# Reverse conversion
1456+
box_xyxyr = ops.box_convert(box_cxcywhr, in_fmt="cxcywhr", out_fmt="xyxyr")
1457+
assert torch.allclose(box_xyxyr, box_tensor)
1458+
1459+
def test_bbox_xywhr_cxcywhr(self):
1460+
box_tensor = torch.tensor(
1461+
[
1462+
[0, 0, 100, 100, 0],
1463+
[0, 0, 0, 0, 0],
1464+
[10, 15, 20, 20, 0],
1465+
[23, 35, 70, 60, 0],
1466+
[4.0, 2.0, 4.0, 2.0, 0.0],
1467+
[5.0, 5.0, 4.0, 2.0, 90.0],
1468+
[8.0, 4.0, 4.0, 2.0, 180.0],
1469+
[7.0, 1.0, 4.0, 2.0, -90.0],
1470+
],
1471+
dtype=torch.float,
1472+
)
1473+
1474+
exp_cxcywhr = torch.tensor(
1475+
[
1476+
[50, 50, 100, 100, 0],
1477+
[0, 0, 0, 0, 0],
1478+
[20, 25, 20, 20, 0],
1479+
[58, 65, 70, 60, 0],
1480+
[6, 3, 4, 2, 0],
1481+
[6, 3, 4, 2, 90],
1482+
[6, 3, 4, 2, 180],
1483+
[6, 3, 4, 2, -90],
1484+
],
1485+
dtype=torch.float,
1486+
)
1487+
1488+
assert exp_cxcywhr.size() == torch.Size([8, 5])
1489+
box_cxcywhr = ops.box_convert(box_tensor, in_fmt="xywhr", out_fmt="cxcywhr")
1490+
assert torch.allclose(box_cxcywhr, exp_cxcywhr)
1491+
1492+
# Reverse conversion
1493+
box_xywhr = ops.box_convert(box_cxcywhr, in_fmt="cxcywhr", out_fmt="xywhr")
1494+
assert torch.allclose(box_xywhr, box_tensor)
1495+
1496+
def test_bbox_xyxyr_to_xyxyxyxy(self):
1497+
box_tensor = torch.tensor([[4, 5, 6, 1, 90]], dtype=torch.float)
1498+
exp_xyxyxyxy = torch.tensor([[4, 5, 4, 1, 6, 1, 6, 5]], dtype=torch.float)
1499+
1500+
assert exp_xyxyxyxy.size() == torch.Size([1, 8])
1501+
box_xyxyxyxy = ops.box_convert(box_tensor, in_fmt="xyxyr", out_fmt="xyxyxyxy")
1502+
assert_equal(box_xyxyxyxy, exp_xyxyxyxy)
1503+
1504+
def test_bbox_cxcywhr_to_xyxyxyxy(self):
1505+
box_tensor = torch.tensor([[5, 3, 4, 2, 90]], dtype=torch.float)
1506+
exp_xyxyxyxy = torch.tensor([[4, 5, 4, 1, 6, 1, 6, 5]], dtype=torch.float)
1507+
1508+
assert exp_xyxyxyxy.size() == torch.Size([1, 8])
1509+
box_xyxyxyxy = ops.box_convert(box_tensor, in_fmt="cxcywhr", out_fmt="xyxyxyxy")
1510+
assert_equal(box_xyxyxyxy, exp_xyxyxyxy)
1511+
1512+
def test_bbox_xywhr_to_xyxyxyxy(self):
1513+
box_tensor = torch.tensor([[4, 5, 4, 2, 90]], dtype=torch.float)
1514+
exp_xyxyxyxy = torch.tensor([[4, 5, 4, 1, 6, 1, 6, 5]], dtype=torch.float)
1515+
1516+
assert exp_xyxyxyxy.size() == torch.Size([1, 8])
1517+
box_xyxyxyxy = ops.box_convert(box_tensor, in_fmt="xywhr", out_fmt="xyxyxyxy")
1518+
assert_equal(box_xyxyxyxy, exp_xyxyxyxy)
1519+
1520+
@pytest.mark.parametrize("inv_infmt", ["xwyh", "cxwyh", "xwyhr", "cxwyhr", "xxxxyyyy"])
1521+
@pytest.mark.parametrize("inv_outfmt", ["xwcx", "xhwcy", "xwcxr", "xhwcyr", "xyxyxxyy"])
13441522
def test_bbox_invalid(self, inv_infmt, inv_outfmt):
13451523
box_tensor = torch.tensor(
13461524
[[0, 0, 100, 100], [0, 0, 0, 0], [10, 15, 20, 20], [23, 35, 70, 60]], dtype=torch.float

0 commit comments

Comments
 (0)