示例#1
0
void
test_query_from_file(int verbose)
{
    VlKDForest *forest;
    int i;
    float *data, *query;
    vl_size dim;
    vl_size num;

    /*
     * load
     */
    data = load_data("data.bin", &dim, &num);
    forest = load_VlKDForest("forest.bin");
    forest->data = data;

    if((query = (float *)malloc(dim * sizeof(float))) == NULL){
        printf("not enough memoey\n");
        exit(1);
    }
    for(i = 0;i < dim; i++) query[i] = 0.5;
    if(verbose) printf("has created a query\n");

    /*
     * search neighbors
     */
    vl_size numNeighbors = 10;
    unsigned int numComparisons = 0 ;
    unsigned int maxNumComparisons = 0 ;
    VlKDForestNeighbor * neighbors ;

    vl_kdforest_set_max_num_comparisons (forest, maxNumComparisons) ;
    neighbors = vl_malloc (sizeof(VlKDForestNeighbor) * numNeighbors) ;

    numComparisons = vl_kdforest_query (forest, neighbors, numNeighbors,
                                        query);

    for(i = 0;i < numNeighbors; i++){
        printf("%d %f\n",
               neighbors[i].index + 1, neighbors[i].distance);

        /* check distance */
        if(fabs(
                dist_l2(dim, query, &data[neighbors[i].index * dim]) -
                neighbors[i].distance) > 1e-6){
            printf("%d distance is different. %f\n",
                  dist_l2(dim, query, &data[neighbors[i].index * dim]) );
        }
        /* check order */
        if(i != 0 && neighbors[i-1].distance > neighbors[i].distance){
            printf("order is wrong.\n");
        }
    }

    vl_free(neighbors);

    vl_kdforest_delete(forest);
    free(data);
    free(query);
}
示例#2
0
  void LLC::clear_data()
  {
    if (kdforest_model_ != NULL)
    {
      vl_kdforest_delete(kdforest_model_);
      kdforest_model_ = NULL;
    }

    base_.reset();
    dim_ = 0;
    num_base_ = 0;
  }
示例#3
0
//void xcorr(float *tr1, float *tr2, double *corp, long long shift,
void knn (float *buffer_delayembedding_selec, float * density_vals , vl_size len_array, int num_neighbours)
{
    
    VL_PRINT ("KNN search !") ;
    printf("size of each frame = %llu\n", len_array);
    
    // instantiate a KD TREE object
    // VlKDForest returns new KDForest.
    // The data dimension dimension and the number of trees numTrees must not be smaller than one.
    VlKDForest* kdtree_obj;
    
    // define params for the object
    kdtree_obj = vl_kdforest_new(VL_TYPE_FLOAT, 3, 1, VlDistanceL2);
    // populate it with the data to be subsampled
    vl_kdforest_build	(kdtree_obj,len_array,buffer_delayembedding_selec);
    
    // instantiate a searcher object for querying knn
    // A searcher is an object attached to the forest which must be created before running the queries. Each query has to be invoked with the searcher as its argument; make it point to the declared and defined kdtree
    VlKDForestSearcher* kdforest_search_obj=vl_kdforest_new_searcher(kdtree_obj);
    VlKDForestNeighbor neighbours[num_neighbours+1];
    float *query = malloc(3*sizeof(float));
    
  /*  for (int i=0; i<len_array; i++)
    {
        printf("% d th original :%f   %f   %f\n",i, buffer_delayembedding_selec [3*i],buffer_delayembedding_selec [3*i+1],buffer_delayembedding_selec [3*i+2]);
    } */
    for (int i=0; i<len_array; i++)
    {
      //  printf("DBG: buffer selec[%d] :%f   %f   %f\n",i, buffer_delayembedding_selec [3*i],buffer_delayembedding_selec [3*i+1],buffer_delayembedding_selec [3*i+2]);
        get_query(buffer_delayembedding_selec , query , i);
       int nvisited = vl_kdforestsearcher_query(kdforest_search_obj, &neighbours[0], num_neighbours+1, query);
        
    //    printf("visited nodes = %d ", nvisited);
    //  printf("for knn of %dth node : (%f,%f,%f)  :", i,*query,*(query+1),*(query+2));
        
            density_vals[i]=1/sqrtf(neighbours[num_neighbours].distance);

           // printf("DB %f \n",( density_vals[i]));

        
    }
    

    // Destroy all created instances
     vl_kdforest_delete	(	kdtree_obj	);
    free(query);
    
}
示例#4
0
  void LLC::SetUp()
  {
    if (base_.get() == NULL || dim_ == 0 || num_base_ == 0)
    {
      cerr << "ERROR: must set the base before." << endl;
      exit(-1);
    }

    if (kdforest_model_ != NULL)
      vl_kdforest_delete(kdforest_model_);

    kdforest_model_ = vl_kdforest_new(VL_TYPE_FLOAT, dim_, num_tree_,
                                      dist_method_);

    vl_kdforest_set_thresholding_method(kdforest_model_, thrd_method_);
    vl_kdforest_set_max_num_comparisons(kdforest_model_, max_comp_);
    vl_kdforest_build(kdforest_model_, num_base_, base_.get());

    has_setup_ = true;
  }
示例#5
0
void
mexFunction(int nout, mxArray *out[],
            int nin, const mxArray *in[])
{
  enum {IN_FOREST = 0, IN_DATA, IN_QUERY, IN_END} ;
  enum {OUT_INDEX = 0, OUT_DISTANCE} ;

  int            verbose = 0 ;
  int            opt ;
  int            next = IN_END ;
  mxArray const *optarg ;

  VlKDForest * forest ;
  mxArray const * forest_array = in[IN_FOREST] ;
  mxArray const * data_array = in[IN_DATA] ;
  mxArray const * query_array = in[IN_QUERY] ;
  mxArray * index_array ;
  mxArray * distance_array ;
  void * query ;
  vl_uint32 * index ;
  void * distance ;
  vl_size numNeighbors = 1 ;
  vl_size numQueries ;
  vl_uindex qi, ni;
  unsigned int numComparisons = 0 ;
  unsigned int maxNumComparisons = 0 ;
  VlKDForestNeighbor * neighbors ;
  mxClassID dataClass ;

  VL_USE_MATLAB_ENV ;

  /* -----------------------------------------------------------------
   *                                               Check the arguments
   * -------------------------------------------------------------- */

  if (nin < 3) {
    vlmxError(vlmxErrNotEnoughInputArguments, NULL) ;
  }
  if (nout > 2) {
    vlmxError(vlmxErrTooManyOutputArguments, NULL) ;
  }

  forest = new_kdforest_from_array (forest_array, data_array) ;

  dataClass = mxGetClassID (data_array) ;
  if (mxGetClassID (query_array) != dataClass) {
    vlmxError(vlmxErrInvalidArgument,
             "QUERY must have the same storage class as DATA.") ;
  }
  if (! vlmxIsReal (query_array)) {
    vlmxError(vlmxErrInvalidArgument,
             "QUERY must be real.") ;
  }
  if (! vlmxIsMatrix (query_array, forest->dimension, -1)) {
    vlmxError(vlmxErrInvalidArgument,
             "QUERY must be a matrix with TREE.NUMDIMENSIONS rows.") ;
  }

  while ((opt = vlmxNextOption (in, nin, options, &next, &optarg)) >= 0) {
    switch (opt) {
      case opt_num_neighs :
        if (! vlmxIsScalar(optarg) ||
            (numNeighbors = mxGetScalar(optarg)) < 1) {
          vlmxError(vlmxErrInvalidArgument,
                   "NUMNEIGHBORS must be a scalar not smaller than one.") ;
        }
        break;

      case opt_max_num_comparisons :
        if (! vlmxIsScalar(optarg)) {
          vlmxError(vlmxErrInvalidArgument,
                   "MAXNUMCOMPARISONS must be a scalar.") ;
        }
        maxNumComparisons = mxGetScalar(optarg) ;
        break;

      case opt_verbose :
        ++ verbose ;
        break ;
    }
  }

  vl_kdforest_set_max_num_comparisons (forest, maxNumComparisons) ;

  neighbors = vl_malloc (sizeof(VlKDForestNeighbor) * numNeighbors) ;

  query = mxGetData (query_array) ;
  numQueries = mxGetN (query_array) ;

  out[OUT_INDEX] = index_array = mxCreateNumericMatrix
    (numNeighbors, numQueries, mxUINT32_CLASS, mxREAL) ;

  out[OUT_DISTANCE] = distance_array = mxCreateNumericMatrix
    (numNeighbors, numQueries, dataClass, mxREAL) ;

  index = mxGetData (index_array) ;
  distance = mxGetData (distance_array) ;

  if (verbose) {
    VL_PRINTF ("vl_kdforestquery: number of queries: %d\n", numQueries) ;
    VL_PRINTF ("vl_kdforestquery: number of neighbors per query: %d\n", numNeighbors) ;
    VL_PRINTF ("vl_kdforestquery: max num of comparisons per query: %d\n",
               vl_kdforest_get_max_num_comparisons (forest)) ;
  }

  for (qi = 0 ; qi < numQueries ; ++ qi) {
    numComparisons += vl_kdforest_query (forest, neighbors, numNeighbors,
                                         query) ;
    switch (dataClass) {
      case mxSINGLE_CLASS:
      {
        float * distance_ = (float*) distance ;
        for (ni = 0 ; ni < numNeighbors ; ++ni) {
          *index++     = neighbors[ni].index + 1 ;
          *distance_++ = neighbors[ni].distance ;
        }
        query = (float*)query + vl_kdforest_get_data_dimension (forest) ;
        distance = distance_ ;
        break ;
      }
      case mxDOUBLE_CLASS:
      {
        double * distance_ = (double*) distance ;
        for (ni = 0 ; ni < numNeighbors ; ++ni) {
          *index++     = neighbors[ni].index + 1 ;
          *distance_++ = neighbors[ni].distance ;
        }
        query = (double*)query + vl_kdforest_get_data_dimension (forest)  ;
        distance = distance_ ;
        break ;
      }
      default:
        abort() ;
    }
  }

  if (verbose) {
    VL_PRINTF ("vl_kdforestquery: number of comparisons per query: %.3f\n",
               ((double) numComparisons) / numQueries) ;
    VL_PRINTF ("vl_kdforestquery: number of comparisons per neighbor: %.3f\n",
               ((double) numComparisons) / (numQueries * numNeighbors)) ;
  }

  vl_kdforest_delete (forest) ;
  vl_free (neighbors) ;
}
示例#6
0
void
test_simple(int verbose)
{
    VlKDForest *forest;
    int i, j;
    float *data, *query;
    vl_size dim = 128;
    vl_size num = 10000;
    vl_size numTrees = 1;

    /*
     * create a test data
     */
    if((data = create_data(dim ,num)) == NULL){
        printf("not enough memoey\n");
        exit(1);
    }
    if(verbose) printf("has created a test data\n");

    if((query = (float *)malloc(dim * sizeof(float))) == NULL){
        printf("not enough memoey\n");
        exit(1);
    }
    for(i = 0;i < dim; i++) query[i] = 0.5;
    if(verbose) printf("has created a query\n");

    /*
     * build a kd-tree forest
     */
    forest = kdtreebuild(1, dim, numTrees, VL_KDTREE_MEDIAN, num, data);
    if(verbose) printf("has created a forest\n");
    
    if(verbose && 0){
        for(j = 0;j < numTrees; j++){
            printf("dataIndex[%d] = [", j);
            for(i = 0;i < forest->numData; i++){
                printf("%d ",
                       forest->trees[j]->dataIndex[i].index + 1);
            }
            printf("]\n");
        }
    }

    /*
     * save
     */
    save_data("data.bin", data, dim, num);
    save_VlKDForest("forest.bin", forest);

    /*
     * search neighbors
     */
    vl_size numNeighbors = 10;
    unsigned int numComparisons = 0 ;
    unsigned int maxNumComparisons = 0 ;
    VlKDForestNeighbor * neighbors ;

    vl_kdforest_set_max_num_comparisons (forest, maxNumComparisons) ;
    neighbors = vl_malloc (sizeof(VlKDForestNeighbor) * numNeighbors) ;

    numComparisons = vl_kdforest_query (forest, neighbors, numNeighbors,
                                        query);

    for(i = 0;i < numNeighbors; i++){
        printf("%d %f\n",
               neighbors[i].index + 1, neighbors[i].distance);

        /* check distance */
        if(fabs(
                dist_l2(dim, query, &data[neighbors[i].index * dim]) -
                neighbors[i].distance) > 1e-6){
            printf("%d distance is different. %f\n",
                  dist_l2(dim, query, &data[neighbors[i].index * dim]) );
        }
        /* check order */
        if(i != 0 && neighbors[i-1].distance > neighbors[i].distance){
            printf("order is wrong.\n");
        }
    }

    vl_free(neighbors);
    vl_kdforest_delete(forest);
    free(data);
    free(query);
}
示例#7
0
void
mexFunction(int nout, mxArray *out[],
            int nin, const mxArray *in[])
{
  enum {IN_DATA = 0, IN_END} ;
  enum {OUT_TREE = 0} ;

  int            verbose = 0 ;
  int            opt ;
  int            next = IN_END ;
  mxArray const *optarg ;

  VlKDForest * forest ;
  void * data ;
  vl_size numData ;
  vl_size dimension ;
  mxClassID dataClass ;
  vl_type dataType ;
  int thresholdingMethod = VL_KDTREE_MEDIAN ;
  vl_size numTrees = 1 ;

  VL_USE_MATLAB_ENV ;

  /* -----------------------------------------------------------------
   *                                               Check the arguments
   * -------------------------------------------------------------- */

  if (nin < 1) {
    vlmxError(vlmxErrInvalidArgument,
             "At least one argument required") ;
  } else if (nout > 2) {
    vlmxError(vlmxErrInvalidArgument,
             "Too many output arguments");
  }

  dataClass = mxGetClassID(IN(DATA)) ;

  if (! vlmxIsMatrix (IN(DATA), -1, -1) ||
      ! vlmxIsReal (IN(DATA))) {
    vlmxError(vlmxErrInvalidArgument,
             "DATA must be a real matrix ") ;
  }

  switch (dataClass) {
    case mxSINGLE_CLASS : dataType = VL_TYPE_FLOAT ; break ;
    case mxDOUBLE_CLASS : dataType = VL_TYPE_DOUBLE ; break ;
    default:
      vlmxError(vlmxErrInvalidArgument,
               "DATA must be either SINGLE or DOUBLE") ;
  }

  while ((opt = vlmxNextOption (in, nin, options, &next, &optarg)) >= 0) {
    char buffer [1024] ;
    switch (opt) {
      case opt_threshold_method :
        mxGetString (optarg, buffer, sizeof(buffer)/sizeof(buffer[0])) ;
        if (! vlmxIsString(optarg, -1)) {
          vlmxError(vlmxErrInvalidOption,
                   "THRESHOLDMETHOD must be a string") ;
        }
        if (vl_string_casei_cmp(buffer, "median") == 0) {
          thresholdingMethod = VL_KDTREE_MEDIAN ;
        } else if (vl_string_casei_cmp(buffer, "mean") == 0) {
          thresholdingMethod = VL_KDTREE_MEAN ;
        } else {
          vlmxError(vlmxErrInvalidOption,
                   "Unknown thresholding method %s", buffer) ;
        }
        break ;

      case opt_num_trees :
        if (! vlmxIsScalar(optarg) ||
            (numTrees = mxGetScalar(optarg)) < 1) {
          vlmxError(vlmxErrInvalidOption,
                   "NUMTREES must be not smaller than one") ;
        }
        break ;

      case opt_verbose :
        ++ verbose ;
        break ;
    }
  }

  data = mxGetData (IN(DATA)) ;
  numData = mxGetN (IN(DATA)) ;
  dimension = mxGetM (IN(DATA)) ;

  forest = vl_kdforest_new (dataType, dimension, numTrees) ;
  vl_kdforest_set_thresholding_method (forest, thresholdingMethod) ;

  if (verbose) {
    char const * str = 0 ;
    mexPrintf("vl_kdforestbuild: data %s [%d x %d]\n",
              vl_get_type_name (dataType), dimension, numData) ;
    switch (vl_kdforest_get_thresholding_method(forest)) {
      case VL_KDTREE_MEAN : str = "mean" ; break ;
      case VL_KDTREE_MEDIAN : str = "median" ; break ;
      default: abort() ;
    }
    mexPrintf("vl_kdforestbuild: threshold selection method: %s\n", str) ;
    mexPrintf("vl_kdforestbuild: number of trees: %d\n",
              vl_kdforest_get_num_trees(forest)) ;
  }

  /* -----------------------------------------------------------------
   *                                                            Do job
   * -------------------------------------------------------------- */

  vl_kdforest_build (forest, numData, data) ;

  if (verbose) {
    vl_uindex ti ;
    for (ti = 0 ; ti < vl_kdforest_get_num_trees(forest) ; ++ ti) {
      mexPrintf("vl_kdforestbuild: tree %d: depth %d, num nodes %d\n",
                ti,
                vl_kdforest_get_depth_of_tree(forest, ti),
                vl_kdforest_get_num_nodes_of_tree(forest, ti)) ;
    }
  }

  out[OUT_TREE] = new_array_from_kdforest (forest) ;
  vl_kdforest_delete (forest) ;
}
示例#8
0
void kkdforest_del()
{
    if (forest != 0) vl_kdforest_delete(forest);
}
示例#9
0
void
mexFunction(int nout, mxArray *out[],
            int nin, const mxArray *in[])
{
  enum {IN_FOREST = 0, IN_DATA, IN_QUERY, IN_END} ;
  enum {OUT_INDEX = 0, OUT_DISTANCE} ;

  int verbose = 0 ;
  int opt ;
  int next = IN_END ;
  mxArray const *optarg ;

  VlKDForest * forest ;
  mxArray const * forest_array = in[IN_FOREST] ;
  mxArray const * data_array = in[IN_DATA] ;
  mxArray const * query_array = in[IN_QUERY] ;
  void * query ;
  vl_uint32 * index ;
  void * distance ;
  vl_size numNeighbors = 1 ;
  vl_size numQueries ;
  unsigned int numComparisons = 0 ;
  unsigned int maxNumComparisons = 0 ;
  mxClassID dataClass ;
  vl_index i ;

  VL_USE_MATLAB_ENV ;

  /* -----------------------------------------------------------------
   *                                               Check the arguments
   * -------------------------------------------------------------- */

  if (nin < 3) {
    vlmxError(vlmxErrNotEnoughInputArguments, NULL) ;
  }
  if (nout > 2) {
    vlmxError(vlmxErrTooManyOutputArguments, NULL) ;
  }

  forest = new_kdforest_from_array (forest_array, data_array) ;

  dataClass = mxGetClassID (data_array) ;
  if (mxGetClassID (query_array) != dataClass) {
    vlmxError(vlmxErrInvalidArgument,
              "QUERY must have the same storage class as DATA.") ;
  }
  if (! vlmxIsReal (query_array)) {
    vlmxError(vlmxErrInvalidArgument,
              "QUERY must be real.") ;
  }
  if (! vlmxIsMatrix (query_array, forest->dimension, -1)) {
    vlmxError(vlmxErrInvalidArgument,
              "QUERY must be a matrix with TREE.NUMDIMENSIONS rows.") ;
  }

  while ((opt = vlmxNextOption (in, nin, options, &next, &optarg)) >= 0) {
    switch (opt) {
      case opt_num_neighs :
        if (! vlmxIsScalar(optarg) ||
            (numNeighbors = mxGetScalar(optarg)) < 1) {
          vlmxError(vlmxErrInvalidArgument,
                    "NUMNEIGHBORS must be a scalar not smaller than one.") ;
        }
        break;

      case opt_max_num_comparisons :
        if (! vlmxIsScalar(optarg)) {
          vlmxError(vlmxErrInvalidArgument,
                    "MAXNUMCOMPARISONS must be a scalar.") ;
        }
        maxNumComparisons = mxGetScalar(optarg) ;
        break;

      case opt_verbose :
        ++ verbose ;
        break ;
    }
  }

  vl_kdforest_set_max_num_comparisons (forest, maxNumComparisons) ;

  query = mxGetData (query_array) ;
  numQueries = mxGetN (query_array) ;

  out[OUT_INDEX] = mxCreateNumericMatrix (numNeighbors, numQueries, mxUINT32_CLASS, mxREAL) ;
  out[OUT_DISTANCE] = mxCreateNumericMatrix (numNeighbors, numQueries, dataClass, mxREAL) ;

  index = mxGetData (out[OUT_INDEX]) ;
  distance = mxGetData (out[OUT_DISTANCE]) ;

  if (verbose) {
    VL_PRINTF ("vl_kdforestquery: number of queries: %d\n", numQueries) ;
    VL_PRINTF ("vl_kdforestquery: number of neighbors per query: %d\n", numNeighbors) ;
    VL_PRINTF ("vl_kdforestquery: max num of comparisons per query: %d\n",
               vl_kdforest_get_max_num_comparisons (forest)) ;
  }

  numComparisons = vl_kdforest_query_with_array (forest, index, numNeighbors, numQueries, distance, query) ;
  
  vl_kdforest_delete(forest) ;
  
  /* adjust for MATLAB indexing */
  for (i = 0 ; i < (signed) (numNeighbors * numQueries) ; ++i) { index[i] ++ ; }

  if (verbose) {
    VL_PRINTF ("vl_kdforestquery: number of comparisons per query: %.3f\n",
               ((double) numComparisons) / numQueries) ;
    VL_PRINTF ("vl_kdforestquery: number of comparisons per neighbor: %.3f\n",
               ((double) numComparisons) / (numQueries * numNeighbors)) ;
  }
}