From 38fafa672ce41bd0b9f04ae1492a622fa3598f83 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Sun, 24 Sep 2023 21:27:59 +0200 Subject: [PATCH 1/5] minor changes for pgm files --- image.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/image.c b/image.c index 6818cf8..3fcbd1e 100644 --- a/image.c +++ b/image.c @@ -14,12 +14,13 @@ void big_endian_to_c_uint(const char * bytes, void * target, int size) { void read_until_space_or_newline(char * buff, int maxCount, FILE * fptr){ int bufferOffset = 0; - char c = -1; + char c; + int counter = 0; do{ c = (char)getc(fptr); buff[bufferOffset++] = c; - }while(!feof(fptr) && c != 0 && c != ' ' && c !='\n'); + }while(!feof(fptr) && c != 0 && c != ' ' && c !='\n' && counter++ < maxCount); buff[bufferOffset-1] = 0; } @@ -29,8 +30,7 @@ Image * load_pgm_image(char * image_file_string){ image->label = -1; - char buffer[100]; - int magic_number = 0; + char buffer[2048]; fgets(buffer, 4, fptr); if(buffer[0] != 'P' || buffer[1] != '5'){ printf("Wrong file Format"); @@ -40,17 +40,16 @@ Image * load_pgm_image(char * image_file_string){ fgets(buffer, 1024, fptr); } - int image_width, image_height, image_length, image_white ; + int image_width, image_height, image_white ; read_until_space_or_newline(buffer, 10, fptr); - image_width = strtol(buffer, NULL, 10); + image_width = (int)strtol(buffer, NULL, 10); read_until_space_or_newline(buffer, 10, fptr); - image_height = strtol(buffer, NULL, 10); + image_height = (int)strtol(buffer, NULL, 10); read_until_space_or_newline(buffer, 10, fptr); - image_white = strtol(buffer, NULL, 10); + image_white = (int)strtol(buffer, NULL, 10); - image_length = image_width * image_height; image->pixel_values = matrix_create(image_height, image_width); for(int i = 0; i < image_height; i++){ From 1cfdf93f7caf531c2d24231a51731c5c6222d493 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Sun, 24 Sep 2023 22:29:41 +0200 Subject: [PATCH 2/5] (fix) evaluation trained with 50000 images evaluated with 10000 --- main.c | 15 +++++++-------- neuronal_network.c | 3 ++- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index b2f7302..1d17aa5 100644 --- a/main.c +++ b/main.c @@ -8,7 +8,7 @@ #include "util.h" void parsingErrorPrintHelp(){ - printf("Syntax: c_net [train | detect]\n"); + printf("Syntax: c_net [train | predict]\n"); printf("commands:\n"); printf("train\t train the network\n"); printf("predict\t load a pgm image and predict_demo the number\n"); @@ -58,19 +58,18 @@ void train(int argc, char** arguments) { } char *save_path = arguments[6]; int imported = 0; - Image **images = import_images(image_file, label_file, &imported, 50000); + Image ** images = import_images(image_file, label_file, &imported, 60000); + Image ** evaluation_images = images+50000; -// for(int i = 0; i < imported; i++){ -// matrix_save(images[i]->pixel_values, "images.txt"); -// } -// exit(1); + int training_image_count = 50000; + int testing_image_count = 10000; Neural_Network *nn = new_network(28 * 28, neurons_per_layer, hidden_count, 10, learning_rate); randomize_network(nn, 1); printf("training_network\n"); for(int epoch = 1; epoch <= epochs; epoch++){ printf("epoch %d\n", epoch); - for (int i = 0; i < imported; i++) { + for (int i = 0; i < training_image_count; i++) { if (i % 1000 == 0) { updateBar(i * 100 / imported); } @@ -78,7 +77,7 @@ void train(int argc, char** arguments) { } updateBar(100); printf("\n"); - printf("accuracy %lf\n", measure_network_accuracy(nn, images, 10000)); + printf("accuracy %lf\n", measure_network_accuracy(nn, evaluation_images, testing_image_count)); } printf("done training!\n"); save_network(nn, save_path); diff --git a/neuronal_network.c b/neuronal_network.c index 8cf8616..756b71d 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -117,7 +117,8 @@ double measure_network_accuracy(Neural_Network* network, Image** images, int amo int num_correct = 0; printf("evaluating network\n"); - for (int i = 50001; i < amount; i++) { + if(amount > 10000) amount = 10000; + for (int i = 0; i < amount; i++) { updateBar(i*100/amount); Matrix* prediction = predict_image(network, images[i]); From 754d170616a8e55b1be531a41e98909d2865ccf9 Mon Sep 17 00:00:00 2001 From: jastornig Date: Sun, 24 Sep 2023 20:36:11 +0000 Subject: [PATCH 3/5] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0af1ae5..39e9622 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -48,7 +48,7 @@ release: assets: links: - - name: c_net + - name: c_net linux download (precompiled) url: '${CI_PROJECT_URL}/-/jobs/${BUILD_JOB_ID}/artifacts/file/c_net' script: echo "Define your deployment script!" From 746f4cf3bd716b5ded967b3a29c3d4e246f76009 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Sun, 24 Sep 2023 22:36:40 +0200 Subject: [PATCH 4/5] deleted unused functions --- matrix.c | 25 ------------------------- matrix.h | 2 -- 2 files changed, 27 deletions(-) diff --git a/matrix.c b/matrix.c index ba7c258..2a9acfc 100644 --- a/matrix.c +++ b/matrix.c @@ -216,21 +216,6 @@ Matrix* scale(Matrix* matrix, double value) { return result_matrix; } -Matrix* addScalar(Matrix* matrix, double value) { - - // create a copy of the original matrix - Matrix* result_matrix = matrix_copy(matrix); - - // iterate over all numbers in the matrix and add the scalar value - for (int i = 0; i < result_matrix->rows; i++) { - for (int j = 0; j < result_matrix->columns; j++) { - result_matrix->numbers[i][j] += value; - } - } - - // return the copy - return result_matrix; -} Matrix* transpose(Matrix* matrix) { @@ -249,16 +234,6 @@ Matrix* transpose(Matrix* matrix) { } -double matrix_sum(Matrix* matrix) { - double sum = 0; - for (int i = 0; i < matrix->rows; i++) { - for (int j = 0; j < matrix->columns; j++) { - sum += matrix->numbers[i][j]; - } - } - return sum; -} - void matrix_save(Matrix* matrix, char* file_string){ // open the file in append mode diff --git a/matrix.h b/matrix.h index 2e0e5a9..4ece217 100644 --- a/matrix.h +++ b/matrix.h @@ -36,6 +36,4 @@ Matrix* subtract(Matrix* matrix1, Matrix* matrix2); Matrix* dot(Matrix* matrix1, Matrix* matrix2); Matrix* apply(double (*function)(double), Matrix* matrix); Matrix* scale(Matrix* matrix, double value); -Matrix* addScalar(Matrix* matrix, double value); Matrix* transpose(Matrix* matrix); -double matrix_sum(Matrix* matrix); \ No newline at end of file From aa58f99045c7a34fd7ee0fe7c25037280f481fc6 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Mon, 25 Sep 2023 11:11:07 +0200 Subject: [PATCH 5/5] minor changes --- image.c | 4 ++++ main.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/image.c b/image.c index 3fcbd1e..f52fa2c 100644 --- a/image.c +++ b/image.c @@ -26,6 +26,10 @@ void read_until_space_or_newline(char * buff, int maxCount, FILE * fptr){ Image * load_pgm_image(char * image_file_string){ FILE * fptr = fopen(image_file_string, "r"); + if(!fptr){ + printf("could not open image file. exit\n"); + exit(1); + } Image *image = malloc(sizeof(Image)); image->label = -1; diff --git a/main.c b/main.c index 1d17aa5..f4c966e 100644 --- a/main.c +++ b/main.c @@ -23,7 +23,7 @@ void parsingErrorTrain(){ void parsingErrorDetect(){ printf("invalid syntax\n"); - printf("Syntax: c_net predict_demo [path_to_network] [image_file]"); + printf("Syntax: c_net predict_demo [path_to_network] [image_file]\n"); } void predict_demo(int argc, char** arguments){