-
Notifications
You must be signed in to change notification settings - Fork 9
/
featextractor.py
267 lines (233 loc) · 9.8 KB
/
featextractor.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import logging
import sys
from annotatedimage import *
from bbox import *
from network import *
class FeatureExtractorParams():
"""
Base class for the parameters.
The subclass must have the field 'name'.
"""
def __init__(self):
raise NotImplementedError()
class FeatureExtractor():
"""
Extractor features from an AnnotatedImage.
You must construct the objects through the method 'create_instance',
and extract the features using the method 'extract'
"""
def __init__(self):
"""
The constructors for all the FeatureExtractors is private.
"""
self.name = 'FeatureExtractor' # mandatory field
raise NotImplementedError()
def extract(self, bboxes):
"""
The base method to extract the features.
Normally, it should return a ndarray of size [len(bboxes), num_features]
"""
raise NotImplementedError()
def get_cache(self):
"""
Returns a Python object containing feature-dependent data, useful
to speed-up future feature extraction calls.
"""
raise NotImplementedError()
@staticmethod
def create_feature_extractor(anno_image, params):
"""
Factory for the FeatureExtractors, taking an AnnotatedImage and a
FeatureExtractorParams instance as input
"""
assert isinstance(params, FeatureExtractorParams)
if isinstance(params, FeatureExtractorNetworkParams):
return FeatureExtractorNetwork(anno_image, params)
elif isinstance(params, FeatureExtractorFakeParams):
return FeatureExtractorFake(anno_image, params)
elif isinstance(params, FeatureExtractorMatlabGTSSParams):
return FeatureExtractorMatlabGTSS(anno_image, params)
else:
raise ValueError('FeatureExtractorParams instance not recognized')
#=============================================================================
class FeatureExtractorFakeParams(FeatureExtractorParams):
def __init__(self):
pass
class FeatureExtractorFake(FeatureExtractor):
"""
Fake feature extractor module, for debugging purposes.
"""
def __init__(self, anno_image, params):
""" *** PRIVATE CONSTRUCTOR *** """
self.name = 'FeatureExtractorFake'
self.num_feats = 5
def extract(self, bboxes):
"""
Return a matrix of ones.
"""
return np.ones(shape=(len(bboxes), self.num_feats), dtype=float)
def get_cache(self):
return 123
#=============================================================================
class FeatureExtractorNetworkParams(FeatureExtractorParams):
def __init__(self, netparams, layer = 'softmax', \
cache_features = True):
self.netparams = netparams
self.layer = layer
self.cache_features = cache_features
def get_id_desc(self):
name = str(self.netparams.__class__).replace('Params','')
return 'name:{0}-layer:{1}'.format(name, self.layer)
class FeatureExtractorNetwork(FeatureExtractor):
"""
Extract features using a Network object.
The cached features are saved in a dictionary field of key
FeatureExtractorNetworkParams.get_id_desc(), which is a dictionary
with the following fields:
featdata: ndarray of size [num_bboxes, num_features]
featidx: {bbox_key -> idx}
where bbox_key is a string 'xmin-ymin-xmax-ymax' and idx
refers to the idx in featdata
NOTE: for efficiency reason and simplicitly, the current implementation
allows only one type of network during the entire life of
this class.
"""
# network to use during the life of any FeatureExtractorCaffe object
network_ = None
params_ = None
def __init__(self, anno_image, params):
"""
*** PRIVATE CONSTRUCTOR ***
Input: AnnotatedImage and FeatureExtractorNetworkParams
"""
from annotatedimage import * # HACK: known circular import problem
assert isinstance(anno_image, AnnotatedImage)
assert isinstance(params, FeatureExtractorNetworkParams)
self.name = 'FeatureExtractorNetwork'
self.anno_image = anno_image
self.img = anno_image.get_image() # just for efficiency
assert self.img.shape[0] == self.anno_image.image_height
assert self.img.shape[1] == self.anno_image.image_width
self.params = params
params_ = params
if FeatureExtractorNetwork.network_:
assert FeatureExtractorNetwork.params_ == self.params, \
'Only a single network is allowed during the life of '\
'FeatureExtractorNetwork'
else:
FeatureExtractorNetwork.params_ = self.params
# inizialize the cache
modulename = self.name
name = self.params.get_id_desc()
if modulename in self.anno_image.features:
self.cache = self.anno_image.features[modulename]
else:
self.cache = {}
if name not in self.cache:
self.cache[name] = {}
self.cache[name]['featdata'] = None
self.cache[name]['featidx'] = {}
def __getstate__(self):
raise RuntimeError('__getstate__ is not supported for '\
'FeatureExtractorNetwork')
def extract(self, bboxes):
# for each bbox:
width = self.anno_image.image_width
height = self.anno_image.image_height
feats = None
for idx_bbox, bbox in enumerate(bboxes):
# convert the bbox to absolute, integer values
bb = bbox.copy().rescale_to_outer_box(width, height)
bb.convert_coordinates_to_integers()
modulename = self.name
name = self.params.get_id_desc()
key = '{0}-{1}-{2}-{3}'.format(bb.xmin, bb.ymin, bb.xmax, bb.ymax)
try:
# try to see if in the cache there are the features we want
netfeat = self.cache[name]
feat = netfeat['featdata'][netfeat['featidx'][key], :]
except:
# the features are not present :-( we extract them
logging.info('Extracting feats for image {0}, key {1}'.format( \
self.anno_image.image_name, key))
# we crop appropriately the image
img = self.img.copy()
img = img[bb.ymin:bb.ymax, bb.xmin:bb.xmax]
# if the Network has never been used, we need to create it
if FeatureExtractorNetwork.network_ == None:
FeatureExtractorNetwork.network_ = Network.create_network( \
self.params.netparams)
feat = FeatureExtractorNetwork.network_.evaluate( \
img, layer_name=self.params.layer)
feat = np.atleast_2d(feat)
if feat.shape[0] > 1:
feat = feat.T # feat must be a horizontal vector
assert feat.shape[0] == 1
# save the feature in the cache, if requested
if self.params.cache_features:
if self.cache[name]['featdata'] == None:
self.cache[name]['featdata'] = feat
else:
self.cache[name]['featdata'] = \
np.vstack([self.cache[name]['featdata'], feat])
self.cache[name]['featidx'][key] = \
self.cache[name]['featdata'].shape[0] - 1
# copy the features
if feats == None:
feats = np.ndarray(shape=(len(bboxes), feat.size), dtype=float)
feats[idx_bbox, :] = feat.copy()
# return
assert feats.shape[0] == len(bboxes)
assert feats.shape[1] > 0
return feats
def get_cache(self):
return self.cache
#=============================================================================
class FeatureExtractorMatlabGTSSParams(FeatureExtractorParams):
def __init__(self, mat_dir):
self.mat_dir = mat_dir
def get_id_desc(self):
return self.mat_dir
class FeatureExtractorMatlabGTSS(FeatureExtractor):
"""
TODO documentation.
"""
def __init__(self, anno_image, params):
"""
*** PRIVATE CONSTRUCTOR ***
Input: AnnotatedImage and FeatureExtractorNetworkParams
"""
from annotatedimage import * # HACK: known circular import problem
assert isinstance(anno_image, AnnotatedImage)
assert isinstance(params, FeatureExtractorMatlabGTSSParams)
self.name = 'FeatureExtractorMatlabGTSS'
self.anno_image = anno_image
self.params = params
# inizialize the cache
modulename = self.name
name = self.params.get_id_desc()
assert modulename in self.anno_image.features
self.cache = self.anno_image.features[modulename]
name = self.params.get_id_desc()
self.cache[name]['featdata'] = self.cache[name]['featdata'].tocsc()
assert name in self.cache
def __getstate__(self):
raise RuntimeError('__getstate__ is not supported for '\
'FeatureExtractorMatlabGTSS')
def extract(self, bboxes):
# retrieve the feats indexes to retrieve
name = self.params.get_id_desc()
feats = None
for idx_bb, bb in enumerate(bboxes):
idx = self.cache[name]['featidx'][bb.get_coordinates_str()]
feat = self.cache[name]['featdata'][idx, :]
feat = feat.todense() # convert to dense format
if feats == None:
feats = np.empty((len(bboxes), feat.size), dtype=float)
feats[idx_bb, :] = feat
# return
assert feats.shape[0] == len(bboxes)
assert feats.shape[1] > 0
return feats
def get_cache(self):
return self.cache