diff options
author | Eric Dao <eric@erickhangdao.com> | 2023-05-12 14:09:07 -0400 |
---|---|---|
committer | Eric Dao <eric@erickhangdao.com> | 2023-05-12 14:09:07 -0400 |
commit | 191e4bd8beae134295d481773823142d2fdc6a98 (patch) | |
tree | aaca4e7f8e229f9bd551d9bb1583b8ecd7a59d4a /OASIS/C/src/main.c | |
parent | 186daed7e179241377c117e9d208ccd301d4d712 (diff) | |
download | ura-master.tar.gz ura-master.tar.bz2 ura-master.zip |
Diffstat (limited to 'OASIS/C/src/main.c')
-rw-r--r-- | OASIS/C/src/main.c | 108 |
1 files changed, 0 insertions, 108 deletions
diff --git a/OASIS/C/src/main.c b/OASIS/C/src/main.c deleted file mode 100644 index b84e29f..0000000 --- a/OASIS/C/src/main.c +++ /dev/null @@ -1,108 +0,0 @@ -#include "main.h"
-
-uint64_t seconds() {
- struct timespec start;
- clock_gettime(CLOCK_MONOTONIC_RAW, &start);
- return start.tv_sec;
-}
-
-int main() {
- uint64_t start = seconds();
- // Allocate memory
- char** file_names = (char**) malloc((NUM_IMAGES) * sizeof(char*));
- for (uint8_t i=0; i<NUM_IMAGES; i++) {
- file_names[i] = (char*) malloc(FILE_NAME_SIZE * sizeof(char));
- // Populate file name array with file path
- file_names[i] = (i < NUM_DNEG_IMAGES) ? strcpy(file_names[i], DNEG_FOLDER_PATH) : strcpy(file_names[i], DPOS_FOLDER_PATH);
- }
- uint8_t*** data = (uint8_t***) malloc((NUM_IMAGES) * sizeof(uint8_t**));
- for (uint8_t i=0; i<NUM_IMAGES; i++) {
- data[i] = (uint8_t**) malloc(IMAGE_HEIGHT * sizeof(uint8_t*));
- for (uint8_t j=0; j<IMAGE_HEIGHT; j++) data[i][j] = (uint8_t*)malloc(IMAGE_WIDTH * sizeof(uint8_t));
- }
-
- float** data_flatten = (float**) malloc((NUM_IMAGES) * sizeof(float*));
- for (uint8_t i=0; i<NUM_IMAGES; i++) data_flatten[i] = (float*) malloc(FEATURE_VECTOR_SIZE * sizeof(float));
-
- int32_t* labels = (int32_t*) malloc((NUM_IMAGES) * sizeof(int32_t));
-
- uint8_t num_train = (uint8_t) (0.8 * NUM_IMAGES);
- uint8_t num_test = NUM_IMAGES - num_train;
-
- float** data_train = (float**) malloc (num_train * sizeof(float*));
- float** data_test = (float**) malloc (num_test * sizeof(float*));
-
- int32_t* labels_train = (int32_t*) malloc(num_train * sizeof(int32_t));
- int32_t* labels_test = (int32_t*) malloc(num_test * sizeof(int32_t));
-
- float** data_reduced_dimensions = (float**) malloc((NUM_IMAGES) * sizeof(float*));
- for (uint32_t i=0; i<NUM_IMAGES; i++) data_reduced_dimensions[i] = (float*) malloc(NUM_COMPONENTS * sizeof(float));
-
- float* alphas = (float*) malloc((NUM_IMAGES) * sizeof(float));
-
- // Concatenate the file name array with the image name
- read_dir(DNEG_FOLDER_PATH, DPOS_FOLDER_PATH, file_names);
- printf("read\n");
-
- // Read in an array of 2D images, then flatten
- for (uint8_t i=0; i<NUM_IMAGES; i++) {
- labels[i] = (i < NUM_DNEG_IMAGES) ? -1 : 1;
- read_bmp(file_names[i], data[i]);
- flatten_image(data[i], data_flatten[i]);
- }
-
- printf("flattened\n");
-
- // PCA Transform
- pca_covariance_method(data_flatten, NUM_IMAGES, FEATURE_VECTOR_SIZE, data_reduced_dimensions, NUM_COMPONENTS);
- printf("pca\n");
-
- // Shuffle data and labels
- shuffle_data_labels(data_reduced_dimensions, labels, NUM_IMAGES);
- printf("shuffled\n");
-
- // Populate the training and testing data
- for (uint8_t i=0; i<num_train; i++) {
- data_train[i] = data_reduced_dimensions[i];
- labels_train[i] = labels[i];
- }
-
- for (uint8_t i=num_train; i<NUM_IMAGES; i++) {
- data_test[i - num_train] = data_reduced_dimensions[i];
- labels_test[i - num_train] = labels[i];
- }
-
- printf("split\n");
-
- // Train SVM
- float bias;
- train_svm(data_train, num_train, NUM_COMPONENTS, alphas, &bias, labels, PENALTY_PARAM);
- printf("trained\n");
-
- // Test SVM
- float accuracy = test_svm(data_test, labels_test, num_test, NUM_COMPONENTS, alphas, bias);
- printf("Accuracy: %.2f%%\n", accuracy);
-
- // Free memory
- for (uint8_t i=0; i<NUM_IMAGES; i++) {
- free(file_names[i]);
- free(data_flatten[i]);
- free(data_reduced_dimensions[i]);
- for (uint8_t j=0; j<IMAGE_HEIGHT; j++) free(data[i][j]);
- free(data[i]);
- }
- free(file_names);
- free(data);
- free(data_flatten);
- free(data_reduced_dimensions);
- free(labels);
- free(labels_train);
- free(labels_test);
- free(data_train);
- free(data_test);
- free(alphas);
-
- uint64_t end = seconds();
- printf("Time Elapsed: %lds\n", end-start);
- return 0;
-}
\ No newline at end of file |