Skip to content

Commit c7bf271

Browse files
authored
Update the pad_bbox_batch_to_shape (#56)
* Update the pad_bbox_batch_to_shape 1. Fix the current implementation which has the typo in it (tartarget_shapeget) 2. Add docstring about the sample usage. 3. Add validation check for shape mismatch. 4. Add unit test. * Fix format issue * Fix more format issue * Raise error when target_shape is smaller than bbox shape
1 parent 2a1dbad commit c7bf271

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

keras_cv/utils/bbox.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,45 @@ def pad_bbox_batch_to_shape(bboxes, target_shape, padding_values=-1):
102102
103103
Boxes represented by all -1s are ignored by COCO metrics.
104104
105+
Sample usage:
106+
bbox = [[1, 2, 3, 4], [5, 6, 7, 8]] # 2 bboxes with with xywh or corner format.
107+
target_shape = [3, 4] # Add 1 more dummy bbox
108+
result = pad_bbox_batch_to_shape(bbox, target_shape)
109+
# result == [[1, 2, 3, 4], [5, 6, 7, 8], [-1, -1, -1, -1]]
110+
111+
target_shape = [2, 5] # Add 1 more index after the current 4 coordinates.
112+
result = pad_bbox_batch_to_shape(bbox, target_shape)
113+
# result == [[1, 2, 3, 4, -1], [5, 6, 7, 8, -1]]
114+
105115
Args:
106116
bboxes: tf.Tensor of bounding boxes in any format.
107-
target_shape: Target shape to pad bboxes to.
117+
target_shape: Target shape to pad bounding box to. This should have the same
118+
rank as the bbboxs. Note that if the target_shape contains any dimension
119+
that is smaller than the bounding box shape, then no value will be padded
108120
padding_values: value to pad, defaults to -1 to mask out in coco metrics.
109121
Returns:
110122
bboxes padded to target shape.
123+
124+
Raises:
125+
ValueError, when target shape has smaller rank or dimension value when
126+
comparing with shape of bounding boxes.
111127
"""
112128
bbox_shape = tf.shape(bboxes)
129+
if len(bbox_shape) != len(target_shape):
130+
raise ValueError(
131+
"Target shape should have same rank as the bounding box. "
132+
f"Got bbox shape = {bbox_shape}, "
133+
f"target_shape = {target_shape}"
134+
)
135+
for dim in range(len(target_shape)):
136+
if bbox_shape[dim] > target_shape[dim]:
137+
raise ValueError(
138+
"Target shape should be larger than bounding box shape "
139+
"in all dimensions. "
140+
f"Got bbox shape = {bbox_shape}, "
141+
f"target_shape = {target_shape}"
142+
)
113143
paddings = [
114-
[0, target_shape - bbox_shape[i]]
115-
for (i, tartarget_shapeget) in enumerate(target_shape)
144+
[0, target_shape[dim] - bbox_shape[dim]] for dim in range(len(target_shape))
116145
]
117146
return tf.pad(bboxes, paddings, mode="CONSTANT", constant_values=padding_values)

keras_cv/utils/bbox_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,27 @@ def test_xywh_to_corner(self):
7474
self.assertAllClose(
7575
bbox.xywh_to_corners(padded_xywh_bbox_3d), padded_corner_bbox_3d
7676
)
77+
78+
def test_bbox_padding(self):
79+
bboxes = [[1, 2, 3, 4], [5, 6, 7, 8]]
80+
target_shape = [3, 4]
81+
result = bbox.pad_bbox_batch_to_shape(bboxes, target_shape)
82+
self.assertAllClose(result, [[1, 2, 3, 4], [5, 6, 7, 8], [-1, -1, -1, -1]])
83+
84+
target_shape = [2, 5]
85+
result = bbox.pad_bbox_batch_to_shape(bboxes, target_shape)
86+
self.assertAllClose(result, [[1, 2, 3, 4, -1], [5, 6, 7, 8, -1]])
87+
88+
# Make sure to raise error if the rank is different between bbox and target
89+
# shape
90+
with self.assertRaisesRegexp(
91+
ValueError, "Target shape should have same rank"
92+
):
93+
bbox.pad_bbox_batch_to_shape(bboxes, [1, 2, 3])
94+
95+
# Make sure raise error if the target shape is smaller
96+
target_shape = [3, 2]
97+
with self.assertRaisesRegexp(
98+
ValueError, "Target shape should be larger than bounding box shape"
99+
):
100+
bbox.pad_bbox_batch_to_shape(bboxes, target_shape)

0 commit comments

Comments
 (0)