def query(query_image, fetch_limit): print "crow_search.py :: query :: query_image :: ", query_image # Return List ret_lst = [] # Loading the image.. img = extract_features.load_img(crow_constants.CROW_IMAGE_DIR + query_image) # Extracting features for the query image. d = extract_features.format_img_for_vgg(img) net = caffe.Net(crow_constants.PROTOTXT_PATH, crow_constants.CAFFE_MODEL_PATH, caffe.TEST) Q = extract_features.extract_raw_features(net, "pool5", d) # Processing the query and ranking the results. agg_fn = crow.apply_crow_aggregation Q = agg_fn(Q) # Normalize and PCA to final feature Q, _ = crow.run_feature_processing_pipeline([Q], params=whitening_params) # Fetching the candidates. inds, dists = evaluate.get_nn(Q, data) for i in range(int(fetch_limit)): ret_lst.append(image_names[inds[i]]) return ret_lst
def gpu_task(prototxt, caffemodel, layer, path_images, out, gpu=0): num_images = len(path_images) h5f = h5py.File(out, 'w') # set gpu card caffe.set_device(gpu) caffe.set_mode_gpu() # init NN net = caffe.Net(prototxt, caffemodel, caffe.TEST) net.forward() features = [] image_names = [] for i, path in enumerate(path_images): print "%d(%d), %s"%((i+1), num_images, os.path.basename(path)) d = opencv_format_img_for_vgg(path, True) # extract feature feat = extract_raw_features(net, layer, d) #feat = extract_multi_raw_features(net, layer, d) # apply maxpooling #feat = apply_maxpooling_aggregation(feat) feat = apply_crow_aggregation(feat) features.append(feat) image_names.append(os.path.basename(path)) features = np.array(features) h5f['feats'] = features h5f['names'] = image_names h5f.close() print "gpu %d task has finished..." % (int(gpu))
def init(): global is_initialized, net, whitening_params, data, image_names if not is_initialized: net = caffe.Net(crow_constants.PROTOTXT_PATH, crow_constants.CAFFE_MODEL_PATH, caffe.TEST) # Checking if pre-extracted features are present. if not os.path.isdir(crow_constants.CROW_FEATURES_PATH): # Creating the features directory. os.mkdir(crow_constants.CROW_FEATURES_PATH) # Iterating over the images. for path in os.listdir(crow_constants.CROW_IMAGE_DIR): # Laoding the image.. img = extract_features.load_img(crow_constants.CROW_IMAGE_DIR + path) # Skip if the image failed to load if img is None: continue # Extracting features for the images d = extract_features.format_img_for_vgg(img) X = extract_features.extract_raw_features(net, "pool5", d) filename = os.path.splitext(os.path.basename(path))[0] np.save( os.path.join(crow_constants.CROW_FEATURES_PATH, filename), X) # Selecting the aggregation function to apply. agg_fn = crow.apply_crow_aggregation # Computing whitening params whitening_params = evaluate.fit_whitening( crow_constants.CROW_FEATURES_PATH, agg_fn, 128) # Loading the pre-extracted features. data, image_names = evaluate.load_and_aggregate_features( crow_constants.CROW_FEATURES_PATH, agg_fn) data, _ = evaluate.run_feature_processing_pipeline( np.vstack(data), params=whitening_params) # Setting is_initialized as True is_initialized = True print "crow_search.py :: init :: End"
def query_images(groundtruth_dir, image_dir, dataset, cropped=True): """ Extract features from the Oxford or Paris dataset. :param str groundtruth_dir: the directory of the groundtruth files (which includes the query files) :param str image_dir: the directory of dataset images :param str dataset: the name of the dataset, either 'oxford' or 'paris' :param bool cropped: flag to optionally disable cropping :yields Image img: the Image object :yields str query_name: the name of the query """ imgs = [] query_names = [] fake_query_names = [] feats_crop = [] import caffe modelDir = "/home/yuanyong/models" #ResNet MODEL = "vgg.model" PROTO = "pool5.prototxt" caffemodel = os.path.join(modelDir, MODEL) prototxt = os.path.join(modelDir, PROTO) # set gpu card layer = 'pool5' caffe.set_device(5) caffe.set_mode_gpu() # init NN net = caffe.Net(prototxt, caffemodel, caffe.TEST) net.forward() for f in glob.iglob(os.path.join(groundtruth_dir, '*_query.txt')): fake_query_name = os.path.splitext(os.path.basename(f))[0].replace( '_query', '') fake_query_names.append(fake_query_name) query_name, x, y, w, h = open(f).read().strip().split(' ') if dataset == 'oxford': query_name = query_name.replace('oxc1_', '') query_names.append('%s.jpg' % query_name) img = cv2.imread(os.path.join(image_dir, '%s.jpg' % query_name), 1) # BGR if cropped: x, y, w, h = map(float, (x, y, w, h)) x, y, w, h = map(lambda d: int(round(d)), (x, y, w, h)) else: x, y, w, h = (0, 0, img.shape[1], img.shape[0]) img = img[y:y + h, x:x + w] d = np.float32(img) # VggNet d -= np.array((104.00698793, 116.66876762, 122.67891434)) d = d.transpose((2, 0, 1)) feat = extract_raw_features(net, layer, d) feat = apply_rmac_aggregation(feat) # L2-normalize feature feat = normalize(feat, copy=False) feats_crop.append(feat) imgs.append(img) return imgs, feats_crop, query_names, fake_query_names
if __name__ == '__main__': from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument('--dataset', dest='dataset', type=str, required=True, help='dataset to extract queries for') parser.add_argument('--images', dest='images', type=str, default='data/', help='directory containing image files') parser.add_argument('--groundtruth', dest='groundtruth', type=str, default='groundtruth/', help='directory containing groundtruth files') parser.add_argument('--out', dest='out', type=str, default='pool5_queries/', help='path to save output') parser.add_argument('--layer', dest='layer', type=str, default='pool5', help='model layer to extract') parser.add_argument('--prototxt', dest='prototxt', type=str, default='vgg/VGG_ILSVRC_16_pool5.prototxt', help='path to prototxt') parser.add_argument('--caffemodel', dest='caffemodel', type=str, default='vgg/VGG_ILSVRC_16_layers.caffemodel', help='path to model params') args = parser.parse_args() images_dir = os.path.join(args.dataset, args.images) groundtruth_dir = os.path.join(args.dataset, args.groundtruth) out_dir = os.path.join(args.dataset, args.out) # Load networks caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST) if not os.path.exists(out_dir): os.makedirs(out_dir) for img, name in query_images(groundtruth_dir, images_dir, args.dataset): d = format_img_for_vgg(img) X = extract_raw_features(net, args.layer, d) np.save(os.path.join(out_dir, '%s' % name), X)
help='model layer to extract') parser.add_argument('--prototxt', dest='prototxt', type=str, default='vgg/VGG_ILSVRC_16_pool5.prototxt', help='path to prototxt') parser.add_argument('--caffemodel', dest='caffemodel', type=str, default='vgg/VGG_ILSVRC_16_layers.caffemodel', help='path to model params') args = parser.parse_args() images_dir = os.path.join(args.dataset, args.images) groundtruth_dir = os.path.join(args.dataset, args.groundtruth) out_dir = os.path.join(args.dataset, args.out) # Load networks caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST) if not os.path.exists(out_dir): os.makedirs(out_dir) for img, name in query_images(groundtruth_dir, images_dir, args.dataset): d = format_img_for_vgg(img) X = extract_raw_features(net, args.layer, d) np.save(os.path.join(out_dir, '%s' % name), X)
import caffe import os from PIL import Image import numpy as np import scipy.io as scio from extract_features import load_img, format_img_for_vgg, extract_raw_features fold = '/home1/qcz/DataSet' dataset = 'oxford5k' name = 'ashmolean_000283.jpg' imgPath = os.path.join(fold, dataset, 'images', name) prototxt = 'vgg/VGG_ILSVRC_16_pool5.prototxt' caffemodel = 'vgg/VGG_ILSVRC_16_layers.caffemodel' feaName = 'pool5' caffe.set_mode_gpu() caffe.set_device(1) net = caffe.Net(prototxt, caffe.TEST) net.copy_from(caffemodel) im = load_img(imgPath) im = format_img_for_vgg(im) fea = extract_raw_features(net, feaName, im) scio.savemat('fea.mat', {'fea': fea}) print 'fea'