From f98bb5cbaac82dcffdedaa0e066bfd2198711d34 Mon Sep 17 00:00:00 2001 From: Thomas Schleicher Date: Tue, 19 Sep 2023 11:11:44 +0200 Subject: [PATCH 1/3] Changes --- CMakeLists.txt | 2 +- image.c | 20 ++++++++++++++++++-- image.h | 6 +++--- main.c | 5 ++++- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5038efd..f1b596c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(c_net C) set(CMAKE_C_STANDARD 11) -add_executable(c_net main.c matrix.c) +add_executable(c_net main.c matrix.c image.c) diff --git a/image.c b/image.c index ccb485b..e0af441 100644 --- a/image.c +++ b/image.c @@ -1,10 +1,12 @@ #pragma once #include +#include + #include "image.h" #include "matrix.h" -Img** import_images(char* image_file_string, char* label_file_string, int number_of_images) { +Image** import_images(char* image_file_string, char* label_file_string, int number_of_images) { // create file pointer for the image and label data FILE* image_file = fopen(image_file_string, 'r'); @@ -12,9 +14,23 @@ Img** import_images(char* image_file_string, char* label_file_string, int number // check if the file could be opened if(image_file == NULL || label_file == NULL) { - printf("ERROR: File could not be opened! ()"); + printf("ERROR: File could not be opened! (import_images)"); } + do { + ch = fgetc(label_file); + printf("%c", ch); + + // Checking if character is not EOF. + // If it is EOF stop reading. + } while (ch != EOF); + + + // allocate memory for the storage of images +// Image** images = malloc(sizeof(Image) * number_of_images); + + fclose(image_file); + fclose(label_file); } void img_print (Img* img) { diff --git a/image.h b/image.h index ddd20d5..9bbf257 100644 --- a/image.h +++ b/image.h @@ -5,6 +5,6 @@ typedef struct { int image_label; } Image; -Img** import_images(char* image_file_string, char* label_file_string, int number_of_images); -void img_print (Img* img); -void img_free (Img* img); \ No newline at end of file +Image** import_images(char* image_file_string, char* label_file_string, int number_of_images); +void img_print (Image* image); +void img_free (Image* image); \ No newline at end of file diff --git a/main.c b/main.c index 5fdc7bf..d21096e 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,10 @@ #include +#include + #include "matrix.h" -#include +#include "image.h" int main() { + Image** images = import_images("train-images.idx3-ubyte", "train-labels.idx3-ubyte", 20); } \ No newline at end of file From 52d97f3c54ba937a79bab3b1b14404aed3e9fb72 Mon Sep 17 00:00:00 2001 From: Jakob Stornig Date: Tue, 19 Sep 2023 11:19:56 +0200 Subject: [PATCH 2/3] debug build --- image.c | 8 +++----- image.h | 1 + matrix.c | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/image.c b/image.c index e0af441..a35f1b5 100644 --- a/image.c +++ b/image.c @@ -1,5 +1,3 @@ -#pragma once - #include #include @@ -16,7 +14,7 @@ Image** import_images(char* image_file_string, char* label_file_string, int numb if(image_file == NULL || label_file == NULL) { printf("ERROR: File could not be opened! (import_images)"); } - + char ch; do { ch = fgetc(label_file); printf("%c", ch); @@ -33,7 +31,7 @@ Image** import_images(char* image_file_string, char* label_file_string, int numb fclose(label_file); } -void img_print (Img* img) { +void img_print (Image* img) { //print the image matrix_print(img->pixel_values); @@ -42,6 +40,6 @@ void img_print (Img* img) { printf("%d", img->image_label); } -void img_free (Img* img) { +void img_free (Image* img) { } diff --git a/image.h b/image.h index 9bbf257..ee252c7 100644 --- a/image.h +++ b/image.h @@ -1,4 +1,5 @@ #pragma once +#include "matrix.h" typedef struct { Matrix* pixel_values; diff --git a/matrix.c b/matrix.c index 7954e59..b229631 100644 --- a/matrix.c +++ b/matrix.c @@ -265,5 +265,5 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) { else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j]; } } - return mat; + return result_matrix; } From 2c768550f3461363783589b2ef3c1f274acc61f1 Mon Sep 17 00:00:00 2001 From: Thomas Schleicher Date: Tue, 19 Sep 2023 13:04:34 +0200 Subject: [PATCH 3/3] Ready to rumble --- image.c | 46 +++++++++++++++++++++++++++++++++++----------- image.h | 2 ++ main.c | 2 +- matrix.c | 2 +- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/image.c b/image.c index e0af441..42c32ba 100644 --- a/image.c +++ b/image.c @@ -6,34 +6,58 @@ #include "image.h" #include "matrix.h" +int endian_swap(int input) { + return ((input >> 24) & 0xff) | // move byte 3 to byte 0 + ((input << 8) & 0xff0000) | // move byte 1 to byte 2 + ((input >> 8) & 0xff00) | // move byte 2 to byte 1 + ((input << 24) & 0xff000000); // byte 0 to byte 3 +} + +int validate_files(FILE* image_file, FILE* label_file) { + + // read magic number from files + int magic_number_label, magic_number_images; + fread(&magic_number_label, 4, 1, label_file); + fread(&magic_number_images, 4, 1, image_file); + + // compare magic numbers with pre-defined value + if(endian_swap(magic_number_label) != 2049 || endian_swap(magic_number_images) != 2051) { + return 0; + } + + return 1; +} + Image** import_images(char* image_file_string, char* label_file_string, int number_of_images) { // 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'); + FILE* image_file = fopen(image_file_string, "r"); + FILE* label_file = fopen(label_file_string, "r"); // check if the file could be opened if(image_file == NULL || label_file == NULL) { printf("ERROR: File could not be opened! (import_images)"); + exit(1); } - do { - ch = fgetc(label_file); - printf("%c", ch); + // check magic number of the files + if(validate_files(image_file, label_file)) { + printf("ERROR: File validation failed! (validate_files)"); + exit(1); + } - // Checking if character is not EOF. - // If it is EOF stop reading. - } while (ch != EOF); + + // Jakob Section // allocate memory for the storage of images -// Image** images = malloc(sizeof(Image) * number_of_images); + Image** images = malloc(sizeof(Image) * number_of_images); fclose(image_file); fclose(label_file); } -void img_print (Img* img) { +void img_print (Image* img) { //print the image matrix_print(img->pixel_values); @@ -42,6 +66,6 @@ void img_print (Img* img) { printf("%d", img->image_label); } -void img_free (Img* img) { +void img_free (Image* img) { } diff --git a/image.h b/image.h index 9bbf257..b3f0577 100644 --- a/image.h +++ b/image.h @@ -1,5 +1,7 @@ #pragma once +#include "matrix.h" + typedef struct { Matrix* pixel_values; int image_label; diff --git a/main.c b/main.c index d21096e..82750fe 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,6 @@ #include "image.h" int main() { - Image** images = import_images("train-images.idx3-ubyte", "train-labels.idx3-ubyte", 20); + Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", 20); } \ No newline at end of file diff --git a/matrix.c b/matrix.c index 7954e59..b229631 100644 --- a/matrix.c +++ b/matrix.c @@ -265,5 +265,5 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) { else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j]; } } - return mat; + return result_matrix; }