progress bar and rename function measure accuracy

This commit is contained in:
Jakob Stornig 2023-09-21 11:22:31 +02:00
parent f10814c56c
commit e0c46fc46a
7 changed files with 45 additions and 9 deletions

View file

@ -3,5 +3,5 @@ project(c_net C)
set(CMAKE_C_STANDARD 11) 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) target_link_libraries(c_net m)

17
image.c
View file

@ -3,6 +3,7 @@
#include "image.h" #include "image.h"
#include "matrix.h" #include "matrix.h"
#include "util.h"
void big_endian_to_c_uint(const char * bytes, void * target, int size) { void big_endian_to_c_uint(const char * bytes, void * target, int size) {
char* helper = (char*)target; 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 // create file pointer for the image and label data
FILE* image_file = fopen(image_file_string, "r"); FILE* image_file = fopen(image_file_string, "r");
FILE* label_file = fopen(label_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]; char word_buffer[4];
int buffer_size = sizeof(word_buffer); 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 //Read description of label file
fread(word_buffer, buffer_size, 1, 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); exit(1);
} }
if(count == 0){ if(count <= 0){
count = image_count; count = image_count;
} }
@ -83,6 +84,9 @@ Image** import_images(char* image_file_string, char* label_file_string, unsigned
unsigned char byteBuffer[image_length]; unsigned char byteBuffer[image_length];
for(int i = 0; i < count; i++){ for(int i = 0; i < count; i++){
if(i%1000 == 0){
updateBar(i*100/count);
}
images[i] = malloc(sizeof(Image)); images[i] = malloc(sizeof(Image));
fread(&images[i]->label, 1, 1, label_file); fread(&images[i]->label, 1, 1, label_file);
fread(&byteBuffer, image_width*image_height, 1, image_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(image_file);
fclose(label_file); fclose(label_file);
updateBar(100);
printf("\n");
return images; return images;
} }
@ -115,7 +122,7 @@ void img_visualize(Image* img){
} }
putc('\n', stdout); putc('\n', stdout);
} }
printf("Should be %d", img->label); printf("Should be %d\n", img->label);
} }
void img_free (Image* img) { void img_free (Image* img) {

View file

@ -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. * @param count maximum number of images to be loaded. If it is 0, all available images are loaded.
* @return * @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_print (Image* image);
void img_visualize(Image*image); void img_visualize(Image*image);
void img_free (Image* image); void img_free (Image* image);

View file

@ -126,7 +126,7 @@ Neural_Network* load_network(char* file) {
return saved_network; 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; int num_correct = 0;
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
Matrix* prediction = predict_image(network, images[i]); Matrix* prediction = predict_image(network, images[i]);

View file

@ -35,7 +35,7 @@ void free_network(Neural_Network* network);
void save_network(Neural_Network* network); void save_network(Neural_Network* network);
Neural_Network* load_network(char* file); 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_image(Neural_Network* network, Image* image);
Matrix* predict(Neural_Network* network, Matrix* image_data); Matrix* predict(Neural_Network* network, Matrix* image_data);

20
util.c Normal file
View file

@ -0,0 +1,20 @@
//
// Created by jakob on 21.09.2023.
//
#include "util.h"
#include <stdio.h>
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);
}

9
util.h Normal file
View file

@ -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);