def main(unused_argv):
  # Read list of images.
  print('Reading list of images...')
  image_paths = _ReadImageList(cmd_args.list_images_path)
  num_images = len(image_paths)
  print(f'done! Found {num_images} images')

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

  # Create output directory if necessary.
  if not tf.io.gfile.exists(cmd_args.output_dir):
    tf.io.gfile.makedirs(cmd_args.output_dir)

  # Tell TensorFlow that the model will be built into the default Graph.
  with tf.Graph().as_default():
    with tf.compat.v1.Session() as sess:
      init_op = tf.compat.v1.global_variables_initializer()
      sess.run(init_op)

      extractor_fn = extractor.MakeExtractor(sess, config)

      start = time.clock()
      for i in range(num_images):
        # Write to log-info once in a while.
        if i == 0:
          print('Starting to extract DELF features from images...')
        elif i % _STATUS_CHECK_ITERATIONS == 0:
          elapsed = (time.clock() - start)
          print(
              f'Processing image {i} out of {num_images}, last '
              f'{_STATUS_CHECK_ITERATIONS} images took {elapsed} seconds'
              )
          start = time.clock()

        # If descriptor already exists, skip its computation.
        out_desc_filename = os.path.splitext(os.path.basename(
            image_paths[i]))[0] + _DELF_EXT
        out_desc_fullpath = os.path.join(cmd_args.output_dir, out_desc_filename)
        if tf.io.gfile.exists(out_desc_fullpath):
          print(f'Skipping {image_paths[i]}')
          continue

        im = np.array(utils.RgbLoader(image_paths[i]))

        # Extract and save features.
        extracted_features = extractor_fn(im)
        locations_out = extracted_features['local_features']['locations']
        descriptors_out = extracted_features['local_features']['descriptors']
        feature_scales_out = extracted_features['local_features']['scales']
        attention_out = extracted_features['local_features']['attention']

        feature_io.WriteToFile(out_desc_fullpath, locations_out,
                               feature_scales_out, descriptors_out,
                               attention_out)
def main(argv):
    if len(argv) > 1:
        raise RuntimeError('Too many command-line arguments.')

    tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)

    # Read list of query images from dataset file.
    tf.compat.v1.logging.info(
        'Reading list of query images and boxes from dataset file...')
    query_list, _, ground_truth = dataset.ReadDatasetFile(
        cmd_args.dataset_file_path)
    num_images = len(query_list)
    tf.compat.v1.logging.info('done! Found %d images', num_images)

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.io.gfile.GFile(cmd_args.delf_config_path, 'r') as f:
        text_format.Merge(f.read(), config)

    # Create output directory if necessary.
    if not tf.io.gfile.exists(cmd_args.output_features_dir):
        tf.io.gfile.makedirs(cmd_args.output_features_dir)

    with tf.Graph().as_default():
        with tf.compat.v1.Session() as sess:
            # Initialize variables, construct DELF extractor.
            init_op = tf.compat.v1.global_variables_initializer()
            sess.run(init_op)
            extractor_fn = extractor.MakeExtractor(sess, config)

            start = time.clock()
            for i in range(num_images):
                query_image_name = query_list[i]
                input_image_filename = os.path.join(
                    cmd_args.images_dir, query_image_name + _IMAGE_EXTENSION)
                output_feature_filename = os.path.join(
                    cmd_args.output_features_dir,
                    query_image_name + _DELF_EXTENSION)
                if tf.io.gfile.exists(output_feature_filename):
                    tf.compat.v1.logging.info('Skipping %s', query_image_name)
                    continue

                # Crop query image according to bounding box.
                bbox = [int(round(b)) for b in ground_truth[i]['bbx']]
                im = np.array(_PilLoader(input_image_filename).crop(bbox))

                # Extract and save features.
                (locations_out, descriptors_out, feature_scales_out,
                 attention_out) = extractor_fn(im)

                feature_io.WriteToFile(output_feature_filename, locations_out,
                                       feature_scales_out, descriptors_out,
                                       attention_out)

            elapsed = (time.clock() - start)
            print('Processed %d query images in %f seconds' %
                  (num_images, elapsed))
def extract_features(list_images_path,
                     output_dir,
                     config_path=r"src\delf_config.pbtxt"):
    output_dir = str(output_dir)

    # Read list of images.
    print('Reading list of images...')
    image_paths = [str(path) for path in list_images_path]
    num_images = len(image_paths)
    print(f'done! Found {num_images} images')

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

    # Create output directory if necessary.
    if not tf.io.gfile.exists(output_dir):
        tf.io.gfile.makedirs(output_dir)

    extractor_fn = extractor.MakeExtractor(config)

    start = time.time()
    for i in range(num_images):
        # Report progress once in a while.
        if i == 0:
            print('Starting to extract DELF features from images...')
        elif i % _STATUS_CHECK_ITERATIONS == 0:
            elapsed = (time.time() - start)
            print(f'Processing image {i} out of {num_images}, last '
                  f'{_STATUS_CHECK_ITERATIONS} images took {elapsed} seconds')
            start = time.time()

        # If descriptor already exists, skip its computation.
        out_desc_filename = os.path.splitext(os.path.basename(
            image_paths[i]))[0] + _DELF_EXT
        out_desc_fullpath = os.path.join(output_dir, out_desc_filename)
        if tf.io.gfile.exists(out_desc_fullpath):
            print(f'Skipping {image_paths[i]}')
            continue

        im = np.array(utils.RgbLoader(image_paths[i]))

        # Extract and save features.
        extracted_features = extractor_fn(im)
        locations_out = extracted_features['local_features']['locations']
        descriptors_out = extracted_features['local_features']['descriptors']
        feature_scales_out = extracted_features['local_features']['scales']
        attention_out = extracted_features['local_features']['attention']

        feature_io.WriteToFile(out_desc_fullpath, locations_out,
                               feature_scales_out, descriptors_out,
                               attention_out)
Пример #4
0
def main(unused_argv):
  tf.logging.set_verbosity(tf.logging.INFO)

  # Read list of images.
  tf.logging.info('Reading list of images...')
  image_paths = _ReadImageList(cmd_args.list_images_path)
  num_images = len(image_paths)
  tf.logging.info('done! Found %d images', num_images)

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

  # Create output directory if necessary.
  if not os.path.exists(cmd_args.output_dir):
    os.makedirs(cmd_args.output_dir)

  with tf.Graph().as_default():
      filename_queue = tf.train.string_input_producer(image_paths, shuffle=False)
      print("PRINTING THE fileNAME QUEUE", filename_queue)
      reader = tf.WholeFileReader()
      print("PRINTING THE reader", reader)
      _, value = reader.read(filename_queue)
      print("PRINTING the VALUE", value)
      image_data = 'data/oxford5k_images/hertford_000057.jpg'
      test_image_tf1 = tf.read_file(image_data)
      image_tf1 = tf.image.decode_jpeg(value, channels=3)
      image_tf1 = tf.image.resize_image_with_crop_or_pad(image_tf1,100,100)
      #image_tf1 = np.random.randn(100,100,3)
      print("THE IMAGE", image_tf1)
      """
      ADDED FOR CHECKING PURPOSE ONLY
      """
      imagetest = image_tf1
      imagetest = tf.expand_dims(imagetest, 0)
      print("size of tensor === ",imagetest)
      print("THE IMAGE FORMED", imagetest)
      print("MAKING FIRST FUNCTION")
      modelsssss = feature_extractor.BuildModel('resnet_v1_50/block3', 'softplus', 'use_l2_normalized_feature',1)
      print("CALLING THE MODEL")
      with tf.Session() as sess:
          # Initialize variables.
          init_op = tf.global_variables_initializer()
          print("SHOWING OPERATION", init_op)
          tf.logging.info('Starting session...')
          sess.run(init_op)
          tf.logging.info('Starting to load the models to be used...')
          feature_map, feature_map1 = modelsssss(imagetest, False, False)
          sess.run([feature_map,feature_map1])
          print(feature_map)
          print(feature_map1)
      print("CAME BACK TO ORIGINAL")
def main(delf_config_path, dataset_file_path, images_dir, output_features_dir):
    image_list = []
    image_list.append(dataset_file_path)
    num_images = len(image_list)
    print('done! Found %d images' % num_images)

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.io.gfile.GFile(delf_config_path, 'r') as f:
        text_format.Parse(f.read(), config)

    extractor_fn = extractor.MakeExtractor(config)

    start = time.time()
    for i in range(num_images):
        if i == 0:
            print('Starting to extract features...')
        elif i % _STATUS_CHECK_ITERATIONS == 0:
            elapsed = (time.time() - start)
            print('Processing image %d out of %d, last %d '
                  'images took %f seconds' %
                  (i, num_images, _STATUS_CHECK_ITERATIONS, elapsed))
            start = time.time()

        image_name = image_list[i]
        input_image_filename = os.path.join(images_dir,
                                            image_name + _IMAGE_EXTENSION)

        if config.use_local_features:
            output_local_feature_filename = os.path.join(
                output_features_dir, image_name + _DELG_LOCAL_EXTENSION)
            if not tf.io.gfile.exists(output_local_feature_filename):
                should_skip_local = False

        pil_im = utils.RgbLoader(input_image_filename)
        resize_factor = 1.0

        im = np.array(pil_im)

        # Extract and save features.
        extracted_features = extractor_fn(im, resize_factor)
        #if config.use_global_features:
        #  global_descriptor = extracted_features['global_descriptor']
        #  datum_io.WriteToFile(global_descriptor, output_global_feature_filename)
        if config.use_local_features:
            locations = extracted_features['local_features']['locations']
            descriptors = extracted_features['local_features']['descriptors']
            feature_scales = extracted_features['local_features']['scales']
            attention = extracted_features['local_features']['attention']
            feature_io.WriteToFile(output_local_feature_filename, locations,
                                   feature_scales, descriptors, attention)
def main(argv):
    if len(argv) > 1:
        raise RuntimeError('Too many command-line arguments.')

    # Read list of query images from dataset file.
    print('Reading list of query images and boxes from dataset file...')
    query_list, _, ground_truth = dataset.ReadDatasetFile(
        cmd_args.dataset_file_path)
    num_images = len(query_list)
    print(f'done! Found {num_images} images')

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.io.gfile.GFile(cmd_args.delf_config_path, 'r') as f:
        text_format.Merge(f.read(), config)

    # Create output directory if necessary.
    if not tf.io.gfile.exists(cmd_args.output_features_dir):
        tf.io.gfile.makedirs(cmd_args.output_features_dir)

    extractor_fn = extractor.MakeExtractor(config)

    start = time.time()
    for i in range(num_images):
        query_image_name = query_list[i]
        input_image_filename = os.path.join(
            cmd_args.images_dir, query_image_name + _IMAGE_EXTENSION)
        output_feature_filename = os.path.join(
            cmd_args.output_features_dir, query_image_name + _DELF_EXTENSION)
        if tf.io.gfile.exists(output_feature_filename):
            print(f'Skipping {query_image_name}')
            continue

        # Crop query image according to bounding box.
        bbox = [int(round(b)) for b in ground_truth[i]['bbx']]
        im = np.array(utils.RgbLoader(input_image_filename).crop(bbox))

        # Extract and save features.
        extracted_features = extractor_fn(im)
        locations_out = extracted_features['local_features']['locations']
        descriptors_out = extracted_features['local_features']['descriptors']
        feature_scales_out = extracted_features['local_features']['scales']
        attention_out = extracted_features['local_features']['attention']

        feature_io.WriteToFile(output_feature_filename, locations_out,
                               feature_scales_out, descriptors_out,
                               attention_out)

    elapsed = (time.time() - start)
    print('Processed %d query images in %f seconds' % (num_images, elapsed))
Пример #7
0
  def testResizeImageWorks(self, max_image_size, min_image_size, expected_shape,
                           expected_scale_factor):
    # Construct image of size 4x2x3.
    image = np.array([[[0, 0, 0], [1, 1, 1]], [[2, 2, 2], [3, 3, 3]],
                      [[4, 4, 4], [5, 5, 5]], [[6, 6, 6], [7, 7, 7]]],
                     dtype='uint8')

    # Set up config.
    config = delf_config_pb2.DelfConfig(
        max_image_size=max_image_size, min_image_size=min_image_size)

    resized_image, scale_factor = extractor.ResizeImage(image, config)
    self.assertAllEqual(resized_image.shape, expected_shape)
    self.assertAllClose(scale_factor, expected_scale_factor)
Пример #8
0
    def testResizeImageRoundingWorks(self, max_image_size, min_image_size,
                                     resize_factor, square_output,
                                     expected_shape, expected_scale_factors):
        # Construct image of size 3x2x3.
        image = np.array([[[0, 0, 0], [1, 1, 1]], [[2, 2, 2], [3, 3, 3]],
                          [[4, 4, 4], [5, 5, 5]]],
                         dtype='uint8')

        # Set up config.
        config = delf_config_pb2.DelfConfig(max_image_size=max_image_size,
                                            min_image_size=min_image_size)

        resized_image, scale_factors = extractor.ResizeImage(
            image, config, resize_factor, square_output)
        self.assertAllEqual(resized_image.shape, expected_shape)
        self.assertAllClose(scale_factors, expected_scale_factors)
    def __init__(self, config_path):
        tf.logging.set_verbosity(tf.logging.INFO)

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

        # Tell TensorFlow that the model will be built into the default Graph.
        with tf.Graph().as_default():
            self.sess = tf.Session()
            # Initialize variables.
            init_op = tf.global_variables_initializer()
            self.sess.run(init_op)

            # Loading model that will be used.
            tf.saved_model.loader.load(self.sess,
                                       [tf.saved_model.tag_constants.SERVING],
                                       self.config.model_path)
            self.graph = tf.get_default_graph()
Пример #10
0
def main(args, kwargs, centroids, n_values):
    """Generates graph."""
    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.gfile.FastGFile(args.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():
        with tf.Session() as sess:
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            extractor_fn = extractor.MakeExtractor(sess, config)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            bench(args, kwargs, sess, extractor_fn, centroids, n_values)
Пример #11
0
def main(argv):
    if len(argv) > 1:
        raise RuntimeError('Too many command-line arguments.')

    # Read list of images.
    print('Reading list of images...')
    image_paths = _ReadImageList(FLAGS.list_images_path)
    num_images = len(image_paths)
    print(f'done! Found {num_images} images')

    # Load images in memory.
    print('Loading images, %d times per image...' % FLAGS.repeat_per_image)
    im_array = []
    for filename in image_paths:
        im = np.array(utils.RgbLoader(filename))
        for _ in range(FLAGS.repeat_per_image):
            im_array.append(im)
    np.random.shuffle(im_array)
    print('done!')

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.io.gfile.GFile(FLAGS.delf_config_path, 'r') as f:
        text_format.Parse(f.read(), config)

    extractor_fn = extractor.MakeExtractor(config)

    start = time.time()
    for i, im in enumerate(im_array):
        if i == 0:
            print('Starting to extract DELF features from images...')
        elif i % _STATUS_CHECK_ITERATIONS == 0:
            elapsed = (time.time() - start)
            print(f'Processing image {i} out of {len(im_array)}, last '
                  f'{_STATUS_CHECK_ITERATIONS} images took {elapsed} seconds,'
                  f'ie {elapsed/_STATUS_CHECK_ITERATIONS} secs/image.')
            start = time.time()

        # Extract and save features.
        extracted_features = extractor_fn(im)
Пример #12
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)

    # Read list of images.
    tf.logging.info('Reading list of images...')
    image_paths = _ReadImageList(cmd_args.list_images_path)
    num_images = len(image_paths)
    tf.logging.info('done! Found %d images', num_images)

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

    # Create output directory if necessary.
    if not os.path.exists(cmd_args.output_dir):
        os.makedirs(cmd_args.output_dir)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Reading list of images.
        filename_queue = tf.train.string_input_producer(image_paths,
                                                        shuffle=False)
        reader = tf.WholeFileReader()
        _, value = reader.read(filename_queue)
        image_tf = tf.image.decode_jpeg(value, channels=3)

        with tf.Session() as sess:
            # Initialize variables.
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            # 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()
            input_image = graph.get_tensor_by_name('input_image:0')
            input_score_threshold = graph.get_tensor_by_name(
                'input_abs_thres:0')
            input_image_scales = graph.get_tensor_by_name('input_scales:0')
            input_max_feature_num = graph.get_tensor_by_name(
                'input_max_feature_num:0')
            boxes = graph.get_tensor_by_name('boxes:0')
            raw_descriptors = graph.get_tensor_by_name('features:0')
            feature_scales = graph.get_tensor_by_name('scales:0')
            attention_with_extra_dim = graph.get_tensor_by_name('scores:0')
            attention = tf.reshape(attention_with_extra_dim,
                                   [tf.shape(attention_with_extra_dim)[0]])

            locations, descriptors = feature_extractor.DelfFeaturePostProcessing(
                boxes, raw_descriptors, config)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start = time.clock()
            for i in range(num_images):
                # Write to log-info once in a while.
                if i == 0:
                    tf.logging.info(
                        'Starting to extract DELF features from images...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    tf.logging.info(
                        'Processing image %d out of %d, last %d '
                        'images took %f seconds', i, num_images,
                        _STATUS_CHECK_ITERATIONS, elapsed)
                    start = time.clock()

                # # Get next image.
                im = sess.run(image_tf)

                # If descriptor already exists, skip its computation.
                out_desc_filename = os.path.splitext(
                    os.path.basename(image_paths[i]))[0] + _DELF_EXT
                out_desc_fullpath = os.path.join(cmd_args.output_dir,
                                                 out_desc_filename)
                if tf.gfile.Exists(out_desc_fullpath):
                    tf.logging.info('Skipping %s', image_paths[i])
                    continue

                feed_dict = {
                    input_image: im,
                    input_score_threshold:
                    config.delf_local_config.score_threshold,
                    input_image_scales: list(config.image_scales),
                    input_max_feature_num:
                    config.delf_local_config.max_feature_num
                }

                options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
                run_metadata = tf.RunMetadata()

                # Extract and save features.
                (locations_out, descriptors_out, feature_scales_out,
                 attention_out) = sess.run(
                     [locations, descriptors, feature_scales, attention],
                     feed_dict=feed_dict,
                     options=options,
                     run_metadata=run_metadata)
                cg = CompGraph('delf_extract_features', run_metadata,
                               tf.get_default_graph())

                cg_tensor_dict = cg.get_tensors()
                cg_sorted_keys = sorted(cg_tensor_dict.keys())
                cg_sorted_items = []
                for cg_key in cg_sorted_keys:
                    cg_sorted_items.append(cg_tensor_dict[cg_key].shape)

                #cg_sorted_shape = sess.run(cg_sorted_items, feed_dict=feed_dict)
                cg.op_analysis(dict(zip(cg_sorted_keys, cg_sorted_items)),
                               'delf_extract_features.pickle')

                serialized_desc = feature_io.WriteToFile(
                    out_desc_fullpath, locations_out, feature_scales_out,
                    descriptors_out, attention_out)

            # Finalize enqueue threads.
            coord.request_stop()
            coord.join(threads)
Пример #13
0
def main(unused_argv):
    print('Export descriptor on hpatches.')
    print(cmd_args)
    tf.logging.set_verbosity(tf.logging.INFO)
    # Read list of images.
    tf.logging.info('Reading list of images...')
    image_paths = _ReadImageList(cmd_args.list_images_path)
    num_images = len(image_paths)
    tf.logging.info('done! Found %d images', num_images)
    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.gfile.FastGFile(cmd_args.config_path, 'r') as f:
        text_format.Merge(f.read(), config)

    # Create output directory if necessary.
    if os.path.isdir(cmd_args.output_dir) == False:
        os.mkdir(cmd_args.output_dir)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Reading list of images.
        filename_queue = tf.train.string_input_producer(image_paths, shuffle=False)
        reader = tf.WholeFileReader()
        _, value = reader.read(filename_queue)
        image_tf = tf.image.decode_png(value, channels=3)

        with tf.Session() as sess:
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            extractor_fn = extractor.MakeExtractor(sess, config)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start = time.clock()
            for i in range(num_images):
                # Write to log-info once in a while.
                if i == 0:
                    tf.logging.info('Starting to extract DELF features from images...')
                    # Output node names to file.
                    #node_names = [node.name for node in tf.get_default_graph().as_graph_def().node]
                    #f = open('node_names.txt', 'w')
                    #for name in node_names:
                    #    f.write(name + '\n')
                    #f.close()
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    tf.logging.info(
                        'Processing image %d out of %d, last %d '
                        'images took %f seconds', i, num_images, _STATUS_CHECK_ITERATIONS,
                        elapsed)
                    start = time.clock()

                # Get next image.
                patches = sess.run(image_tf)
                patches = patches.reshape((-1, 65, 65, 3))
                num_patches = patches.shape[0]

                input_dir, img_name = os.path.split(image_paths[i])
                out_desc_dir = os.path.join(cmd_args.output_dir, input_dir.split('/')[-1])
                out_desc_fullpath = os.path.join(out_desc_dir, img_name[:-3]+'csv')
                if os.path.isdir(out_desc_dir) == False:
                    os.mkdir(out_desc_dir)
                print(out_desc_fullpath)
                output_file = open(out_desc_fullpath, 'w')
                for i_patch in range(num_patches):
                    # Extract and save features.
                    im = patches[i_patch, :, :, :]
                    (locations_out, descriptors_out, feature_scales_out,
                        attention_out, feature_map_out) = extractor_fn(im)

                    # Output descriptors to file.
                    desc = feature_map_out[0, int(feature_map_out.shape[1]/2), int(feature_map_out.shape[2]/2), :]
                    for i_dim in range(desc.shape[0]-1):
                        output_file.write('{:>8.5f}'.format(desc[i_dim]) + ', ')
                    output_file.write(str(desc[i_dim+1]) + '\n')

                output_file.close()

            # Finalize enqueue threads.
            coord.request_stop()
            coord.join(threads)
    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)
Пример #15
0
def main(argv):
    if len(argv) > 1:
        raise RuntimeError('Too many command-line arguments.')

    # Read list of images from dataset file.
    print('Reading list of images from dataset file...')
    query_list, index_list, ground_truth = dataset.ReadDatasetFile(
        FLAGS.dataset_file_path)
    if FLAGS.image_set == 'query':
        image_list = query_list
    else:
        image_list = index_list
    num_images = len(image_list)
    print('done! Found %d images' % num_images)

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.io.gfile.GFile(FLAGS.delf_config_path, 'r') as f:
        text_format.Parse(f.read(), config)

    # Create output directory if necessary.
    if not tf.io.gfile.exists(FLAGS.output_features_dir):
        tf.io.gfile.makedirs(FLAGS.output_features_dir)

    with tf.Graph().as_default():
        with tf.compat.v1.Session() as sess:
            # Initialize variables, construct DELG extractor.
            init_op = tf.compat.v1.global_variables_initializer()
            sess.run(init_op)
            extractor_fn = extractor.MakeExtractor(sess, config)

            start = time.time()
            for i in range(num_images):
                if i == 0:
                    print('Starting to extract features...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.time() - start)
                    print('Processing image %d out of %d, last %d '
                          'images took %f seconds' %
                          (i, num_images, _STATUS_CHECK_ITERATIONS, elapsed))
                    start = time.time()

                image_name = image_list[i]
                input_image_filename = os.path.join(
                    FLAGS.images_dir, image_name + _IMAGE_EXTENSION)

                # Compose output file name and decide if image should be skipped.
                should_skip_global = True
                should_skip_local = True
                if config.use_global_features:
                    output_global_feature_filename = os.path.join(
                        FLAGS.output_features_dir,
                        image_name + _DELG_GLOBAL_EXTENSION)
                    if not tf.io.gfile.exists(output_global_feature_filename):
                        should_skip_global = False
                if config.use_local_features:
                    output_local_feature_filename = os.path.join(
                        FLAGS.output_features_dir,
                        image_name + _DELG_LOCAL_EXTENSION)
                    if not tf.io.gfile.exists(output_local_feature_filename):
                        should_skip_local = False
                if should_skip_global and should_skip_local:
                    print('Skipping %s' % image_name)
                    continue

                pil_im = utils.RgbLoader(input_image_filename)
                resize_factor = 1.0
                if FLAGS.image_set == 'query':
                    # Crop query image according to bounding box.
                    original_image_size = max(pil_im.size)
                    bbox = [int(round(b)) for b in ground_truth[i]['bbx']]
                    pil_im = pil_im.crop(bbox)
                    cropped_image_size = max(pil_im.size)
                    resize_factor = cropped_image_size / original_image_size

                im = np.array(pil_im)

                # Extract and save features.
                extracted_features = extractor_fn(im, resize_factor)
                if config.use_global_features:
                    global_descriptor = extracted_features['global_descriptor']
                    datum_io.WriteToFile(global_descriptor,
                                         output_global_feature_filename)
                if config.use_local_features:
                    locations = extracted_features['local_features'][
                        'locations']
                    descriptors = extracted_features['local_features'][
                        'descriptors']
                    feature_scales = extracted_features['local_features'][
                        'scales']
                    attention = extracted_features['local_features'][
                        'attention']
                    feature_io.WriteToFile(output_local_feature_filename,
                                           locations, feature_scales,
                                           descriptors, attention)
Пример #16
0
def main(unused_argv):
  tf.logging.set_verbosity(tf.logging.INFO)

  # Read list of images.
  tf.logging.info('Reading list of images...')
  image_paths = _ReadImageList(cmd_args.list_images_path)
  num_images = len(image_paths)
  tf.logging.info('done! Found %d images', num_images)

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

  # Create output directory if necessary.
  if not tf.gfile.Exists(cmd_args.output_dir):
    tf.gfile.MakeDirs(cmd_args.output_dir)

  # Tell TensorFlow that the model will be built into the default Graph.
  with tf.Graph().as_default():
    # Reading list of images.
    filename_queue = tf.train.string_input_producer(image_paths, shuffle=False)
    reader = tf.WholeFileReader()
    _, value = reader.read(filename_queue)
    image_tf = tf.image.decode_jpeg(value, channels=3)

    with tf.Session() as sess:
      init_op = tf.global_variables_initializer()
      sess.run(init_op)

      extractor_fn = extractor.MakeExtractor(sess, config)

      # Start input enqueue threads.
      coord = tf.train.Coordinator()
      threads = tf.train.start_queue_runners(sess=sess, coord=coord)
      start = time.clock()
      for i in range(num_images):
        # Write to log-info once in a while.
        if i == 0:
          tf.logging.info('Starting to extract DELF features from images...')
        elif i % _STATUS_CHECK_ITERATIONS == 0:
          elapsed = (time.clock() - start)
          tf.logging.info(
              'Processing image %d out of %d, last %d '
              'images took %f seconds', i, num_images, _STATUS_CHECK_ITERATIONS,
              elapsed)
          start = time.clock()

        # # Get next image.
        im = sess.run(image_tf)

        # If descriptor already exists, skip its computation.
        out_desc_filename = os.path.splitext(os.path.basename(
            image_paths[i]))[0] + _DELF_EXT
        out_desc_fullpath = os.path.join(cmd_args.output_dir, out_desc_filename)
        if tf.gfile.Exists(out_desc_fullpath):
          tf.logging.info('Skipping %s', image_paths[i])
          continue

        # Extract and save features.
        (locations_out, descriptors_out, feature_scales_out,
         attention_out) = extractor_fn(im)

        feature_io.WriteToFile(out_desc_fullpath, locations_out,
                               feature_scales_out, descriptors_out,
                               attention_out)

      # Finalize enqueue threads.
      coord.request_stop()
      coord.join(threads)
def get_feature_from_path(image_paths, config_path):
    """
  with filename queue, batch proces. 
  """
    tf.logging.set_verbosity(tf.logging.INFO)

    num_images = len(image_paths)
    tf.logging.info('done! Found %d images', num_images)

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

    location_np_list = []
    descriptor_np_list = []
    feature_scale_np_list = []
    attention_np_list = []

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Reading list of images.
        filename_queue = tf.train.string_input_producer(image_paths,
                                                        shuffle=False)
        reader = tf.WholeFileReader()
        _, value = reader.read(filename_queue)
        image_tf = tf.image.decode_jpeg(value, channels=3)

        with tf.Session() as sess:
            # Initialize variables.
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            # 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()
            input_image = graph.get_tensor_by_name('input_image:0')
            input_score_threshold = graph.get_tensor_by_name(
                'input_abs_thres:0')
            input_image_scales = graph.get_tensor_by_name('input_scales:0')
            input_max_feature_num = graph.get_tensor_by_name(
                'input_max_feature_num:0')
            boxes = graph.get_tensor_by_name('boxes:0')
            raw_descriptors = graph.get_tensor_by_name('features:0')
            feature_scales = graph.get_tensor_by_name('scales:0')
            attention_with_extra_dim = graph.get_tensor_by_name('scores:0')
            attention = tf.reshape(attention_with_extra_dim,
                                   [tf.shape(attention_with_extra_dim)[0]])

            locations, descriptors = feature_extractor.DelfFeaturePostProcessing(
                boxes, raw_descriptors, config)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start = time.clock()
            for i in range(num_images):
                # Write to log-info once in a while.
                if i == 0:
                    tf.logging.info(
                        'Starting to extract DELF features from images...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    tf.logging.info(
                        'Processing image %d out of %d, last %d '
                        'images took %f seconds', i, num_images,
                        _STATUS_CHECK_ITERATIONS, elapsed)
                    start = time.clock()

                # # Get next image.
                im = sess.run(image_tf)

                # Extract and save features.
                (locations_out, descriptors_out, feature_scales_out,
                 attention_out) = sess.run(
                     [locations, descriptors, feature_scales, attention],
                     feed_dict={
                         input_image:
                         im,
                         input_score_threshold:
                         config.delf_local_config.score_threshold,
                         input_image_scales:
                         list(config.image_scales),
                         input_max_feature_num:
                         config.delf_local_config.max_feature_num
                     })
                location_np_list.append(locations_out)
                descriptor_np_list.append(descriptors_out)
                feature_scale_np_list.append(feature_scales_out)
                attention_np_list.append(attention_out)

            # Finalize enqueue threads.
            coord.request_stop()
            coord.join(threads)

        return location_np_list, descriptor_np_list, feature_scale_np_list, attention_np_list
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)

    # Read list of images.
    tf.logging.info('Reading list of images...')
    image_paths = _ReadImageList(cmd_args.list_images_path)
    num_images = len(image_paths)
    tf.logging.info('done! Found %d images', num_images)

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

    # Create output directory if necessary.
    if not os.path.exists(cmd_args.output_dir):
        os.makedirs(cmd_args.output_dir)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Reading list of images.
        filename_queue = tf.train.string_input_producer(image_paths,
                                                        shuffle=False)
        print("PRINTING THE fileNAME QUEUE", filename_queue)
        reader = tf.WholeFileReader()
        print("PRINTING THE reader", reader)
        _, value = reader.read(filename_queue)
        print("PRINTING the VALUE", value)
        image_tf = tf.image.decode_jpeg(value, channels=3)
        print("THE IMAGE", image_tf)
        """
    ADDED FOR CHECKING PURPOSE ONLY
    """
        imagetest = image_tf
        imagetest = tf.expand_dims(imagetest, 0)
        print("size of tensor === ", imagetest)
        print("THE IMAGE FORMED", imagetest)
        print("MAKING FIRST FUNCTION")
        modelsssss = feature_extractor.BuildModel('resnet_v1_50/block3',
                                                  'softplus',
                                                  'use_l2_normalized_feature',
                                                  1)
        print("CALLING THE MODEL")
        buildedModel, _ = modelsssss(imagetest, False, False)
        print("CAME BACK TO ORIGINAL")

        with tf.Session() as sess:
            # Initialize variables.
            init_op = tf.global_variables_initializer()
            print("SHOWING OPERATION", init_op)
            tf.logging.info('Starting session...')
            sess.run(init_op)
            tf.logging.info('Starting to load the models to be used...')
            # 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()
            input_image = graph.get_tensor_by_name('input_image:0')
            input_score_threshold = graph.get_tensor_by_name(
                'input_abs_thres:0')
            input_image_scales = graph.get_tensor_by_name('input_scales:0')
            input_max_feature_num = graph.get_tensor_by_name(
                'input_max_feature_num:0')
            boxes = graph.get_tensor_by_name('boxes:0')
            raw_descriptors = graph.get_tensor_by_name('features:0')
            feature_scales = graph.get_tensor_by_name('scales:0')
            attention_with_extra_dim = graph.get_tensor_by_name('scores:0')
            attention = tf.reshape(attention_with_extra_dim,
                                   [tf.shape(attention_with_extra_dim)[0]])

            locations, descriptors = feature_extractor.DelfFeaturePostProcessing(
                boxes, raw_descriptors, config)
            tf.logging.info('Finished loading the models ...')
            print('Hi')
            print('------------------------locations--------------- = ',
                  locations)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start = time.clock()
            for i in range(num_images):
                # Write to log-info once in a while.
                if i == 0:
                    tf.logging.info(
                        'Starting to extract DELF features from images...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    tf.logging.info(
                        'Processing image %d out of %d, last %d '
                        'images took %f seconds', i, num_images,
                        _STATUS_CHECK_ITERATIONS, elapsed)
                    start = time.clock()

                # # Get next image.
                im = sess.run(image_tf)

                # If descriptor already exists, skip its computation.
                out_desc_filename = os.path.splitext(
                    os.path.basename(image_paths[i]))[0] + _DELF_EXT
                out_desc_fullpath = os.path.join(cmd_args.output_dir,
                                                 out_desc_filename)
                if tf.gfile.Exists(out_desc_fullpath):
                    tf.logging.info('Skipping %s', image_paths[i])
                    continue

                # Extract and save features.
                (locations_out, descriptors_out, feature_scales_out,
                 attention_out) = sess.run(
                     [locations, descriptors, feature_scales, attention],
                     feed_dict={
                         input_image:
                         im,
                         input_score_threshold:
                         config.delf_local_config.score_threshold,
                         input_image_scales:
                         list(config.image_scales),
                         input_max_feature_num:
                         config.delf_local_config.max_feature_num
                     })

                feature_io.WriteToFile(out_desc_fullpath, locations_out,
                                       feature_scales_out, descriptors_out,
                                       attention_out)

            # Finalize enqueue threads.
            coord.request_stop()
            coord.join(threads)
Пример #19
0
def main(unused_argv):
  tf.logging.set_verbosity(tf.logging.INFO)

  # Read list of images.
  tf.logging.info('Reading list of images...')
  image_paths = _ReadImageList(cmd_args.list_images_path, cmd_args.prefix)
  num_images = len(image_paths)
  tf.logging.info('done! Found %d images', num_images)

  # Parse DelfConfig proto.
  config = delf_config_pb2.DelfConfig()
  with tf.gfile.FastGFile(cmd_args.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():
    # Reading list of images.
    filename_queue = tf.train.string_input_producer(image_paths, shuffle=False)
    reader = tf.WholeFileReader()
    _, value = reader.read(filename_queue)
    image_tf = tf.image.decode_jpeg(value, channels=3)
    image_tf = tf.image.resize(image_tf, (448, 448))

    with tf.Session() as sess:
      init_op = tf.global_variables_initializer()
      sess.run(init_op)

      extractor_fn = extractor.MakeExtractor(sess, config)

      # Start input enqueue threads.
      coord = tf.train.Coordinator()
      threads = tf.train.start_queue_runners(sess=sess, coord=coord)
      start = time.clock()
      for i in range(num_images):
        # Write to log-info once in a while.
        if i == 0:
          tf.logging.info('Starting to extract DELF features from images...')
        elif i % _STATUS_CHECK_ITERATIONS == 0:
          elapsed = (time.clock() - start)
          tf.logging.info(
              'Processing image %d out of %d, last %d '
              'images took %f seconds', i, num_images, _STATUS_CHECK_ITERATIONS,
              elapsed)
          start = time.clock()

        # # Get next image.
        im = sess.run(image_tf)

        # If descriptor already exists, skip its computation.
        save_path = image_paths[i]
        save_path = save_path.replace('undist_images', 'reg_feat')
        save_path = save_path.replace('.jpg', '.bin')
        if cmd_args.skip_extracted and tf.gfile.Exists(save_path):
          tf.logging.info('Skipping %s', image_paths[i])
          continue

        # Extract and save features.
        reg_feat_out = extractor_fn(im)
        reg_feat_out = np.squeeze(reg_feat_out, axis=0)

        dir_name = os.path.dirname(save_path)
        if not os.path.exists(dir_name):
            os.mkdir(dir_name)
        reg_feat_out.astype(np.float32).tofile(save_path)

      # Finalize enqueue threads.
      coord.request_stop()
      coord.join(threads)
Пример #20
0
def ExtractBoxesAndFeaturesToFiles(image_names, image_paths, delf_config_path,
                                   detector_model_dir, detector_thresh,
                                   output_features_dir, output_boxes_dir,
                                   output_mapping):
    """Extracts boxes and features, saving them to files.

  Boxes are saved to <image_name>.boxes files. DELF features are extracted for
  the entire image and saved into <image_name>.delf files. In addition, DELF
  features are extracted for each high-confidence bounding box in the image, and
  saved into files named <image_name>_0.delf, <image_name>_1.delf, etc.

  It checks if descriptors/boxes already exist, and skips computation for those.

  Args:
    image_names: List of image names. These are used to compose output file
      names for boxes and features.
    image_paths: List of image paths. image_paths[i] is the path for the image
      named by image_names[i]. `image_names` and `image_paths` must have the
      same number of elements.
    delf_config_path: Path to DelfConfig proto text file.
    detector_model_dir: Directory where detector SavedModel is located.
    detector_thresh: Threshold used to decide if an image's detected box
      undergoes feature extraction.
    output_features_dir: Directory where DELF features will be written to.
    output_boxes_dir: Directory where detected boxes will be written to.
    output_mapping: CSV file which maps each .delf file name to the image ID and
      detected box ID.

  Raises:
    ValueError: If len(image_names) and len(image_paths) are different.
  """
    num_images = len(image_names)
    if len(image_paths) != num_images:
        raise ValueError(
            'image_names and image_paths have different number of items')

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.io.gfile.GFile(delf_config_path, 'r') as f:
        text_format.Merge(f.read(), config)

    # Create output directories if necessary.
    if not tf.io.gfile.exists(output_features_dir):
        tf.io.gfile.makedirs(output_features_dir)
    if not tf.io.gfile.exists(output_boxes_dir):
        tf.io.gfile.makedirs(output_boxes_dir)
    if not tf.io.gfile.exists(os.path.dirname(output_mapping)):
        tf.io.gfile.makedirs(os.path.dirname(output_mapping))

    names_ids_and_boxes = []
    with tf.Graph().as_default():
        with tf.compat.v1.Session() as sess:
            # Initialize variables, construct detector and DELF extractor.
            init_op = tf.compat.v1.global_variables_initializer()
            sess.run(init_op)
            detector_fn = detector.MakeDetector(sess,
                                                detector_model_dir,
                                                import_scope='detector')
            delf_extractor_fn = extractor.MakeExtractor(
                sess, config, import_scope='extractor_delf')

            start = time.clock()
            for i in range(num_images):
                if i == 0:
                    print('Starting to extract features/boxes...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    print('Processing image %d out of %d, last %d '
                          'images took %f seconds' %
                          (i, num_images, _STATUS_CHECK_ITERATIONS, elapsed))
                    start = time.clock()

                image_name = image_names[i]
                output_feature_filename_whole_image = os.path.join(
                    output_features_dir, image_name + _DELF_EXTENSION)
                output_box_filename = os.path.join(output_boxes_dir,
                                                   image_name + _BOX_EXTENSION)

                pil_im = utils.RgbLoader(image_paths[i])
                width, height = pil_im.size

                # Extract and save boxes.
                if tf.io.gfile.exists(output_box_filename):
                    print('Skipping box computation for %s' % image_name)
                    (boxes_out, scores_out, class_indices_out
                     ) = box_io.ReadFromFile(output_box_filename)
                else:
                    (boxes_out, scores_out, class_indices_out) = detector_fn(
                        np.expand_dims(pil_im, 0))
                    # Using only one image per batch.
                    boxes_out = boxes_out[0]
                    scores_out = scores_out[0]
                    class_indices_out = class_indices_out[0]
                    box_io.WriteToFile(output_box_filename, boxes_out,
                                       scores_out, class_indices_out)

                # Select boxes with scores greater than threshold. Those will be the
                # ones with extracted DELF features (besides the whole image, whose DELF
                # features are extracted in all cases).
                num_delf_files = 1
                selected_boxes = []
                for box_ind, box in enumerate(boxes_out):
                    if scores_out[box_ind] >= detector_thresh:
                        selected_boxes.append(box)
                num_delf_files += len(selected_boxes)

                # Extract and save DELF features.
                for delf_file_ind in range(num_delf_files):
                    if delf_file_ind == 0:
                        box_name = image_name
                        output_feature_filename = output_feature_filename_whole_image
                    else:
                        box_name = image_name + '_' + str(delf_file_ind - 1)
                        output_feature_filename = os.path.join(
                            output_features_dir, box_name + _DELF_EXTENSION)

                    names_ids_and_boxes.append(
                        [box_name, i, delf_file_ind - 1])

                    if tf.io.gfile.exists(output_feature_filename):
                        print('Skipping DELF computation for %s' % box_name)
                        continue

                    if delf_file_ind >= 1:
                        bbox_for_cropping = selected_boxes[delf_file_ind - 1]
                        bbox_for_cropping_pil_convention = [
                            int(math.floor(bbox_for_cropping[1] * width)),
                            int(math.floor(bbox_for_cropping[0] * height)),
                            int(math.ceil(bbox_for_cropping[3] * width)),
                            int(math.ceil(bbox_for_cropping[2] * height))
                        ]
                        pil_cropped_im = pil_im.crop(
                            bbox_for_cropping_pil_convention)
                        im = np.array(pil_cropped_im)
                    else:
                        im = np.array(pil_im)

                    extracted_features = delf_extractor_fn(im)
                    locations_out = extracted_features['local_features'][
                        'locations']
                    descriptors_out = extracted_features['local_features'][
                        'descriptors']
                    feature_scales_out = extracted_features['local_features'][
                        'scales']
                    attention_out = extracted_features['local_features'][
                        'attention']

                    feature_io.WriteToFile(output_feature_filename,
                                           locations_out, feature_scales_out,
                                           descriptors_out, attention_out)

    # Save mapping from output DELF name to image id and box id.
    _WriteMappingBasenameToIds(names_ids_and_boxes, output_mapping)
Пример #21
0
    def __init__(self, model_path=None, use_hub=False):

        # Parse DelfConfig proto.
        delf_config = delf_config_pb2.DelfConfig()
        delf_config_path = 'delf_config.pbtxt'
        with tf.gfile.FastGFile(delf_config_path, 'r') as f:
            text_format.Merge(f.read(), delf_config)

        tf.reset_default_graph()
        tf.logging.set_verbosity(tf.logging.FATAL)

        if use_hub:
            import tensorflow_hub as hub

            # set hub model
            self.image_placeholder = tf.placeholder(shape=(None, None, 3),
                                                    dtype=tf.float32)
            #             hub_module = hub.Module("https://tfhub.dev/google/delf/1")
            hub_module = hub.Module('https://modeldepot.io/mikeshi/delf')
            print("use modeldepot")

            # input setting module input
            module_inputs = {
                'image':
                self.image_placeholder,
                'score_threshold':
                delf_config.delf_local_config.score_threshold,
                'image_scales':
                tf.constant([round(v, 4) for v in delf_config.image_scales]),
                'max_feature_num':
                delf_config.delf_local_config.max_feature_num
            }

            # get end points
            self.end_points = hub_module(module_inputs, as_dict=True)

            # get session
            self.sess = tf.Session(config=tf.ConfigProto(
                allow_soft_placement=True, log_device_placement=True))

            # global initializer
            self.sess.run(tf.global_variables_initializer())
            self.sess.run(tf.tables_initializer())

        else:
            # check ckpt file
            assert tf.train.checkpoint_exists(
                model_path), "{} is not a tensorflow checkpoint file".format(
                    model_path)
            ckpt = tf.train.latest_checkpoint(model_path)

            # set placeholder
            self.image_placeholder = tf.placeholder(shape=(224, 224, 3),
                                                    dtype=tf.float32)

            # set graph input
            graph_inputs = {
                'image': self.image_placeholder,
                'config': delf_config
            }

            # build and get end points of graph
            self.end_points = build_delf_graph(graph_inputs)

            # get session
            self.sess = tf.Session(config=tf.ConfigProto(
                allow_soft_placement=True, log_device_placement=True))

            # global initializer
            self.sess.run(tf.global_variables_initializer())

            # load weight from ckpt file
            restore_var = [
                v for v in tf.global_variables() if 'resnet' in v.name
            ]
            saver = tf.train.Saver(restore_var)
            saver.restore(self.sess, ckpt)

        # 1.3
        dim = 40  # dimension
        n_subq = 8  # number of sub-quantizers
        n_centroids = 32  # number of centroids for each sub-vector
        n_bits = 5  # number of bits for each sub-vector
        n_probe = 3  # number of voronoi cell to explore
        coarse_quantizer = faiss.IndexFlatL2(dim)
        pq = faiss.IndexIVFPQ(coarse_quantizer, dim, n_centroids, n_subq,
                              n_bits)
        pq.nprobe = n_probe
        self.pq = pq
        print('PQ complete')
def main(unused_argv):
    os.environ["CUDA_VISIBLE_DEVICES"] = cmd_args.cuda_device

    tf.logging.set_verbosity(tf.logging.INFO)

    # Read list of images.
    tf.logging.info('Reading list of images...')
    image_paths = _ReadImageList(cmd_args.curr_idx)
    num_images = len(image_paths)
    tf.logging.info('done! Found %d images', num_images)

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

    # Create output directory if necessary.
    if not os.path.exists(cmd_args.output_dir):
        os.makedirs(cmd_args.output_dir, exist_ok=True)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Reading list of images.
        filename_queue = tf.train.string_input_producer(image_paths,
                                                        shuffle=False)
        reader = tf.WholeFileReader()
        _, value = reader.read(filename_queue)
        image_tf = tf.image.decode_image(value, channels=3)

        count = 0
        with tf.Session(config=sess_config) as sess:
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            extractor_fn = MakeExtractor(sess, config)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start = time.clock()
            for i in range(num_images):
                # Write to log-info once in a while.
                if i == 0:
                    tf.logging.info(
                        'Starting to extract DELF features from images...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    tf.logging.info(
                        'Processing image %d out of %d, last %d '
                        'images took %f seconds', i, num_images,
                        _STATUS_CHECK_ITERATIONS, elapsed)
                    start = time.clock()

                try:
                    im = sess.run(image_tf)
                except tf.errors.NotFoundError as e:
                    count += 1
                    tf.logging.warning(
                        f"{'*' * 15} Decode error {e} - {count}")
                    continue

                if len(im.shape) == 4 or im.shape[-1] != 3:
                    tf.logging.warning(
                        f"{'*' * 15} Skipping image {i} which probably is a GIF or broken image"
                    )
                    continue

                # If descriptor already exists, skip its computation.
                out_desc_filename = os.path.splitext(
                    os.path.basename(image_paths[i]))[0] + _DELF_EXT
                out_desc_parent_dir = os.path.join(
                    cmd_args.output_dir, '/'.join(out_desc_filename[:3]))
                if not os.path.exists(out_desc_parent_dir):
                    os.makedirs(out_desc_parent_dir)

                out_desc_fullpath = os.path.join(out_desc_parent_dir,
                                                 out_desc_filename)
                if tf.gfile.Exists(out_desc_fullpath):
                    tf.logging.info('Skipping %s', image_paths[i])
                    continue

                # Extract and save features.
                (locations_out, descriptors_out, feature_scales_out,
                 attention_out) = extractor_fn(im)

                feature_io.WriteToFile(out_desc_fullpath, locations_out,
                                       feature_scales_out, descriptors_out,
                                       attention_out)

            # Finalize enqueue threads.
            coord.request_stop()
            coord.join(threads)
Пример #23
0
def main(*args):
  args = parse_args()
  print('delf.main: args=', vars(args), file=sys.stderr)
  
  image_paths = [path.rstrip() for path in sys.stdin]
  
  # Config
  config = delf_config_pb2.DelfConfig()
  with tf.gfile.FastGFile(args.config_path, 'r') as f:
    text_format.Merge(f.read(), config)
  
  with tf.Graph().as_default():
    
    # --
    # IO
    filename_queue = tf.train.string_input_producer(image_paths, shuffle=False)
    reader         = tf.WholeFileReader()
    _, value       = reader.read(filename_queue)
    image_tf       = tf.image.decode_jpeg(value, channels=3)
    
    with tf.Session() as sess:
      
      # --
      # Define graph
      
      init_op = tf.global_variables_initializer()
      sess.run(init_op)
      
      tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], os.path.expanduser(config.model_path))
      
      graph = tf.get_default_graph()
      
      input_image              = graph.get_tensor_by_name('input_image:0')
      input_score_threshold    = graph.get_tensor_by_name('input_abs_thres:0')
      input_image_scales       = graph.get_tensor_by_name('input_scales:0')
      input_max_feature_num    = graph.get_tensor_by_name('input_max_feature_num:0')
      boxes                    = graph.get_tensor_by_name('boxes:0')
      raw_descriptors          = graph.get_tensor_by_name('features:0')
      feature_scales           = graph.get_tensor_by_name('scales:0')
      attention_with_extra_dim = graph.get_tensor_by_name('scores:0')
      attention                = tf.reshape(attention_with_extra_dim, [tf.shape(attention_with_extra_dim)[0]])
      
      locations, descriptors = feature_extractor.DelfFeaturePostProcessing(boxes, raw_descriptors, config)
      
      coord = tf.train.Coordinator()
      threads = tf.train.start_queue_runners(sess=sess, coord=coord)
      
      # --
      # Run on images
      
      for image_path in tqdm(image_paths):
        
        if args.hash_filenames:
          outpath = md5(image_path).hexdigest()
        else:
          outpath = os.path.splitext(os.path.basename(image_path))[0]
        
        outpath = os.path.join(args.output_dir, outpath  + '.delf')
        
        img = sess.run(image_tf)
        
        print(json.dumps({"path" : image_path, "key"  : outpath}))
        sys.stdout.flush()
        
        # Extract and save features.
        (locations_out, descriptors_out, feature_scales_out, attention_out) = sess.run(
             [locations, descriptors, feature_scales, attention],
             feed_dict={
                 input_image            : img,
                 input_score_threshold  : config.delf_local_config.score_threshold,
                 input_image_scales     : list(config.image_scales),
                 input_max_feature_num  : config.delf_local_config.max_feature_num
             })
        
        if not args.to_h5:
          _ = feature_io.WriteToFile(outpath, locations_out, feature_scales_out, descriptors_out, attention_out)
        else:
          raise Exception('delf.__main__.py: not implemented yet')
      
      coord.request_stop()
      coord.join(threads)
Пример #24
0
def main(csv_file, image_base_path, output_path):
    image_paths = []
    with open(csv_file, 'r') as f:
        f.readline()
        for line in f:
            id_ = line.split(',')[0]
            image_paths.append(image_base_path + '/' + id_[0] + '/' + id_[1] +
                               '/' + id_[2] + '/' + id_ + '.jpg')
    num_images = len(image_paths)

    tf.logging.set_verbosity(tf.logging.INFO)
    tf.logging.info('done! Found %d images', num_images)

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

    # Create output directory if necessary.
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Reading list of images.
        filename_queue = tf.train.string_input_producer(image_paths,
                                                        shuffle=False)
        reader = tf.WholeFileReader()
        _, value = reader.read(filename_queue)
        image_tf = tf.image.decode_jpeg(value, channels=3)

        with tf.Session() as sess:
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            extractor_fn = MakeExtractor(sess, config)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start = time.clock()
            for i in tqdm.tqdm(range(num_images)):
                # Write to log-info once in a while.
                if i == 0:
                    tf.logging.info(
                        'Starting to extract DELF features from images...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    tf.logging.info(
                        'Processing image %d out of %d, last %d '
                        'images took %f seconds', i, num_images,
                        _STATUS_CHECK_ITERATIONS, elapsed)
                    start = time.clock()

                # # Get next image.
                im = sess.run(image_tf)

                # If descriptor already exists, skip its computation.
                out_desc_filename = os.path.splitext(
                    os.path.basename(image_paths[i]))[0] + '.npz'
                out_desc_fullpath = os.path.join(output_path,
                                                 out_desc_filename)
                if tf.gfile.Exists(out_desc_fullpath):
                    tf.logging.info('Skipping %s', image_paths[i])
                    continue

                # Extract and save features.
                (locations_out, descriptors_out, feature_scales_out,
                 attention_out) = extractor_fn(im)

                np.savez(out_desc_fullpath,
                         loc=locations_out,
                         desc=descriptors_out.astype(np.float32),
                         feat=feature_scales_out)

            # Finalize enqueue threads.
            coord.request_stop()
            coord.join(threads)
def createFeatures(listpath, configpath, outputpath):


  # Read list of images.
  #print ('Reading list of images')
  image_paths = _ReadImageList(listpath)
  num_images = len(image_paths)
  #print ('done! Found %d images', num_images)

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

  # Create output directory if necessary.
  if not os.path.exists(outputpath):
    os.makedirs(outputpath)

  # Tell TensorFlow that the model will be built into the default Graph.
  with tf.Graph().as_default():
    # Reading list of images.
    filename_queue = tf.train.string_input_producer(image_paths, shuffle=False)
    reader = tf.WholeFileReader()
    _, value = reader.read(filename_queue)
    image_tf = tf.image.decode_jpeg(value, channels=3)

    with tf.Session() as sess:
      # Initialize variables.
      init_op = tf.global_variables_initializer()
      sess.run(init_op)

      # 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()
      input_image = graph.get_tensor_by_name('input_image:0')
      input_score_threshold = graph.get_tensor_by_name('input_abs_thres:0')
      input_image_scales = graph.get_tensor_by_name('input_scales:0')
      input_max_feature_num = graph.get_tensor_by_name(
          'input_max_feature_num:0')
      boxes = graph.get_tensor_by_name('boxes:0')
      raw_descriptors = graph.get_tensor_by_name('features:0')
      feature_scales = graph.get_tensor_by_name('scales:0')
      attention_with_extra_dim = graph.get_tensor_by_name('scores:0')
      attention = tf.reshape(attention_with_extra_dim,
                             [tf.shape(attention_with_extra_dim)[0]])

      locations, descriptors = feature_extractor.DelfFeaturePostProcessing(
          boxes, raw_descriptors, config)

      # Start input enqueue threads.
      coord = tf.train.Coordinator()
      threads = tf.train.start_queue_runners(sess=sess, coord=coord)
      start = clock()
      for i in range(num_images):
        # Write to log-info once in a while.
        if i == 0:
          print('Starting to extract DELF features from images...')
        elif i % _STATUS_CHECK_ITERATIONS == 0:
          elapsed = (clock() - start)
          #print ('Processing image %d out of %d, last %d '
                          #'images took %f seconds', i, num_images,
                          #_STATUS_CHECK_ITERATIONS, elapsed)
          start = clock()

        # # Get next image.
        im = sess.run(image_tf)

        # If descriptor already exists, skip its computation.
        out_desc_filename = os.path.splitext(os.path.basename(
            image_paths[i]))[0] + _DELF_EXT
        out_desc_fullpath = os.path.join(outputpath, out_desc_filename)
        if tf.gfile.Exists(out_desc_fullpath):
          #print('Skipping %s', image_paths[i])
          continue

        # Extract and save features.
        (locations_out, descriptors_out, feature_scales_out,
         attention_out) = sess.run(
             [locations, descriptors, feature_scales, attention],
             feed_dict={
                 input_image:
                     im,
                 input_score_threshold:
                     config.delf_local_config.score_threshold,
                 input_image_scales:
                     list(config.image_scales),
                 input_max_feature_num:
                     config.delf_local_config.max_feature_num
             })

        feature_io.WriteToFile(out_desc_fullpath, locations_out,
                               feature_scales_out, descriptors_out,
                               attention_out)

      # Finalize enqueue threads.
      coord.request_stop()
      coord.join(threads)
def main(argv):
    if len(argv) > 1:
        raise RuntimeError('Too many command-line arguments.')

    tf.logging.set_verbosity(tf.logging.INFO)

    # Read list of index images from dataset file.
    tf.logging.info('Reading list of index images from dataset file...')
    _, index_list, _ = dataset.ReadDatasetFile(cmd_args.dataset_file_path)
    num_images = len(index_list)
    tf.logging.info('done! Found %d images', num_images)

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.gfile.GFile(cmd_args.delf_config_path, 'r') as f:
        text_format.Merge(f.read(), config)

    # Create output directories if necessary.
    if not os.path.exists(cmd_args.output_features_dir):
        os.makedirs(cmd_args.output_features_dir)
    if not os.path.exists(cmd_args.output_boxes_dir):
        os.makedirs(cmd_args.output_boxes_dir)

    index_names_ids_and_boxes = []
    with tf.Graph().as_default():
        with tf.Session() as sess:
            # Initialize variables, construct detector and DELF extractor.
            init_op = tf.global_variables_initializer()
            sess.run(init_op)
            detector_fn = extract_boxes.MakeDetector(
                sess, cmd_args.detector_model_dir, import_scope='detector')
            delf_extractor_fn = extract_features.MakeExtractor(
                sess, config, import_scope='extractor_delf')

            start = time.clock()
            for i in range(num_images):
                if i == 0:
                    print(
                        'Starting to extract features/boxes from index images...'
                    )
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    print('Processing index image %d out of %d, last %d '
                          'images took %f seconds' %
                          (i, num_images, _STATUS_CHECK_ITERATIONS, elapsed))
                    start = time.clock()

                index_image_name = index_list[i]
                input_image_filename = os.path.join(
                    cmd_args.images_dir, index_image_name + _IMAGE_EXTENSION)
                output_feature_filename_whole_image = os.path.join(
                    cmd_args.output_features_dir,
                    index_image_name + _DELF_EXTENSION)
                output_box_filename = os.path.join(
                    cmd_args.output_boxes_dir,
                    index_image_name + _BOX_EXTENSION)

                pil_im = _PilLoader(input_image_filename)
                width, height = pil_im.size

                # Extract and save boxes.
                if tf.gfile.Exists(output_box_filename):
                    tf.logging.info('Skipping box computation for %s',
                                    index_image_name)
                    (boxes_out, scores_out, class_indices_out
                     ) = box_io.ReadFromFile(output_box_filename)
                else:
                    (boxes_out, scores_out, class_indices_out) = detector_fn(
                        np.expand_dims(pil_im, 0))
                    # Using only one image per batch.
                    boxes_out = boxes_out[0]
                    scores_out = scores_out[0]
                    class_indices_out = class_indices_out[0]
                    box_io.WriteToFile(output_box_filename, boxes_out,
                                       scores_out, class_indices_out)

                # Select boxes with scores greater than threshold. Those will be the
                # ones with extracted DELF features (besides the whole image, whose DELF
                # features are extracted in all cases).
                num_delf_files = 1
                selected_boxes = []
                for box_ind, box in enumerate(boxes_out):
                    if scores_out[box_ind] >= cmd_args.detector_thresh:
                        selected_boxes.append(box)
                num_delf_files += len(selected_boxes)

                # Extract and save DELF features.
                for delf_file_ind in range(num_delf_files):
                    if delf_file_ind == 0:
                        index_box_name = index_image_name
                        output_feature_filename = output_feature_filename_whole_image
                    else:
                        index_box_name = index_image_name + '_' + str(
                            delf_file_ind - 1)
                        output_feature_filename = os.path.join(
                            cmd_args.output_features_dir,
                            index_box_name + _DELF_EXTENSION)

                    index_names_ids_and_boxes.append(
                        [index_box_name, i, delf_file_ind - 1])

                    if tf.gfile.Exists(output_feature_filename):
                        tf.logging.info('Skipping DELF computation for %s',
                                        index_box_name)
                        continue

                    if delf_file_ind >= 1:
                        bbox_for_cropping = selected_boxes[delf_file_ind - 1]
                        bbox_for_cropping_pil_convention = [
                            int(math.floor(bbox_for_cropping[1] * width)),
                            int(math.floor(bbox_for_cropping[0] * height)),
                            int(math.ceil(bbox_for_cropping[3] * width)),
                            int(math.ceil(bbox_for_cropping[2] * height))
                        ]
                        pil_cropped_im = pil_im.crop(
                            bbox_for_cropping_pil_convention)
                        im = np.array(pil_cropped_im)
                    else:
                        im = np.array(pil_im)

                    (locations_out, descriptors_out, feature_scales_out,
                     attention_out) = delf_extractor_fn(im)

                    feature_io.WriteToFile(output_feature_filename,
                                           locations_out, feature_scales_out,
                                           descriptors_out, attention_out)

    # Save mapping from output DELF name to index image id and box id.
    _WriteMappingBasenameToIds(index_names_ids_and_boxes,
                               cmd_args.output_index_mapping)