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 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 sample_mini_batch(self, anchor_box_list_gt, anchor_box_list, class_labels): with tf.variable_scope('avod_create_mb_mask'): # Get IoU for every anchor all_ious = box_list_ops.iou(anchor_box_list_gt, anchor_box_list) max_ious = tf.reduce_max(all_ious, axis=0) max_iou_indices = tf.argmax(all_ious, axis=0) # Sample a pos/neg mini-batch from anchors with highest IoU match mini_batch_utils = self.dataset.kitti_utils.mini_batch_utils mb_mask, mb_pos_mask = mini_batch_utils.sample_avod_mini_batch( max_ious) mb_class_label_indices = mini_batch_utils.mask_class_label_indices( mb_pos_mask, mb_mask, max_iou_indices, class_labels) mb_gt_indices = tf.boolean_mask(max_iou_indices, mb_mask) return mb_mask, mb_class_label_indices, mb_gt_indices
def test_iou_mask_ops(self): # corners are in [y1, x1, y2, x2] format corners_pred = tf.constant([[4.0, 3.0, 7.0, 5.0], [14.0, 14.0, 16.0, 16.0], [0.0, 0.0, 21.0, 19.0], [3.0, 4.0, 5.0, 7.0]]) corners_gt = tf.constant([[4.0, 3.0, 7.0, 6.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) # 3 classes class_indices = tf.constant([1., 2., 3.]) exp_ious = [[0.66666669, 0., 0.02255639, 0.15384616], [0., 0.25, 0.00250627, 0.], [0.015, 0.01, 0.90692127, 0.015]] exp_max_ious = np.array([0.66666669, 0.25, 0.90692127, 0.15384616]) exp_max_indices = np.array([0, 1, 2, 0]) exp_pos_mask = np.array([True, False, True, False]) exp_class_and_background_indices = np.array([1, 0, 3, 0]) # Convert to box_list format boxes_pred = box_list.BoxList(corners_pred) boxes_gt = box_list.BoxList(corners_gt) # Calculate IoU iou = box_list_ops.iou(boxes_gt, boxes_pred) # Get max IoU, the dimension should match the anchors we are # evaluating max_ious = tf.reduce_max(iou, axis=0) max_iou_indices = tf.argmax(iou, axis=0) # Sample a mini-batch from anchors with highest IoU match mini_batch_size = 4 # Custom positive/negative iou ranges neg_2d_iou_range = [0.0, 0.3] pos_2d_iou_range = [0.6, 0.7] mb_mask, mb_pos_mask = \ self.mb_utils.sample_mini_batch(max_ious, mini_batch_size, neg_2d_iou_range, pos_2d_iou_range) mb_class_indices = self.mb_utils.mask_class_label_indices( mb_pos_mask, mb_mask, max_iou_indices, class_indices) with self.test_session() as sess: iou_out = sess.run(iou) max_ious_out, max_iou_indices_out = sess.run( [max_ious, max_iou_indices]) mb_mask_out, mb_pos_mask_out = sess.run([mb_mask, mb_pos_mask]) class_indices_out = sess.run(mb_class_indices) self.assertAllClose(iou_out, exp_ious) self.assertAllClose(max_ious_out, exp_max_ious) self.assertAllEqual(max_iou_indices_out, exp_max_indices) self.assertAllEqual(exp_pos_mask, mb_pos_mask_out) self.assertAllEqual(class_indices_out, exp_class_and_background_indices)