def graph_fn(anchor_means, groundtruth_box_corners,
              groundtruth_keypoints):
   similarity_calc = region_similarity_calculator.IouSimilarity()
   matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                          unmatched_threshold=0.5)
   box_coder = keypoint_box_coder.KeypointBoxCoder(
       num_keypoints=6, scale_factors=[10.0, 10.0, 5.0, 5.0])
   target_assigner = targetassigner.TargetAssigner(
       similarity_calc, matcher, box_coder, unmatched_cls_target=None)
   anchors_boxlist = box_list.BoxList(anchor_means)
   groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
   groundtruth_boxlist.add_field(fields.BoxListFields.keypoints,
                                 groundtruth_keypoints)
   result = target_assigner.assign(anchors_boxlist, groundtruth_boxlist)
   (cls_targets, cls_weights, reg_targets, reg_weights, _) = result
   return (cls_targets, cls_weights, reg_targets, reg_weights)
 def test_very_small_width_nan_after_encoding(self):
     boxes = [[10., 10., 10.0000001, 20.]]
     keypoints = [[[10., 10.], [10.0000001, 20.]]]
     anchors = [[15., 12., 30., 18.]]
     expected_rel_codes = [[
         -0.833333, 0., -21.128731, 0.510826, -0.833333, -0.833333,
         -0.833333, 0.833333
     ]]
     boxes = box_list.BoxList(tf.constant(boxes))
     boxes.add_field(fields.BoxListFields.keypoints, tf.constant(keypoints))
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = keypoint_box_coder.KeypointBoxCoder(2)
     rel_codes = coder.encode(boxes, anchors)
     with self.test_session() as sess:
         rel_codes_out, = sess.run([rel_codes])
         self.assertAllClose(rel_codes_out, expected_rel_codes)
def build(box_coder_config):
    """Builds a box coder object based on the box coder config.

  Args:
    box_coder_config: A box_coder.proto object containing the config for the
      desired box coder.

  Returns:
    BoxCoder based on the config.

  Raises:
    ValueError: On empty box coder proto.
  """
    if not isinstance(box_coder_config, box_coder_pb2.BoxCoder):
        raise ValueError(
            'box_coder_config not of type box_coder_pb2.BoxCoder.')

    if box_coder_config.WhichOneof(
            'box_coder_oneof') == 'faster_rcnn_box_coder':
        return faster_rcnn_box_coder.FasterRcnnBoxCoder(scale_factors=[
            box_coder_config.faster_rcnn_box_coder.y_scale,
            box_coder_config.faster_rcnn_box_coder.x_scale,
            box_coder_config.faster_rcnn_box_coder.height_scale,
            box_coder_config.faster_rcnn_box_coder.width_scale
        ])
    if box_coder_config.WhichOneof('box_coder_oneof') == 'keypoint_box_coder':
        return keypoint_box_coder.KeypointBoxCoder(
            box_coder_config.keypoint_box_coder.num_keypoints,
            scale_factors=[
                box_coder_config.keypoint_box_coder.y_scale,
                box_coder_config.keypoint_box_coder.x_scale,
                box_coder_config.keypoint_box_coder.height_scale,
                box_coder_config.keypoint_box_coder.width_scale
            ])
    if (box_coder_config.WhichOneof('box_coder_oneof') ==
            'mean_stddev_box_coder'):
        return mean_stddev_box_coder.MeanStddevBoxCoder(
            stddev=box_coder_config.mean_stddev_box_coder.stddev)
    if box_coder_config.WhichOneof('box_coder_oneof') == 'square_box_coder':
        return square_box_coder.SquareBoxCoder(scale_factors=[
            box_coder_config.square_box_coder.y_scale,
            box_coder_config.square_box_coder.x_scale,
            box_coder_config.square_box_coder.length_scale
        ])
    raise ValueError('Empty box coder.')
 def test_get_correct_relative_codes_after_encoding(self):
     boxes = [[10., 10., 20., 15.], [0.2, 0.1, 0.5, 0.4]]
     keypoints = [[[15., 12.], [10., 15.]], [[0.5, 0.3], [0.2, 0.4]]]
     num_keypoints = len(keypoints[0])
     anchors = [[15., 12., 30., 18.], [0.1, 0.0, 0.7, 0.9]]
     expected_rel_codes = [[
         -0.5, -0.416666, -0.405465, -0.182321, -0.5, -0.5, -0.833333, 0.
     ],
                           [
                               -0.083333, -0.222222, -0.693147, -1.098612,
                               0.166667, -0.166667, -0.333333, -0.055556
                           ]]
     boxes = box_list.BoxList(tf.constant(boxes))
     boxes.add_field(fields.BoxListFields.keypoints, tf.constant(keypoints))
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = keypoint_box_coder.KeypointBoxCoder(num_keypoints)
     rel_codes = coder.encode(boxes, anchors)
     with self.test_session() as sess:
         rel_codes_out, = sess.run([rel_codes])
         self.assertAllClose(rel_codes_out, expected_rel_codes)
 def test_get_correct_relative_codes_after_encoding_with_scaling(self):
     boxes = [[10., 10., 20., 15.], [0.2, 0.1, 0.5, 0.4]]
     keypoints = [[[15., 12.], [10., 15.]], [[0.5, 0.3], [0.2, 0.4]]]
     num_keypoints = len(keypoints[0])
     anchors = [[15., 12., 30., 18.], [0.1, 0.0, 0.7, 0.9]]
     scale_factors = [2, 3, 4, 5]
     expected_rel_codes = [[
         -1., -1.25, -1.62186, -0.911608, -1.0, -1.5, -1.666667, 0.
     ],
                           [
                               -0.166667, -0.666667, -2.772588, -5.493062,
                               0.333333, -0.5, -0.666667, -0.166667
                           ]]
     boxes = box_list.BoxList(tf.constant(boxes))
     boxes.add_field(fields.BoxListFields.keypoints, tf.constant(keypoints))
     anchors = box_list.BoxList(tf.constant(anchors))
     coder = keypoint_box_coder.KeypointBoxCoder(
         num_keypoints, scale_factors=scale_factors)
     rel_codes = coder.encode(boxes, anchors)
     with self.test_session() as sess:
         rel_codes_out, = sess.run([rel_codes])
         self.assertAllClose(rel_codes_out, expected_rel_codes)