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
f82ade47
Commit
f82ade47
authored
Oct 02, 2020
by
Zhenyu Tan
Committed by
A. Unique TensorFlower
Oct 02, 2020
Browse files
Internal change
PiperOrigin-RevId: 335128787
parent
5a533fd4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
19 deletions
+48
-19
official/vision/beta/modeling/layers/roi_sampler.py
official/vision/beta/modeling/layers/roi_sampler.py
+7
-1
official/vision/keras_cv/ops/iou_similarity.py
official/vision/keras_cv/ops/iou_similarity.py
+41
-18
No files found.
official/vision/beta/modeling/layers/roi_sampler.py
View file @
f82ade47
...
@@ -61,6 +61,7 @@ class ROISampler(tf.keras.layers.Layer):
...
@@ -61,6 +61,7 @@ class ROISampler(tf.keras.layers.Layer):
'background_iou_low_threshold'
:
background_iou_low_threshold
,
'background_iou_low_threshold'
:
background_iou_low_threshold
,
}
}
self
.
_sim_calc
=
keras_cv
.
ops
.
IouSimilarity
()
self
.
_box_matcher
=
keras_cv
.
ops
.
BoxMatcher
(
self
.
_box_matcher
=
keras_cv
.
ops
.
BoxMatcher
(
thresholds
=
[
thresholds
=
[
background_iou_low_threshold
,
background_iou_high_threshold
,
background_iou_low_threshold
,
background_iou_high_threshold
,
...
@@ -114,7 +115,12 @@ class ROISampler(tf.keras.layers.Layer):
...
@@ -114,7 +115,12 @@ class ROISampler(tf.keras.layers.Layer):
gt_boxes
=
tf
.
cast
(
gt_boxes
,
dtype
=
boxes
.
dtype
)
gt_boxes
=
tf
.
cast
(
gt_boxes
,
dtype
=
boxes
.
dtype
)
boxes
=
tf
.
concat
([
boxes
,
gt_boxes
],
axis
=
1
)
boxes
=
tf
.
concat
([
boxes
,
gt_boxes
],
axis
=
1
)
similarity_matrix
=
box_ops
.
bbox_overlap
(
boxes
,
gt_boxes
)
boxes_invalid_mask
=
tf
.
less
(
tf
.
reduce_max
(
boxes
,
axis
=-
1
,
keepdims
=
True
),
0.0
)
gt_invalid_mask
=
tf
.
less
(
tf
.
reduce_max
(
gt_boxes
,
axis
=-
1
,
keepdims
=
True
),
0.0
)
similarity_matrix
=
self
.
_sim_calc
(
boxes
,
gt_boxes
,
boxes_invalid_mask
,
gt_invalid_mask
)
matched_gt_indices
,
match_indicators
=
self
.
_box_matcher
(
similarity_matrix
)
matched_gt_indices
,
match_indicators
=
self
.
_box_matcher
(
similarity_matrix
)
positive_matches
=
tf
.
greater_equal
(
match_indicators
,
0
)
positive_matches
=
tf
.
greater_equal
(
match_indicators
,
0
)
negative_matches
=
tf
.
equal
(
match_indicators
,
-
1
)
negative_matches
=
tf
.
equal
(
match_indicators
,
-
1
)
...
...
official/vision/keras_cv/ops/iou_similarity.py
View file @
f82ade47
...
@@ -97,12 +97,15 @@ def iou(gt_boxes, boxes):
...
@@ -97,12 +97,15 @@ def iou(gt_boxes, boxes):
tf
.
truediv
(
intersections
,
unions
))
tf
.
truediv
(
intersections
,
unions
))
class
IouSimilarity
()
:
class
IouSimilarity
:
"""Class to compute similarity based on Intersection over Union (IOU) metric.
"""Class to compute similarity based on Intersection over Union (IOU) metric.
"""
"""
def
__call__
(
self
,
groundtruth_boxes
,
anchors
):
def
__init__
(
self
,
mask_val
=-
1
):
self
.
mask_val
=
mask_val
def
__call__
(
self
,
boxes_1
,
boxes_2
,
boxes_1_masks
=
None
,
boxes_2_masks
=
None
):
"""Compute pairwise IOU similarity between ground truth boxes and anchors.
"""Compute pairwise IOU similarity between ground truth boxes and anchors.
B: batch_size
B: batch_size
...
@@ -110,32 +113,52 @@ class IouSimilarity():
...
@@ -110,32 +113,52 @@ class IouSimilarity():
M: Number of anchor boxes.
M: Number of anchor boxes.
Args:
Args:
groundtruth_boxes: a float Tensor with M boxes.
boxes_1: a float Tensor with M or B * M boxes.
anchors: a float Tensor with N boxes.
boxes_2: a float Tensor with N or B * N boxes, the rank must be less than
or equal to rank of `boxes_1`.
boxes_1_masks: a boolean Tensor with M or B * M boxes. Optional.
boxes_2_masks: a boolean Tensor with N or B * N boxes. Optional.
Returns:
Returns:
A Tensor with shape [M, N] or [B, M, N] representing pairwise
A Tensor with shape [M, N] or [B, M, N] representing pairwise
iou scores, anchor per row and groundtruth_box per colulmn.
iou scores, anchor per row and groundtruth_box per colulmn.
Input shape:
Input shape:
groundtruth_boxes: [N, 4], or [B, N, 4]
boxes_1: [N, 4], or [B, N, 4]
anchors: [M, 4], or [B, M, 4]
boxes_2: [M, 4], or [B, M, 4]
boxes_1_masks: [N, 1], or [B, N, 1]
boxes_2_masks: [M, 1], or [B, M, 1]
Output shape:
Output shape:
[M, N], or [B, M, N]
[M, N], or [B, M, N]
"""
"""
groundtruth
_rank
=
len
(
groundtruth_
boxes
.
shape
)
boxes_1
_rank
=
len
(
boxes
_1
.
shape
)
anchor
_rank
=
len
(
anchors
.
shape
)
boxes_2
_rank
=
len
(
boxes_2
.
shape
)
if
groundtruth
_rank
<
2
or
groundtruth
_rank
>
3
:
if
boxes_1
_rank
<
2
or
boxes_1
_rank
>
3
:
raise
ValueError
(
'`groudtruth_boxes` must be rank 2 or 3, got {}'
.
format
(
raise
ValueError
(
grou
n
dtruth_rank
))
'`
groudtruth_
boxes` must be rank 2 or 3, got {}'
.
format
(
boxes_1_
rank
))
if
anchor
_rank
<
2
or
anchor
_rank
>
3
:
if
boxes_2
_rank
<
2
or
boxes_2
_rank
>
3
:
raise
ValueError
(
'`anchors` must be rank 2 or 3, got {}'
.
format
(
raise
ValueError
(
anchor_rank
))
'`
anchor
s` must be rank 2 or 3, got {}'
.
format
(
boxes_2
_rank
))
if
groundtruth
_rank
<
anchor
_rank
:
if
boxes_1
_rank
<
boxes_2
_rank
:
raise
ValueError
(
'`groundtruth_boxes` is unbatched while `anchors` is '
raise
ValueError
(
'`groundtruth_boxes` is unbatched while `anchors` is '
'batched is not a valid use case, got groundtruth_box '
'batched is not a valid use case, got groundtruth_box '
'rank {}, and anchors rank {}'
.
format
(
'rank {}, and anchors rank {}'
.
format
(
groundtruth_rank
,
anchor_rank
))
boxes_1_rank
,
boxes_2_rank
))
return
iou
(
groundtruth_boxes
,
anchors
)
result
=
iou
(
boxes_1
,
boxes_2
)
if
boxes_1_masks
is
None
and
boxes_2_masks
is
None
:
return
result
background_mask
=
None
mask_val_t
=
tf
.
cast
(
self
.
mask_val
,
result
.
dtype
)
*
tf
.
ones_like
(
result
)
perm
=
[
1
,
0
]
if
boxes_2_rank
==
2
else
[
0
,
2
,
1
]
if
boxes_1_masks
is
not
None
and
boxes_2_masks
is
not
None
:
background_mask
=
tf
.
logical_or
(
boxes_1_masks
,
tf
.
transpose
(
boxes_2_masks
,
perm
))
elif
boxes_1_masks
is
not
None
:
background_mask
=
boxes_1_masks
else
:
background_mask
=
tf
.
logical_or
(
tf
.
zeros
(
tf
.
shape
(
boxes_2
)[:
-
1
],
dtype
=
tf
.
bool
),
tf
.
transpose
(
boxes_2_masks
,
perm
))
return
tf
.
where
(
background_mask
,
mask_val_t
,
result
)
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