def testExtractKeypointDescriptor(self):
    image = tf.constant(
        [[[0, 255, 255], [128, 64, 196]], [[0, 0, 32], [32, 128, 16]]],
        dtype=tf.uint8)

    # Arbitrary model function used to test ExtractKeypointDescriptor. The
    # generated feature_map is a replicated version of the image, concatenated
    # with zeros to achieve the required dimensionality. The attention is simply
    # the norm of the input image pixels.
    def _test_model_fn(image, normalized_image, reuse):
      del normalized_image, reuse  # Unused variables in the test.
      image_shape = tf.shape(image)
      attention = tf.squeeze(tf.norm(image, axis=3))
      feature_map = tf.concat(
          [
              tf.tile(image, [1, 1, 1, 341]),
              tf.zeros([1, image_shape[1], image_shape[2], 1])
          ],
          axis=3)
      return attention, feature_map

    boxes, feature_scales, features, scores = (
        feature_extractor.ExtractKeypointDescriptor(
            image,
            layer_name='resnet_v1_50/block3',
            image_scales=tf.constant([1.0]),
            iou=1.0,
            max_feature_num=10,
            abs_thres=1.5,
            model_fn=_test_model_fn))

    exp_boxes = [[-145.0, -145.0, 145.0, 145.0], [-113.0, -145.0, 177.0, 145.0]]
    exp_feature_scales = [1.0, 1.0]
    exp_features = np.array(
        np.concatenate(
            (np.tile([[-1.0, 127.0 / 128.0, 127.0 / 128.0], [-1.0, -1.0, -0.75]
                     ], [1, 341]), np.zeros([2, 1])),
            axis=1))
    exp_scores = [[1.723042], [1.600781]]

    with self.test_session() as sess:
      boxes_out, feature_scales_out, features_out, scores_out = sess.run(
          [boxes, feature_scales, features, scores])

    self.assertAllEqual(exp_boxes, boxes_out)
    self.assertAllEqual(exp_feature_scales, feature_scales_out)
    self.assertAllClose(exp_features, features_out)
    self.assertAllClose(exp_scores, scores_out)
    def __init__(self, config_path):
        tf.logging.set_verbosity(tf.logging.INFO)

        # Parse DelfConfig proto.
        config = delf_config_pb2.DelfConfig()
        self.config = config
        with tf.gfile.FastGFile(config_path, 'r') as f:
            text_format.Merge(f.read(), config)

        # Tell TensorFlow that the model will be built into the default Graph.
        with tf.Graph().as_default():
            self.byte_value = tf.placeholder(tf.string)
            self.image_tf = tf.image.decode_jpeg(self.byte_value, channels=3)

            self.input_image = tf.placeholder(tf.uint8, [None, None, 3])

            self.input_score_threshold = tf.placeholder(tf.float32)
            self.input_image_scales = tf.placeholder(tf.float32, [None])
            self.input_max_feature_num = tf.placeholder(tf.int32)

            model_fn = feature_extractor.BuildModel(
                'resnet_v1_50/block3', 'softplus', 'use_l2_normalized_feature',
                1)

            boxes, self.feature_scales, features, scores, self.attention = feature_extractor.ExtractKeypointDescriptor(
                self.input_image,
                layer_name='resnet_v1_50/block3',
                image_scales=self.input_image_scales,
                iou=1.0,
                max_feature_num=self.input_max_feature_num,
                abs_thres=self.input_score_threshold,
                model_fn=model_fn)

            ## Optimistic restore.
            latest_checkpoint = config.model_path + 'variables/variables'
            variables_to_restore = tf.global_variables()

            reader = tf.train.NewCheckpointReader(latest_checkpoint)
            saved_shapes = reader.get_variable_to_shape_map()

            variable_names_to_restore = [
                var.name.split(':')[0] for var in variables_to_restore
            ]
            for shape in saved_shapes:
                if not shape in variable_names_to_restore:
                    print(shape)

            for var_name in variable_names_to_restore:
                if not var_name in saved_shapes:
                    print(
                        "WARNING. Saved weight not exists in checkpoint. Init var:",
                        var_name)

            var_names = sorted([(var.name, var.name.split(':')[0])
                                for var in variables_to_restore
                                if var.name.split(':')[0] in saved_shapes])
            restore_vars = []
            with tf.variable_scope('', reuse=True):
                for var_name, saved_var_name in var_names:
                    try:
                        curr_var = tf.get_variable(saved_var_name)
                        var_shape = curr_var.get_shape().as_list()
                        if var_shape == saved_shapes[saved_var_name]:
                            # print("restore var:", saved_var_name)
                            restore_vars.append(curr_var)
                    except ValueError:
                        print("Ignore due to ValueError on getting var:",
                              saved_var_name)
            saver = tf.train.Saver(restore_vars)

            sess = tf.Session()
            self.sess = sess
            # Initialize variables.
            init_op = tf.global_variables_initializer()
            sess.run(init_op)
            saver.restore(sess, latest_checkpoint)

            # # Loading model that will be used.
            # tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING],
            #                             config.model_path)

            graph = tf.get_default_graph()
            self.attention_score = tf.reshape(
                scores, [tf.shape(scores)[0]])  # remove extra dim.

            self.locations, self.descriptors = feature_extractor.DelfFeaturePostProcessing(
                boxes, features, config)