Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity box coder, similarity calculator, target assigner #8962

Merged
merged 48 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
0006ba7
target assigner mods
kmindspark Jul 24, 2020
c99e578
progress on pr
kmindspark Jul 24, 2020
c594cec
pr
kmindspark Jul 24, 2020
7359586
Merge remote-tracking branch 'upstream/master' into detr-push-3
kmindspark Jul 24, 2020
9bd3fe6
save progress
kmindspark Jul 24, 2020
ceb406b
ready pr
kmindspark Jul 27, 2020
c95500c
updates
kmindspark Jul 27, 2020
4753d6e
Merge remote-tracking branch 'upstream/master' into detr-push-3
kmindspark Jul 27, 2020
55db4ad
fix
kmindspark Jul 27, 2020
70cb851
fix
kmindspark Jul 27, 2020
322d444
minor style elements
kmindspark Jul 27, 2020
ba65cc7
fix tests
kmindspark Jul 28, 2020
7b165eb
fix tests
kmindspark Jul 28, 2020
3564e7c
fix
kmindspark Jul 29, 2020
a679728
fix
kmindspark Jul 29, 2020
8948ba3
fix file
kmindspark Jul 30, 2020
5f71a45
fix
kmindspark Jul 30, 2020
7723b20
fix pr
kmindspark Jul 30, 2020
980d176
fix
kmindspark Jul 30, 2020
dabfc27
fix
kmindspark Jul 30, 2020
f0bc684
fix this pr
kmindspark Jul 30, 2020
4022aae
self code-review to clean up
kmindspark Jul 30, 2020
22b5b0c
self code-review to clean up
kmindspark Jul 30, 2020
245e9d1
add detr box coder test
kmindspark Jul 31, 2020
d31aba8
using already pushed files
kmindspark Aug 7, 2020
356c98b
Merge remote-tracking branch 'upstream/master' into detr-push-3
kmindspark Aug 7, 2020
824b70f
region similarity calculator mod
kmindspark Aug 7, 2020
c8cd7d1
progress on model lib
kmindspark Aug 7, 2020
43eaeb0
remove detr lib
kmindspark Aug 7, 2020
98516e5
small fix
kmindspark Aug 7, 2020
4f7965f
remove detr lib
kmindspark Aug 7, 2020
e0b082e
fix documentation for box coder
kmindspark Aug 10, 2020
ab96cb3
separate out DETR
kmindspark Aug 11, 2020
8e77b75
remove box coders
kmindspark Aug 11, 2020
3d757d5
add line
kmindspark Aug 11, 2020
4f135c7
compress target assigner
kmindspark Aug 11, 2020
d54c86d
make suggested fixes to target assigner and similarity calculator
kmindspark Aug 11, 2020
e09e056
target assigner and similarity calculator fixes
kmindspark Aug 12, 2020
8f5ed2d
fixes to simplify
kmindspark Aug 15, 2020
656ec2a
fixes to simplify
kmindspark Aug 15, 2020
9d4b102
clean target assigner
kmindspark Aug 16, 2020
323ea89
remove detr building
kmindspark Aug 18, 2020
a6f36d2
work on cleaning up further
kmindspark Aug 18, 2020
111c9d3
target assigner test
kmindspark Aug 18, 2020
de3a34b
fix issues with target assigner for pr
kmindspark Aug 19, 2020
e350c59
fix num classes
kmindspark Aug 19, 2020
1ed7ef3
fix naming
kmindspark Aug 20, 2020
0bc599e
flip around similarity calculation
kmindspark Aug 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
  • Loading branch information
kmindspark committed Jul 30, 2020
commit 980d176ae157312ae196506e5f1cb53a51441a81
105 changes: 105 additions & 0 deletions research/object_detection/box_coders/detr_box_coder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Faster RCNN box coder.

Faster RCNN box coder follows the coding schema described below:
ty = (y - ya) / ha
tx = (x - xa) / wa
th = log(h / ha)
tw = log(w / wa)
where x, y, w, h denote the box's center coordinates, width and height
respectively. Similarly, xa, ya, wa, ha denote the anchor's center
coordinates, width and height. tx, ty, tw and th denote the anchor-encoded
center, width and height respectively.

See http://arxiv.org/abs/1506.01497 for details.
"""

import tensorflow.compat.v1 as tf

from object_detection.core import box_coder
from object_detection.core import box_list

EPSILON = 1e-8


class DETRBoxCoder(box_coder.BoxCoder):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, it might be best to follow CenterNet's pattern here to create a single class that does the target assignment.

"""Faster RCNN box coder."""

def __init__(self, scale_factors=None):
"""Constructor for FasterRcnnBoxCoder.

Args:
scale_factors: List of 4 positive scalars to scale ty, tx, th and tw.
If set to None, does not perform scaling. For Faster RCNN,
the open-source implementation recommends using [10.0, 10.0, 5.0, 5.0].
"""
if None:
assert len(scale_factors) == 4
for scalar in scale_factors:
assert scalar > 0
self._scale_factors = scale_factors

@property
def code_size(self):
return 4

def _encode(self, boxes, anchors):
"""Encode a box collection with respect to anchor collection.

Args:
boxes: BoxList holding N boxes to be encoded.
anchors: BoxList of anchors.

Returns:
a tensor representing N anchor-encoded boxes of the format
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there references to anchors and Faster-RCNN for the DETR? As far as I understand, one of the big advantages of the model is the simplification e.g. through dropping anchors?

Is there any requirement to utilize box_coder.BoxCoder, even if one does not need anchor-based encodings and only transforms yxyx to yxhw and vice versa?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason the box coder is left in for now is because the target assigner uses it to convert the groundtruth to the right format, and no need to change the internals of the target assigner whereas this gives some flexibility. All this box coder does is use the identity function so it can plug into the framework nicely. We may choose to remove it, but that's the reason it's here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for the explanation. Then, I would adjust the docstrings and remove all the Faster RCNN references e.g. in the constructor or line 40. Just to avoid the confusion with anchors etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Box Coders have been removed.

[ty, tx, th, tw].
"""
# Convert anchors to the center coordinate representation.
ycenter, xcenter, h, w = boxes.get_center_coordinates_and_sizes()
# Avoid NaN in division and log below.
h += EPSILON
w += EPSILON

tx = xcenter
ty = ycenter
tw = w #tf.log(w)
th = h #tf.log(h)

return tf.transpose(tf.stack([ty, tx, th, tw]))

def _decode(self, rel_codes, anchors):
"""Decode relative codes to boxes.

Args:
rel_codes: a tensor representing N anchor-encoded boxes.
anchors: BoxList of anchors.

Returns:
boxes: BoxList holding N bounding boxes.
"""
ty, tx, th, tw = tf.unstack(tf.transpose(rel_codes))

w = tw
h = th
ycenter = ty
xcenter = tx
ymin = ycenter - h / 2.
xmin = xcenter - w / 2.
ymax = ycenter + h / 2.
xmax = xcenter + w / 2.
return box_list.BoxList(tf.transpose(tf.stack([ymin, xmin, ymax, xmax])))

8 changes: 6 additions & 2 deletions research/object_detection/core/target_assigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def create_target_assigner(reference, stage=None,
elif reference == 'DETR':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to add it here. In DETR meta architecture you can directly create DETRTargetAssigner()

similarity_calc = sim_calc.DETRSimilarity()
matcher = hungarian_matcher.HungarianBipartiteMatcher()
box_coder_instance = None
box_coder_instance = detr_box_coder.DETRBoxCoder()

else:
raise ValueError('No valid combination of reference and stage.')
Expand Down Expand Up @@ -481,6 +481,7 @@ def batch_assign(target_assigner,
function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]).
gt_weights_batch: A list of 1-D tf.float32 tensors of shape
[num_boxes] containing weights for groundtruth boxes.
class_predictions: A

Returns:
batch_cls_targets: a tensor with shape [batch_size, num_anchors,
Expand Down Expand Up @@ -521,7 +522,10 @@ def batch_assign(target_assigner,
match_list = []
if gt_weights_batch is None:
gt_weights_batch = [None] * len(gt_class_targets_batch)
class_predictions = tf.unstack(class_predictions)
if class_predictions:
class_predictions = tf.unstack(class_predictions)
else:
class_predictions = [None] * len(gt_class_targets_batch)
for anchors, gt_boxes, gt_class_targets, gt_weights, class_preds in zip(
anchors_batch, gt_box_batch, gt_class_targets_batch, gt_weights_batch,
class_predictions):
Expand Down
3 changes: 2 additions & 1 deletion research/object_detection/core/target_assigner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from object_detection.box_coders import keypoint_box_coder
from object_detection.box_coders import mean_stddev_box_coder
from object_detection.box_coders import detr_box_coder
from object_detection.core import box_list
from object_detection.core import region_similarity_calculator
from object_detection.core import standard_fields as fields
Expand Down Expand Up @@ -1927,7 +1928,7 @@ def graph_fn(anchor_means, groundtruth_box_corners,
groundtruth_labels, predicted_labels):
similarity_calc = region_similarity_calculator.DETRSimilarity()
matcher = hungarian_matcher.HungarianBipartiteMatcher()
box_coder = box
box_coder = detr_box_coder.DETRBoxCoder()
target_assigner = targetassigner.TargetAssigner(
similarity_calc, matcher, box_coder)
anchors_boxlist = box_list.BoxList(anchor_means)
Expand Down