Ejemplo n.º 1
0
"""
Script to extract CNN features using a caffe model.

Usage: python extract_features.py /path/to/input/image.jpg /path/to/output/features.txt
"""

from caffetools.extract.DeepFeatures import DeepFeatures
from caffetools.lmdb.numpyserializer import NumPySerializer
import argparse

# parse arguments
parser = argparse.ArgumentParser(description='Script to extract CNN features into LMDB')
parser.add_argument('input_image_path', help='path to input image')
parser.add_argument('output_features_path', help='path to output features text file')
args = parser.parse_args()

# extract features
d = DeepFeatures()
features = d.extract_features(args.input_image_path)

# save to text file
with open(args.output_features_path, 'w') as f:
	features_row_vector = features.reshape(1,-1)
	features_string = NumPySerializer.numpy_to_string(features_row_vector)
	f.write(features_string)
	print "Features saved to '%s'" % args.output_features_path

parser = argparse.ArgumentParser(description='Script to extract CNN features into numpy file')
parser.add_argument('images_list_path', help='path to text file of list of images')
parser.add_argument('images_path', help='path to base directory of images')
parser.add_argument('output_path', help='path to output numpy file')
args = parser.parse_args()

# check
if not os.path.exists(args.images_path):
	raise OSError('images_path does not exist: {0}'.format(args.images_path))

# open images list
with open(args.images_list_path, 'r') as f:
	images_list = [line.strip() for line in f]

# loop over images
d = DeepFeatures()
num_images = len(images_list)
data = None
for index, image in enumerate(tqdm(images_list)):
	path_to_image = os.path.join(args.images_path, image)

	# extract features
	features = d.extract_features(path_to_image)
	if not data:
		feature_dim = features.shape[0]
		data = np.zeros((num_images, feature_dim))

	# put in numpy matrix
	data[index, :] = features

# save to numpy file