예제 #1
0
 def testWriteAndReadToFile(self):
     data = np.array([[[-1.0, 125.0, -2.5], [14.5, 3.5, 0.0]],
                      [[20.0, 0.0, 30.0], [25.5, 36.0, 42.0]]])
     filename = os.path.join(FLAGS.test_tmpdir, 'test.datum')
     datum_io.WriteToFile(data, filename)
     data_read = datum_io.ReadFromFile(filename)
     self.assertAllEqual(data_read, data)
예제 #2
0
def ExtractAggregatedRepresentationsToFiles(image_names, features_dir,
                                            aggregation_config_path,
                                            mapping_path,
                                            output_aggregation_dir):
    """Extracts aggregated feature representations, saving them to files.

  It checks if the aggregated representation for an image already exists,
  and skips computation for those.

  Args:
    image_names: List of image names. These are used to compose input file names
      for the feature files, and the output file names for aggregated
      representations.
    features_dir: Directory where DELF features are located.
    aggregation_config_path: Path to AggregationConfig proto text file with
      configuration to be used for extraction.
    mapping_path: Optional CSV file which maps each .delf file name to the index
      image ID and detected box ID. If regional aggregation is performed, this
      should be set. Otherwise, this is ignored.
    output_aggregation_dir: Directory where aggregation output will be written
      to.

  Raises:
    ValueError: If AggregationConfig is malformed, or `mapping_path` is
      missing.
  """
    num_images = len(image_names)

    # Parse AggregationConfig proto, and select output extension.
    config = aggregation_config_pb2.AggregationConfig()
    with tf.io.gfile.GFile(aggregation_config_path, 'r') as f:
        text_format.Merge(f.read(), config)
    output_extension = '.'
    if config.use_regional_aggregation:
        output_extension += 'r'
    if config.aggregation_type == _VLAD:
        output_extension += _VLAD_EXTENSION_SUFFIX
    elif config.aggregation_type == _ASMK:
        output_extension += _ASMK_EXTENSION_SUFFIX
    elif config.aggregation_type == _ASMK_STAR:
        output_extension += _ASMK_STAR_EXTENSION_SUFFIX
    else:
        raise ValueError('Invalid aggregation type: %d' %
                         config.aggregation_type)

    # Read index mapping path, if provided.
    if mapping_path:
        images_to_box_feature_files = _ReadMappingBasenameToBoxNames(
            mapping_path, image_names)

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

    with tf.compat.v1.Session() as sess:
        extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation(
            sess, config)

        start = time.clock()
        for i in range(num_images):
            if i == 0:
                print('Starting to extract aggregation from images...')
            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]

            # Compose output file name, skip extraction for this image if it already
            # exists.
            output_aggregation_filename = os.path.join(
                output_aggregation_dir, image_name + output_extension)
            if tf.io.gfile.exists(output_aggregation_filename):
                print('Skipping %s' % image_name)
                continue

            # Load DELF features.
            if config.use_regional_aggregation:
                if not mapping_path:
                    raise ValueError(
                        'Requested regional aggregation, but mapping_path was not '
                        'provided')
                descriptors_list = []
                num_features_per_box = []
                for box_feature_file in images_to_box_feature_files[
                        image_name]:
                    delf_filename = os.path.join(
                        features_dir, box_feature_file + _DELF_EXTENSION)
                    _, _, box_descriptors, _, _ = feature_io.ReadFromFile(
                        delf_filename)
                    # If `box_descriptors` is empty, reshape it such that it can be
                    # concatenated with other descriptors.
                    if not box_descriptors.shape[0]:
                        box_descriptors = np.reshape(
                            box_descriptors,
                            [0, config.feature_dimensionality])
                    descriptors_list.append(box_descriptors)
                    num_features_per_box.append(box_descriptors.shape[0])

                descriptors = np.concatenate(descriptors_list)
            else:
                input_delf_filename = os.path.join(
                    features_dir, image_name + _DELF_EXTENSION)
                _, _, descriptors, _, _ = feature_io.ReadFromFile(
                    input_delf_filename)
                # If `descriptors` is empty, reshape it to avoid extraction failure.
                if not descriptors.shape[0]:
                    descriptors = np.reshape(
                        descriptors, [0, config.feature_dimensionality])
                num_features_per_box = None

            # Extract and save aggregation. If using VLAD, only
            # `aggregated_descriptors` needs to be saved.
            (aggregated_descriptors,
             feature_visual_words) = extractor.Extract(descriptors,
                                                       num_features_per_box)
            if config.aggregation_type == _VLAD:
                datum_io.WriteToFile(aggregated_descriptors,
                                     output_aggregation_filename)
            else:
                datum_io.WritePairToFile(aggregated_descriptors,
                                         feature_visual_words.astype('uint32'),
                                         output_aggregation_filename)
예제 #3
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)
예제 #4
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, _ = dataset.ReadDatasetFile(
        cmd_args.dataset_file_path)
    if cmd_args.use_query_images:
        image_list = query_list
    else:
        image_list = index_list
    num_images = len(image_list)
    print('done! Found %d images' % num_images)

    # Parse AggregationConfig proto, and select output extension.
    config = aggregation_config_pb2.AggregationConfig()
    with tf.gfile.GFile(cmd_args.aggregation_config_path, 'r') as f:
        text_format.Merge(f.read(), config)
    output_extension = '.'
    if config.use_regional_aggregation:
        output_extension += 'r'
    if config.aggregation_type == _VLAD:
        output_extension += _VLAD_EXTENSION_SUFFIX
    elif config.aggregation_type == _ASMK:
        output_extension += _ASMK_EXTENSION_SUFFIX
    elif config.aggregation_type == _ASMK_STAR:
        output_extension += _ASMK_STAR_EXTENSION_SUFFIX
    else:
        raise ValueError('Invalid aggregation type: %d' %
                         config.aggregation_type)

    # Read index mapping path, if provided.
    if cmd_args.index_mapping_path:
        images_to_box_feature_files = _ReadMappingBasenameToBoxNames(
            cmd_args.index_mapping_path, image_list)

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

    with tf.Session() as sess:
        extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation(
            sess, config)

        start = time.clock()
        for i in range(num_images):
            if i == 0:
                print('Starting to extract aggregation from images...')
            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_list[i]

            # Compose output file name, skip extraction for this image if it already
            # exists.
            output_aggregation_filename = os.path.join(
                cmd_args.output_aggregation_dir, image_name + output_extension)
            if tf.io.gfile.exists(output_aggregation_filename):
                print('Skipping %s' % image_name)
                continue

            # Load DELF features.
            if config.use_regional_aggregation:
                if not cmd_args.index_mapping_path:
                    raise ValueError(
                        'Requested regional aggregation, but index_mapping_path was not '
                        'provided')
                descriptors_list = []
                num_features_per_box = []
                for box_feature_file in images_to_box_feature_files[
                        image_name]:
                    delf_filename = os.path.join(
                        cmd_args.features_dir,
                        box_feature_file + _DELF_EXTENSION)
                    _, _, box_descriptors, _, _ = feature_io.ReadFromFile(
                        delf_filename)
                    # If `box_descriptors` is empty, reshape it such that it can be
                    # concatenated with other descriptors.
                    if not box_descriptors.shape[0]:
                        box_descriptors = np.reshape(
                            box_descriptors,
                            [0, config.feature_dimensionality])
                    descriptors_list.append(box_descriptors)
                    num_features_per_box.append(box_descriptors.shape[0])

                descriptors = np.concatenate(descriptors_list)
            else:
                input_delf_filename = os.path.join(
                    cmd_args.features_dir, image_name + _DELF_EXTENSION)
                _, _, descriptors, _, _ = feature_io.ReadFromFile(
                    input_delf_filename)
                num_features_per_box = None

            # Extract and save aggregation. If using VLAD, only
            # `aggregated_descriptors` needs to be saved.
            (aggregated_descriptors,
             feature_visual_words) = extractor.Extract(descriptors,
                                                       num_features_per_box)
            if config.aggregation_type == _VLAD:
                datum_io.WriteToFile(aggregated_descriptors,
                                     output_aggregation_filename)
            else:
                datum_io.WritePairToFile(aggregated_descriptors,
                                         feature_visual_words.astype('uint32'),
                                         output_aggregation_filename)