Commit c99e5783 authored by Kaushik Shivakumar's avatar Kaushik Shivakumar
Browse files

progress on pr

parent 0006ba72
......@@ -34,7 +34,6 @@ from object_detection.core import box_list
from object_detection.utils import ops
from object_detection.utils import shape_utils
class SortOrder(object):
"""Enum class for sort order.
......@@ -303,6 +302,53 @@ def iou(boxlist1, boxlist2, scope=None):
tf.equal(intersections, 0.0),
tf.zeros_like(intersections), tf.truediv(intersections, unions))
def l1(boxlist1, boxlist2, scope=None):
"""Computes l1 loss (pairwise) between two boxlists.
Args:
boxlist1: BoxList holding N boxes
boxlist2: BoxList holding M boxes
scope: name scope.
Returns:
a tensor with shape [N, M] representing the pairwise L1 loss.
"""
with tf.name_scope(scope, 'PairwiseL1'):
ycenter1, xcenter1, h1, w1 = boxlist1.get_center_coordinates_and_sizes()
ycenter2, xcenter2, h2, w2 = boxlist2.get_center_coordinates_and_sizes()
ycenters = tf.abs(tf.expand_dims(ycenter2, axis=0) - \
tf.expand_dims(tf.transpose(ycenter1), axis=1))
xcenters = tf.abs(tf.expand_dims(xcenter2, axis=0) - \
tf.expand_dims(tf.transpose(xcenter1), axis=1))
heights = tf.abs(tf.expand_dims(h2, axis=0) - \
tf.expand_dims(tf.transpose(h1), axis=1))
widths = tf.abs(tf.expand_dims(w2, axis=0) - \
tf.expand_dims(tf.transpose(w1), axis=1))
return ycenters + xcenters + heights + widths
def giou_loss(boxlist1, boxlist2, scope=None):
"""
Computes generalized IOU loss between two boxlists pairwise,
as described at giou.stanford.edu.
Args:
boxlist1: BoxList holding N boxes
boxlist2: BoxList holding M boxes
scope: name scope.
Returns:
a tensor with shape [N, M] representing the pairwise GIoU loss.
"""
# Import done internally so this dependency is not required for
# the OD API as a whole.
import tensorflow_addons as tfa
with tf.name_scope(scope, "PairwiseGIoU"):
N = boxlist1.num_boxes()
M = boxlist2.num_boxes()
boxes1 = tf.repeat(boxlist1.get(), repeats=M, axis=0)
boxes2 = tf.tile(boxlist2.get(), multiples=[N, 1])
return tf.reshape(tfa.losses.giou_loss(boxes1, boxes2), [N, M])
def matched_iou(boxlist1, boxlist2, scope=None):
"""Compute intersection-over-union between corresponding boxes in boxlists.
......@@ -319,11 +365,15 @@ def matched_iou(boxlist1, boxlist2, scope=None):
intersections = matched_intersection(boxlist1, boxlist2)
areas1 = area(boxlist1)
areas2 = area(boxlist2)
print("AREAS AND INTERSECTION", areas1, areas2, intersections)
unions = areas1 + areas2 - intersections
return tf.where(
tf.equal(intersections, 0.0),
tf.zeros_like(intersections), tf.truediv(intersections, unions))
def matched_giou(boxlist1, boxlist2, scope=None):
with tf.name_scope(scope, 'MatchedGIOU'):
pass
def ioa(boxlist1, boxlist2, scope=None):
"""Computes pairwise intersection-over-area between box collections.
......
......@@ -31,11 +31,12 @@ import tensorflow.compat.v1 as tf
from object_detection.core import box_list_ops
from object_detection.core import standard_fields as fields
EPSILON = 1e-8
class RegionSimilarityCalculator(six.with_metaclass(ABCMeta, object)):
"""Abstract base class for region similarity calculator."""
def compare(self, boxlist1, boxlist2, scope=None):
def compare(self, boxlist1, boxlist2, scope=None, groundtruth_labels=None, predicted_labels=None):
"""Computes matrix of pairwise similarity between BoxLists.
This op (to be overridden) computes a measure of pairwise similarity between
......@@ -53,10 +54,10 @@ class RegionSimilarityCalculator(six.with_metaclass(ABCMeta, object)):
a (float32) tensor of shape [N, M] with pairwise similarity score.
"""
with tf.name_scope(scope, 'Compare', [boxlist1, boxlist2]) as scope:
return self._compare(boxlist1, boxlist2)
return self._compare(boxlist1, boxlist2, groundtruth_labels, predicted_labels)
@abstractmethod
def _compare(self, boxlist1, boxlist2):
def _compare(self, boxlist1, boxlist2, groundtruth_labels=None, predicted_labels=None):
pass
......@@ -78,6 +79,27 @@ class IouSimilarity(RegionSimilarityCalculator):
"""
return box_list_ops.iou(boxlist1, boxlist2)
class DETRSimiliarity(RegionSimilarityCalculator):
"""Class to compute similarity based on Intersection over Union (IOU) metric.
This class computes pairwise similarity between two BoxLists based on IOU.
"""
def _compare(self, boxlist1, boxlist2, groundtruth_labels=None, predicted_labels=None):
"""Compute pairwise IOU similarity between the two BoxLists.
Args:
boxlist1: BoxList holding N boxes.
boxlist2: BoxList holding M boxes.
Returns:
A tensor with shape [N, M] representing pairwise iou scores.
"""
classification_scores = tf.matmul(groundtruth_labels,
tf.nn.softmax(predicted_labels), transpose_b=True)
return -5 * box_list_ops.l1(boxlist1, boxlist2) + \
classification_scores + \
2 * (1 - box_list_ops.giou_loss(boxlist1, boxlist2))
class NegSqDistSimilarity(RegionSimilarityCalculator):
"""Class to compute similarity based on the squared distance metric.
......@@ -86,7 +108,7 @@ class NegSqDistSimilarity(RegionSimilarityCalculator):
negative squared distance metric.
"""
def _compare(self, boxlist1, boxlist2):
def _compare(self, boxlist1, boxlist2, groundtruth_labels=None, predicted_labels=None):
"""Compute matrix of (negated) sq distances.
Args:
......@@ -98,7 +120,6 @@ class NegSqDistSimilarity(RegionSimilarityCalculator):
"""
return -1 * box_list_ops.sq_dist(boxlist1, boxlist2)
class IoaSimilarity(RegionSimilarityCalculator):
"""Class to compute similarity based on Intersection over Area (IOA) metric.
......@@ -106,7 +127,7 @@ class IoaSimilarity(RegionSimilarityCalculator):
pairwise intersections divided by the areas of second BoxLists.
"""
def _compare(self, boxlist1, boxlist2):
def _compare(self, boxlist1, boxlist2, groundtruth_labels=None, predicted_labels=None):
"""Compute pairwise IOA similarity between the two BoxLists.
Args:
......@@ -138,7 +159,7 @@ class ThresholdedIouSimilarity(RegionSimilarityCalculator):
super(ThresholdedIouSimilarity, self).__init__()
self._iou_threshold = iou_threshold
def _compare(self, boxlist1, boxlist2):
def _compare(self, boxlist1, boxlist2, groundtruth_labels=None, predicted_labels=None):
"""Compute pairwise IOU similarity between the two BoxLists and score.
Args:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment