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)
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))
Пример #6
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)
Пример #7
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)
Пример #8
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)
Пример #9
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)
Пример #10
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)
Пример #11
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)
Пример #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, 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)