def test_iouworks_on_empty_inputs(self): 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) boxes_empty = box_list.BoxList(tf.zeros((0, 4))) iou_empty_1 = box_list_ops.iou(boxes1, boxes_empty) iou_empty_2 = box_list_ops.iou(boxes_empty, boxes2) iou_empty_3 = box_list_ops.iou(boxes_empty, boxes_empty) with self.test_session() as sess: iou_output_1, iou_output_2, iou_output_3 = sess.run( [iou_empty_1, iou_empty_2, iou_empty_3]) self.assertAllEqual(iou_output_1.shape, (2, 0)) self.assertAllEqual(iou_output_2.shape, (0, 3)) self.assertAllEqual(iou_output_3.shape, (0, 0))
def get_box_loss(pred, gt, num_classes): lamd_coord = 5.0 xmin = pred[:, 0] - pred[:, 2] / 2 xmax = pred[:, 0] + pred[:, 2] / 2 ymin = pred[:, 1] - pred[:, 3] / 2 ymax = pred[:, 1] + pred[:, 3] / 2 boxes1 = tf.transpose(tf.stack([ymin, xmin, ymax, xmax])) xmin = gt[0] - gt[2] / 2 xmax = gt[0] + gt[2] / 2 ymin = gt[1] - gt[3] / 2 ymax = gt[1] + gt[3] / 2 boxes2 = tf.transpose(tf.stack([ymin, xmin, ymax, xmax])) boxes2 = tf.expand_dims(boxes2, 0) boxes_obj1 = box_list.BoxList(boxes1) boxes_obj2 = box_list.BoxList(boxes2) iou_score = box_list_ops.iou(boxes_obj2, boxes_obj1) boxes_obj1.add_field('scores', iou_score[0]) boxes_obj1.add_field('classes', pred[:, 5:]) boxes_obj1.add_field('objness', pred[:, 4]) boxes_obj1 = box_list_ops.sort_by_field(boxes_obj1, 'scores') matched_info = boxes_obj1.get_center_coordinates_and_sizes()[0] pred_cen = matched_info[0:2] pred_size = tf.sqrt(matched_info[2:4]) gt_center = tf.stack([gt[1], gt[0]]) gt_size = tf.stack([gt[3], gt[2]]) geo_loss = tf.losses.mean_squared_error( gt_center, pred_cen) + tf.losses.mean_squared_error( gt_size, pred_size) geo_loss = lamd_coord * geo_loss pred_confi = boxes_obj1.get_field('objness')[0] - 1 confi_loss = pred_confi * pred_confi predi_class = boxes_obj1.get_field('classes')[0] box_class = gt[4:] class_loss = tf.losses.mean_squared_error(box_class, predi_class) return confi_loss + geo_loss + class_loss
def test_iou(self): 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]]) exp_output = [[2.0 / 16.0, 0, 6.0 / 400.0], [1.0 / 16.0, 0.0, 5.0 / 400.0]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) iou = box_list_ops.iou(boxes1, boxes2) with self.test_session() as sess: iou_output = sess.run(iou) self.assertAllClose(iou_output, exp_output)
def _compare(self, boxlist1, boxlist2): """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. """ return box_list_ops.iou(boxlist1, boxlist2)
def _compare(self, boxlist1, boxlist2): """Compute pairwise IOU similarity between the two BoxLists and score. Args: boxlist1: BoxList holding N boxes. Must have a score field. boxlist2: BoxList holding M boxes. Returns: A tensor with shape [N, M] representing scores threholded by pairwise iou scores. """ ious = box_list_ops.iou(boxlist1, boxlist2) scores = boxlist1.get_field(fields.BoxListFields.scores) scores = tf.expand_dims(scores, axis=1) row_replicated_scores = tf.tile(scores, [1, tf.shape(ious)[-1]]) thresholded_ious = tf.where(ious > self._iou_threshold, row_replicated_scores, tf.zeros_like(ious)) return thresholded_ious
def _compare(self, boxlist1, boxlist2): """Compute pairwise IOU similarity between the two BoxLists ro RBoxlists. Args: boxlist1: BoxList or RBoxlist holding N boxes. boxlist2: BoxList or RBoxlist holding M boxes. Returns: A tensor with shape [N, M] representing pairwise iou scores. """ if isinstance(boxlist1, rbox_list.RBoxList) and isinstance( boxlist2, rbox_list.RBoxList): return rbox_list_ops.iou(boxlist1, boxlist2) elif isinstance(boxlist1, box_list.BoxList) and isinstance( boxlist2, box_list.BoxList): return box_list_ops.iou(boxlist1, boxlist2) else: raise ValueError( 'boxlist type must be same and BoxList or RBoxsList.')