-
Notifications
You must be signed in to change notification settings - Fork 93
/
image_feature_cnn.py
112 lines (91 loc) · 3.92 KB
/
image_feature_cnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import numpy as np
import os, sys, getopt, time
import cPickle as pickle
# Main path to your caffe installation
caffe_root = '/home/mosessoh/caffe/'
# Model prototxt file
model_prototxt = caffe_root + 'models/bvlc_googlenet/deploy.prototxt'
# Model caffemodel file
model_trained = caffe_root + 'models/bvlc_googlenet/bvlc_googlenet.caffemodel'
# File containing the class labels
imagenet_labels = caffe_root + 'data/ilsvrc12/synset_words.txt'
# Path to the mean image (used for input processing)
mean_path = caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'
# Name of the layer we want to extract
layer_name = 'pool5/7x7_s1'
os.environ['GLOG_minloglevel'] = '3'
sys.path.insert(0, caffe_root + 'python')
import caffe
# Setting this to CPU, but feel free to use GPU if you have CUDA installed
caffe.set_mode_cpu()
# Loading the Caffe model, setting preprocessing parameters
net = caffe.Classifier(model_prototxt, model_trained,
mean=np.load(mean_path).mean(1).mean(1),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
def forward_cnn(image_path):
image_path = image_path.strip()
input_image = caffe.io.load_image(image_path)
prediction = net.predict([input_image], oversample=False)
image_vector = net.blobs[layer_name].data[0].reshape(1,-1)
print 'CNN forward pass completed'
return image_vector
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'caffe_feature_extractor.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'caffe_feature_extractor.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i"):
inputfile = arg
elif opt in ("-o"):
outputfile = arg
print 'Reading images from "', inputfile
print 'Writing vectors to "', outputfile
# Setting this to CPU, but feel free to use GPU if you have CUDA installed
caffe.set_mode_cpu()
# Loading the Caffe model, setting preprocessing parameters
net = caffe.Classifier(model_prototxt, model_trained,
mean=np.load(mean_path).mean(1).mean(1),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
# Loading class labels
with open(imagenet_labels) as f:
labels = f.readlines()
# This prints information about the network layers (names and sizes)
# You can uncomment this, to have a look inside the network and choose which layer to print
#print [(k, v.data.shape) for k, v in net.blobs.items()]
#exit()
if inputfile == 'train_images.txt':
num_imgs = 82783
elif inputfile == 'val_images.txt':
num_imgs = 40504
elif inputfile == 'sample_images.txt':
num_imgs = 141
# Processing one image at a time, printing predictions and writing the vector to a file
start = time.time()
counter = 1
with open(inputfile, 'r') as reader:
for image_path in reader:
print 'Processing %d of %d' % (counter, num_imgs)
if counter % 10 == 0:
print 'Time elapsed (min): %.1f' % (time.time() - start)
image_path = image_path.strip()
input_image = caffe.io.load_image(image_path)
prediction = net.predict([input_image], oversample=False)
image_vector = net.blobs[layer_name].data[0].reshape(1,-1)
image_picklename = os.path.splitext(image_path)[0] + '.p'
pickle.dump(image_vector, open(image_picklename,'w'))
counter += 1
print 'Time elapsed (s): %.4f' % (time.time() - start)
print 'Avg Time per Image (s): %.4f' % ((time.time() - start)/num_imgs)
# if __name__ == "__main__":
# main(sys.argv[1:])