-
Notifications
You must be signed in to change notification settings - Fork 1
/
_vlfeat.pyx
291 lines (239 loc) · 9.11 KB
/
_vlfeat.pyx
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import numpy as np
cimport numpy as np
cimport cython
from libcpp cimport bool
from libcpp.string cimport string
from cython.operator cimport dereference as deref
from arma cimport Mat, pyarma_from_double, pyarma_to_double
from arma cimport pyarma_from_float, pyarma_to_float
cdef extern from "vlfeat.hpp":
cdef void c_vl_sift "vl_sift" (Mat[float] &, Mat[double] &, Mat[float] &, \
int, int, int, double, double, double, double, double, \
bool, bool, int)
cdef void c_vl_dsift "vl_dsift" (Mat[float] &, Mat[double] &, Mat[float] &, \
Mat[double] &, Mat[double] &, Mat[double] &, \
Mat[double] &, bool, bool, double, bool, int)
cdef void c_vl_imsmooth_f "vl_imsmooth" (Mat[float] &, Mat[float] &, \
double, string, string, int, int)
cdef void c_vl_imsmooth_d "vl_imsmooth" (Mat[double] &, Mat[double] &, \
double, string, string, int, int)
cdef void c_vl_homkermap "vl_homkermap" (Mat[float] &, Mat[float] &, int, \
string, string, double, double)
cdef void c_vl_kmeans "vl_kmeans" (Mat[float] &, Mat[float] &, int, \
string, string, string, double, int, int, int, int, int)
cdef void c_vl_gmm "vl_gmm" (Mat[float] &, int, \
Mat[float] &, Mat[float] &, Mat[float] &, Mat[float] &, \
string, Mat[float] &, Mat[float] &, Mat[float] &, \
int, int, Mat[double] &, int)
cdef void c_vl_fisher "vl_fisher" (Mat[float] &, \
Mat[float] &, Mat[float] &, Mat[float] &, \
Mat[float] &, bool, bool, bool, bool, int)
def vl_sift(np.ndarray[np.float32_t, ndim=2] data,
frames=None,
int octaves = -1,
int levels = -1,
int firstOctave = -1,
double peakThresh = -1,
double edgeThresh = -1,
double normThresh = -1,
double magnif = -1,
double windowSize = -1,
bool orientations = False,
bool floatDescriptors = False,
int verbose = 0):
cdef np.ndarray[double, ndim=2] f
cdef np.ndarray[float, ndim=2] d
cdef Mat[float] _I
cdef Mat[double] _f
cdef Mat[float] _d
_I = pyarma_to_float(data)
if frames == None:
_f = Mat[double]()
else:
_f = pyarma_to_double(frames)
_d = Mat[float]()
c_vl_sift(<const Mat[float] &>_I, _f, _d,
octaves, levels, firstOctave, peakThresh,
edgeThresh, normThresh, magnif, windowSize, orientations,
floatDescriptors, verbose)
if frames == None:
f = pyarma_from_double(_f)
else:
f = frames
d = pyarma_from_float(_d)
return f, d
def vl_dsift(np.ndarray[float, ndim=2] data,
bounds = None,
step = None,
size = None,
geometry = None,
bool fast = True,
bool norm = False,
double windowSize = -1.0,
bool floatDescriptors = False,
int verbose = 0):
cdef np.ndarray[double, ndim=2] f
cdef np.ndarray[float, ndim=2] d
cdef Mat[float] _I
cdef Mat[double] _f
cdef Mat[float] _d
cdef Mat[double] _bounds
cdef Mat[double] _step
cdef Mat[double] _size
cdef Mat[double] _geometry
_I = pyarma_to_float(data)
_f = Mat[double]()
_d = Mat[float]()
if bounds == None:
_bounds = Mat[double]()
else:
_bounds = pyarma_to_double(np.float64(bounds, order='F').reshape((-1,1)))
if step == None:
_step = Mat[double]()
else:
_step = pyarma_to_double(np.float64(step, order='F').reshape((-1,1)))
if size == None:
_size = Mat[double]()
else:
_size = pyarma_to_double(np.float64(size, order='F').reshape((-1,1)))
if geometry == None:
_geometry = Mat[double]()
else:
_geometry = pyarma_to_double(np.float64(geometry, order='F').reshape((-1,1)))
c_vl_dsift(<const Mat[float] &>_I, _f, _d,
_bounds, _step, _size, _geometry,
fast, norm, windowSize, floatDescriptors, verbose)
f = pyarma_from_double(_f)
d = pyarma_from_float(_d)
return f, d
def vl_imsmooth_f(np.ndarray[float, ndim=2] I,
double sigma,
string padding = "continuity",
string kernel = "gaussian",
int subsample = 1,
int verbose = 0):
cdef np.ndarray[float, ndim=2] Is
cdef Mat[float] _I
cdef Mat[float] _Is
_I = pyarma_to_float(I)
_Is = Mat[float]()
c_vl_imsmooth_f(<const Mat[float] &>_I, _Is,
sigma, padding, kernel, subsample, verbose)
Is = pyarma_from_float(_Is)
return Is
def vl_imsmooth_d(np.ndarray[double, ndim=2] I,
double sigma,
string padding = "continuity",
string kernel = "gaussian",
int subsample = 1,
int verbose = 0):
cdef np.ndarray[double, ndim=2] Is
cdef Mat[double] _I
cdef Mat[double] _Is
_I = pyarma_to_double(I)
_Is = Mat[double]()
c_vl_imsmooth_d(<const Mat[double] &>_I, _Is,
sigma, padding, kernel, subsample, verbose)
Is = pyarma_from_double(_Is)
return Is
def vl_imsmooth(I,
double sigma,
string padding = "continuity",
string kernel = "gaussian",
int subsample = 1,
int verbose = 0):
if I.dtype == np.float32:
return vl_imsmooth_f(I, sigma, padding, kernel, subsample, verbose)
elif I.dtype == np.float64:
return vl_imsmooth_d(I, sigma, padding, kernel, subsample, verbose)
else:
return None
def vl_homkermap(np.ndarray[float, ndim=2] X, int n,
string kernel = "kchi2",
string window = "rectangular",
double gamma = 1.0,
double period = -1):
cdef Mat[float] _X
cdef Mat[float] _V
_X = pyarma_to_float(X)
_V = Mat[float]()
c_vl_homkermap(<const Mat[float] &>_X, _V, n,
kernel, window, gamma, period)
cdef np.ndarray[float, ndim=2] V
V = pyarma_from_float(_V)
return V
def vl_kmeans(np.ndarray[float, ndim=2] X, int numCenters,
string algorithm = "lloyd",
string distance = "l2",
string initialization = "plusplus",
double minEnergyVariation = -1,
int numRepetitions = 1,
int numTrees = 3,
int maxNumComparisons = 100,
int maxNumIterations = 100,
int verbose = 0):
cdef Mat[float] _X
cdef Mat[float] _Y
_X = pyarma_to_float(X)
_Y = Mat[float]()
c_vl_kmeans(<const Mat[float] &>_X, _Y, numCenters,
algorithm, distance, initialization, minEnergyVariation,
numRepetitions, numTrees, maxNumComparisons, maxNumIterations,
verbose)
cdef np.ndarray[float, ndim=2] Y
Y = pyarma_from_float(_Y)
return Y
def vl_gmm(np.ndarray[float, ndim=2] X, int numClusters,
string initialization = 'rand',
initMeans = None,
initCovariances = None,
initPriors = None,
int maxNumIterations = 100,
int numRepetitions = 1,
covarianceBound = None,
int verbose = 0):
cdef Mat[float] _X
cdef Mat[float] _initMeans
cdef Mat[float] _initCovariances
cdef Mat[float] _initPriors
cdef Mat[double] _covarianceBound
_X = pyarma_to_float(X)
if initMeans is not None:
_initMeans = pyarma_to_float(initMeans)
if initCovariances is not None:
_initCovariances = pyarma_to_float(initCovariances)
if initPriors is not None:
_initPriors = pyarma_to_float(initPriors)
if covarianceBound is not None:
_covarianceBound = pyarma_to_double(np.float64(covarianceBound, order='F').reshape((-1,1)))
cdef Mat[float] _means, _covariances, _priors, _posteriors
c_vl_gmm(_X, numClusters, \
_means, _covariances, _priors, _posteriors, \
initialization, _initMeans, _initCovariances, _initPriors, \
maxNumIterations, numRepetitions, _covarianceBound, verbose)
cdef np.ndarray[float, ndim=2] means, covariances, priors, posteriors
means = pyarma_from_float(_means)
covariances = pyarma_from_float(_covariances)
priors = pyarma_from_float(_priors)
posteriors = pyarma_from_float(_posteriors)
return means, covariances, priors, posteriors
def vl_fisher(np.ndarray[float, ndim=2] X,
np.ndarray[float, ndim=2] means,
np.ndarray[float, ndim=2] covariances,
np.ndarray[float, ndim=2] priors,
bool normalized = False,
bool squareRoot = False,
bool improved = False,
bool fast = False,
int verbosity = 0):
cdef Mat[float] _X, _means, _covariances, _priors
_X = pyarma_to_float(X)
_means = pyarma_to_float(means)
_covariances = pyarma_to_float(covariances)
_priors = pyarma_to_float(priors)
cdef Mat[float] _enc
c_vl_fisher(_X, _means, _covariances, _priors, _enc,
normalized, squareRoot, improved, fast, verbosity)
cdef np.ndarray[float, ndim=2] enc
enc = pyarma_from_float(_enc)
return enc