-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_features.py
205 lines (144 loc) · 5.73 KB
/
get_features.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from params import get_params
import sys
# We need to add the source code path to the python path if we want to call modules such as 'utils'
params = get_params()
sys.path.insert(0,params['src'])
from train_codebook import train_codebook
from get_assignments import get_assignments
from build_bow import bow
from get_local_features import image_local_features
import os, time
import numpy as np
import pickle
import cv2
from sklearn.cluster import MiniBatchKMeans
from sklearn.preprocessing import normalize, StandardScaler
from sklearn.decomposition import PCA
import warnings
warnings.filterwarnings("ignore")
# get features
def get_features(params,pca=None,scaler=None):
# Read image names
with open(os.path.join(params['root'],params['root_save'],params['image_lists'],params['split'] + '.txt'),'r') as f:
image_list = f.readlines()
# Initialize keypoint detector and feature extractor
detector, extractor = init_detect_extract(params)
# Initialize feature dictionary
features = {}
# Get trained codebook
km = pickle.load(open(os.path.join(params['root'],params['root_save'],
params['codebooks_dir'],'codebook_'
+ str(params['descriptor_size']) + "_"
+ params['descriptor_type']
+ "_" + params['keypoint_type'] + '.cb'),'rb'))
for image_name in image_list:
# Read image
im = cv2.imread(os.path.join(params['root'],params['database'],params['split'],'images',image_name.rstrip()))
# Resize image
im = resize_image(params,im)
# Extract local features
feats = image_local_features(im,detector,extractor)
if feats is not None:
if params['normalize_feats']:
feats = normalize(feats)
# If we scaled training features
if scaler is not None:
scaler.transform(feats)
# Whiten if needed
if pca is not None:
pca.transform(feats)
# Compute assignemnts
assignments = get_assignments(km,feats)
# Generate bow vector
feats = bow(assignments,km)
else:
# Empty features
feats = np.zeros(params['descriptor_size'])
# Add entry to dictionary
features[image_name] = feats
# Save dictionary to disk with unique name
save_file = os.path.join(params['root'],params['root_save'],params['feats_dir'],
params['split'] + "_" + str(params['descriptor_size']) + "_"
+ params['descriptor_type'] + "_" + params['keypoint_type'] + '.p')
pickle.dump(features,open(save_file,'wb'))
# Cambia tamany imatge
def resize_image(params,im):
# Get image dimensions
height, width = im.shape[:2]
# If the image width is smaller than the proposed small dimension, keep the original size !
resize_dim = min(params['max_size'],width)
# We don't want to lose aspect ratio:
dim = (resize_dim, height * resize_dim/width)
# Resize and return new image
return cv2.resize(im,dim)
#
def init_detect_extract(params):
'''
Initialize detector and extractor from parameters
'''
if params['descriptor_type'] == 'RootSIFT':
extractor = RootSIFT()
else:
extractor = cv2.DescriptorExtractor_create(params['descriptor_type'])
detector = cv2.FeatureDetector_create(params['keypoint_type'])
return detector, extractor
#
def stack_features(params):
'''
Get local features for all training images together
'''
# Init detector and extractor
detector, extractor = init_detect_extract(params)
# Read image names
with open(os.path.join(params['root'],params['root_save'],params['image_lists'],params['split'] + '.txt'),'r') as f:
image_list = f.readlines()
X = []
for image_name in image_list:
# Read image
im = cv2.imread(os.path.join(params['root'],params['database'],params['split'],'images',image_name.rstrip()))
# Resize image
im = resize_image(params,im)
feats = image_local_features(im,detector,extractor)
# Stack all local descriptors together
if feats is not None:
if len(X) == 0:
X = feats
else:
X = np.vstack((X,feats))
if params['normalize_feats']:
X = normalize(X)
if params['whiten']:
pca = PCA(whiten=True)
pca.fit_transform(X)
else:
pca = None
# Scale data to 0 mean and unit variance
if params['scale']:
scaler = StandardScaler()
scaler.fit_transform(X)
else:
scaler = None
return X, pca, scaler
if __name__ == "__main__":
params = get_params()
# Change to training set
params['split'] = 'train'
print "Apilant descriptors..."
# Save features for training set
t = time.time()
X, pca, scaler = stack_features(params)
print "Fet! Temps utilitzat:", time.time() - t
print "Nombre de descriptors d'entrenament", np.shape(X)
print "Entrenant codebook..."
t = time.time()
train_codebook(params,X)
print "Fet! Temps utilitzat:", time.time() - t
print "Emmagatzemant baul de descriptors per al set d'entrenament..."
t = time.time()
get_features(params, pca,scaler)
print "Fet! Temps utilitzat:", time.time() - t
params['split'] = 'val'
print "Emmagatzemant baul de descriptors per al set de validacio..."
t = time.time()
get_features(params)
print "Fet! Temps utilitzat", time.time() - t