diff --git a/CMakeLists.txt b/CMakeLists.txt index 113e5e8..461b2a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(c_net C) set(CMAKE_C_STANDARD 11) -add_executable(c_net main.c matrix.c image.c neuronal_network.c) +add_executable(c_net main.c matrix.c image.c neuronal_network.c util.c util.h) target_link_libraries(c_net m) diff --git a/image.c b/image.c index 2ff1f67..c35626f 100644 --- a/image.c +++ b/image.c @@ -3,6 +3,7 @@ #include "image.h" #include "matrix.h" +#include "util.h" void big_endian_to_c_uint(const char * bytes, void * target, int size) { char* helper = (char*)target; @@ -12,8 +13,8 @@ void big_endian_to_c_uint(const char * bytes, void * target, int size) { } -Image** import_images(char* image_file_string, char* label_file_string, unsigned int* _number_imported, unsigned int count) { - +Image** import_images(char* image_file_string, char* label_file_string, int* _number_imported, int count) { + printf("Loading Images\n"); // create file pointer for the image and label data FILE* image_file = fopen(image_file_string, "r"); FILE* label_file = fopen(label_file_string, "r"); @@ -28,7 +29,7 @@ Image** import_images(char* image_file_string, char* label_file_string, unsigned char word_buffer[4]; int buffer_size = sizeof(word_buffer); - unsigned int magic_number_label, magic_number_images, label_count, image_count; + int magic_number_label, magic_number_images, label_count, image_count; //Read description of label file fread(word_buffer, buffer_size, 1, label_file); @@ -55,7 +56,7 @@ Image** import_images(char* image_file_string, char* label_file_string, unsigned exit(1); } - if(count == 0){ + if(count <= 0){ count = image_count; } @@ -83,6 +84,9 @@ Image** import_images(char* image_file_string, char* label_file_string, unsigned unsigned char byteBuffer[image_length]; for(int i = 0; i < count; i++){ + if(i%1000 == 0){ + updateBar(i*100/count); + } images[i] = malloc(sizeof(Image)); fread(&images[i]->label, 1, 1, label_file); fread(&byteBuffer, image_width*image_height, 1, image_file); @@ -97,6 +101,9 @@ Image** import_images(char* image_file_string, char* label_file_string, unsigned fclose(image_file); fclose(label_file); + + updateBar(100); + printf("\n"); return images; } @@ -115,7 +122,7 @@ void img_visualize(Image* img){ } putc('\n', stdout); } - printf("Should be %d", img->label); + printf("Should be %d\n", img->label); } void img_free (Image* img) { diff --git a/image.h b/image.h index 969251a..5f146df 100644 --- a/image.h +++ b/image.h @@ -25,7 +25,7 @@ static const int MAGIC_NUMBER_IMAGES = 2051; * @param count maximum number of images to be loaded. If it is 0, all available images are loaded. * @return */ -Image ** import_images(char* image_file_string, char* label_file_string, unsigned int* number_imported, unsigned int count); +Image ** import_images(char* image_file_string, char* label_file_string, int* number_imported, int count); void img_print (Image* image); void img_visualize(Image*image); void img_free (Image* image); \ No newline at end of file diff --git a/neuronal_network.c b/neuronal_network.c index e3a939e..dce7544 100644 --- a/neuronal_network.c +++ b/neuronal_network.c @@ -126,7 +126,7 @@ Neural_Network* load_network(char* file) { return saved_network; } -double predict_images(Neural_Network* network, Image** images, int amount) { +double measure_network_accuracy(Neural_Network* network, Image** images, int amount) { int num_correct = 0; for (int i = 0; i < amount; i++) { Matrix* prediction = predict_image(network, images[i]); diff --git a/neuronal_network.h b/neuronal_network.h index 9c8791d..30e7c89 100644 --- a/neuronal_network.h +++ b/neuronal_network.h @@ -35,7 +35,7 @@ void free_network(Neural_Network* network); void save_network(Neural_Network* network); Neural_Network* load_network(char* file); -double predict_images(Neural_Network* network, Image** images, int amount); +double measure_network_accuracy(Neural_Network* network, Image** images, int amount); Matrix* predict_image(Neural_Network* network, Image* image); Matrix* predict(Neural_Network* network, Matrix* image_data); diff --git a/util.c b/util.c new file mode 100644 index 0000000..e55bd3a --- /dev/null +++ b/util.c @@ -0,0 +1,20 @@ +// +// Created by jakob on 21.09.2023. +// + +#include "util.h" +#include +void updateBar(int percent_done){ + const int BAR_LENGTH = 30; + int numChar = percent_done * BAR_LENGTH / 100; + + printf("\r["); + for(int i = 0; i < numChar; i++){ + printf("#"); + } + for(int i = 0; i < BAR_LENGTH - numChar; i++){ + printf("."); + } + printf("] (%d%%)", percent_done); + fflush(stdout); +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..a24bfe7 --- /dev/null +++ b/util.h @@ -0,0 +1,9 @@ +// +// Created by jakob on 21.09.2023. +// + +#ifndef C_NET_UTIL_H +#define C_NET_UTIL_H + +#endif //C_NET_UTIL_H +void updateBar(int percentDone);