Browse Source

functions elimination

master
anapt 7 years ago
parent
commit
353e6975a7
  1. 326
      mean-shift.c

326
mean-shift.c

@ -1,61 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "serial_declarations.h" #include <sys/time.h>
#include <stdbool.h> #include <stdbool.h>
extern int NUMBER_OF_POINTS; #include <stdio.h>
extern int DIMENSIONS; #include <stdlib.h>
extern char* POINTS_FILENAME; #include <sys/time.h>
extern char* LABELS_FILENAME;
typedef struct parameters {
double epsilon;
bool verbose;
bool display;
} parameters;
//Function get_args parses command line arguments.
void get_args(int argc, char **argv, int *h);
//Function meanshift recursively shifts original points according to th
//mean-shift algorithm saving the result to shiftedPoints. Struct opt has user
//options, h is the desirable deviation, iteration is this call's iteration
//number.
int meanshift(double **original_points, double ***shifted_points, int h
, parameters *opt, int iteration);
//Function norm returns the second norm of matrix of dimensions rowsXcols.
double norm(double **matrix, int rows, int cols);
//Function multiply calculates the product of matrices 1 and 2 into output.
void multiply(double **matrix1, double **matrix2, double **output);
//Function calculateDistance returns the distance between x and y vectors.
double calculateDistance(double *y, double *x);
//Function alloc_2d_double allocates rows*cols bytes of continuous memory.
double **alloc_2d_double(int rows, int cols);
//Function duplicate copies the values of source array to dest array.
void duplicate(double **source, int rows, int cols, double ***dest);
//Function print_matrix prints array of dimensions rowsXcols to the console.
void print_matrix(double **array, int rows, int cols);
//Function save_matrix prints matrix in a csv file with path/filename
//"output/output_iteration". If a file already exists new lines are concatenated.
void save_matrix(double **matrix
, int iteration);
int NUMBER_OF_POINTS = 600; int NUMBER_OF_POINTS = 600;
int DIMENSIONS = 2; int DIMENSIONS = 2;
@ -65,64 +22,11 @@ char* LABELS_FILENAME = "data/L.bin";
struct timeval startwtime, endwtime; struct timeval startwtime, endwtime;
double seq_time; double seq_time;
int main(int argc, char **argv){ typedef struct parameters {
int h = 1; double epsilon;
bool verbose;
//get_args(argc, argv, &h); commented out while in development bool display;
} parameters;
FILE *f;
// f = fopen(X, "rb");
// fseek(f, 0L, SEEK_END);
// long int pos = ftell(f);
// fclose(f);
// int elements = pos / sizeof(double); // number of total elements (points*dimension)
// int points = elements/DIMENSIONS;
// //printf("points : %d \n", points);
f = fopen(POINTS_FILENAME, "rb");
double **vectors;
vectors = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS);
for (int i=0; i<NUMBER_OF_POINTS; i++){
int out = fread(vectors[i], sizeof(double), DIMENSIONS, f);
}
save_matrix(vectors, 0);
// initializing file that will contain the labels (train)
f = fopen(LABELS_FILENAME, "rb");
// NOTE : Labels were classified as <class 'numpy.uint8'>
// variables of type uint8 are stored as 1-byte (8-bit) unsigned integers
fseek(f, 0L, SEEK_END);
long int pos = ftell(f);
rewind(f);
//printf("position : %ld \n", pos);
int label_elements = pos/ sizeof(char);
char *labels = (char*)malloc(label_elements* sizeof(char));
fseek(f, 0L, SEEK_SET);
int out = fread(labels, sizeof(char), label_elements, f);
fclose(f);
// MEAN SHIFT OPTIONS
parameters params;
params.epsilon = 0.0001;
params.verbose = false;
params.display = false;
parameters *opt;
opt = &params;
double **shifted_points;
// tic
gettimeofday (&startwtime, NULL);
int iterations = meanshift(vectors, &shifted_points, h, opt, 1);
// toc
gettimeofday (&endwtime, NULL);
seq_time = (double)((endwtime.tv_usec - startwtime.tv_usec)/1.0e6 + endwtime.tv_sec - startwtime.tv_sec);
printf("%s wall clock time = %f\n","Mean Shift", seq_time);
//TODO write output points to file -> plot later
//save_matrix(shifted_points, iterations);
}
void get_args(int argc, char **argv, int *h){ void get_args(int argc, char **argv, int *h){
if (argc != 6) { if (argc != 6) {
@ -142,6 +46,84 @@ void get_args(int argc, char **argv, int *h){
LABELS_FILENAME = argv[5]; LABELS_FILENAME = argv[5];
} }
// TODO check why there's is a difference in the norm calculate in matlab
double norm(double **matrix, int rows, int cols){
double sum=0, temp_mul=0;
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
temp_mul = matrix[i][j] * matrix[i][j];
sum = sum + temp_mul;
}
}
double norm = sqrt(sum);
return norm;
}
void multiply(double **matrix1, double **matrix2, double **output){
// W dims are NUMBER_OF_POINTS NUMBER_OF_POINTS
// and x dims are NUMBER_OF_POINTS DIMENSIONS
for (int i=0; i<NUMBER_OF_POINTS; i++){
for (int j=0; j<DIMENSIONS; j++){
output[i][j] = 0;
for (int k=0; k<NUMBER_OF_POINTS; k++){
output[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
double calculateDistance(double *y, double *x){
double sum = 0, dif;
for (int i=0; i<DIMENSIONS; i++){
dif = y[i]-x[i];
sum += dif * dif;
}
double distance = sqrt(sum);
return distance;
}
double **alloc_2d_double(int rows, int cols) {
double *data = (double *) malloc(rows*cols*sizeof(double));
double **array = (double **) malloc(rows*sizeof(double*));
for (int i=0; i<rows; i++)
array[i] = &(data[cols*i]);
return array;
}
void duplicate(double **source, int rows, int cols, double ***dest){
for (int i=0; i<rows; i++){
for (int j=0; j<cols; j++){
(*dest)[i][j] = source[i][j];
}
}
}
void print_matrix(double **array, int rows, int cols){
for (int i=0; i<cols; i++){
for (int j=0; j<rows; j++){
printf("%f ", array[j][i]);
}
printf("\n");
}
}
void save_matrix(double **matrix, int iteration){
char filename[18];
snprintf(filename, sizeof(filename), "%s%d", "output/output_", iteration);
FILE *file;
file = fopen(filename, "w");
for (int rows=0; rows<NUMBER_OF_POINTS; ++rows){
for (int cols=0; cols<DIMENSIONS; ++cols){
fprintf(file, "%f", matrix[rows][cols]);
if (cols != DIMENSIONS - 1){
fprintf(file, ",");
}
}
fprintf(file, "\n");
}
}
int meanshift(double **original_points, double ***shifted_points, int h int meanshift(double **original_points, double ***shifted_points, int h
, parameters *opt, int iteration){ , parameters *opt, int iteration){
@ -169,8 +151,16 @@ int meanshift(double **original_points, double ***shifted_points, int h
for (int i=0; i<NUMBER_OF_POINTS; i++){ for (int i=0; i<NUMBER_OF_POINTS; i++){
double sum = 0; double sum = 0;
for (int j=0; j<NUMBER_OF_POINTS; j++){ for (int j=0; j<NUMBER_OF_POINTS; j++){
double dist = calculateDistance((*shifted_points)[i] // double dist = calculateDistance((*shifted_points)[i]
, original_points[j]); // , original_points[j]);
double dif;
double sum=0;
for (int k=0; k<DIMENSIONS; k++){
printf("%f, %f \n",(*shifted_points)[i][k], original_points[i][k]);
dif = (*shifted_points)[i][k]-original_points[i][k];
sum += dif * dif;
}
double dist = sqrt(sum);
if (i == j){ if (i == j){
kernel_matrix[i][j] = 1; kernel_matrix[i][j] = 1;
@ -225,81 +215,63 @@ int meanshift(double **original_points, double ***shifted_points, int h
return iteration; return iteration;
} }
// TODO check why there's is a difference in the norm calculate in matlab
double norm(double **matrix, int rows, int cols){
double sum=0, temp_mul=0;
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
temp_mul = matrix[i][j] * matrix[i][j];
sum = sum + temp_mul;
}
}
double norm = sqrt(sum);
return norm;
}
void multiply(double **matrix1, double **matrix2, double **output){
// W dims are NUMBER_OF_POINTS NUMBER_OF_POINTS
// and x dims are NUMBER_OF_POINTS DIMENSIONS
int main(int argc, char **argv){
int h = 1;
//get_args(argc, argv, &h); commented out while in development
FILE *f;
// f = fopen(X, "rb");
// fseek(f, 0L, SEEK_END);
// long int pos = ftell(f);
// fclose(f);
// int elements = pos / sizeof(double); // number of total elements (points*dimension)
// int points = elements/DIMENSIONS;
// //printf("points : %d \n", points);
f = fopen(POINTS_FILENAME, "rb");
double **vectors;
vectors = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS);
for (int i=0; i<NUMBER_OF_POINTS; i++){ for (int i=0; i<NUMBER_OF_POINTS; i++){
for (int j=0; j<DIMENSIONS; j++){ int out = fread(vectors[i], sizeof(double), DIMENSIONS, f);
output[i][j] = 0;
for (int k=0; k<NUMBER_OF_POINTS; k++){
output[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
} }
}
double calculateDistance(double *y, double *x){ save_matrix(vectors, 0);
double sum = 0, dif;
for (int i=0; i<DIMENSIONS; i++){
dif = y[i]-x[i];
sum += dif * dif;
}
double distance = sqrt(sum);
return distance;
}
double **alloc_2d_double(int rows, int cols) { // initializing file that will contain the labels (train)
double *data = (double *) malloc(rows*cols*sizeof(double)); f = fopen(LABELS_FILENAME, "rb");
double **array = (double **) malloc(rows*sizeof(double*)); // NOTE : Labels were classified as <class 'numpy.uint8'>
for (int i=0; i<rows; i++) // variables of type uint8 are stored as 1-byte (8-bit) unsigned integers
array[i] = &(data[cols*i]); fseek(f, 0L, SEEK_END);
return array; long int pos = ftell(f);
} rewind(f);
//printf("position : %ld \n", pos);
int label_elements = pos/ sizeof(char);
char *labels = (char*)malloc(label_elements* sizeof(char));
fseek(f, 0L, SEEK_SET);
int out = fread(labels, sizeof(char), label_elements, f);
fclose(f);
void duplicate(double **source, int rows, int cols, double ***dest){ // MEAN SHIFT OPTIONS
for (int i=0; i<rows; i++){ parameters params;
for (int j=0; j<cols; j++){ params.epsilon = 0.0001;
(*dest)[i][j] = source[i][j]; params.verbose = false;
} params.display = false;
} parameters *opt;
} opt = &params;
void print_matrix(double **array, int rows, int cols){ double **shifted_points;
for (int i=0; i<cols; i++){ // tic
for (int j=0; j<rows; j++){ gettimeofday (&startwtime, NULL);
printf("%f ", array[j][i]);
}
printf("\n");
}
}
void save_matrix(double **matrix, int iteration){ int iterations = meanshift(vectors, &shifted_points, h, opt, 1);
char filename[18];
snprintf(filename, sizeof(filename), "%s%d", "output/output_", iteration); // toc
FILE *file; gettimeofday (&endwtime, NULL);
file = fopen(filename, "w"); seq_time = (double)((endwtime.tv_usec - startwtime.tv_usec)/1.0e6 + endwtime.tv_sec - startwtime.tv_sec);
for (int rows=0; rows<NUMBER_OF_POINTS; ++rows){ printf("%s wall clock time = %f\n","Mean Shift", seq_time);
for (int cols=0; cols<DIMENSIONS; ++cols){
fprintf(file, "%f", matrix[rows][cols]);
if (cols != DIMENSIONS - 1){
fprintf(file, ",");
}
}
fprintf(file, "\n");
}
}
//TODO write output points to file -> plot later
//save_matrix(shifted_points, iterations);
}
Loading…
Cancel
Save