@@ -647,6 +647,102 @@ def test_convert_boxes_to_roi_format(self):
647
647
self .assertTrue (torch .equal (ref_tensor , ops ._utils .convert_boxes_to_roi_format (box_sequence )))
648
648
649
649
650
+ class BoxTester (unittest .TestCase ):
651
+ def test_bbox_same (self ):
652
+ box_tensor = torch .tensor ([[0 , 0 , 100 , 100 ], [0 , 0 , 0 , 0 ],
653
+ [10 , 15 , 30 , 35 ], [23 , 35 , 93 , 95 ]], dtype = torch .float )
654
+
655
+ exp_xyxy = torch .tensor ([[0 , 0 , 100 , 100 ], [0 , 0 , 0 , 0 ],
656
+ [10 , 15 , 30 , 35 ], [23 , 35 , 93 , 95 ]], dtype = torch .float )
657
+
658
+ box_same = ops .box_convert (box_tensor , in_fmt = "xyxy" , out_fmt = "xyxy" )
659
+ self .assertEqual (exp_xyxy .size (), torch .Size ([4 , 4 ]))
660
+ self .assertEqual (exp_xyxy .dtype , box_tensor .dtype )
661
+ assert torch .all (torch .eq (box_same , exp_xyxy )).item ()
662
+
663
+ box_same = ops .box_convert (box_tensor , in_fmt = "xywh" , out_fmt = "xywh" )
664
+ self .assertEqual (exp_xyxy .size (), torch .Size ([4 , 4 ]))
665
+ self .assertEqual (exp_xyxy .dtype , box_tensor .dtype )
666
+ assert torch .all (torch .eq (box_same , exp_xyxy )).item ()
667
+
668
+ box_same = ops .box_convert (box_tensor , in_fmt = "cxcywh" , out_fmt = "cxcywh" )
669
+ self .assertEqual (exp_xyxy .size (), torch .Size ([4 , 4 ]))
670
+ self .assertEqual (exp_xyxy .dtype , box_tensor .dtype )
671
+ assert torch .all (torch .eq (box_same , exp_xyxy )).item ()
672
+
673
+ def test_bbox_xyxy_xywh (self ):
674
+ # Simple test convert boxes to xywh and back. Make sure they are same.
675
+ # box_tensor is in x1 y1 x2 y2 format.
676
+ box_tensor = torch .tensor ([[0 , 0 , 100 , 100 ], [0 , 0 , 0 , 0 ],
677
+ [10 , 15 , 30 , 35 ], [23 , 35 , 93 , 95 ]], dtype = torch .float )
678
+ exp_xywh = torch .tensor ([[0 , 0 , 100 , 100 ], [0 , 0 , 0 , 0 ],
679
+ [10 , 15 , 20 , 20 ], [23 , 35 , 70 , 60 ]], dtype = torch .float )
680
+
681
+ box_xywh = ops .box_convert (box_tensor , in_fmt = "xyxy" , out_fmt = "xywh" )
682
+ self .assertEqual (exp_xywh .size (), torch .Size ([4 , 4 ]))
683
+ self .assertEqual (exp_xywh .dtype , box_tensor .dtype )
684
+ assert torch .all (torch .eq (box_xywh , exp_xywh )).item ()
685
+
686
+ # Reverse conversion
687
+ box_xyxy = ops .box_convert (box_xywh , in_fmt = "xywh" , out_fmt = "xyxy" )
688
+ self .assertEqual (box_xyxy .size (), torch .Size ([4 , 4 ]))
689
+ self .assertEqual (box_xyxy .dtype , box_tensor .dtype )
690
+ assert torch .all (torch .eq (box_xyxy , box_tensor )).item ()
691
+
692
+ def test_bbox_xyxy_cxcywh (self ):
693
+ # Simple test convert boxes to xywh and back. Make sure they are same.
694
+ # box_tensor is in x1 y1 x2 y2 format.
695
+ box_tensor = torch .tensor ([[0 , 0 , 100 , 100 ], [0 , 0 , 0 , 0 ],
696
+ [10 , 15 , 30 , 35 ], [23 , 35 , 93 , 95 ]], dtype = torch .float )
697
+ exp_cxcywh = torch .tensor ([[50 , 50 , 100 , 100 ], [0 , 0 , 0 , 0 ],
698
+ [20 , 25 , 20 , 20 ], [58 , 65 , 70 , 60 ]], dtype = torch .float )
699
+
700
+ box_cxcywh = ops .box_convert (box_tensor , in_fmt = "xyxy" , out_fmt = "cxcywh" )
701
+ self .assertEqual (exp_cxcywh .size (), torch .Size ([4 , 4 ]))
702
+ self .assertEqual (exp_cxcywh .dtype , box_tensor .dtype )
703
+ assert torch .all (torch .eq (box_cxcywh , exp_cxcywh )).item ()
704
+
705
+ # Reverse conversion
706
+ box_xyxy = ops .box_convert (box_cxcywh , in_fmt = "cxcywh" , out_fmt = "xyxy" )
707
+ self .assertEqual (box_xyxy .size (), torch .Size ([4 , 4 ]))
708
+ self .assertEqual (box_xyxy .dtype , box_tensor .dtype )
709
+ assert torch .all (torch .eq (box_xyxy , box_tensor )).item ()
710
+
711
+ def test_bbox_xywh_cxcywh (self ):
712
+ box_tensor = torch .tensor ([[0 , 0 , 100 , 100 ], [0 , 0 , 0 , 0 ],
713
+ [10 , 15 , 20 , 20 ], [23 , 35 , 70 , 60 ]], dtype = torch .float )
714
+
715
+ # This is wrong
716
+ exp_cxcywh = torch .tensor ([[50 , 50 , 100 , 100 ], [0 , 0 , 0 , 0 ],
717
+ [20 , 25 , 20 , 20 ], [58 , 65 , 70 , 60 ]], dtype = torch .float )
718
+
719
+ box_cxcywh = ops .box_convert (box_tensor , in_fmt = "xywh" , out_fmt = "cxcywh" )
720
+ self .assertEqual (exp_cxcywh .size (), torch .Size ([4 , 4 ]))
721
+ self .assertEqual (exp_cxcywh .dtype , box_tensor .dtype )
722
+ assert torch .all (torch .eq (box_cxcywh , exp_cxcywh )).item ()
723
+
724
+ # Reverse conversion
725
+ box_xywh = ops .box_convert (box_cxcywh , in_fmt = "cxcywh" , out_fmt = "xywh" )
726
+ self .assertEqual (box_xywh .size (), torch .Size ([4 , 4 ]))
727
+ self .assertEqual (box_xywh .dtype , box_tensor .dtype )
728
+ assert torch .all (torch .eq (box_xywh , box_tensor )).item ()
729
+
730
+ # def test_bbox_convert_jit(self):
731
+ # box_tensor = torch.tensor([[0, 0, 100, 100], [0, 0, 0, 0],
732
+ # [10, 15, 30, 35], [23, 35, 93, 95]], dtype=torch.float)
733
+
734
+ # scripted_fn = torch.jit.script(ops.box_convert)
735
+ # TOLERANCE = 1e-3
736
+
737
+ # box_xywh = ops.box_convert(box_tensor, in_fmt="xyxy", out_fmt="xywh")
738
+ # scripted_xywh = scripted_fn(box_tensor, 'xyxy', 'xywh')
739
+ # self.assertTrue((scripted_xywh - box_xywh).abs().max() < TOLERANCE)
740
+
741
+ # box_cxcywh = ops.box_convert(box_tensor, in_fmt="xyxy", out_fmt="cxcywh")
742
+ # scripted_cxcywh = scripted_fn(box_tensor, 'xyxy', 'cxcywh')
743
+ # self.assertTrue((scripted_cxcywh - box_cxcywh).abs().max() < TOLERANCE)
744
+
745
+
650
746
class BoxAreaTester (unittest .TestCase ):
651
747
def test_box_area (self ):
652
748
# A bounding box of area 10000 and a degenerate case
0 commit comments