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
ceb406bb
Commit
ceb406bb
authored
Jul 27, 2020
by
Kaushik Shivakumar
Browse files
ready pr
parent
9bd3fe6d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
153 additions
and
12 deletions
+153
-12
research/object_detection/core/losses.py
research/object_detection/core/losses.py
+13
-12
research/object_detection/core/losses_test.py
research/object_detection/core/losses_test.py
+38
-0
research/object_detection/utils/ops.py
research/object_detection/utils/ops.py
+63
-0
research/object_detection/utils/ops_test.py
research/object_detection/utils/ops_test.py
+39
-0
No files found.
research/object_detection/core/losses.py
View file @
ceb406bb
...
@@ -36,7 +36,7 @@ import tensorflow.compat.v1 as tf
...
@@ -36,7 +36,7 @@ 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
from
object_detection.
utils
import
shape_utils
import
tensorflow_addons
as
tfa
import
tensorflow_addons
as
tfa
class
Loss
(
six
.
with_metaclass
(
abc
.
ABCMeta
,
object
)):
class
Loss
(
six
.
with_metaclass
(
abc
.
ABCMeta
,
object
)):
...
@@ -203,11 +203,10 @@ class WeightedIOULocalizationLoss(Loss):
...
@@ -203,11 +203,10 @@ 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
=
prediction_tensor
#
box_list.BoxList(tf.reshape(prediction_tensor, [-1, 4]))
predicted_boxes
=
box_list
.
BoxList
(
tf
.
reshape
(
prediction_tensor
,
[
-
1
,
4
]))
target_boxes
=
target_tensor
#
box_list.BoxList(tf.reshape(target_tensor, [-1, 4]))
target_boxes
=
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
):
class
WeightedGIOULocalizationLoss
(
Loss
):
...
@@ -232,14 +231,16 @@ class WeightedGIOULocalizationLoss(Loss):
...
@@ -232,14 +231,16 @@ class WeightedGIOULocalizationLoss(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
=
prediction_tensor
# box_list.BoxList(tf.reshape(prediction_tensor, [-1, 4]))
batch_size
,
num_anchors
,
_
=
shape_utils
.
combined_static_and_dynamic_shape
(
target_boxes
=
target_tensor
#box_list.BoxList(tf.reshape(target_tensor, [-1, 4]))
prediction_tensor
)
#loss_function = tfa.losses.GIouLoss()
predicted_boxes
=
ops
.
cy_cx_h_w_to_ymin_xmin_ymax_xmax_coords
(
per_anchor_iou_loss
=
tfa
.
losses
.
giou_loss
(
predicted_boxes
,
target_boxes
)
tf
.
reshape
(
prediction_tensor
,
[
-
1
,
4
]))
# #1.0 - box_list_ops.matched_iou(predicted_boxes,
target_boxes
=
ops
.
cy_cx_h_w_to_ymin_xmin_ymax_xmax_coords
(
# target_boxes)
tf
.
reshape
(
target_tensor
,
[
-
1
,
4
]))
#print("Weights", weights)
return
tf
.
reshape
(
weights
,
[
-
1
])
*
per_anchor_iou_loss
per_anchor_iou_loss
=
1
-
ops
.
giou
(
predicted_boxes
,
target_boxes
)
return
tf
.
reshape
(
tf
.
reshape
(
weights
,
[
-
1
])
*
per_anchor_iou_loss
,
[
batch_size
,
num_anchors
])
class
WeightedSigmoidClassificationLoss
(
Loss
):
class
WeightedSigmoidClassificationLoss
(
Loss
):
...
...
research/object_detection/core/losses_test.py
View file @
ceb406bb
...
@@ -197,6 +197,44 @@ class WeightedIOULocalizationLossTest(test_case.TestCase):
...
@@ -197,6 +197,44 @@ class WeightedIOULocalizationLossTest(test_case.TestCase):
loss_output
=
self
.
execute
(
graph_fn
,
[])
loss_output
=
self
.
execute
(
graph_fn
,
[])
self
.
assertAllClose
(
loss_output
,
exp_loss
)
self
.
assertAllClose
(
loss_output
,
exp_loss
)
class
WeightedGIOULocalizationLossTest
(
test_case
.
TestCase
):
def
testReturnsCorrectLoss
(
self
):
def
graph_fn
():
prediction_tensor
=
tf
.
constant
([[[
1.5
,
0
,
2.4
,
1
],
[
0
,
0
,
1
,
1
],
[
0
,
0
,
0
,
0
]]])
target_tensor
=
tf
.
constant
([[[
1.5
,
0
,
2.4
,
1
],
[
0
,
0
,
1
,
1
],
[
7.5
,
7.5
,
2.5
,
2.5
]]])
weights
=
[[
1.0
,
.
5
,
2.0
]]
loss_op
=
losses
.
WeightedGIOULocalizationLoss
()
loss
=
loss_op
(
prediction_tensor
,
target_tensor
,
weights
=
weights
)
loss
=
tf
.
reduce_sum
(
loss
)
return
loss
exp_loss
=
3.5
loss_output
=
self
.
execute
(
graph_fn
,
[])
self
.
assertAllClose
(
loss_output
,
exp_loss
)
def
testReturnsCorrectLossWithNoLabels
(
self
):
def
graph_fn
():
prediction_tensor
=
tf
.
constant
([[[
1.5
,
0
,
2.4
,
1
],
[
0
,
0
,
1
,
1
],
[
0
,
0
,
.
5
,
.
25
]]])
target_tensor
=
tf
.
constant
([[[
1.5
,
0
,
2.4
,
1
],
[
0
,
0
,
1
,
1
],
[
50
,
50
,
500.5
,
100.25
]]])
weights
=
[[
1.0
,
.
5
,
2.0
]]
losses_mask
=
tf
.
constant
([
False
],
tf
.
bool
)
loss_op
=
losses
.
WeightedGIOULocalizationLoss
()
loss
=
loss_op
(
prediction_tensor
,
target_tensor
,
weights
=
weights
,
losses_mask
=
losses_mask
)
loss
=
tf
.
reduce_sum
(
loss
)
return
loss
exp_loss
=
0.0
loss_output
=
self
.
execute
(
graph_fn
,
[])
self
.
assertAllClose
(
loss_output
,
exp_loss
)
class
WeightedSigmoidClassificationLossTest
(
test_case
.
TestCase
):
class
WeightedSigmoidClassificationLossTest
(
test_case
.
TestCase
):
...
...
research/object_detection/utils/ops.py
View file @
ceb406bb
...
@@ -1134,3 +1134,66 @@ def decode_image(tensor_dict):
...
@@ -1134,3 +1134,66 @@ def decode_image(tensor_dict):
tensor_dict
[
fields
.
InputDataFields
.
image
],
channels
=
3
)
tensor_dict
[
fields
.
InputDataFields
.
image
],
channels
=
3
)
tensor_dict
[
fields
.
InputDataFields
.
image
].
set_shape
([
None
,
None
,
3
])
tensor_dict
[
fields
.
InputDataFields
.
image
].
set_shape
([
None
,
None
,
3
])
return
tensor_dict
return
tensor_dict
def
giou
(
boxes1
,
boxes2
):
"""
Computes generalized IOU between two tensors. Each box should be
represented as [ymin, xmin, ymax, xmax].
Args:
boxes1: a tensor with shape [num_boxes, 4]
boxes2: a tensor with shape [num_boxes, 4]
Returns:
a tensor of shape [num_boxes] containing GIoUs
"""
def
two_boxes_giou
(
boxes
):
boxes1
,
boxes2
=
boxes
pred_ymin
=
tf
.
gather_nd
(
boxes1
,
[
0
])
pred_xmin
=
tf
.
gather_nd
(
boxes1
,
[
1
])
pred_ymax
=
tf
.
gather_nd
(
boxes1
,
[
2
])
pred_xmax
=
tf
.
gather_nd
(
boxes1
,
[
3
])
gt_ymin
=
tf
.
gather_nd
(
boxes2
,
[
0
])
gt_xmin
=
tf
.
gather_nd
(
boxes2
,
[
1
])
gt_ymax
=
tf
.
gather_nd
(
boxes2
,
[
2
])
gt_xmax
=
tf
.
gather_nd
(
boxes2
,
[
3
])
gt_area
=
(
gt_ymax
-
gt_ymin
)
*
(
gt_xmax
-
gt_xmin
)
pred_area
=
(
pred_ymax
-
pred_ymin
)
*
(
pred_xmax
-
pred_xmin
)
x1I
=
tf
.
maximum
(
pred_xmin
,
gt_xmin
)
x2I
=
tf
.
minimum
(
pred_xmax
,
gt_xmax
)
y1I
=
tf
.
maximum
(
pred_ymin
,
gt_ymin
)
y2I
=
tf
.
minimum
(
pred_ymax
,
gt_ymax
)
intersection_area
=
(
y2I
-
y1I
)
*
(
x2I
-
x1I
)
if
(
y2I
>
y1I
and
x2I
>
x1I
)
else
0.0
x1C
=
tf
.
minimum
(
pred_xmin
,
gt_xmin
)
x2C
=
tf
.
maximum
(
pred_xmax
,
gt_xmax
)
y1C
=
tf
.
minimum
(
pred_ymin
,
gt_ymin
)
y2C
=
tf
.
maximum
(
pred_ymax
,
gt_ymax
)
hull_area
=
(
y2C
-
y1C
)
*
(
x2C
-
x1C
)
union_area
=
gt_area
+
pred_area
-
intersection_area
IoU
=
intersection_area
/
union_area
if
union_area
!=
0.0
else
1.0
gIoU
=
IoU
-
(
hull_area
-
union_area
)
/
hull_area
if
hull_area
!=
0.0
else
IoU
return
gIoU
return
shape_utils
.
static_or_dynamic_map_fn
(
two_boxes_giou
,
[
boxes1
,
boxes2
])
def
cy_cx_h_w_to_ymin_xmin_ymax_xmax_coords
(
input_tensor
):
"""Converts input boxes from center to corner representation."""
reshaped_encodings
=
tf
.
reshape
(
input_tensor
,
[
-
1
,
4
])
ycenter
=
tf
.
gather
(
reshaped_encodings
,
[
0
],
axis
=
1
)
xcenter
=
tf
.
gather
(
reshaped_encodings
,
[
1
],
axis
=
1
)
h
=
tf
.
gather
(
reshaped_encodings
,
[
2
],
axis
=
1
)
w
=
tf
.
gather
(
reshaped_encodings
,
[
3
],
axis
=
1
)
ymin
=
ycenter
-
h
/
2.
xmin
=
xcenter
-
w
/
2.
ymax
=
ycenter
+
h
/
2.
xmax
=
xcenter
+
w
/
2.
return
tf
.
squeeze
(
tf
.
stack
([
ymin
,
xmin
,
ymax
,
xmax
],
axis
=
1
))
\ No newline at end of file
research/object_detection/utils/ops_test.py
View file @
ceb406bb
...
@@ -1630,8 +1630,47 @@ class TestGatherWithPaddingValues(test_case.TestCase):
...
@@ -1630,8 +1630,47 @@ class TestGatherWithPaddingValues(test_case.TestCase):
self
.
assertAllClose
(
expected_gathered_tensor
,
gathered_tensor_np
)
self
.
assertAllClose
(
expected_gathered_tensor
,
gathered_tensor_np
)
class
TestGIoU
(
test_case
.
TestCase
):
def
test_giou
(
self
):
expected_giou_tensor
=
[
1
,
0
,
-
1
/
3
,
1
/
25
,
1
,
-
3
/
4
]
def
graph_fn
():
boxes1
=
tf
.
constant
([[
3
,
3
,
5
,
5
],
[
3
,
4
,
5
,
6
],
[
3
,
3
,
5
,
5
],
[
2
,
1
,
7
,
6
],
[
1
,
1
,
1
,
1
],
[
0
,
0
,
0
,
0
]],
dtype
=
tf
.
float32
)
boxes2
=
tf
.
constant
([[
3
,
3
,
5
,
5
],
[
3
,
2
,
5
,
4
],
[
3
,
7
,
5
,
9
],
[
4
,
3
,
5
,
4
],
[
1
,
1
,
1
,
1
],
[
5
,
5
,
10
,
10
]],
dtype
=
tf
.
float32
)
giou
=
ops
.
giou
(
boxes1
,
boxes2
)
self
.
assertEqual
(
giou
.
dtype
,
tf
.
float32
)
return
giou
giou
=
self
.
execute
(
graph_fn
,
[])
self
.
assertAllClose
(
expected_giou_tensor
,
giou
)
class
TestCoordinateConversion
(
test_case
.
TestCase
):
def
test_coord_conv
(
self
):
expected_box_tensor
=
[
[
0.5
,
0.5
,
5.5
,
5.5
],
[
2
,
1
,
4
,
7
]
]
def
graph_fn
():
boxes
=
tf
.
constant
([[
3
,
3
,
5
,
5
],
[
3
,
4
,
2
,
6
]],
dtype
=
tf
.
float32
)
converted
=
ops
.
cy_cx_h_w_to_ymin_xmin_ymax_xmax_coords
(
boxes
)
self
.
assertEqual
(
converted
.
dtype
,
tf
.
float32
)
return
converted
converted
=
self
.
execute
(
graph_fn
,
[])
self
.
assertAllClose
(
expected_box_tensor
,
converted
)
...
...
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