-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplacesCNN.py
130 lines (100 loc) · 4.52 KB
/
placesCNN.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os, sys, re, time
import numpy as np
import matplotlib.pyplot as plt
import caffe
import path_params
def get_labels(labels, scores, attribute_responses, scene_attributeNames, mode, available_GPU_ID):
places_labels = []
final_label_list = []
scene_type_list = []
scene_attributes_list = []
for idx, output_prob in enumerate(scores['prob']):
vote = 0
toplabels_idx = output_prob.argsort()[::-1][:5] # reverse sort and take five largest items
if output_prob[toplabels_idx[0]] > .1 : # threshold for bad labels
for top5_idx in toplabels_idx:
if labels[top5_idx][-1] == '1':
vote = vote + 1
if vote > 2:
scene_type = 'Indoor'
else:
scene_type = 'Outdoor'
else:
scene_type = 'None'
label_list = []
for label_prob, label_idx in zip(output_prob[toplabels_idx], toplabels_idx):
if label_prob > .2 :
label_list.append('(' + re.findall(r"[\w]+", labels[label_idx])[1] + ', ' + str(float('%.2f' %label_prob)) + ')')
label_list = ', '.join(map(str, label_list))
places_labels.append(label_list)
scene_type_list.append(scene_type)
print places_labels
## Scene attributes
attribute_response = attribute_responses[idx]
attribute_index = attribute_response.argsort()[::-1][:5]
scene_attributes = scene_attributeNames[attribute_index]
scene_attributes = ", ".join(scene_attributes)
scene_attributes_list.append(scene_attributes)
return scene_type_list, places_labels, scene_attributes_list
def get_scene_attribute_responses(scene_attributeValues, fc7):
# Returs the scene attributes for the fc7 features
scene_attributeValues = np.transpose(scene_attributeValues)
attribute_responses = np.dot(fc7, scene_attributeValues)
return attribute_responses
def placesCNN(pycaffe_path, model_path, image_files):
start = time.time()
sys.path.insert(0, pycaffe_path)
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
if mode == 'gpu':
caffe.set_mode_gpu()
caffe.set_device(available_GPU_ID[0])
else:
caffe.set_mode_cpu()
model_prototxt = path_params.placesCNN_prototxt
model_trained = path_params.placesCNN_caffemodel
mean_path = path_params.placesCNN_mean
mu = np.load(mean_path).mean(1).mean(1)
net = caffe.Net(model_prototxt, # defines the structure of the model
model_trained, # contains the trained weights
caffe.TEST)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension
transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))
# Assign batchsize
batch_size = 10
data_blob_shape = net.blobs['data'].data.shape
data_blob_shape = list(data_blob_shape)
net.blobs['data'].reshape(batch_size, data_blob_shape[1], data_blob_shape[2], data_blob_shape[3])
scores = None
chunks_done = 0
for chunk in [image_files[x:x+batch_size] for x in xrange(0, len(image_files), batch_size)]:
print "Processing %.2f%% done ..." %((batch_size*chunks_done*100)/float(len(image_files)))
chunks_done = chunks_done + 1
if len(chunk) < batch_size:
net.blobs['data'].reshape(len(chunk), data_blob_shape[1], data_blob_shape[2], data_blob_shape[3])
net.blobs['data'].data[...] = map(lambda y: transformer.preprocess('data', caffe.io.load_image(y)), chunk)
output = net.forward()
if scores is None:
scores = {}
scores['prob'] = output['prob'].copy()
fc7 = net.blobs['fc7'].data[...].copy()
# fc8 = net.blobs['fc8'].data[...].copy()
# fc6 = net.blobs['fc6'].data[...].copy()
else:
scores['prob'] = np.vstack((scores['prob'],output['prob']))
fc7 = np.vstack((fc7, net.blobs['fc7'].data[...].copy()))
# fc8 = np.vstack((fc8, net.blobs['fc8'].data[...].copy()))
# fc6 = np.vstack((fc6, net.blobs['fc6'].data[...].copy()))
places_labels = path_params.places_labels
labels = np.loadtxt(places_labels, str, delimiter='\t')
scene_attributeValues = np.loadtxt(path_params.scene_values, delimiter = ',')
scene_attributeNames = np.loadtxt(path_params.scene_names, delimiter = '\n', dtype = str)
attribute_responses = get_scene_attribute_responses(scene_attributeValues, fc7)
scene_type_list, places_labels, scene_attributes_list = get_labels(labels, scores, attribute_responses, scene_attributeNames)
end = time.time()
print "Time : %.3f \n" %(end - start)
return fc7, scene_type_list, places_labels, scene_attributes_list