def testWriteAndReadToFileEmptyFile(self):
    tmpdir = tf.compat.v1.test.get_temp_dir()
    filename = os.path.join(tmpdir, 'test.delf')
    feature_io.WriteToFile(filename, np.array([]), np.array([]), np.array([]),
                           np.array([]), np.array([]))
    data_read = feature_io.ReadFromFile(filename)

    self.assertAllEqual(np.array([]), data_read[0])
    self.assertAllEqual(np.array([]), data_read[1])
    self.assertAllEqual(np.array([]), data_read[2])
    self.assertAllEqual(np.array([]), data_read[3])
    self.assertAllEqual(np.array([]), data_read[4])
Beispiel #2
0
    def testWriteAndReadToFile(self):
        locations, scales, descriptors, attention, orientations = create_data()

        filename = os.path.join(FLAGS.test_tmpdir, 'test.delf')
        feature_io.WriteToFile(filename, locations, scales, descriptors,
                               attention, orientations)
        data_read = feature_io.ReadFromFile(filename)

        self.assertAllEqual(locations, data_read[0])
        self.assertAllEqual(scales, data_read[1])
        self.assertAllEqual(descriptors, data_read[2])
        self.assertAllEqual(attention, data_read[3])
        self.assertAllEqual(orientations, data_read[4])
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)
Beispiel #4
0
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)

    extractor_fn = extractor.MakeExtractor(config)

    start = time.time()

    res = []
    for i in tqdm(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(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])).astype(np.float32)
        # 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']
        res.append(extracted_features['global_descriptor'])
        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.')

    # 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))
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)
Beispiel #7
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)
Beispiel #8
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)
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 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)
Beispiel #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, '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)
Beispiel #12
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)
    def get_attention(self, list_images_path, output_dir):
        '''
        从一组图片中提取出关键点特征到文件中
        :param list_images_path: 图片列表的存储路径
        :param output_dir: 输出文件夹
        :return:
        '''
        # Read list of images.
        tf.logging.info('Reading list of images...')
        image_paths = _ReadImageList(list_images_path)
        num_images = len(image_paths)
        tf.logging.info('done! Found %d images', num_images)

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

        with self.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)

            input_image = self.graph.get_tensor_by_name('input_image:0')
            input_score_threshold = self.graph.get_tensor_by_name(
                'input_abs_thres:0')
            input_image_scales = self.graph.get_tensor_by_name(
                'input_scales:0')
            input_max_feature_num = self.graph.get_tensor_by_name(
                'input_max_feature_num:0')
            boxes = self.graph.get_tensor_by_name('boxes:0')
            raw_descriptors = self.graph.get_tensor_by_name('features:0')
            feature_scales = self.graph.get_tensor_by_name('scales:0')
            attention_with_extra_dim = self.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, self.config)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=self.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 = self.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) = self.sess.run(
                     [locations, descriptors, feature_scales, attention],
                     feed_dict={
                         input_image:
                         im,
                         input_score_threshold:
                         self.config.delf_local_config.score_threshold,
                         input_image_scales:
                         list(self.config.image_scales),
                         input_max_feature_num:
                         self.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.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.
                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.clock() - start)
            print('Processed %d query images in %f seconds' %
                  (num_images, elapsed))
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)
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)