def pil_imagenet_loader(path, imsize, bounding_box=None, preprocess=True): """Pillow loader for the images. Args: path: Path to image to be loaded. imsize: Integer, defines the maximum size of longer image side. bounding_box: (x1,y1,x2,y2) tuple to crop the query image. preprocess: Bool, whether to preprocess the images in respect to the ImageNet dataset. Returns: image: `Tensor`, image in ImageNet suitable format. """ img = image_loading_utils.RgbLoader(path) if bounding_box is not None: imfullsize = max(img.size) img = img.crop(bounding_box) imsize = imsize * max(img.size) / imfullsize # Unlike `resize`, `thumbnail` resizes to the largest size that preserves # the aspect ratio, making sure that the output image does not exceed the # original image size and the size specified in the arguments of thumbnail. img.thumbnail((imsize, imsize), Image.ANTIALIAS) img = np.array(img) if preprocess: # Preprocessing for ImageNet data. Converts the images from RGB to BGR, # then zero-centers each color channel with respect to the ImageNet # dataset, without scaling. tf.keras.applications.imagenet_utils.preprocess_input(img, mode='caffe') return img
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(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))
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)