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
c594cecf
Commit
c594cecf
authored
Jul 24, 2020
by
Kaushik Shivakumar
Browse files
pr
parent
c99e5783
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
5 deletions
+61
-5
research/object_detection/core/box_list_ops_test.py
research/object_detection/core/box_list_ops_test.py
+25
-1
research/object_detection/core/losses.py
research/object_detection/core/losses.py
+36
-4
No files found.
research/object_detection/core/box_list_ops_test.py
View file @
c594cecf
...
@@ -217,7 +217,6 @@ class BoxListOpsTest(test_case.TestCase):
...
@@ -217,7 +217,6 @@ class BoxListOpsTest(test_case.TestCase):
def
test_iou
(
self
):
def
test_iou
(
self
):
def
graph_fn
():
def
graph_fn
():
corners1
=
tf
.
constant
([[
4.0
,
3.0
,
7.0
,
5.0
],
[
5.0
,
6.0
,
10.0
,
7.0
]])
corners1
=
tf
.
constant
([[
4.0
,
3.0
,
7.0
,
5.0
],
[
5.0
,
6.0
,
10.0
,
7.0
]])
corners1
=
tf
.
constant
([[
4.0
,
3.0
,
7.0
,
5.0
],
[
5.0
,
6.0
,
10.0
,
7.0
]])
corners2
=
tf
.
constant
([[
3.0
,
4.0
,
6.0
,
8.0
],
[
14.0
,
14.0
,
15.0
,
15.0
],
corners2
=
tf
.
constant
([[
3.0
,
4.0
,
6.0
,
8.0
],
[
14.0
,
14.0
,
15.0
,
15.0
],
[
0.0
,
0.0
,
20.0
,
20.0
]])
[
0.0
,
0.0
,
20.0
,
20.0
]])
...
@@ -229,6 +228,31 @@ class BoxListOpsTest(test_case.TestCase):
...
@@ -229,6 +228,31 @@ class BoxListOpsTest(test_case.TestCase):
iou_output
=
self
.
execute
(
graph_fn
,
[])
iou_output
=
self
.
execute
(
graph_fn
,
[])
self
.
assertAllClose
(
iou_output
,
exp_output
)
self
.
assertAllClose
(
iou_output
,
exp_output
)
def
test_l1
(
self
):
def
graph_fn
():
corners1
=
tf
.
constant
([[
4.0
,
3.0
,
7.0
,
5.0
],
[
5.0
,
6.0
,
10.0
,
7.0
]])
corners2
=
tf
.
constant
([[
3.0
,
4.0
,
6.0
,
8.0
],
[
14.0
,
14.0
,
15.0
,
15.0
],
[
0.0
,
0.0
,
20.0
,
20.0
]])
boxes1
=
box_list
.
BoxList
(
corners1
)
boxes2
=
box_list
.
BoxList
(
corners2
)
l1
=
box_list_ops
.
l1
(
boxes1
,
boxes2
)
return
l1
exp_output
=
[[
5.0
,
22.5
,
45.5
],
[
8.5
,
19.0
,
40.0
]]
l1_output
=
self
.
execute
(
graph_fn
,
[])
self
.
assertAllClose
(
l1_output
,
exp_output
)
def
test_giou
(
self
):
def
graph_fn
():
corners1
=
tf
.
constant
([[
5.0
,
7.0
,
7.0
,
9.0
]])
corners2
=
tf
.
constant
([[
5.0
,
7.0
,
7.0
,
9.0
],
[
5.0
,
11.0
,
7.0
,
13.0
]])
boxes1
=
box_list
.
BoxList
(
corners1
)
boxes2
=
box_list
.
BoxList
(
corners2
)
giou
=
box_list_ops
.
giou_loss
(
boxes1
,
boxes2
)
return
giou
exp_output
=
[[
0.0
,
4.0
/
3.0
]]
giou_output
=
self
.
execute
(
graph_fn
,
[])
self
.
assertAllClose
(
giou_output
,
exp_output
)
def
test_matched_iou
(
self
):
def
test_matched_iou
(
self
):
def
graph_fn
():
def
graph_fn
():
corners1
=
tf
.
constant
([[
4.0
,
3.0
,
7.0
,
5.0
],
[
5.0
,
6.0
,
10.0
,
7.0
]])
corners1
=
tf
.
constant
([[
4.0
,
3.0
,
7.0
,
5.0
],
[
5.0
,
6.0
,
10.0
,
7.0
]])
...
...
research/object_detection/core/losses.py
View file @
c594cecf
...
@@ -36,7 +36,8 @@ import tensorflow.compat.v1 as tf
...
@@ -36,7 +36,8 @@ import tensorflow.compat.v1 as tf
from
object_detection.core
import
box_list
from
object_detection.core
import
box_list
from
object_detection.core
import
box_list_ops
from
object_detection.core
import
box_list_ops
from
object_detection.utils
import
ops
from
object_detection.utils
import
ops
from
object_detection.box_coders
import
detr_box_coder
import
tensorflow_addons
as
tfa
class
Loss
(
six
.
with_metaclass
(
abc
.
ABCMeta
,
object
)):
class
Loss
(
six
.
with_metaclass
(
abc
.
ABCMeta
,
object
)):
"""Abstract base class for loss functions."""
"""Abstract base class for loss functions."""
...
@@ -180,7 +181,6 @@ class WeightedSmoothL1LocalizationLoss(Loss):
...
@@ -180,7 +181,6 @@ class WeightedSmoothL1LocalizationLoss(Loss):
reduction
=
tf
.
losses
.
Reduction
.
NONE
reduction
=
tf
.
losses
.
Reduction
.
NONE
),
axis
=
2
)
),
axis
=
2
)
class
WeightedIOULocalizationLoss
(
Loss
):
class
WeightedIOULocalizationLoss
(
Loss
):
"""IOU localization loss function.
"""IOU localization loss function.
...
@@ -203,12 +203,44 @@ class WeightedIOULocalizationLoss(Loss):
...
@@ -203,12 +203,44 @@ class WeightedIOULocalizationLoss(Loss):
loss: a float tensor of shape [batch_size, num_anchors] tensor
loss: a float tensor of shape [batch_size, num_anchors] tensor
representing the value of the loss function.
representing the value of the loss function.
"""
"""
predicted_boxes
=
box_list
.
BoxList
(
tf
.
reshape
(
prediction_tensor
,
[
-
1
,
4
]))
predicted_boxes
=
prediction_tensor
#
box_list.BoxList(tf.reshape(prediction_tensor, [-1, 4]))
target_boxes
=
box_list
.
BoxList
(
tf
.
reshape
(
target_tensor
,
[
-
1
,
4
]))
target_boxes
=
target_tensor
#
box_list.BoxList(tf.reshape(target_tensor, [-1, 4]))
per_anchor_iou_loss
=
1.0
-
box_list_ops
.
matched_iou
(
predicted_boxes
,
per_anchor_iou_loss
=
1.0
-
box_list_ops
.
matched_iou
(
predicted_boxes
,
target_boxes
)
target_boxes
)
print
(
"Weights"
,
weights
)
return
tf
.
reshape
(
weights
,
[
-
1
])
*
per_anchor_iou_loss
return
tf
.
reshape
(
weights
,
[
-
1
])
*
per_anchor_iou_loss
class
WeightedGIOULocalizationLoss
(
Loss
):
"""IOU localization loss function.
Sums the IOU for corresponding pairs of predicted/groundtruth boxes
and for each pair assign a loss of 1 - IOU. We then compute a weighted
sum over all pairs which is returned as the total loss.
"""
def
_compute_loss
(
self
,
prediction_tensor
,
target_tensor
,
weights
):
"""Compute loss function.
Args:
prediction_tensor: A float tensor of shape [batch_size, num_anchors, 4]
representing the decoded predicted boxes
target_tensor: A float tensor of shape [batch_size, num_anchors, 4]
representing the decoded target boxes
weights: a float tensor of shape [batch_size, num_anchors]
Returns:
loss: a float tensor of shape [batch_size, num_anchors] tensor
representing the value of the loss function.
"""
predicted_boxes
=
prediction_tensor
# box_list.BoxList(tf.reshape(prediction_tensor, [-1, 4]))
target_boxes
=
target_tensor
#box_list.BoxList(tf.reshape(target_tensor, [-1, 4]))
#loss_function = tfa.losses.GIouLoss()
per_anchor_iou_loss
=
tfa
.
losses
.
giou_loss
(
predicted_boxes
,
target_boxes
)
# #1.0 - box_list_ops.matched_iou(predicted_boxes,
# target_boxes)
#print("Weights", weights)
return
tf
.
reshape
(
weights
,
[
-
1
])
*
per_anchor_iou_loss
# * per_anchor_iou_loss
class
WeightedSigmoidClassificationLoss
(
Loss
):
class
WeightedSigmoidClassificationLoss
(
Loss
):
"""Sigmoid cross entropy classification loss function."""
"""Sigmoid cross entropy classification loss function."""
...
...
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