Skip to content

Instantly share code, notes, and snippets.

@vbalnt
Last active September 7, 2015 15:44
Show Gist options
  • Save vbalnt/7b531ecced4c8c1ed03c to your computer and use it in GitHub Desktop.
Save vbalnt/7b531ecced4c8c1ed03c to your computer and use it in GitHub Desktop.
The training code sample for the offline test selection for BOLD - Binary Online Learned Descriptor (CVPR 2015)
/* Creation of the test array & calling of the learning func */
bintest orb_tests[n_tests];
learn_orb_tests_g2(training_data, orb_tests ,1024,32,10000);
/* fwrite_bintests(orb_tests,1024,"orb1024.descr"); */
/* Actual learning func */
void learn_orb_tests_g2(dataset data,bintest *ltests,int dims,int patch_size,int nlearn)
{
bintest *all_tests;
int n_tests = 10000;
all_tests = (bintest*) malloc(n_tests * sizeof(bintest));
create_brief_tests_g2(all_tests, n_tests, 1024);
double *variances;
variances = (double*) malloc(n_tests * sizeof(double));
vl_tic();
#pragma omp parallel for
for (int i=1; i < n_tests; i++) {
variances[i] = get_test_variance(data, all_tests[i],nlearn);
}
VL_PRINTF("variances took : %.10f s (simd) \n", vl_toc ()/8) ;
/* argsort variances */
int *ind;
ind =(int*) malloc(n_tests * sizeof(int));
for(int q=0;q<n_tests;q++){
ind[q] = q;
}
vars_values = variances;
qsort(ind, n_tests, sizeof(*ind), cmp_argsort_vars);
int orb_cnt = 0;
/* add the first test */
ltests[0] = all_tests[ind[0]];
orb_cnt++;
double dist;
bintest rtest;
double thres = 0.4;
while (orb_cnt<dims)
{
vl_tic();
thres = thres - 0.1;
for (int i = 0; i < n_tests; i++) {
rtest = all_tests[ind[i]];
int good = 1;
for (int k = 0; k < orb_cnt; k++) {
dist = cmp_data_bitstrings(data,rtest,ltests[k],nlearn);
if (dist<thres)
{
good = 0;
break;
}
}
if (good) /* add it */
{
ltests[orb_cnt] = all_tests[ind[i]];
orb_cnt++;
if (orb_cnt==dims)
{
break;
}
printf("added test: %d \n" ,orb_cnt);
}
}
VL_PRINTF("loop all pairs took : %.10f s (simd) \n", vl_toc ()/8) ;
}
free(all_tests);
free(variances);
}
typedef struct bintest
{
int p1;
int p2;
} bintest;
void create_brief_tests_g2(bintest *tests,int dims,int patch_size)
{
int x1,x2,y1,y2;
int S = 3;
for (int i = 0; i < dims; i++) {
x1 = S + (rand() % (32 - 2*S));
y1 = S +( rand() % (32 - 2*S));
x2 = S + (rand() % (32 - 2*S));
y2 = S + (rand() % (32 - 2*S));
tests[i].p1 = x1 + 32*y1;
tests[i].p2 = x2 + 32*y2;
}
}
double cmp_data_bitstrings(dataset data,bintest t1,bintest t2,int nlearn)
{
double dist = 0;
int r1,r2;
for (int i = 0; i < nlearn; i++) {
r1 = (data.smoothed[i][t1.p1] < data.smoothed[i][t1.p2]);
r2 = (data.smoothed[i][t2.p1] < data.smoothed[i][t2.p2]);
dist = dist + (r1^r2);
}
return dist/nlearn;
}
double get_test_variance(dataset data,bintest t,int nlearn)
{
double p =0;
for (int i = 0; i < nlearn; i++) {
p = p + (data.smoothed[i][t.p1] < data.smoothed[i][t.p2]);
}
p = p / nlearn;
return p * (1-p);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment