MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / convert

Method convert

detectron2/structures/boxes.py:44–130  ·  view source on GitHub ↗

Args: box: can be a k-tuple, k-list or an Nxk array/tensor, where k = 4 or 5 from_mode, to_mode (BoxMode) Returns: The converted box of the same type.

(box: _RawBoxType, from_mode: "BoxMode", to_mode: "BoxMode")

Source from the content-addressed store, hash-verified

42
43 @staticmethod
44 def convert(box: _RawBoxType, from_mode: "BoxMode", to_mode: "BoxMode") -> _RawBoxType:
45 """
46 Args:
47 box: can be a k-tuple, k-list or an Nxk array/tensor, where k = 4 or 5
48 from_mode, to_mode (BoxMode)
49
50 Returns:
51 The converted box of the same type.
52 """
53 if from_mode == to_mode:
54 return box
55
56 original_type = type(box)
57 is_numpy = isinstance(box, np.ndarray)
58 single_box = isinstance(box, (list, tuple))
59 if single_box:
60 assert len(box) == 4 or len(box) == 5, (
61 "BoxMode.convert takes either a k-tuple/list or an Nxk array/tensor,"
62 " where k == 4 or 5"
63 )
64 arr = torch.tensor(box)[None, :]
65 else:
66 # avoid modifying the input box
67 if is_numpy:
68 arr = torch.from_numpy(np.asarray(box)).clone()
69 else:
70 arr = box.clone()
71
72 assert to_mode.value not in [
73 BoxMode.XYXY_REL,
74 BoxMode.XYWH_REL,
75 ] and from_mode.value not in [
76 BoxMode.XYXY_REL,
77 BoxMode.XYWH_REL,
78 ], "Relative mode not yet supported!"
79
80 if from_mode == BoxMode.XYWHA_ABS and to_mode == BoxMode.XYXY_ABS:
81 assert (
82 arr.shape[-1] == 5
83 ), "The last dimension of input shape must be 5 for XYWHA format"
84 original_dtype = arr.dtype
85 arr = arr.double()
86
87 w = arr[:, 2]
88 h = arr[:, 3]
89 a = arr[:, 4]
90 c = torch.abs(torch.cos(a * math.pi / 180.0))
91 s = torch.abs(torch.sin(a * math.pi / 180.0))
92 # This basically computes the horizontal bounding rectangle of the rotated box
93 new_w = c * w + s * h
94 new_h = c * h + s * w
95
96 # convert center to top-left corner
97 arr[:, 0] -= new_w / 2.0
98 arr[:, 1] -= new_h / 2.0
99 # bottom-right corner
100 arr[:, 2] = arr[:, 0] + new_w
101 arr[:, 3] = arr[:, 1] + new_h

Callers 15

create_instancesFunction · 0.80
draw_dataset_dictMethod · 0.80
convert_PIL_to_numpyFunction · 0.80
convert_image_to_rgbFunction · 0.80
transform_proposalsFunction · 0.80
annotations_to_instancesFunction · 0.80
convert_to_coco_dictFunction · 0.80
convert_to_coco_dictFunction · 0.80
convert_to_coco_dictFunction · 0.80
instances_to_coco_jsonFunction · 0.80

Calls 4

flattenMethod · 0.80
cloneMethod · 0.45
toMethod · 0.45
catMethod · 0.45

Tested by 4

_convert_xy_to_whMethod · 0.64
process_annotationMethod · 0.64