def test_iou_zero_target(self): self.assertAllClose(iou_utils.iou_loss(self.pb, self.zeros, 'iou'), [0.0, 0.0]) self.assertAllClose(iou_utils.iou_loss(self.pb, self.zeros, 'ciou'), [0.0, 0.0]) self.assertAllClose(iou_utils.iou_loss(self.pb, self.zeros, 'diou'), [0.0, 0.0]) self.assertAllClose(iou_utils.iou_loss(self.pb, self.zeros, 'giou'), [0.0, 0.0])
def call(self, y_true, box_outputs): num_positives, box_targets = y_true normalizer = num_positives * 4.0 box_iou_loss = iou_utils.iou_loss(box_outputs, box_targets, self.iou_loss_type) box_iou_loss = tf.reduce_sum(box_iou_loss) / normalizer return box_iou_loss
def test_ciou_grad(self): pb = tf.concat([self.pb, self.zeros], axis=-1) tb = tf.concat([self.tb, self.zeros], axis=-1) with tf.GradientTape() as tape: tape.watch([pb, tb]) loss = iou_utils.iou_loss(pb, tb, 'ciou') grad = tape.gradient(loss, [tb, pb]) self.assertAlmostEqual(tf.reduce_sum(grad[0]).numpy(), 0.16687772) self.assertAlmostEqual(tf.reduce_sum(grad[1]).numpy(), -0.16687769)
def call(self, y_true, box_outputs): anchor_boxes = tf.tile( self.input_anchors.boxes, [box_outputs.shape[0] // self.input_anchors.boxes.shape[0], 1]) num_positives, box_targets = y_true box_outputs = anchors.decode_box_outputs(box_outputs, anchor_boxes) box_targets = anchors.decode_box_outputs(box_targets, anchor_boxes) normalizer = num_positives * 4.0 box_iou_loss = iou_utils.iou_loss(box_outputs, box_targets, self.iou_loss_type) box_iou_loss = tf.reduce_sum(box_iou_loss) / normalizer return box_iou_loss
def call(self, y_true, box_outputs): input_anchors = BoxList( tf.tile(self.input_anchors.boxes, [box_outputs.shape[0] // self.input_anchors.boxes.shape[0], 1])) num_positives, box_targets = y_true box_outputs = self.box_coder.decode(box_outputs, input_anchors) box_targets = self.box_coder.decode(box_targets, input_anchors) normalizer = num_positives * 4.0 box_iou_loss = iou_utils.iou_loss(box_outputs.data['boxes'], box_targets.data['boxes'], self.iou_loss_type) box_iou_loss = tf.reduce_sum(box_iou_loss) / normalizer return box_iou_loss
def test_iou_multiple_anchors_mixed(self): pb = tf.concat([self.pb, self.zeros], axis=-1) tb = tf.concat([self.tb, self.zeros], axis=-1) self.assertAllClose(iou_utils.iou_loss(pb, tb, 'iou'), [0.875, 1.0])
def test_iou_multiple_anchors(self): pb = tf.tile(self.pb, [1, 2]) tb = tf.tile(self.tb, [1, 2]) self.assertAllClose(iou_utils.iou_loss(pb, tb, 'iou'), [1.75, 2.0])
def test_giou(self): self.assertAllClose(iou_utils.iou_loss(self.pb, self.tb, 'giou'), [1.075000, 1.933333])
def test_diou(self): self.assertAllClose(iou_utils.iou_loss(self.pb, self.tb, 'diou'), [1.406532, 1.531532])
def test_ciou(self): self.assertAllClose(iou_utils.iou_loss(self.pb, self.tb, 'ciou'), [1.408893, 1.548753])
def test_iou(self): self.assertAllClose(iou_utils.iou_loss(self.pb, self.tb, 'iou'), [0.875, 1.])
def _box_iou_loss(box_outputs, box_targets, num_positives, iou_loss_type): """Computes box iou loss.""" normalizer = num_positives * 4.0 box_iou_loss = iou_utils.iou_loss(box_outputs, box_targets, iou_loss_type) box_iou_loss = tf.reduce_sum(box_iou_loss) / normalizer return box_iou_loss
def test_diou(self): self.assertAllClose(iou_utils.iou_loss(self.pb, self.tb, 'diou'), [0.9969512, 1.6243094])
def test_ciou(self): self.assertAllClose(iou_utils.iou_loss(self.pb, self.tb, 'ciou'), [0.99931306, 1.6415315])