|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -"""Shared utility functions for working with bounding boxes.""" |
| 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: |
| 19 | +
|
| 20 | +1. LEFT, TOP, RIGHT, BOTTOM, where LEFT, TOP represent the top-left corner |
| 21 | + coordinates, and RIGHT, BOTTOM represent the bottom-right corner coordinates. |
| 22 | +2. X, Y, WIDTH, HEIGHT, where X and Y are the coordinates for the center of the box. |
| 23 | +
|
| 24 | +Math wise: |
| 25 | +LEFT = X - WIDTH / 2 |
| 26 | +TOP = Y - HEIGHT / 2 |
| 27 | +RIGHT = X + WIDTH / 2 |
| 28 | +BOTTOM = Y + HEIGHT / 2 |
| 29 | +
|
| 30 | +X = (LEFT + RIGHT) / 2 |
| 31 | +Y = (TOP + BOTTOM) / 2 |
| 32 | +WIDTH = RIGHT - LEFT |
| 33 | +HEIGHT = BOTTOM - TOP |
| 34 | +
|
| 35 | +Note that these two formats are both commonly used. Corners format are mostly used |
| 36 | +for IOU computation, whereas XYWH are easy for bounding box generation with different |
| 37 | +center and width/height ratio. |
| 38 | +""" |
16 | 39 |
|
17 | 40 | import tensorflow as tf |
18 | 41 |
|
19 | | -# These are the dimensions used in Tensors to represent each corresponding side. |
| 42 | +# These are the indexes used in Tensors to represent each corresponding side. |
20 | 43 | LEFT, TOP, RIGHT, BOTTOM = 0, 1, 2, 3 |
21 | 44 |
|
22 | | -# These are the dimensions that you can use for bboxes in corners format. |
| 45 | +# These are the indexes that you can use for bounding box in XYWH format. |
23 | 46 | X, Y, WIDTH, HEIGHT = 0, 1, 2, 3 |
24 | 47 |
|
25 | 48 | # Regardless of format these constants are consistent. |
26 | | -# Class is held in the 4th index |
| 49 | +# Class is held in the 5th index |
27 | 50 | CLASS = 4 |
28 | | -# Confidence exists only on y_pred, and is in the 5th index. |
| 51 | +# Confidence exists only on y_pred, and is in the 6th index. |
29 | 52 | CONFIDENCE = 5 |
30 | 53 |
|
31 | 54 |
|
32 | | -def convert_corners_to_xywh(bboxes): |
33 | | - """Converts bboxes in corners format to xywh format.""" |
| 55 | +def corners_to_xywh(bboxes): |
| 56 | + """Converts bboxes in corners format to XYWH format. |
| 57 | +
|
| 58 | + Args: |
| 59 | + bboxes: a Tensor which has at least 2D rank, with shape [..., 4] |
| 60 | +
|
| 61 | + Returns: |
| 62 | + converted bboxes with same shape, but in XYWH format. |
| 63 | + """ |
| 64 | + left, top, right, bottom, rest = tf.split(bboxes, [1, 1, 1, 1, -1], axis=-1) |
34 | 65 | return tf.concat( |
35 | 66 | [ |
36 | | - (bboxes[..., :2] + bboxes[..., 2:4]) / 2.0, |
37 | | - bboxes[..., 2:4] - bboxes[..., :2], |
38 | | - bboxes[..., 4:], |
| 67 | + # We use ... here in case user has higher rank of inputs. |
| 68 | + (left + right) / 2.0, # X |
| 69 | + (top + bottom) / 2.0, # Y |
| 70 | + right - left, # WIDTH |
| 71 | + bottom - top, # HEIGHT |
| 72 | + rest, # In case there is any more index after the BOTTOM. |
39 | 73 | ], |
40 | 74 | axis=-1, |
41 | 75 | ) |
42 | 76 |
|
43 | 77 |
|
44 | 78 | def xywh_to_corners(bboxes): |
45 | | - """Converts bboxes in xywh format to corners format.""" |
| 79 | + """Converts bboxes in XYWH format to corners format. |
| 80 | +
|
| 81 | + Args: |
| 82 | + bboxes: a Tensor which has at least 2D rank, with shape [..., 4] |
| 83 | +
|
| 84 | + Returns: |
| 85 | + converted bboxes with same shape, but in corners format. |
| 86 | + """ |
| 87 | + x, y, width, height, rest = tf.split(bboxes, [1, 1, 1, 1, -1], axis=-1) |
46 | 88 | return tf.concat( |
47 | 89 | [ |
48 | | - bboxes[..., :2] - bboxes[..., 2:4] / 2.0, |
49 | | - bboxes[..., :2] + bboxes[..., 2:4] / 2.0, |
50 | | - bboxes[..., 4:], |
| 90 | + x - width / 2.0, |
| 91 | + y - height / 2.0, |
| 92 | + x + width / 2.0, |
| 93 | + y + height / 2.0, |
| 94 | + rest, # In case there is any more index after the HEIGHT. |
51 | 95 | ], |
52 | 96 | axis=-1, |
53 | 97 | ) |
|
0 commit comments