From 51db464d45ca9799eb2ee0840b64d7626dc77a59 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sun, 21 Jan 2018 02:17:07 +0200 Subject: [PATCH] Change meanshift func to recursive Change constants to variables Init arguments parsing --- serial.c | 33 +++++----- serialDeclarations.c | 145 ++++++++++++++++++++++++------------------- serialDeclarations.h | 12 ++-- 3 files changed, 106 insertions(+), 84 deletions(-) diff --git a/serial.c b/serial.c index faafbc2..67b263a 100755 --- a/serial.c +++ b/serial.c @@ -4,17 +4,18 @@ #include "serialDeclarations.h" +int NUMBER_OF_POINTS = 600; +int DIMENSIONS = 2; +char* POINTS_FILENAME = "data/X.bin"; +char* LABELS_FILENAME = "data/L.bin"; + struct timeval startwtime, endwtime; double seq_time; int main(int argc, char **argv){ + int h = 1; -// if (argc<2){ -// printf("%s\n", "Specify the k"); -// return 1; -// } -// = atoi(argv[1]); // the k-parameter - + //get_args(argc, argv, &h); commented out while in development FILE *f; // f = fopen(X, "rb"); @@ -22,19 +23,19 @@ int main(int argc, char **argv){ // long int pos = ftell(f); // fclose(f); // int elements = pos / sizeof(double); // number of total elements (points*dimension) -// int points = elements/COLUMNS; +// int points = elements/DIMENSIONS; // //printf("points : %d \n", points); - f = fopen(X, "rb"); - double ** vectors; - vectors = alloc_2d_double(ROWS, COLUMNS); - for (int i=0; i // variables of type uint8 are stored as 1-byte (8-bit) unsigned integers fseek(f, 0L, SEEK_END); @@ -48,7 +49,6 @@ int main(int argc, char **argv){ fclose(f); // MEAN SHIFT OPTIONS - int h = 1; parameters params; params.epsilon = 0.0001; params.verbose = false; @@ -56,10 +56,11 @@ int main(int argc, char **argv){ parameters *opt; opt = ¶ms; + double **shiftedPoints; // tic gettimeofday (&startwtime, NULL); - meanshift(vectors, h, opt); + int iterations = meanshift(vectors, &shiftedPoints, h, opt, 1); // toc gettimeofday (&endwtime, NULL); @@ -67,5 +68,5 @@ int main(int argc, char **argv){ printf("%s wall clock time = %f\n","Mean Shift", seq_time); //TODO write output points to file -> plot later - + //save_matrix(vectors, iterations); } \ No newline at end of file diff --git a/serialDeclarations.c b/serialDeclarations.c index 4947958..7cfe344 100644 --- a/serialDeclarations.c +++ b/serialDeclarations.c @@ -6,76 +6,94 @@ #include "serialDeclarations.h" -void meanshift(double **originalPoints, int h, parameters *opt){ +void get_args(int argc, char **argv, int *h){ + if (argc != 6) { + printf("Usage: %s h N D Pd Pl\nwhere:\n", argv[0]); + printf("\th is the variance\n"); + printf("\tN is the the number of points\n"); + printf("\tD is the number of dimensions of each point\n"); + printf("\tPd is the path of the dataset file\n"); + printf("\tPl is the path of the labels file\n"); + exit(1); + } + + *h = atoi(argv[1]); + NUMBER_OF_POINTS = atoi(argv[2]); + DIMENSIONS = atoi(argv[3]); + POINTS_FILENAME = argv[4]; + LABELS_FILENAME = argv[5]; +} + +int meanshift(double **originalPoints, double ***shiftedPoints, int h + , parameters *opt, int iteration){ - double **y; - y = alloc_2d_double(ROWS, COLUMNS); - y = duplicate(originalPoints, y, ROWS, COLUMNS); + // allocates space and copies original points on first iteration + if (iteration == 1){ + (*shiftedPoints) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); + (*shiftedPoints) = duplicate(originalPoints, (*shiftedPoints) + , NUMBER_OF_POINTS, DIMENSIONS); + } // mean shift vector double **meanShiftVector; - meanShiftVector = alloc_2d_double(ROWS, COLUMNS); + meanShiftVector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); // initialize elements of meanShiftVector to inf - for (int i=0;iepsilon); - - double ** kernelMatrix = alloc_2d_double(ROWS, ROWS); - double *denominator = malloc(ROWS * sizeof(double)); - - /** iterate until convergence **/ - // printf("norm : %f \n", norm(m, ROWS, COLUMNS)); - while (norm(meanShiftVector, ROWS, COLUMNS) > opt->epsilon) { - iter = iter +1; - // find pairwise distance matrix (inside radius) - // [I, D] = rangesearch(x,y,h); - for (int i=0; i opt->epsilon) { + return meanshift(originalPoints, shiftedPoints, h, opt, ++iteration); } + + return iteration; } // TODO check why there's is a difference in the norm calculate in matlab @@ -92,12 +110,13 @@ double norm(double **matrix, int rows, int cols){ } void multiply(double **matrix1, double **matrix2, double **output){ - // W dims are ROWS ROWS and x dims are ROWS COLUMNS + // W dims are NUMBER_OF_POINTS NUMBER_OF_POINTS + // and x dims are NUMBER_OF_POINTS DIMENSIONS - for (int i=0; i -#define X "data/X.bin" -#define L "data/L.bin" -#define COLUMNS 2 -#define ROWS 600 +extern int NUMBER_OF_POINTS; +extern int DIMENSIONS; +extern char* POINTS_FILENAME; +extern char* LABELS_FILENAME; typedef struct parameters { double epsilon; @@ -14,7 +14,9 @@ typedef struct parameters { bool display; } parameters; -void meanshift(double **x, int h, struct parameters *opt); +void get_args(int argc, char **argv, int *h); +int meanshift(double **originalPoints, double ***shiftedPoints, int h + , parameters *opt, int iteration); double norm(double ** m, int rows, int cols); void multiply(double ** matrix1, double ** matrix2, double ** output); double calculateDistance(double *, double *);