1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
1212# See the License for the specific language governing permissions and 
1313# limitations under the License. 
14- 
15- """Shared utility functions for working with bounding boxes. 
16- 
17- Usually bounding boxes is a 2D Tensor with shape [batch, 4]. The second dimension 
18- will contain 4 numbers based on 2 different formats.  In KerasCV, we will use the 
19- `corners` format, which is [LEFT, TOP, RIGHT, BOTTOM]. 
20- 
21- In this file, provide utility functions for manipulating bounding boxes and converting 
22- their formats. 
23- """ 
24- 
2514import  tensorflow  as  tf 
2615
27- # These are the indexes used in Tensors to represent each corresponding side. 
28- LEFT , TOP , RIGHT , BOTTOM  =  0 , 1 , 2 , 3 
29- 
30- # Regardless of format these constants are consistent. 
31- # Class is held in the 5th index 
32- CLASS  =  4 
33- # Confidence exists only on y_pred, and is in the 6th index. 
34- CONFIDENCE  =  5 
35- 
36- 
37- def  convert_to_corners (bounding_boxes , format ):
38-     """Converts bounding_boxes to corners format. 
39- 
40-     Converts bounding boxes from the provided format to corners format, which is: 
41-     `[left, top, right, bottom]`. 
42- 
43-     args: 
44-         format:  one of "coco" or "yolo".  The formats are as follows- 
45-             coco=[x_min, y_min, width, height] 
46-             yolo=[x_center, y_center, width, height] 
47-     """ 
48-     if  format  ==  "coco" :
49-         return  _coco_to_corners (bounding_boxes )
50-     elif  format  ==  "yolo" :
51-         return  _yolo_to_corners (bounding_boxes )
52-     else :
53-         raise  ValueError (
54-             "Unsupported format passed to convert_to_corners().  " 
55-             f"Want one 'coco' or 'yolo', got format=={ format }  
56-         )
57- 
58- 
59- def  _yolo_to_corners (bounding_boxes ):
60-     x , y , width , height , rest  =  tf .split (bounding_boxes , [1 , 1 , 1 , 1 , - 1 ], axis = - 1 )
61-     return  tf .concat (
62-         [
63-             x  -  width  /  2.0 ,
64-             y  -  height  /  2.0 ,
65-             x  +  width  /  2.0 ,
66-             y  +  height  /  2.0 ,
67-             rest ,  # In case there is any more index after the HEIGHT. 
68-         ],
69-         axis = - 1 ,
70-     )
71- 
72- 
73- def  _coco_to_corners (bounding_boxes ):
74-     x , y , width , height , rest  =  tf .split (bounding_boxes , [1 , 1 , 1 , 1 , - 1 ], axis = - 1 )
75-     return  tf .concat (
76-         [
77-             x ,
78-             y ,
79-             x  +  width ,
80-             y  +  height ,
81-             rest ,  # In case there is any more index after the HEIGHT. 
82-         ],
83-         axis = - 1 ,
84-     )
85- 
8616
87- def  pad_bounding_box_batch_to_shape (bounding_boxes , target_shape , padding_values = - 1 ):
17+ def  pad_batch_to_shape (bounding_boxes , target_shape , padding_values = - 1 ):
8818    """Pads a list of bounding boxes with -1s. 
8919
9020    Boxes represented by all -1s are ignored by COCO metrics. 
@@ -93,17 +23,17 @@ def pad_bounding_box_batch_to_shape(bounding_boxes, target_shape, padding_values
9323    bounding_box = [[1, 2, 3, 4], [5, 6, 7, 8]]   # 2 bounding_boxes with with xywh or 
9424        corners format. 
9525    target_shape = [3, 4]   # Add 1 more dummy bounding_box 
96-     result = pad_bounding_box_batch_to_shape (bounding_box, target_shape) 
26+     result = pad_batch_to_shape (bounding_box, target_shape) 
9727    # result == [[1, 2, 3, 4], [5, 6, 7, 8], [-1, -1, -1, -1]] 
9828
9929    target_shape = [2, 5]   # Add 1 more index after the current 4 coordinates. 
100-     result = pad_bounding_box_batch_to_shape (bounding_box, target_shape) 
30+     result = pad_batch_to_shape (bounding_box, target_shape) 
10131    # result == [[1, 2, 3, 4, -1], [5, 6, 7, 8, -1]] 
10232
10333    Args: 
10434        bounding_boxes: tf.Tensor of bounding boxes in any format. 
10535        target_shape: Target shape to pad bounding box to. This should have the same 
106-             rank as the bbounding_boxs . Note that if the target_shape contains any 
36+             rank as the bounding_boxes . Note that if the target_shape contains any 
10737            dimension that is smaller than the bounding box shape, then no value will be 
10838            padded. 
10939        padding_values: value to pad, defaults to -1 to mask out in coco metrics. 
0 commit comments