Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
be2a21a6
Commit
be2a21a6
authored
Aug 03, 2021
by
Frederick Liu
Committed by
A. Unique TensorFlower
Aug 03, 2021
Browse files
[vision] Add GIOU
https://giou.stanford.edu/GIoU.pdf
.
PiperOrigin-RevId: 388547681
parent
59e1ab8a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
0 deletions
+70
-0
official/vision/beta/ops/box_ops.py
official/vision/beta/ops/box_ops.py
+70
-0
No files found.
official/vision/beta/ops/box_ops.py
View file @
be2a21a6
...
@@ -624,6 +624,76 @@ def bbox_overlap(boxes, gt_boxes):
...
@@ -624,6 +624,76 @@ def bbox_overlap(boxes, gt_boxes):
return
iou
return
iou
def
bbox_generalized_overlap
(
boxes
,
gt_boxes
):
"""Calculates the GIOU between proposal and ground truth boxes.
The generalized intersection of union is an adjustment of the traditional IOU
metric which provides continuous updates even for predictions with no overlap.
This metric is defined in https://giou.stanford.edu/GIoU.pdf. Note, some
`gt_boxes` may have been padded. The returned `giou` tensor for these boxes
will be -1.
Args:
boxes: a `Tensor` with a shape of [batch_size, N, 4]. N is the number of
proposals before groundtruth assignment (e.g., rpn_post_nms_topn). The
last dimension is the pixel coordinates in [ymin, xmin, ymax, xmax] form.
gt_boxes: a `Tensor` with a shape of [batch_size, max_num_instances, 4].
This tensor may have paddings with a negative value and will also be in
the [ymin, xmin, ymax, xmax] format.
Returns:
giou: a `Tensor` with as a shape of [batch_size, N, max_num_instances].
"""
with
tf
.
name_scope
(
'bbox_generalized_overlap'
):
assert
boxes
.
shape
.
as_list
(
)[
-
1
]
==
4
,
'Boxes must be defined by 4 coordinates.'
assert
gt_boxes
.
shape
.
as_list
(
)[
-
1
]
==
4
,
'Groundtruth boxes must be defined by 4 coordinates.'
bb_y_min
,
bb_x_min
,
bb_y_max
,
bb_x_max
=
tf
.
split
(
value
=
boxes
,
num_or_size_splits
=
4
,
axis
=
2
)
gt_y_min
,
gt_x_min
,
gt_y_max
,
gt_x_max
=
tf
.
split
(
value
=
gt_boxes
,
num_or_size_splits
=
4
,
axis
=
2
)
# Calculates the hull area for each pair of boxes, with one from
# boxes and the other from gt_boxes.
# Outputs for coordinates are of shape [batch_size, N, max_num_instances]
h_xmin
=
tf
.
minimum
(
bb_x_min
,
tf
.
transpose
(
gt_x_min
,
[
0
,
2
,
1
]))
h_xmax
=
tf
.
maximum
(
bb_x_max
,
tf
.
transpose
(
gt_x_max
,
[
0
,
2
,
1
]))
h_ymin
=
tf
.
minimum
(
bb_y_min
,
tf
.
transpose
(
gt_y_min
,
[
0
,
2
,
1
]))
h_ymax
=
tf
.
maximum
(
bb_y_max
,
tf
.
transpose
(
gt_y_max
,
[
0
,
2
,
1
]))
h_area
=
tf
.
maximum
((
h_xmax
-
h_xmin
),
0
)
*
tf
.
maximum
((
h_ymax
-
h_ymin
),
0
)
# Add a small epsilon to avoid divide-by-zero.
h_area
=
h_area
+
1e-8
# Calculates the intersection area.
i_xmin
=
tf
.
maximum
(
bb_x_min
,
tf
.
transpose
(
gt_x_min
,
[
0
,
2
,
1
]))
i_xmax
=
tf
.
minimum
(
bb_x_max
,
tf
.
transpose
(
gt_x_max
,
[
0
,
2
,
1
]))
i_ymin
=
tf
.
maximum
(
bb_y_min
,
tf
.
transpose
(
gt_y_min
,
[
0
,
2
,
1
]))
i_ymax
=
tf
.
minimum
(
bb_y_max
,
tf
.
transpose
(
gt_y_max
,
[
0
,
2
,
1
]))
i_area
=
tf
.
maximum
((
i_xmax
-
i_xmin
),
0
)
*
tf
.
maximum
((
i_ymax
-
i_ymin
),
0
)
# Calculates the union area.
bb_area
=
(
bb_y_max
-
bb_y_min
)
*
(
bb_x_max
-
bb_x_min
)
gt_area
=
(
gt_y_max
-
gt_y_min
)
*
(
gt_x_max
-
gt_x_min
)
# Adds a small epsilon to avoid divide-by-zero.
u_area
=
bb_area
+
tf
.
transpose
(
gt_area
,
[
0
,
2
,
1
])
-
i_area
+
1e-8
# Calculates IoU.
iou
=
i_area
/
u_area
# Calculates GIoU.
giou
=
iou
-
(
h_area
-
u_area
)
/
h_area
# Fills -1 for GIoU entries between the padded ground truth boxes.
gt_invalid_mask
=
tf
.
less
(
tf
.
reduce_max
(
gt_boxes
,
axis
=-
1
,
keepdims
=
True
),
0.0
)
padding_mask
=
tf
.
broadcast_to
(
tf
.
transpose
(
gt_invalid_mask
,
[
0
,
2
,
1
]),
tf
.
shape
(
giou
))
giou
=
tf
.
where
(
padding_mask
,
-
tf
.
ones_like
(
giou
),
giou
)
return
giou
def
box_matching
(
boxes
,
gt_boxes
,
gt_classes
):
def
box_matching
(
boxes
,
gt_boxes
,
gt_classes
):
"""Match boxes to groundtruth boxes.
"""Match boxes to groundtruth boxes.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment