def ExtractFeatures(self, input_image, input_scales,
                        input_global_scales_ind):
        extracted_features = export_model_utils.ExtractGlobalFeatures(
            input_image,
            input_scales,
            input_global_scales_ind,
            lambda x: self._model(x, training=False),
            multi_scale_pool_type=self._multi_scale_pool_type,
            normalize_global_descriptor=self._normalize_global_descriptor,
            normalization_function=_NormalizeImages())

        named_output_tensors = {}
        named_output_tensors['global_descriptors'] = tf.identity(
            extracted_features, name='global_descriptors')
        return named_output_tensors
Beispiel #2
0
    def ExtractFeatures(self, input_image, input_scales,
                        input_global_scales_ind):
        extracted_features = export_model_utils.ExtractGlobalFeatures(
            input_image,
            input_scales,
            input_global_scales_ind,
            lambda x: self._model.backbone.build_call(x, training=False),
            multi_scale_pool_type=self._multi_scale_pool_type,
            normalize_global_descriptor=self._normalize_global_descriptor)
        print("inside ExtractFeatures called")
        named_output_tensors = {}
        if self._multi_scale_pool_type == 'None':
            named_output_tensors['global_descriptors'] = tf.identity(
                extracted_features, name='global_descriptors')
        else:
            named_output_tensors['global_descriptor'] = tf.identity(
                extracted_features, name='global_descriptor')

        return named_output_tensors
def main(argv):
    if len(argv) > 1:
        raise app.UsageError('Too many command-line arguments.')

    export_path = FLAGS.export_path
    if os.path.exists(export_path):
        raise ValueError('Export_path already exists.')

    with tf.Graph().as_default() as g, tf.compat.v1.Session(graph=g) as sess:

        # Setup the model for extraction.
        model = delf_model.Delf(block3_strides=False, name='DELF')

        # Initial forward pass to build model.
        images = tf.zeros((1, 321, 321, 3), dtype=tf.float32)
        model(images)

        # Setup the multiscale extraction.
        input_image = tf.compat.v1.placeholder(tf.uint8,
                                               shape=(None, None, 3),
                                               name='input_image')
        if FLAGS.input_scales_list is None:
            input_scales = tf.compat.v1.placeholder(tf.float32,
                                                    shape=[None],
                                                    name='input_scales')
        else:
            input_scales = tf.constant(
                [float(s) for s in FLAGS.input_scales_list],
                dtype=tf.float32,
                shape=[len(FLAGS.input_scales_list)],
                name='input_scales')

        extracted_features = export_model_utils.ExtractGlobalFeatures(
            input_image,
            input_scales,
            lambda x: model.backbone(x, training=False),
            multi_scale_pool_type=FLAGS.multi_scale_pool_type,
            normalize_global_descriptor=FLAGS.normalize_global_descriptor)

        # Load the weights.
        checkpoint_path = FLAGS.ckpt_path
        model.load_weights(checkpoint_path)
        print('Checkpoint loaded from ', checkpoint_path)

        named_input_tensors = {'input_image': input_image}
        if FLAGS.input_scales_list is None:
            named_input_tensors['input_scales'] = input_scales

        # Outputs to the exported model.
        named_output_tensors = {}
        if FLAGS.multi_scale_pool_type == 'None':
            named_output_tensors['global_descriptors'] = tf.identity(
                extracted_features, name='global_descriptors')
        else:
            named_output_tensors['global_descriptor'] = tf.identity(
                extracted_features, name='global_descriptor')

        # Export the model.
        signature_def = (
            tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
                inputs=_build_tensor_info(named_input_tensors),
                outputs=_build_tensor_info(named_output_tensors)))

        print('Exporting trained model to:', export_path)
        builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(
            export_path)

        init_op = None
        builder.add_meta_graph_and_variables(
            sess, [tf.compat.v1.saved_model.tag_constants.SERVING],
            signature_def_map={
                tf.compat.v1.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                signature_def
            },
            main_op=init_op)
        builder.save()