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
c5763f44
Commit
c5763f44
authored
Apr 18, 2022
by
A. Unique TensorFlower
Browse files
Internal change
PiperOrigin-RevId: 442678068
parent
a97432c9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
31 deletions
+30
-31
official/vision/utils/object_detection/box_list_ops.py
official/vision/utils/object_detection/box_list_ops.py
+30
-31
No files found.
official/vision/utils/object_detection/box_list_ops.py
View file @
c5763f44
...
...
@@ -54,7 +54,7 @@ def area(boxlist, scope=None):
Returns:
a tensor with shape [N] representing box areas.
"""
with
tf
.
name_scope
(
scope
,
'Area'
):
with
tf
.
name_scope
(
scope
or
'Area'
):
y_min
,
x_min
,
y_max
,
x_max
=
tf
.
split
(
value
=
boxlist
.
get
(),
num_or_size_splits
=
4
,
axis
=
1
)
return
tf
.
squeeze
((
y_max
-
y_min
)
*
(
x_max
-
x_min
),
[
1
])
...
...
@@ -71,7 +71,7 @@ def height_width(boxlist, scope=None):
Height: A tensor with shape [N] representing box heights.
Width: A tensor with shape [N] representing box widths.
"""
with
tf
.
name_scope
(
scope
,
'HeightWidth'
):
with
tf
.
name_scope
(
scope
or
'HeightWidth'
):
y_min
,
x_min
,
y_max
,
x_max
=
tf
.
split
(
value
=
boxlist
.
get
(),
num_or_size_splits
=
4
,
axis
=
1
)
return
tf
.
squeeze
(
y_max
-
y_min
,
[
1
]),
tf
.
squeeze
(
x_max
-
x_min
,
[
1
])
...
...
@@ -89,7 +89,7 @@ def scale(boxlist, y_scale, x_scale, scope=None):
Returns:
boxlist: BoxList holding N boxes
"""
with
tf
.
name_scope
(
scope
,
'Scale'
):
with
tf
.
name_scope
(
scope
or
'Scale'
):
y_scale
=
tf
.
cast
(
y_scale
,
tf
.
float32
)
x_scale
=
tf
.
cast
(
x_scale
,
tf
.
float32
)
y_min
,
x_min
,
y_max
,
x_max
=
tf
.
split
(
...
...
@@ -121,7 +121,7 @@ def clip_to_window(boxlist, window, filter_nonoverlapping=True, scope=None):
Returns:
a BoxList holding M_out boxes where M_out <= M_in
"""
with
tf
.
name_scope
(
scope
,
'ClipToWindow'
):
with
tf
.
name_scope
(
scope
or
'ClipToWindow'
):
y_min
,
x_min
,
y_max
,
x_max
=
tf
.
split
(
value
=
boxlist
.
get
(),
num_or_size_splits
=
4
,
axis
=
1
)
win_y_min
,
win_x_min
,
win_y_max
,
win_x_max
=
tf
.
unstack
(
window
)
...
...
@@ -160,7 +160,7 @@ def prune_outside_window(boxlist, window, scope=None):
valid_indices: a tensor with shape [M_out] indexing the valid bounding boxes
in the input tensor.
"""
with
tf
.
name_scope
(
scope
,
'PruneOutsideWindow'
):
with
tf
.
name_scope
(
scope
or
'PruneOutsideWindow'
):
y_min
,
x_min
,
y_max
,
x_max
=
tf
.
split
(
value
=
boxlist
.
get
(),
num_or_size_splits
=
4
,
axis
=
1
)
win_y_min
,
win_x_min
,
win_y_max
,
win_x_max
=
tf
.
unstack
(
window
)
...
...
@@ -194,7 +194,7 @@ def prune_completely_outside_window(boxlist, window, scope=None):
valid_indices: a tensor with shape [M_out] indexing the valid bounding boxes
in the input tensor.
"""
with
tf
.
name_scope
(
scope
,
'PruneCompleteleyOutsideWindow'
):
with
tf
.
name_scope
(
scope
or
'PruneCompleteleyOutsideWindow'
):
y_min
,
x_min
,
y_max
,
x_max
=
tf
.
split
(
value
=
boxlist
.
get
(),
num_or_size_splits
=
4
,
axis
=
1
)
win_y_min
,
win_x_min
,
win_y_max
,
win_x_max
=
tf
.
unstack
(
window
)
...
...
@@ -220,7 +220,7 @@ def intersection(boxlist1, boxlist2, scope=None):
Returns:
a tensor with shape [N, M] representing pairwise intersections
"""
with
tf
.
name_scope
(
scope
,
'Intersection'
):
with
tf
.
name_scope
(
scope
or
'Intersection'
):
y_min1
,
x_min1
,
y_max1
,
x_max1
=
tf
.
split
(
value
=
boxlist1
.
get
(),
num_or_size_splits
=
4
,
axis
=
1
)
y_min2
,
x_min2
,
y_max2
,
x_max2
=
tf
.
split
(
...
...
@@ -245,7 +245,7 @@ def matched_intersection(boxlist1, boxlist2, scope=None):
Returns:
a tensor with shape [N] representing pairwise intersections
"""
with
tf
.
name_scope
(
scope
,
'MatchedIntersection'
):
with
tf
.
name_scope
(
scope
or
'MatchedIntersection'
):
y_min1
,
x_min1
,
y_max1
,
x_max1
=
tf
.
split
(
value
=
boxlist1
.
get
(),
num_or_size_splits
=
4
,
axis
=
1
)
y_min2
,
x_min2
,
y_max2
,
x_max2
=
tf
.
split
(
...
...
@@ -270,7 +270,7 @@ def iou(boxlist1, boxlist2, scope=None):
Returns:
a tensor with shape [N, M] representing pairwise iou scores.
"""
with
tf
.
name_scope
(
scope
,
'IOU'
):
with
tf
.
name_scope
(
scope
or
'IOU'
):
intersections
=
intersection
(
boxlist1
,
boxlist2
)
areas1
=
area
(
boxlist1
)
areas2
=
area
(
boxlist2
)
...
...
@@ -292,7 +292,7 @@ def matched_iou(boxlist1, boxlist2, scope=None):
Returns:
a tensor with shape [N] representing pairwise iou scores.
"""
with
tf
.
name_scope
(
scope
,
'MatchedIOU'
):
with
tf
.
name_scope
(
scope
or
'MatchedIOU'
):
intersections
=
matched_intersection
(
boxlist1
,
boxlist2
)
areas1
=
area
(
boxlist1
)
areas2
=
area
(
boxlist2
)
...
...
@@ -317,7 +317,7 @@ def ioa(boxlist1, boxlist2, scope=None):
Returns:
a tensor with shape [N, M] representing pairwise ioa scores.
"""
with
tf
.
name_scope
(
scope
,
'IOA'
):
with
tf
.
name_scope
(
scope
or
'IOA'
):
intersections
=
intersection
(
boxlist1
,
boxlist2
)
areas
=
tf
.
expand_dims
(
area
(
boxlist2
),
0
)
return
tf
.
truediv
(
intersections
,
areas
)
...
...
@@ -344,7 +344,7 @@ def prune_non_overlapping_boxes(boxlist1,
keep_inds: A tensor with shape [N'] indexing kept bounding boxes in the
first input BoxList `boxlist1`.
"""
with
tf
.
name_scope
(
scope
,
'PruneNonOverlappingBoxes'
):
with
tf
.
name_scope
(
scope
or
'PruneNonOverlappingBoxes'
):
ioa_
=
ioa
(
boxlist2
,
boxlist1
)
# [M, N] tensor
ioa_
=
tf
.
reduce_max
(
ioa_
,
reduction_indices
=
[
0
])
# [N] tensor
keep_bool
=
tf
.
greater_equal
(
ioa_
,
tf
.
constant
(
min_overlap
))
...
...
@@ -364,7 +364,7 @@ def prune_small_boxes(boxlist, min_side, scope=None):
Returns:
A pruned boxlist.
"""
with
tf
.
name_scope
(
scope
,
'PruneSmallBoxes'
):
with
tf
.
name_scope
(
scope
or
'PruneSmallBoxes'
):
height
,
width
=
height_width
(
boxlist
)
is_valid
=
tf
.
logical_and
(
tf
.
greater_equal
(
width
,
min_side
),
tf
.
greater_equal
(
height
,
min_side
))
...
...
@@ -391,7 +391,7 @@ def change_coordinate_frame(boxlist, window, scope=None):
Returns:
Returns a BoxList object with N boxes.
"""
with
tf
.
name_scope
(
scope
,
'ChangeCoordinateFrame'
):
with
tf
.
name_scope
(
scope
or
'ChangeCoordinateFrame'
):
win_height
=
window
[
2
]
-
window
[
0
]
win_width
=
window
[
3
]
-
window
[
1
]
boxlist_new
=
scale
(
...
...
@@ -423,7 +423,7 @@ def sq_dist(boxlist1, boxlist2, scope=None):
Returns:
a tensor with shape [N, M] representing pairwise distances
"""
with
tf
.
name_scope
(
scope
,
'SqDist'
):
with
tf
.
name_scope
(
scope
or
'SqDist'
):
sqnorm1
=
tf
.
reduce_sum
(
tf
.
square
(
boxlist1
.
get
()),
1
,
keep_dims
=
True
)
sqnorm2
=
tf
.
reduce_sum
(
tf
.
square
(
boxlist2
.
get
()),
1
,
keep_dims
=
True
)
innerprod
=
tf
.
matmul
(
...
...
@@ -463,7 +463,7 @@ def boolean_mask(boxlist,
Raises:
ValueError: if `indicator` is not a rank-1 boolean tensor.
"""
with
tf
.
name_scope
(
scope
,
'BooleanMask'
):
with
tf
.
name_scope
(
scope
or
'BooleanMask'
):
if
indicator
.
shape
.
ndims
!=
1
:
raise
ValueError
(
'indicator should have rank 1'
)
if
indicator
.
dtype
!=
tf
.
bool
:
...
...
@@ -521,7 +521,7 @@ def gather(boxlist, indices, fields=None, scope=None, use_static_shapes=False):
ValueError: if specified field is not contained in boxlist or if the
indices are not of type int32
"""
with
tf
.
name_scope
(
scope
,
'Gather'
):
with
tf
.
name_scope
(
scope
or
'Gather'
):
if
len
(
indices
.
shape
.
as_list
())
!=
1
:
raise
ValueError
(
'indices should have rank 1'
)
if
indices
.
dtype
!=
tf
.
int32
and
indices
.
dtype
!=
tf
.
int64
:
...
...
@@ -562,7 +562,7 @@ def concatenate(boxlists, fields=None, scope=None):
contains non BoxList objects), or if requested fields are not contained in
all boxlists
"""
with
tf
.
name_scope
(
scope
,
'Concatenate'
):
with
tf
.
name_scope
(
scope
or
'Concatenate'
):
if
not
isinstance
(
boxlists
,
list
):
raise
ValueError
(
'boxlists should be a list'
)
if
not
boxlists
:
...
...
@@ -612,7 +612,7 @@ def sort_by_field(boxlist, field, order=SortOrder.descend, scope=None):
ValueError: if specified field does not exist
ValueError: if the order is not either descend or ascend
"""
with
tf
.
name_scope
(
scope
,
'SortByField'
):
with
tf
.
name_scope
(
scope
or
'SortByField'
):
if
order
!=
SortOrder
.
descend
and
order
!=
SortOrder
.
ascend
:
raise
ValueError
(
'Invalid sort order'
)
...
...
@@ -653,7 +653,7 @@ def visualize_boxes_in_image(image, boxlist, normalized=False, scope=None):
Returns:
image_and_boxes: an image tensor with shape [height, width, 3]
"""
with
tf
.
name_scope
(
scope
,
'VisualizeBoxesInImage'
):
with
tf
.
name_scope
(
scope
or
'VisualizeBoxesInImage'
):
if
not
normalized
:
height
,
width
,
_
=
tf
.
unstack
(
tf
.
shape
(
image
))
boxlist
=
scale
(
boxlist
,
1.0
/
tf
.
cast
(
height
,
tf
.
float32
),
...
...
@@ -679,7 +679,7 @@ def filter_field_value_equals(boxlist, field, value, scope=None):
ValueError: if boxlist not a BoxList object or if it does not have
the specified field.
"""
with
tf
.
name_scope
(
scope
,
'FilterFieldValueEquals'
):
with
tf
.
name_scope
(
scope
or
'FilterFieldValueEquals'
):
if
not
isinstance
(
boxlist
,
box_list
.
BoxList
):
raise
ValueError
(
'boxlist must be a BoxList'
)
if
not
boxlist
.
has_field
(
field
):
...
...
@@ -710,7 +710,7 @@ def filter_greater_than(boxlist, thresh, scope=None):
ValueError: if boxlist not a BoxList object or if it does not
have a scores field
"""
with
tf
.
name_scope
(
scope
,
'FilterGreaterThan'
):
with
tf
.
name_scope
(
scope
or
'FilterGreaterThan'
):
if
not
isinstance
(
boxlist
,
box_list
.
BoxList
):
raise
ValueError
(
'boxlist must be a BoxList'
)
if
not
boxlist
.
has_field
(
'scores'
):
...
...
@@ -746,7 +746,7 @@ def non_max_suppression(boxlist, thresh, max_output_size, scope=None):
Raises:
ValueError: if thresh is not in [0, 1]
"""
with
tf
.
name_scope
(
scope
,
'NonMaxSuppression'
):
with
tf
.
name_scope
(
scope
or
'NonMaxSuppression'
):
if
not
0
<=
thresh
<=
1.0
:
raise
ValueError
(
'thresh must be between 0 and 1'
)
if
not
isinstance
(
boxlist
,
box_list
.
BoxList
):
...
...
@@ -802,7 +802,7 @@ def to_normalized_coordinates(boxlist,
Returns:
boxlist with normalized coordinates in [0, 1].
"""
with
tf
.
name_scope
(
scope
,
'ToNormalizedCoordinates'
):
with
tf
.
name_scope
(
scope
or
'ToNormalizedCoordinates'
):
height
=
tf
.
cast
(
height
,
tf
.
float32
)
width
=
tf
.
cast
(
width
,
tf
.
float32
)
...
...
@@ -842,7 +842,7 @@ def to_absolute_coordinates(boxlist,
boxlist with absolute coordinates in terms of the image size.
"""
with
tf
.
name_scope
(
scope
,
'ToAbsoluteCoordinates'
):
with
tf
.
name_scope
(
scope
or
'ToAbsoluteCoordinates'
):
height
=
tf
.
cast
(
height
,
tf
.
float32
)
width
=
tf
.
cast
(
width
,
tf
.
float32
)
...
...
@@ -987,10 +987,9 @@ def box_voting(selected_boxes, pool_boxes, iou_thresh=0.5):
# match to any boxes in pool_boxes. For such boxes without any matches, we
# should return the original boxes without voting.
match_assert
=
tf
.
Assert
(
tf
.
reduce_all
(
tf
.
greater
(
num_matches
,
0
)),
[
'Each box in selected_boxes must match with at least one box '
'in pool_boxes.'
])
tf
.
reduce_all
(
tf
.
greater
(
num_matches
,
0
)),
'Each box in selected_boxes must match with at least one box '
'in pool_boxes.'
)
scores
=
tf
.
expand_dims
(
pool_boxes
.
get_field
(
'scores'
),
1
)
scores_assert
=
tf
.
Assert
(
...
...
@@ -1024,7 +1023,7 @@ def get_minimal_coverage_box(boxlist, default_box=None, scope=None):
boxes in the box list. If the boxlist does not contain any boxes, the
default box is returned.
"""
with
tf
.
name_scope
(
scope
,
'CreateCoverageBox'
):
with
tf
.
name_scope
(
scope
or
'CreateCoverageBox'
):
num_boxes
=
boxlist
.
num_boxes
()
def
coverage_box
(
bboxes
):
...
...
@@ -1068,7 +1067,7 @@ def sample_boxes_by_jittering(boxlist,
sampled_boxlist: A boxlist containing num_boxes_to_sample boxes in
normalized coordinates.
"""
with
tf
.
name_scope
(
scope
,
'SampleBoxesByJittering'
):
with
tf
.
name_scope
(
scope
or
'SampleBoxesByJittering'
):
num_boxes
=
boxlist
.
num_boxes
()
box_indices
=
tf
.
random_uniform
([
num_boxes_to_sample
],
minval
=
0
,
...
...
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