-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
358 lines (343 loc) · 8.65 KB
/
utils.cpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include"utils.h"
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include<vector>
extern "C" {
#include <yael/vector.h>
}
#include<global.h>
bool LoadSurfDescriptor(const char *filename,float *&descriptor,int &descriptor_num)
{
descriptor = new float[kSurfDescriptorDim * kMaxSurfDescriptorNUM]();
if(descriptor == NULL)
{
printf("Can't apply memeory\n");
return false;
}
std::ifstream infile(filename);
float x,y,scale,orientation;
int laplacian;
int dim,count;
// read descriptor length/number of ipoints
infile >> dim;
infile >> count;
if(count > kMaxSurfDescriptorNUM) count = kMaxSurfDescriptorNUM;
descriptor_num = count;
for(int i = 0;i != count;++i)
{
// read vals
infile >> scale;
infile >> x;
infile >> y;
infile >> orientation;
infile >> laplacian;
infile >> scale;
for(int j = 0;j != kSurfDescriptorDim;++j)
infile >> descriptor[i * kSurfDescriptorDim + j];
}
return true;
}
int LoadSurfBin(const char *filename, int *fealen,float **features)
{
FILE *fid = fopen(filename,"rb");
if(fid== NULL)
{
printf("I/O error: Unable to open the file %s\n",filename);
return -1;
}
float version;
int featype,dim,len;
fread(&version,sizeof(float),1,fid);
fread(&featype,sizeof(int),1,fid);
if(featype != 1)
{
printf("Error:the feature type isn't surf,featype = %d\n",featype);
return -1;
}
fread(&dim,sizeof(int),1,fid);
fread(&len,sizeof(int),1,fid);
float *features_in = (float *)calloc(len * dim, sizeof(float));
if(features_in == NULL)
{
printf("Can't apply enough memory!\n");
return -1;
}
float x,y,scale,orientation;
int laplacian;
float *temp;
for(int i = 0;i != len;++i)
{
temp = features_in + i * dim;
fread(&x,sizeof(float),1,fid);
fread(&y,sizeof(float),1,fid);
fread(&scale,sizeof(float),1,fid);
fread(&orientation,sizeof(float),1,fid);
fread(&laplacian,sizeof(int),1,fid);
fread(temp,sizeof(float),dim,fid);
}
*fealen = len;
*features = features_in;
fclose(fid);
return 0;
}
// param
// return: 0, 正确返回
// -1, 此文件的错误
int LoadVlad(const char *filename,int &kfNum,float *&features,int *&frameid_out)
{
FILE* featureFile = fopen(filename,"rb");
if(featureFile == NULL)
{
printf("Can't Open file %s\n",filename);
return -1;
}
float version = 0.0;
int featype = 0, dim = 0;
fread(&version,sizeof(float),1,featureFile);
fread(&featype,sizeof(int),1,featureFile);
fread(&dim,sizeof(int),1,featureFile);
if(featype != 10 || dim != kSurfDescriptorDim * kCentroidsNum)
{
printf("This File is't vlad features file or the dim is't right!\n");
fclose(featureFile);
return -1;
}
int count = fread(&kfNum,sizeof(int),1,featureFile);
if (count!=1 || kfNum==0)
{
printf("Read smpfile %s error:count = %d, kfNum = %d\n",filename,count,kfNum);
fclose(featureFile);
return -1;
}
float *pCoarseFeature = (float*)calloc(kfNum * dim,sizeof(float));
if (pCoarseFeature==NULL)
{
printf("failed to allocate memory for feature\n");
fclose(featureFile);
return -1;
}
int *frameid = (int *)calloc(kfNum,sizeof(int));
if(frameid == NULL)
{
printf("failed to allocate memory for feature\n");
fclose(featureFile);
free(pCoarseFeature);
return -1;
}
for(int i = 0;i != kfNum;++i)
{
fread(frameid + i,sizeof(int),1,featureFile);
count = fread(pCoarseFeature + i * dim,sizeof(float),dim,featureFile);
if (count != dim)
{
printf("Read smpfile %s length error:count = %d, kfNum = %d\n",filename,count,kfNum);
free(pCoarseFeature);
free(frameid);
fclose(featureFile);
return -1;
}
}
fclose(featureFile);
// printf("before normalize, norm = %.2f\n",FvecNorm2(pCoarseFeature,kfNum * dim));
// // power normalization
// double alpha = 0.5;
// FvecSignPow(pCoarseFeature,kfNum * dim,alpha);
// // L2 normalization
// FvecsNormalize2(pCoarseFeature,kfNum,dim);
// printf("after normalize, norm = %.2f\n",FvecNorm2(pCoarseFeature,kfNum * dim));
features = pCoarseFeature;
if(frameid_out) frameid_out = frameid;
else free(frameid);
return 0;
}
bool LoadVideoPcaVlad(const char *filename, int &framenum_out, float *&features_out, unsigned int *&frameid_out)
{
FILE* fin = fopen(filename,"rb");
if(fin == NULL)
{
printf("Can't Open file %s\n",filename);
return false;
}
float version = 0.0;
int featype = 0, dim = 0, framenum = 0;
fread(&version,sizeof(float),1,fin);
fread(&featype,sizeof(int),1,fin);
fread(&dim,sizeof(int),1,fin);
if(featype != 10 || dim != kPcaVladDim)
{
printf("This File is't pcavlad features file or the dim is't right!\n");
fclose(fin);
return false;
}
int count = fread(&framenum,sizeof(int),1,fin);
if (count != 1)
{
printf("Read smpfile %s error:count = %d, framenum = %d\n",filename,count,framenum);
fclose(fin);
return false;
}
if(framenum == 0)
{
framenum_out = framenum;
return true;
}
unsigned int *frameid = (unsigned int *)calloc(framenum, sizeof(unsigned int));
float *features = (float *)calloc(framenum * dim, sizeof(float));
if (features == NULL || frameid == NULL)
{
printf("failed to allocate memory for feature\n");
fclose(fin);
return false;
}
for(int i = 0; i != framenum; ++i)
{
fread(frameid + i, sizeof(unsigned int), 1, fin);
count = fread(features + i * dim, sizeof(float), dim ,fin);
if(count != dim)
{
printf("Read smpfile %s length error:count = %d, framenum = %d\n",filename,count,framenum);
free(features);
free(frameid);
fclose(fin);
return false;
}
}
fclose(fin);
framenum_out = framenum;
features_out = features;
frameid_out = frameid;
return true;
}
bool LoadBatchPcaVlad(const char *filename, int &filenum_out, float *&features_out, unsigned int *&fileid_out)
{
FILE* fin = fopen(filename,"rb");
if(fin == NULL)
{
printf("Can't Open file %s\n",filename);
return false;
}
float version = 0.0;
int featype = 0, dim = 0, filenum = 0;
fread(&version,sizeof(float),1,fin);
fread(&featype,sizeof(int),1,fin);
fread(&dim,sizeof(int),1,fin);
if(featype != 11 || dim != kPcaVladDim)
{
printf("This File is't pcavlad features file or the dim is't right!\n");
fclose(fin);
return false;
}
int count = fread(&filenum,sizeof(int),1,fin);
if (count != 1 || filenum == 0)
{
printf("Read smpfile %s error:count = %d, filenum = %d\n",filename,count,filenum);
fclose(fin);
return false;
}
unsigned int *fileid = (unsigned int *)calloc(filenum, sizeof(unsigned int));
float *features = (float *)calloc(filenum * dim, sizeof(float));
if (features == NULL || fileid == NULL)
{
printf("failed to allocate memory for feature\n");
fclose(fin);
return false;
}
for(int i = 0; i != filenum; ++i)
{
fread(fileid + i, sizeof(unsigned int), 1, fin);
count = fread(features + i * dim, sizeof(float), dim ,fin);
if(count != dim)
{
printf("Read smpfile %s length error:count = %d, filenum = %d\n",filename,count,filenum);
free(features);
free(fileid);
fclose(fin);
return false;
}
}
fclose(fin);
filenum_out = filenum;
features_out = features;
fileid_out = fileid;
return true;
}
bool LoadSiftgeo(const char *filename,float *&descriptor,int &descriptor_num)
{
unsigned char *siftgeo;
int dim;
if((descriptor_num = bvecs_new_from_siftgeo(filename,&dim,&siftgeo,NULL,NULL)) < 0)
{
free(siftgeo);
return false;
}
descriptor = bvec2fvec(siftgeo,descriptor_num * dim);
free(siftgeo);
return true;
}
// L1 Normalize(original features has unit L2 norm) + square root each element
// param:
// n the number of features
// d the dimension of features
// remark:
// 针对负值情况,参考http://blog.csdn.net/axman/article/details/8979508处理
// "没有必要,只要注意总和变成正数就行了"
void RootOperate(float *features, int n, int d)
{
int i,j;
float eps = 1E-12;
for(i = 0;i != n;++i)
{
//fvec_normalize(features + i * d,d,1); // L1 normalize
float sum = 0.0;
for(j = 0;j != d; ++j)
sum += fabs(features[i*d+j]);
for(j = 0; j != d;++j)
{
if(features[i*d+j] < 0) features[i * d + j] = (float) -sqrt(-features[i * d + j] / (sum + eps)); // 防止sum为0
else features[i * d + j] = (float) sqrt(features[i * d + j] / (sum + eps));
}
}
}
void Normalize2(float *v,int nv)
{
int i = 0;
float sum = 0.0;
for(i = 0;i < nv;++i)
{
sum += v[i] * v[i];
}
sum = sqrt(sum);
if(sum == 0)
{
for(i = 0;i < nv;++i)
v[i] = 1.0 / sqrt(nv);
}
else
{
for(i = 0;i < nv;++i)
v[i] /= sum;
}
}
void FvecsNormalize2(float *v,int n,int d)
{
for(int i = 0;i < n;++i)
{
Normalize2(v + i * d,d);
}
}
void FvecSignPow(float * v, long n, double scal)
{
long i;
for (i = 0 ; i < n ; i++)
v[i] = v[i]>0 ? pow(v[i],scal) : -pow(-v[i],scal);
}
float FvecNorm2(const float *v, int n)
{
int i;
float sum = 0.0;
for(i = 0;i != n;++i)
sum += v[i] * v[i];
return sqrt(sum);
}