예제 #1
0
파일: test_kdtree.c 프로젝트: jun930/vlfeat
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
파일: VLFeat.C 프로젝트: aalto-cbir/PicSOM
  bool VLFeat::Initialize_kmeans() {
    bin_data dat(kmeansname);
      
    kmeans = vl_kmeans_new(VL_TYPE_FLOAT, VlDistanceL2);
    vl_kmeans_set_centers(kmeans, dat.vector_address(0),
			  dat.vdim(), dat.nobjects());

    if (MethodVerbose())
      cout << "VLFeat::Initialize_kmeans() read in kmeans <" << kmeansname
	   << "> dim=" << descriptor_dim() << " nclust=" << nclusters()
	   << endl;

    
    if (kdtree_ntrees) {
      kdtree = vl_kdforest_new(VL_TYPE_FLOAT,  descriptor_dim(), kdtree_ntrees,
			       VlDistanceL2);

      vl_kdforest_build(kdtree, nclusters(), means());
      vl_kdforest_set_max_num_comparisons(kdtree, kdtree_maxcomps);
    }

    do_vlad = true;

    return true;
  }
예제 #3
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;
  }
예제 #4
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) ;
}
예제 #5
0
파일: test_kdtree.c 프로젝트: jun930/vlfeat
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);
}
예제 #6
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)) ;
  }
}