|
| 1 | +# Copyright 2022 The KerasCV Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
1 | 14 | """Contains functions to compute ious of bounding boxes.""" |
2 | 15 | import tensorflow as tf |
3 | 16 |
|
4 | | -from keras_cv.util import bbox |
5 | | - |
6 | | - |
7 | | -def _compute_single_iou(bboxes1, bb2): |
8 | | - """computes the intersection over union between bboxes1 and bb2. |
9 | | -
|
10 | | - bboxes1 and bb2 are expected to come in the 'corners' format, or: |
11 | | - [left, right, top, bottom]. |
12 | | - For example, the bounding box with it's left side at 100, right side at 200, |
13 | | - top at 101, and bottom at 201 would be represented as: |
14 | | - [100, 200, 101, 201] |
15 | | -
|
16 | | - Args: |
17 | | - bboxes1: Tensor representing the first bounding box in 'corners' format. |
18 | | - bb2: Tensor representing the second bounding box in 'corners' format. |
19 | | -
|
20 | | - Returns: |
21 | | - iou: Tensor representing the intersection over union of the two bounding |
22 | | - boxes. |
23 | | - """ |
24 | | - # bounding box indices |
25 | | - |
26 | | - intersection_left = tf.math.maximum(bboxes1[bbox.LEFT], bb2[bbox.LEFT]) |
27 | | - intersection_right = tf.math.minimum(bboxes1[bbox.RIGHT], bb2[bbox.RIGHT]) |
28 | | - |
29 | | - intersection_top = tf.math.maximum(bboxes1[bbox.TOP], bb2[bbox.TOP]) |
30 | | - intersection_bottom = tf.math.minimum(bboxes1[bbox.BOTTOM], bb2[bbox.BOTTOM]) |
31 | | - |
32 | | - area_intersection = _area( |
33 | | - intersection_left, intersection_right, intersection_top, intersection_bottom |
34 | | - ) |
35 | | - area_bboxes1 = _area( |
36 | | - bb1[bbox.LEFT], bb1[bbox.RIGHT], bb1[bbox.TOP], bb1[bbox.BOTTOM] |
37 | | - ) |
38 | | - area_bb2 = _area( |
39 | | - bb2[bbox.LEFT], bb2[bbox.RIGHT], bb2[bbox.TOP], bb2[bbox.BOTTOM] |
40 | | - ) |
41 | | - |
42 | | - area_union = area_bboxes1 + area_bb2 - area_intersection |
43 | | - return tf.math.divide_no_nan(area_intersection, area_union) |
44 | | - |
45 | 17 |
|
46 | 18 | def compute_ious_for_image(boxes1, boxes2): |
47 | 19 | """computes a lookup table vector containing the ious for a given set boxes. |
48 | 20 |
|
49 | 21 | The lookup vector is to be indexed by [`boxes1_index`,`boxes2_index`]. |
50 | 22 |
|
51 | | - Bounding boxes are expected to be in the corners format of `[bbox.LEFT, bbox.RIGHT, |
52 | | - bbox.TOP, bbox.BOTTOM]`. For example, the bounding box with it's left side at 100, |
53 | | - bbox.RIGHT side at 200, bbox.TOP at 101, and bbox.BOTTOM at 201 would be represented |
54 | | - as: |
| 23 | + Bounding boxes are expected to be in the corners format of |
| 24 | + `[bbox.LEFT, bbox.RIGHT, bbox.TOP, bbox.BOTTOM]`. For example, the bounding box |
| 25 | + with it's left side at 100, bbox.RIGHT side at 200, bbox.TOP at 101, and |
| 26 | + bbox.BOTTOM at 201 would be represented as: |
55 | 27 | > [100, 200, 101, 201] |
56 | 28 |
|
57 | 29 | Args: |
|
0 commit comments