VlKDForest * load_VlKDForest(const char *fname) { FILE *fp; size_t n; VlKDForest *self = vl_malloc (sizeof(VlKDForest)) ; if((fp = fopen(fname, "rb")) == NULL) return -1; n = read_VlKDForest(fp, self); fclose(fp); self -> rand = vl_get_rand (); self -> searchHeapArray = 0; self -> searchIdBook = 0; switch (self->dataType) { case VL_TYPE_FLOAT: self -> distanceFunction = (void(*)(void)) vl_get_vector_comparison_function_f (VlDistanceL2) ; break ; case VL_TYPE_DOUBLE : self -> distanceFunction = (void(*)(void)) vl_get_vector_comparison_function_d (VlDistanceL2) ; break ; default : abort() ; } return self; }
static void VL_XCAT(_vl_kmeans_seed_centers_with_rand_data_, SFX) (VlKMeans * self, TYPE const * data, vl_size dimension, vl_size numData, vl_size numCenters) { vl_uindex i, j, k ; VlRand * rand = vl_get_rand () ; self->dimension = dimension ; self->numCenters = numCenters ; self->centers = vl_malloc (sizeof(TYPE) * dimension * numCenters) ; { vl_uindex * perm = vl_malloc (sizeof(vl_uindex) * numData) ; #if (FLT == VL_TYPE_FLOAT) VlFloatVectorComparisonFunction distFn = vl_get_vector_comparison_function_f(self->distance) ; #else VlDoubleVectorComparisonFunction distFn = vl_get_vector_comparison_function_d(self->distance) ; #endif TYPE * distances = vl_malloc (sizeof(TYPE) * numCenters) ; /* get a random permutation of the data point */ for (i = 0 ; i < numData ; ++i) perm[i] = i ; _vl_kmeans_shuffle (perm, numData, rand) ; for (k = 0, i = 0 ; k < numCenters ; ++ i) { /* compare the next data point to all centers collected so far to detect duplicates (if there are enough left) */ if (numCenters - k < numData - i) { vl_bool duplicateDetected = VL_FALSE ; VL_XCAT(vl_eval_vector_comparison_on_all_pairs_, SFX)(distances, dimension, data + dimension * perm[i], 1, (TYPE*)self->centers, k, distFn) ; for (j = 0 ; j < k ; ++j) { duplicateDetected |= (distances[j] == 0) ; } if (duplicateDetected) continue ; } /* ok, it is not a duplicate so we can accept it! */ memcpy ((TYPE*)self->centers + dimension * k, data + dimension * perm[i], sizeof(TYPE) * dimension) ; k ++ ; } vl_free(distances) ; vl_free(perm) ; } }
DWORD WINAPI testThread(LPVOID args) { int j ; int id = *(DWORD*)args ; vl_tic() ; for (j = 0 ; j < 10 ; ++j) { printf("Thread %5d: %d\n", id, vl_rand_int31(vl_get_rand())) ; fflush(stdout) ; } printf("Thread %5d: elapsed time: %.2f s\n", id, vl_toc()) ; return 0 ; }
void * testThread(void * args) { int j ; int id = *(int*)args ; vl_tic() ; for (j = 0 ; j < 10 ; ++j) { printf("Thread %5d: %d\n", id, vl_rand_int31(vl_get_rand())) ; fflush(stdout) ; } printf("Thread %5d: elapsed time: %.2f s\n", id, vl_toc()) ; return NULL ; }
VL_EXPORT VlKDForest * vl_kdforest_new (vl_type dataType, vl_size dimension, vl_size numTrees) { VlKDForest * self = vl_malloc (sizeof(VlKDForest)) ; assert(dataType == VL_TYPE_FLOAT || dataType == VL_TYPE_DOUBLE) ; assert(dimension >= 1) ; assert(numTrees >= 1) ; self -> rand = vl_get_rand () ; self -> dataType = dataType ; self -> numData = 0 ; self -> data = 0 ; self -> dimension = dimension ; self -> numTrees = numTrees ; self -> trees = 0 ; self -> thresholdingMethod = VL_KDTREE_MEDIAN ; self -> splitHeapSize = (numTrees == 1) ? 1 : VL_KDTREE_SPLIT_HEALP_SIZE ; self -> splitHeapNumNodes = 0 ; self -> searchHeapArray = 0 ; self -> searchHeapNumNodes = 0 ; self -> searchMaxNumComparisons = 0 ; self -> searchIdBook = 0 ; self -> searchId = 0 ; switch (self->dataType) { case VL_TYPE_FLOAT: self -> distanceFunction = (void(*)(void)) vl_get_vector_comparison_function_f (VlDistanceL2) ; break ; case VL_TYPE_DOUBLE : self -> distanceFunction = (void(*)(void)) vl_get_vector_comparison_function_d (VlDistanceL2) ; break ; default : abort() ; } return self ; }
VlKDForest * vl_kdforest_new (vl_type dataType, vl_size dimension, vl_size numTrees, VlVectorComparisonType distance) { VlKDForest * self = vl_calloc (sizeof(VlKDForest), 1) ; assert(dataType == VL_TYPE_FLOAT || dataType == VL_TYPE_DOUBLE) ; assert(dimension >= 1) ; assert(numTrees >= 1) ; self -> rand = vl_get_rand () ; self -> dataType = dataType ; self -> numData = 0 ; self -> data = 0 ; self -> dimension = dimension ; self -> numTrees = numTrees ; self -> trees = 0 ; self -> thresholdingMethod = VL_KDTREE_MEDIAN ; self -> splitHeapSize = VL_MIN(numTrees, VL_KDTREE_SPLIT_HEAP_SIZE) ; self -> splitHeapNumNodes = 0 ; self -> distance = distance; self -> maxNumNodes = 0 ; switch (self->dataType) { case VL_TYPE_FLOAT: self -> distanceFunction = (void(*)(void)) vl_get_vector_comparison_function_f (distance) ; break; case VL_TYPE_DOUBLE : self -> distanceFunction = (void(*)(void)) vl_get_vector_comparison_function_d (distance) ; break ; default : abort() ; } return self ; }
/* driver */ void mexFunction(int nout, mxArray *out[], int nin, const mxArray *in[]) { enum { MANIP_STATE, RUN_GENERATOR } mode ; VlRand * rand ; VL_USE_MATLAB_ENV ; rand = vl_get_rand() ; /** ----------------------------------------------------------------- ** Check the arguments ** -------------------------------------------------------------- */ if (nout > 1) { vlmxError(vlmxErrTooManyInputArguments, NULL) ; } if (nin > 0 && ! mxIsNumeric(in[0])) { mode = MANIP_STATE ; } else { mode = RUN_GENERATOR ; } switch (mode) { case RUN_GENERATOR: { enum { maxNumDimensions = 30 } ; vl_size numDimensions = 2, n ; vl_uindex k ; mwSize dimensions [maxNumDimensions] = {1, 1} ; double * x ; if (nin > 1) { /* TWISTER(N1 N2 ...) style */ if (nin >= maxNumDimensions) { vlmxError(vlmxErrTooManyInputArguments, "Too many dimensions specified.") ; } for (k = 0 ; k < (unsigned)nin ; ++k) { if (! vlmxIsPlainScalar(in[k])) { vlmxError(vlmxErrInvalidArgument, "The %d-th argument is not a plain scalar.", k + 1) ; } if (mxGetScalar(in[k]) < 0) { vlmxError(vlmxErrInvalidArgument, "The %d-th argument is negative.", k + 1) ; } dimensions[k] = mxGetScalar(in[k]) ; } numDimensions = k ; } else if (nin == 1) { /* TWISTER([N1 N2 ...]) style */ if (! vlmxIsPlainVector(in[0], -1)) { vlmxError(vlmxErrInvalidArgument, "The argument is not a plain vector.") ; } x = mxGetPr(in[0]) ; n = mxGetNumberOfElements(in[0]) ; numDimensions = VL_MAX(2, n) ; if (numDimensions > maxNumDimensions) { vlmxError(vlmxErrInvalidArgument, "Too many dimensions specified.") ; } if (n == 1) { if (*x < 0) { vlmxError(vlmxErrInvalidArgument, "The specified dimension is negative.") ; } dimensions[0] = dimensions[1] = *x ; } else { for (k = 0 ; k < n ; ++k) { if (x[k] < 0) { vlmxError(vlmxErrInvalidArgument, "One of the specified dimensions is negative.") ; } dimensions[k] = x[k] ; } } } out[0] = mxCreateNumericArray (numDimensions, dimensions, mxDOUBLE_CLASS, mxREAL) ; n = mxGetNumberOfElements (out[0]) ; x = mxGetPr (out[0]) ; for (k = 0 ; k < n ; ++k) { x[k] = vl_rand_res53(rand) ; } } break ; case MANIP_STATE: { enum { buff_size = 32 } ; char buff [buff_size] ; /* check for 'state' string */ if (! vlmxIsString(in[0], -1) || mxGetString(in[0], buff, buff_size) || vl_string_casei_cmp ("state", buff) != 0 ) { vlmxError(vlmxErrInvalidArgument, NULL) ; } /* TWISTER('state') */ if (nin == 1) { vl_uindex i ; vl_uint32 * data ; out[0] = mxCreateNumericMatrix (625, 1, mxUINT32_CLASS, mxREAL) ; data = mxGetData(out[0]) ; for (i = 0 ; i < 624 ; ++i) data[i] = rand->mt[i] ; data[624] = (vl_uint32) rand->mti ; } else { if (vlmxIsPlainScalar(in[1])) { /* TWISTER('state', X) */ vl_uint32 x = (vl_uint32) mxGetScalar(in[1]) ; vl_rand_seed (rand, x) ; } else if (mxIsNumeric(in[1]) && mxGetClassID(in[1]) == mxUINT32_CLASS && mxGetNumberOfElements(in[1]) == 624+1 && ((vl_uint32 const*)mxGetData(in[1]))[624] <= 624 ) { /* TWISTER('state', STATE) */ vl_uindex i ; vl_uint32 * data = mxGetData(in[1]) ; for (i = 0 ; i < 624 ; ++i) rand->mt[i] = data[i] ; rand->mti = data [624] ; } else if (mxIsNumeric(in[1]) && mxGetClassID(in[1]) == mxDOUBLE_CLASS && mxGetNumberOfElements(in[1]) <= 624) { /* TWISTER('state', KEY) */ vl_uint32 key [624] ; double const * x = mxGetPr(in[1]) ; vl_size n = mxGetNumberOfElements(in[1]) ; vl_uindex k ; for (k = 0 ; k < n ; ++k) { key [k] = x [k] ; } vl_rand_seed_by_array (rand, key, n) ; } } } break ; default: abort() ; } }
static void VL_XCAT(_vl_kmeans_seed_centers_plus_plus_, SFX) (VlKMeans * self, TYPE const * data, vl_size dimension, vl_size numData, vl_size numCenters) { vl_uindex x, c ; VlRand * rand = vl_get_rand () ; TYPE * distances = vl_malloc (sizeof(TYPE) * numData) ; TYPE * minDistances = vl_malloc (sizeof(TYPE) * numData) ; #if (FLT == VL_TYPE_FLOAT) VlFloatVectorComparisonFunction distFn = vl_get_vector_comparison_function_f(self->distance) ; #else VlDoubleVectorComparisonFunction distFn = vl_get_vector_comparison_function_d(self->distance) ; #endif self->dimension = dimension ; self->numCenters = numCenters ; self->centers = vl_malloc (sizeof(TYPE) * dimension * numCenters) ; for (x = 0 ; x < numData ; ++x) { minDistances[x] = (TYPE) VL_INFINITY_D ; } /* select the first point at random */ x = vl_rand_uindex (rand, numData) ; c = 0 ; while (1) { TYPE energy = 0 ; TYPE acc = 0 ; TYPE thresh = (TYPE) vl_rand_real1 (rand) ; memcpy ((TYPE*)self->centers + c * dimension, data + x * dimension, sizeof(TYPE) * dimension) ; c ++ ; if (c == numCenters) break ; VL_XCAT(vl_eval_vector_comparison_on_all_pairs_, SFX) (distances, dimension, (TYPE*)self->centers + (c - 1) * dimension, 1, data, numData, distFn) ; for (x = 0 ; x < numData ; ++x) { minDistances[x] = VL_MIN(minDistances[x], distances[x]) ; energy += minDistances[x] ; } for (x = 0 ; x < numData - 1 ; ++x) { acc += minDistances[x] ; if (acc >= thresh * energy) break ; } } vl_free(distances) ; vl_free(minDistances) ; }
VL_EXPORT void VL_XCAT(vl_pegasos_train_binary_svm_,SFX)(T * model, T const * data, vl_size dimension, vl_size numSamples, vl_int8 const * labels, double regularizer, double biasMultiplier, vl_uindex startingIteration, vl_size numIterations, VlRand * randomGenerator) { vl_uindex iteration ; vl_uindex i ; T const * x ; T acc, eta, y, scale = 1 ; double lambda = regularizer ; double sqrtLambda = sqrt(lambda) ; #if (FLT == VL_TYPE_FLOAT) VlFloatVectorComparisonFunction dotFn = #else VlDoubleVectorComparisonFunction dotFn = #endif VL_XCAT(vl_get_vector_comparison_function_,SFX)(VlKernelL2) ; if (randomGenerator == NULL) randomGenerator = vl_get_rand() ; assert(startingIteration >= 1) ; /* The model is stored as scale*model[]. When a sample does not violate the margin, only scale needs to be updated. */ for (iteration = startingIteration ; iteration < startingIteration + numIterations ; ++ iteration) { /* pick a sample */ vl_uindex k = vl_rand_uindex(randomGenerator, numSamples) ; x = data + dimension * k ; y = labels[k] ; /* project on the weight vector */ acc = dotFn(dimension, x, model) ; if (biasMultiplier) acc += biasMultiplier * model[dimension] ; acc *= scale ; /* learning rate */ eta = 1.0 / (iteration * lambda) ; if (y * acc < (T) 1.0) { /* margin violated */ T a = scale * (1 - eta * lambda) ; T b = y * eta ; acc = 0 ; for (i = 0 ; i < dimension ; ++i) { model[i] = a * model[i] + b * x[i] ; acc += model[i] * model[i] ; } if (biasMultiplier) { model[dimension] = a * model[dimension] + b * biasMultiplier ; acc += model[dimension] * model[dimension] ; } scale = VL_MIN((T)1.0 / (sqrtLambda * sqrt(acc + VL_EPSILON_D)), (T)1.0) ; } else { /* margin not violated */ scale *= 1 - eta * lambda ; } } /* denormalize representation */ for (i = 0 ; i < dimension + (biasMultiplier ? 1 : 0) ; ++i) { model[i] *= scale ; } }
* vlfeat * * Created by Andrea Vedaldi on 16/07/2009. * Copyright 2009 UCLA. All rights reserved. * */ #include <vl/random.h> #include <vl/mathop.h> #include <math.h> int main(int argc VL_UNUSED, char**argv VL_UNUSED) { VlRand * rand = vl_get_rand() ; vl_size numData = 100000000 ; vl_size i ; float * X = vl_malloc(sizeof(float) * numData) ; float * Y = vl_malloc(sizeof(float) * numData) ; float * X_end = X + numData ; for (i = 0 ; i < numData ; ++ i) { X[i] = vl_rand_real1(rand) - .5 ; Y[i] = 0 ; } { float * Xi = X ; float * Yi = Y ; vl_tic() ;