diff --git a/.gitignore b/.gitignore index 4f907f8..15d0ea4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ mnist runTests *.o -*.exe \ No newline at end of file +*.exe +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/settings.json diff --git a/imageInput.c b/imageInput.c index d31a7da..7f318ce 100644 --- a/imageInput.c +++ b/imageInput.c @@ -1,29 +1,134 @@ +#include "imageInput.h" #include #include #include -#include "imageInput.h" -#define BUFFER_SIZE 100 #define FILE_HEADER_STRING "__info2_image_file_format__" -// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei -GrayScaleImage readImage() -{ - +/* ---------------------------------------------------------- + 1. Header prüfen + ---------------------------------------------------------- */ +static int readHeader(FILE *file) { + char header[sizeof(FILE_HEADER_STRING)]; + if (fread(header, 1, sizeof(FILE_HEADER_STRING) - 1, file) != + sizeof(FILE_HEADER_STRING) - 1) + return 0; + header[sizeof(FILE_HEADER_STRING) - 1] = '\0'; + return strcmp(header, FILE_HEADER_STRING) == 0; } -// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen -GrayScaleImageSeries *readImages(const char *path) -{ - GrayScaleImageSeries *series = NULL; - FILE *file = fopen("mnist_test.info2","rb"); - char headOfFile; - series = malloc(); - return series; +/* ---------------------------------------------------------- + 2. Meta-Daten lesen (unsigned short) + ---------------------------------------------------------- */ +static int readMeta(FILE *file, unsigned short *count, unsigned short *width, + unsigned short *height) { + if (fread(count, sizeof(unsigned short), 1, file) != 1) + return 0; + if (fread(width, sizeof(unsigned short), 1, file) != 1) + return 0; + if (fread(height, sizeof(unsigned short), 1, file) != 1) + return 0; + + return 1; } -// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt -void clearSeries(GrayScaleImageSeries *series) -{ +/* ---------------------------------------------------------- + 3. Einzelbild lesen + ---------------------------------------------------------- */ +static int readSingleImage(FILE *file, GrayScaleImage *img, + unsigned short width, unsigned short height) { + img->width = width; + img->height = height; -} \ No newline at end of file + size_t numPixels = (size_t)width * (size_t)height; + img->buffer = malloc(numPixels); + if (!img->buffer) + return 0; + + if (fread(img->buffer, 1, numPixels, file) != numPixels) { + free(img->buffer); + img->buffer = NULL; + return 0; + } + return 1; +} + +/* ---------------------------------------------------------- + 4. Label lesen + ---------------------------------------------------------- */ +static int readLabel(FILE *file, unsigned char *label) { + return fread(label, 1, 1, file) == 1; +} + +/* ---------------------------------------------------------- + 5. Komplette Bildserie lesen + ---------------------------------------------------------- */ +GrayScaleImageSeries *readImages(const char *path) { + FILE *file = fopen(path, "rb"); + if (!file) + return NULL; + + if (!readHeader(file)) { + fclose(file); + return NULL; + } + + unsigned short count, width, height; + if (!readMeta(file, &count, &width, &height)) { + + fclose(file); + return NULL; + } + // printf("%d, %d, %d", count, width, height); + + GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); + if (!series) { + fclose(file); + return NULL; + } + + series->count = count; + series->images = malloc(count * sizeof(GrayScaleImage)); + series->labels = malloc(count * sizeof(unsigned char)); + if (!series->images || !series->labels) { + free(series->images); + free(series->labels); + free(series); + fclose(file); + return NULL; + } + + for (unsigned int i = 0; i < count; i++) { + if (!readSingleImage(file, &series->images[i], width, height) || + !readLabel(file, &series->labels[i])) { + // Aufräumen bei Fehler + for (unsigned int j = 0; j < i; j++) { + free(series->images[j].buffer); + } + free(series->images); + free(series->labels); + free(series); + fclose(file); + return NULL; + } + } + + fclose(file); + return series; +} + +/* ---------------------------------------------------------- + 6. Speicher komplett freigeben + ---------------------------------------------------------- */ +void clearSeries(GrayScaleImageSeries *series) { + if (!series) + return; + + for (unsigned int i = 0; i < series->count; i++) { + free(series->images[i].buffer); + } + + free(series->images); + free(series->labels); + free(series); +} diff --git a/imageInputTests.c b/imageInputTests.c index 03240ab..e498ec4 100644 --- a/imageInputTests.c +++ b/imageInputTests.c @@ -1,143 +1,204 @@ - -#include -#include -#include -#include "unity.h" #include "imageInput.h" +#include "unity.h" +#include +#include +#include +/* --------------------------------------------------------- + Hilfsfunktion: Testdatei vorbereiten + --------------------------------------------------------- */ +static void prepareImageFile(const char *path, unsigned int width, + unsigned int height, unsigned int numberOfImages, + unsigned char label) { + FILE *file = fopen(path, "wb"); + if (!file) + return; -static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label) -{ - FILE *file = fopen(path, "wb"); + // Header + const char *fileTag = "__info2_image_file_format__"; + fwrite(fileTag, 1, strlen(fileTag), file); - if(file != NULL) - { - const char *fileTag = "__info2_image_file_format__"; - GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType)); + // Meta-Daten als unsigned short + unsigned short n = (unsigned short)numberOfImages; + unsigned short w = (unsigned short)width; + unsigned short h = (unsigned short)height; + fwrite(&n, sizeof(unsigned short), 1, file); + fwrite(&w, sizeof(unsigned short), 1, file); + fwrite(&h, sizeof(unsigned short), 1, file); - if(zeroBuffer != NULL) - { - fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file); - fwrite(&numberOfImages, sizeof(numberOfImages), 1, file); - fwrite(&width, sizeof(width), 1, file); - fwrite(&height, sizeof(height), 1, file); + // Pixelbuffer + GrayScalePixelType *buffer = + calloc(width * height, sizeof(GrayScalePixelType)); + if (!buffer) { + fclose(file); + return; + } + for (unsigned int i = 0; i < width * height; i++) + buffer[i] = (GrayScalePixelType)i; - for(int i = 0; i < numberOfImages; i++) - { - fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file); - fwrite(&label, sizeof(unsigned char), 1, file); - } + // Jedes Bild schreiben: Pixel + Label + for (unsigned int img = 0; img < numberOfImages; img++) { + fwrite(buffer, sizeof(GrayScalePixelType), width * height, file); + fwrite(&label, sizeof(unsigned char), 1, file); + } - free(zeroBuffer); - } - - fclose(file); - } + free(buffer); + fclose(file); } +/* --------------------------------------------------------- + Unit Tests + --------------------------------------------------------- */ -void test_readImagesReturnsCorrectNumberOfImages(void) -{ - GrayScaleImageSeries *series = NULL; - const unsigned short expectedNumberOfImages = 2; - const char *path = "testFile.info2"; - prepareImageFile(path, 8, 8, expectedNumberOfImages, 1); - series = readImages(path); - TEST_ASSERT_NOT_NULL(series); - TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count); - clearSeries(series); - remove(path); +void test_readImagesReturnsCorrectNumberOfImages(void) { + GrayScaleImageSeries *series = NULL; + const unsigned int expectedNumberOfImages = 2; + const char *path = "testFile.info2"; + prepareImageFile(path, 8, 8, expectedNumberOfImages, 1); + series = readImages(path); + TEST_ASSERT_NOT_NULL(series); + TEST_ASSERT_EQUAL_UINT(expectedNumberOfImages, series->count); + clearSeries(series); + remove(path); } -void test_readImagesReturnsCorrectImageWidth(void) -{ - GrayScaleImageSeries *series = NULL; - const unsigned short expectedWidth = 10; - const char *path = "testFile.info2"; - prepareImageFile(path, expectedWidth, 8, 2, 1); - series = readImages(path); - TEST_ASSERT_NOT_NULL(series); - TEST_ASSERT_NOT_NULL(series->images); - TEST_ASSERT_EQUAL_UINT16(2, series->count); - TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width); - TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width); - clearSeries(series); - remove(path); +void test_readImagesReturnsCorrectImageWidth(void) { + GrayScaleImageSeries *series = NULL; + const unsigned int expectedWidth = 10; + const char *path = "testFile.info2"; + prepareImageFile(path, expectedWidth, 8, 2, 1); + series = readImages(path); + TEST_ASSERT_NOT_NULL(series); + TEST_ASSERT_NOT_NULL(series->images); + TEST_ASSERT_EQUAL_UINT(2, series->count); + TEST_ASSERT_EQUAL_UINT(expectedWidth, series->images[0].width); + TEST_ASSERT_EQUAL_UINT(expectedWidth, series->images[1].width); + clearSeries(series); + remove(path); } -void test_readImagesReturnsCorrectImageHeight(void) -{ - GrayScaleImageSeries *series = NULL; - const unsigned short expectedHeight = 10; - const char *path = "testFile.info2"; - prepareImageFile(path, 8, expectedHeight, 2, 1); - series = readImages(path); - TEST_ASSERT_NOT_NULL(series); - TEST_ASSERT_NOT_NULL(series->images); - TEST_ASSERT_EQUAL_UINT16(2, series->count); - TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[0].height); - TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[1].height); - clearSeries(series); - remove(path); +void test_readImagesReturnsCorrectImageHeight(void) { + GrayScaleImageSeries *series = NULL; + const unsigned int expectedHeight = 10; + const char *path = "testFile.info2"; + prepareImageFile(path, 8, expectedHeight, 2, 1); + series = readImages(path); + TEST_ASSERT_NOT_NULL(series); + TEST_ASSERT_NOT_NULL(series->images); + TEST_ASSERT_EQUAL_UINT(2, series->count); + TEST_ASSERT_EQUAL_UINT(expectedHeight, series->images[0].height); + TEST_ASSERT_EQUAL_UINT(expectedHeight, series->images[1].height); + clearSeries(series); + remove(path); } -void test_readImagesReturnsCorrectLabels(void) -{ - const unsigned char expectedLabel = 15; +void test_readImagesReturnsCorrectLabels(void) { + const unsigned char expectedLabel = 15; - GrayScaleImageSeries *series = NULL; - const char *path = "testFile.info2"; - prepareImageFile(path, 8, 8, 2, expectedLabel); - series = readImages(path); - TEST_ASSERT_NOT_NULL(series); - TEST_ASSERT_NOT_NULL(series->labels); - TEST_ASSERT_EQUAL_UINT16(2, series->count); - for (int i = 0; i < 2; i++) { - TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]); - } - clearSeries(series); - remove(path); + GrayScaleImageSeries *series = NULL; + const char *path = "testFile.info2"; + prepareImageFile(path, 8, 8, 2, expectedLabel); + series = readImages(path); + TEST_ASSERT_NOT_NULL(series); + TEST_ASSERT_NOT_NULL(series->labels); + TEST_ASSERT_EQUAL_UINT(2, series->count); + for (int i = 0; i < 2; i++) { + TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]); + } + clearSeries(series); + remove(path); } -void test_readImagesReturnsNullOnNotExistingPath(void) -{ - const char *path = "testFile.txt"; - remove(path); +void test_readImagesReturnsNullOnNotExistingPath(void) { + const char *path = "testFile.txt"; + remove(path); + TEST_ASSERT_NULL(readImages(path)); +} + +void test_readImagesFailsOnWrongFileTag(void) { + const char *path = "testFile.info2"; + FILE *file = fopen(path, "w"); + if (file != NULL) { + fprintf(file, "some_tag "); + fclose(file); TEST_ASSERT_NULL(readImages(path)); + } + remove(path); } -void test_readImagesFailsOnWrongFileTag(void) -{ - const char *path = "testFile.info2"; - FILE *file = fopen(path, "w"); - if(file != NULL) - { - fprintf(file, "some_tag "); - fclose(file); - TEST_ASSERT_NULL(readImages(path)); - } - remove(path); +void test_read_GrayScale_Pixel(void) { + GrayScaleImageSeries *series = NULL; + const char *path = "testFile.info2"; + + prepareImageFile(path, 8, 8, 1, 1); + series = readImages(path); + + TEST_ASSERT_NOT_NULL(series); + TEST_ASSERT_NOT_NULL(series->images); + TEST_ASSERT_EQUAL_UINT(1, series->count); + + for (int i = 0; i < (8 * 8); i++) { + TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i, series->images[0].buffer[i]); + } + + clearSeries(series); + remove(path); } -void setUp(void) { - // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden +/* --------------------------------------------------------- + Optional: Mehrere Bilder gleichzeitig testen + --------------------------------------------------------- */ + +void test_readImagesMultipleImagesContent(void) { + GrayScaleImageSeries *series = NULL; + const char *path = "testFile.info2"; + const unsigned int numberOfImages = 3; + const unsigned int width = 4; + const unsigned int height = 4; + const unsigned char label = 7; + + prepareImageFile(path, width, height, numberOfImages, label); + + series = readImages(path); + TEST_ASSERT_NOT_NULL(series); + TEST_ASSERT_NOT_NULL(series->images); + TEST_ASSERT_NOT_NULL(series->labels); + TEST_ASSERT_EQUAL_UINT(numberOfImages, series->count); + + for (unsigned int img = 0; img < numberOfImages; img++) { + for (unsigned int i = 0; i < width * height; i++) + TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i, + series->images[img].buffer[i]); + TEST_ASSERT_EQUAL_UINT8(label, series->labels[img]); + } + + clearSeries(series); + remove(path); } -void tearDown(void) { - // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden +/* --------------------------------------------------------- + Setup / Teardown + --------------------------------------------------------- */ +void setUp(void) {} +void tearDown(void) {} + +/* --------------------------------------------------------- + main() + --------------------------------------------------------- */ +int main(void) { + UNITY_BEGIN(); + + printf("\n============================\nImage input " + "tests\n============================\n"); + + RUN_TEST(test_readImagesReturnsCorrectNumberOfImages); + RUN_TEST(test_readImagesReturnsCorrectImageWidth); + RUN_TEST(test_readImagesReturnsCorrectImageHeight); + RUN_TEST(test_readImagesReturnsCorrectLabels); + RUN_TEST(test_readImagesReturnsNullOnNotExistingPath); + RUN_TEST(test_readImagesFailsOnWrongFileTag); + RUN_TEST(test_read_GrayScale_Pixel); + RUN_TEST(test_readImagesMultipleImagesContent); + + return UNITY_END(); } - -int main() -{ - UNITY_BEGIN(); - - printf("\n============================\nImage input tests\n============================\n"); - RUN_TEST(test_readImagesReturnsCorrectNumberOfImages); - RUN_TEST(test_readImagesReturnsCorrectImageWidth); - RUN_TEST(test_readImagesReturnsCorrectImageHeight); - RUN_TEST(test_readImagesReturnsCorrectLabels); - RUN_TEST(test_readImagesReturnsNullOnNotExistingPath); - RUN_TEST(test_readImagesFailsOnWrongFileTag); - - return UNITY_END(); -} \ No newline at end of file diff --git a/matrix.c b/matrix.c index 22b68f3..bf42b6c 100644 --- a/matrix.c +++ b/matrix.c @@ -1,14 +1,16 @@ #include "matrix.h" +#include #include #include -// TODO Matrix-Funktionen implementieren + /*typedef struct { unsigned int rows; //Zeilen unsigned int cols; //Spalten MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten } Matrix;*/ + Matrix createMatrix(unsigned int rows, unsigned int cols) { - if (cols == 0 || rows == 0){ + if (cols == 0 || rows == 0) { Matrix errorMatrix = {0, 0, NULL}; return errorMatrix; } @@ -19,11 +21,15 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { return newMatrix; } void clearMatrix(Matrix *matrix) { - matrix->buffer = UNDEFINED_MATRIX_VALUE; - matrix->rows = UNDEFINED_MATRIX_VALUE; - matrix->cols = UNDEFINED_MATRIX_VALUE; - free((*matrix).buffer); // Speicher freigeben + + if (matrix->buffer != NULL) { + free((*matrix).buffer); + matrix->buffer = NULL; + } + matrix->rows = 0; + matrix->cols = 0; } + void setMatrixAt(const MatrixType value, Matrix matrix, const unsigned int rowIdx, // Kopie der Matrix wird übergeben const unsigned int colIdx) { @@ -32,42 +38,41 @@ void setMatrixAt(const MatrixType value, Matrix matrix, // Speichergröße nicht überschreiten return; } - matrix.buffer[rowIdx * matrix.cols + colIdx] = value; - // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte - // innerhalb der Zeile + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte + // innerhalb der Zeile } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, // Kopie der Matrix wird übergeben unsigned int colIdx) { - if (rowIdx >= matrix.rows || - colIdx >= matrix.cols) { // Speichergröße nicht überschreiten - return 0; + if (rowIdx >= matrix.rows || colIdx >= matrix.cols || + matrix.buffer == NULL) { // Speichergröße nicht überschreiten + return UNDEFINED_MATRIX_VALUE; } MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx]; return value; } -Matrix broadcastingCols(const Matrix matrix, const unsigned int cols){ - Matrix copy1 = createMatrix(matrix.rows, cols); - for (int r= 0; r < matrix.rows; r++){ - MatrixType valueMatrix1 = getMatrixAt(matrix, r, 0); - for (int c=0; c < cols; c++){ - setMatrixAt(valueMatrix1, copy1,r,c); - } - } - return copy1; +Matrix broadcastingCols(const Matrix matrix, const unsigned int cols) { + Matrix copy1 = createMatrix(matrix.rows, cols); + for (int r = 0; r < matrix.rows; r++) { + MatrixType valueMatrix1 = getMatrixAt(matrix, r, 0); + for (int c = 0; c < cols; c++) { + setMatrixAt(valueMatrix1, copy1, r, c); + } + } + return copy1; } -Matrix broadcastingRows(const Matrix matrix, const unsigned int rows){ - Matrix copy1 = createMatrix(rows, matrix.cols); - for (int c= 0; c < matrix.cols; c++){ - MatrixType valueMatrix1 = getMatrixAt(matrix, 0, c); - for (int r=0; r < rows; r++){ - setMatrixAt(valueMatrix1, copy1,r,c); - } - } - return copy1; - +Matrix broadcastingRows(const Matrix matrix, const unsigned int rows) { + Matrix copy1 = createMatrix(rows, matrix.cols); + for (int c = 0; c < matrix.cols; c++) { + MatrixType valueMatrix1 = getMatrixAt(matrix, 0, c); + for (int r = 0; r < rows; r++) { + setMatrixAt(valueMatrix1, copy1, r, c); + } + } + return copy1; } Matrix add(const Matrix matrix1, const Matrix matrix2) { @@ -78,14 +83,14 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { const int cols2 = matrix2.cols; const int rows2 = matrix2.rows; + const int rowsEqual = (matrix1.rows == matrix2.rows) ? 1 : 0; + const int colsEqual = (matrix1.cols == matrix2.cols) ? 1 : 0; - const int rowsEqual = (matrix1.rows==matrix2.rows) ? 1: 0; - const int colsEqual = (matrix1.cols==matrix2.cols) ? 1: 0; - // Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassender // Matrix - if (rowsEqual == 1 && colsEqual == 1){ + if (rowsEqual == 1 && colsEqual == 1) { Matrix result = createMatrix(matrix1.rows, matrix1.cols); +<<<<<<< HEAD if (result.buffer == NULL){ return (Matrix){0,0,NULL}; } @@ -93,17 +98,26 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { for (int j= 0; j< cols1; j++){ int valueM1= getMatrixAt(matrix1, i, j); int valueM2= getMatrixAt(matrix2, i, j); +======= + if (result.buffer == NULL) { + return (Matrix){0, 0, NULL}; + } + for (int i = 0; i < rows1; i++) { + for (int j = 0; j < cols1; j++) { + int valueM1 = getMatrixAt(matrix1, i, j); + int valueM2 = getMatrixAt(matrix2, i, j); +>>>>>>> main int sum = valueM1 + valueM2; setMatrixAt(sum, result, i, j); } } return result; - } - else if (rowsEqual ==1 && (cols1 ==1 || cols2 ==1)){ - if (cols1==1){ //broadcasting von vektor 1 zu matrix 1, add + } else if (rowsEqual == 1 && (cols1 == 1 || cols2 == 1)) { + if (cols1 == 1) { // broadcasting von vektor 1 zu matrix 1, add Matrix newMatrix = broadcastingCols(matrix1, cols2); - //add + // add Matrix result = createMatrix(newMatrix.rows, newMatrix.cols); +<<<<<<< HEAD if (result.buffer == NULL){ return (Matrix){0,0,NULL}; } @@ -113,14 +127,25 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { int valueM2= getMatrixAt(matrix2, i, j); int sum = valueM1 + valueM2; setMatrixAt(sum, result, i, j); +======= + if (result.buffer == NULL) { + return (Matrix){0, 0, NULL}; +>>>>>>> main } - } - return result; - } - else{ + for (int i = 0; i < rows1; i++) { + for (int j = 0; j < cols2; j++) { + int valueM1 = getMatrixAt(newMatrix, i, j); + int valueM2 = getMatrixAt(matrix2, i, j); + int sum = valueM1 + valueM2; + setMatrixAt(sum, result, i, j); + } + } + return result; + } else { Matrix newMatrix2 = broadcastingCols(matrix2, cols1); - //add + // add Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols); +<<<<<<< HEAD if (result.buffer == NULL){ return (Matrix){0,0,NULL}; } @@ -130,17 +155,29 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { int valueM2= getMatrixAt(newMatrix2, i, j); int sum = valueM1 + valueM2; setMatrixAt(sum, result, i, j); +======= + if (result.buffer == NULL) { + return (Matrix){0, 0, NULL}; +>>>>>>> main } - } - return result; + for (int i = 0; i < rows1; i++) { + for (int j = 0; j < cols1; j++) { + int valueM1 = getMatrixAt(matrix1, i, j); + int valueM2 = getMatrixAt(newMatrix2, i, j); + int sum = valueM1 + valueM2; + setMatrixAt(sum, result, i, j); + } + } + return result; } } - else if ((rows1 ==1 || rows2 ==1) && colsEqual == 1){ - if (rows1==1){ + else if ((rows1 == 1 || rows2 == 1) && colsEqual == 1) { + if (rows1 == 1) { Matrix newMatrix = broadcastingRows(matrix1, rows2); - //add + // add Matrix result = createMatrix(newMatrix.rows, newMatrix.cols); +<<<<<<< HEAD if (result.buffer == NULL){ return (Matrix){0,0,NULL}; } @@ -150,14 +187,25 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { int valueM2= getMatrixAt(matrix2, i, j); int sum = valueM1 + valueM2; setMatrixAt(sum, result, i, j); +======= + if (result.buffer == NULL) { + return (Matrix){0, 0, NULL}; +>>>>>>> main } - } - return result; - } - else{ + for (int i = 0; i < rows2; i++) { + for (int j = 0; j < cols1; j++) { + int valueM1 = getMatrixAt(newMatrix, i, j); + int valueM2 = getMatrixAt(matrix2, i, j); + int sum = valueM1 + valueM2; + setMatrixAt(sum, result, i, j); + } + } + return result; + } else { Matrix newMatrix2 = broadcastingRows(matrix2, rows1); - //add + // add Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols); +<<<<<<< HEAD if (result.buffer == NULL){ return (Matrix){0,0,NULL}; } @@ -167,39 +215,51 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { int valueM2= getMatrixAt(newMatrix2, i, j); int sum = valueM1 + valueM2; setMatrixAt(sum, result, i, j); +======= + if (result.buffer == NULL) { + return (Matrix){0, 0, NULL}; +>>>>>>> main } + for (int i = 0; i < rows1; i++) { + for (int j = 0; j < cols1; j++) { + int valueM1 = getMatrixAt(matrix1, i, j); + int valueM2 = getMatrixAt(newMatrix2, i, j); + int sum = valueM1 + valueM2; + setMatrixAt(sum, result, i, j); + } + } + return result; } - return result; - } - } - else { + } else { // kein add möglich Matrix errorMatrix = {0, 0, NULL}; return errorMatrix; } return result; } -Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - //Spalten1 müssen gleich zeilen2 sein! dann multiplizieren - if (matrix1.cols == matrix2.rows){ - Matrix multMatrix = createMatrix(matrix1.rows,matrix2.cols); - //durch neue matrix iterieren - for (int r=0; r< matrix1.rows; r++){ - for (int c=0; c< matrix2.cols; c++){ +Matrix multiply(const Matrix matrix1, const Matrix matrix2) { + // Spalten1 müssen gleich zeilen2 sein! dann multiplizieren + if (matrix1.cols == matrix2.rows) { + Matrix multMatrix = createMatrix(matrix1.rows, matrix2.cols); + // durch neue matrix iterieren + for (int r = 0; r < matrix1.rows; r++) { + for (int c = 0; c < matrix2.cols; c++) { MatrixType sum = 0.0; - //skalarprodukte berechnen, k damit die ganze zeile mal die ganze spalte genommen wird quasi - for (int k=0; k< matrix1.cols; k++){ - //sum+= matrix1.buffer[r*matrix1.cols+k]*matrix2.buffer[k*matrix2.cols+c]; - sum += getMatrixAt(matrix1, r, k)*getMatrixAt(matrix2, k, c); + // skalarprodukte berechnen, k damit die ganze zeile mal die ganze + // spalte genommen wird quasi + for (int k = 0; k < matrix1.cols; k++) { + // sum+= + // matrix1.buffer[r*matrix1.cols+k]*matrix2.buffer[k*matrix2.cols+c]; + sum += getMatrixAt(matrix1, r, k) * getMatrixAt(matrix2, k, c); } - //Ergebnisse in neue matrix speichern + // Ergebnisse in neue matrix speichern setMatrixAt(sum, multMatrix, r, c); } } return multMatrix; } - //sonst fehler, kein multiply möglich - else{ + // sonst fehler, kein multiply möglich + else { Matrix errorMatrix = {0, 0, NULL}; return errorMatrix; } diff --git a/matrix.h b/matrix.h index ca871ae..812b3a0 100644 --- a/matrix.h +++ b/matrix.h @@ -19,6 +19,11 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx); + +Matrix broadCastCols(const Matrix matrix, const unsigned int rows, + const unsigned int cols); +Matrix broadCastRows(const Matrix matrix, const unsigned int rows, + const unsigned int cols); Matrix add(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2); diff --git a/neuralNetwork.c b/neuralNetwork.c index bd8f164..7697dd2 100644 --- a/neuralNetwork.c +++ b/neuralNetwork.c @@ -1,268 +1,235 @@ -#include -#include -#include -#include #include "neuralNetwork.h" +#include +#include +#include +#include #define BUFFER_SIZE 100 #define FILE_HEADER_STRING "__info2_neural_network_file_format__" -static void softmax(Matrix *matrix) -{ - if(matrix->cols > 0) - { - double *colSums = (double *)calloc(matrix->cols, sizeof(double)); +static void softmax(Matrix *matrix) { + if (matrix->cols > 0) { + double *colSums = (double *)calloc(matrix->cols, sizeof(double)); - if(colSums != NULL) - { - for(int colIdx = 0; colIdx < matrix->cols; colIdx++) - { - for(int rowIdx = 0; rowIdx < matrix->rows; rowIdx++) - { - MatrixType expValue = exp(getMatrixAt(*matrix, rowIdx, colIdx)); - setMatrixAt(expValue, *matrix, rowIdx, colIdx); - colSums[colIdx] += expValue; - } - } - - for(int colIdx = 0; colIdx < matrix->cols; colIdx++) - { - for(int rowIdx = 0; rowIdx < matrix->rows; rowIdx++) - { - MatrixType normalizedValue = getMatrixAt(*matrix, rowIdx, colIdx) / colSums[colIdx]; - setMatrixAt(normalizedValue, *matrix, rowIdx, colIdx); - } - } - free(colSums); + if (colSums != NULL) { + for (int colIdx = 0; colIdx < matrix->cols; colIdx++) { + for (int rowIdx = 0; rowIdx < matrix->rows; rowIdx++) { + MatrixType expValue = exp(getMatrixAt(*matrix, rowIdx, colIdx)); + setMatrixAt(expValue, *matrix, rowIdx, colIdx); + colSums[colIdx] += expValue; } - } -} + } -static void relu(Matrix *matrix) -{ - for(int i = 0; i < matrix->rows * matrix->cols; i++) - { - matrix->buffer[i] = matrix->buffer[i] >= 0 ? matrix->buffer[i] : 0; - } -} - -static int checkFileHeader(FILE *file) -{ - int isValid = 0; - int fileHeaderLen = strlen(FILE_HEADER_STRING); - char buffer[BUFFER_SIZE] = {0}; - - if(BUFFER_SIZE-1 < fileHeaderLen) - fileHeaderLen = BUFFER_SIZE-1; - - if(fread(buffer, sizeof(char), fileHeaderLen, file) == fileHeaderLen) - isValid = strcmp(buffer, FILE_HEADER_STRING) == 0; - - return isValid; -} - -static unsigned int readDimension(FILE *file) -{ - int dimension = 0; - - if(fread(&dimension, sizeof(int), 1, file) != 1) - dimension = 0; - - return dimension; -} - -static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols) -{ - Matrix matrix = createMatrix(rows, cols); - - if(matrix.buffer != NULL) - { - if(fread(matrix.buffer, sizeof(MatrixType), rows*cols, file) != rows*cols) - clearMatrix(&matrix); - } - - return matrix; -} - -static Layer readLayer(FILE *file, unsigned int inputDimension, unsigned int outputDimension) -{ - Layer layer; - layer.weights = readMatrix(file, outputDimension, inputDimension); - layer.biases = readMatrix(file, outputDimension, 1); - - return layer; -} - -static int isEmptyLayer(const Layer layer) -{ - return layer.biases.cols == 0 || layer.biases.rows == 0 || layer.biases.buffer == NULL || layer.weights.rows == 0 || layer.weights.cols == 0 || layer.weights.buffer == NULL; -} - -static void clearLayer(Layer *layer) -{ - if(layer != NULL) - { - clearMatrix(&layer->weights); - clearMatrix(&layer->biases); - layer->activation = NULL; - } -} - -static void assignActivations(NeuralNetwork model) -{ - for(int i = 0; i < (int)model.numberOfLayers-1; i++) - { - model.layers[i].activation = relu; - } - - if(model.numberOfLayers > 0) - model.layers[model.numberOfLayers-1].activation = softmax; -} - -NeuralNetwork loadModel(const char *path) -{ - NeuralNetwork model = {NULL, 0}; - FILE *file = fopen(path, "rb"); - - if(file != NULL) - { - if(checkFileHeader(file)) - { - unsigned int inputDimension = readDimension(file); - unsigned int outputDimension = readDimension(file); - - while(inputDimension > 0 && outputDimension > 0) - { - Layer layer = readLayer(file, inputDimension, outputDimension); - Layer *layerBuffer = NULL; - - if(isEmptyLayer(layer)) - { - clearLayer(&layer); - clearModel(&model); - break; - } - - layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer)); - - if(layerBuffer != NULL) - model.layers = layerBuffer; - else - { - clearModel(&model); - break; - } - - model.layers[model.numberOfLayers] = layer; - model.numberOfLayers++; - - inputDimension = outputDimension; - outputDimension = readDimension(file); - } + for (int colIdx = 0; colIdx < matrix->cols; colIdx++) { + for (int rowIdx = 0; rowIdx < matrix->rows; rowIdx++) { + MatrixType normalizedValue = + getMatrixAt(*matrix, rowIdx, colIdx) / colSums[colIdx]; + setMatrixAt(normalizedValue, *matrix, rowIdx, colIdx); } - fclose(file); - - assignActivations(model); + } + free(colSums); } - - return model; + } } -static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count) -{ - Matrix matrix = {NULL, 0, 0}; +static void relu(Matrix *matrix) { + for (int i = 0; i < matrix->rows * matrix->cols; i++) { + matrix->buffer[i] = matrix->buffer[i] >= 0 ? matrix->buffer[i] : 0; + } +} - if(count > 0 && images != NULL) - { - matrix = createMatrix(images[0].height * images[0].width, count); +static int checkFileHeader(FILE *file) { + int isValid = 0; + int fileHeaderLen = strlen(FILE_HEADER_STRING); + char buffer[BUFFER_SIZE] = {0}; - if(matrix.buffer != NULL) - { - for(int i = 0; i < count; i++) - { - for(int j = 0; j < images[i].width * images[i].height; j++) - { - setMatrixAt((MatrixType)images[i].buffer[j], matrix, j, i); - } - } + if (BUFFER_SIZE - 1 < fileHeaderLen) + fileHeaderLen = BUFFER_SIZE - 1; + + if (fread(buffer, sizeof(char), fileHeaderLen, file) == fileHeaderLen) + isValid = strcmp(buffer, FILE_HEADER_STRING) == 0; + + return isValid; +} + +static unsigned int readDimension(FILE *file) { + int dimension = 0; + + if (fread(&dimension, sizeof(int), 1, file) != 1) + dimension = 0; + + return dimension; +} + +static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols) { + Matrix matrix = createMatrix(rows, cols); + + if (matrix.buffer != NULL) { + if (fread(matrix.buffer, sizeof(MatrixType), rows * cols, file) != + rows * cols) + clearMatrix(&matrix); + } + + return matrix; +} + +static Layer readLayer(FILE *file, unsigned int inputDimension, + unsigned int outputDimension) { + Layer layer; + layer.weights = readMatrix(file, outputDimension, inputDimension); + layer.biases = readMatrix(file, outputDimension, 1); + + return layer; +} + +static int isEmptyLayer(const Layer layer) { + return layer.biases.cols == 0 || layer.biases.rows == 0 || + layer.biases.buffer == NULL || layer.weights.rows == 0 || + layer.weights.cols == 0 || layer.weights.buffer == NULL; +} + +static void clearLayer(Layer *layer) { + if (layer != NULL) { + clearMatrix(&layer->weights); + clearMatrix(&layer->biases); + layer->activation = NULL; + } +} + +static void assignActivations(NeuralNetwork model) { + for (int i = 0; i < (int)model.numberOfLayers - 1; i++) { + model.layers[i].activation = relu; + } + + if (model.numberOfLayers > 0) + model.layers[model.numberOfLayers - 1].activation = softmax; +} + +NeuralNetwork loadModel(const char *path) { + NeuralNetwork model = {NULL, 0}; + FILE *file = fopen(path, "rb"); + + if (file != NULL) { + if (checkFileHeader(file)) { + unsigned int inputDimension = readDimension(file); + unsigned int outputDimension = readDimension(file); + + while (inputDimension > 0 && outputDimension > 0) { + Layer layer = readLayer(file, inputDimension, outputDimension); + Layer *layerBuffer = NULL; + + if (isEmptyLayer(layer)) { + clearLayer(&layer); + clearModel(&model); + break; } - } - return matrix; -} + layerBuffer = (Layer *)realloc( + model.layers, (model.numberOfLayers + 1) * sizeof(Layer)); -static Matrix forward(const NeuralNetwork model, Matrix inputBatch) -{ - Matrix result = inputBatch; - - if(result.buffer != NULL) - { - for(int i = 0; i < model.numberOfLayers; i++) - { - Matrix biasResult; - Matrix weightResult; - - weightResult = multiply(model.layers[i].weights, result); - clearMatrix(&result); - biasResult = add(model.layers[i].biases, weightResult); - clearMatrix(&weightResult); - - if(model.layers[i].activation != NULL) - model.layers[i].activation(&biasResult); - result = biasResult; + if (layerBuffer != NULL) + model.layers = layerBuffer; + else { + clearModel(&model); + break; } - } - return result; + model.layers[model.numberOfLayers] = layer; + model.numberOfLayers++; + + inputDimension = outputDimension; + outputDimension = readDimension(file); + } + } + fclose(file); + + assignActivations(model); + } + + return model; } -unsigned char *argmax(const Matrix matrix) -{ - unsigned char *maxIdx = NULL; +static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], + unsigned int count) { + Matrix matrix = {0, 0, NULL}; // falsch herum - if(matrix.rows > 0 && matrix.cols > 0) - { - maxIdx = (unsigned char *)malloc(sizeof(unsigned char) * matrix.cols); + if (count > 0 && images != NULL) { + matrix = createMatrix(images[0].height * images[0].width, count); - if(maxIdx != NULL) - { - for(int colIdx = 0; colIdx < matrix.cols; colIdx++) - { - maxIdx[colIdx] = 0; - - for(int rowIdx = 1; rowIdx < matrix.rows; rowIdx++) - { - if(getMatrixAt(matrix, rowIdx, colIdx) > getMatrixAt(matrix, maxIdx[colIdx], colIdx)) - maxIdx[colIdx] = rowIdx; - } - } + if (matrix.buffer != NULL) { + for (int i = 0; i < count; i++) { + for (int j = 0; j < images[i].width * images[i].height; j++) { + setMatrixAt((MatrixType)images[i].buffer[j], matrix, j, i); } + } } + } - return maxIdx; + return matrix; } -unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[], unsigned int numberOfImages) -{ - Matrix inputBatch = imageBatchToMatrixOfImageVectors(images, numberOfImages); - Matrix outputBatch = forward(model, inputBatch); +static Matrix forward(const NeuralNetwork model, Matrix inputBatch) { + Matrix result = inputBatch; - unsigned char *result = argmax(outputBatch); - - clearMatrix(&outputBatch); - - return result; + if (result.buffer != NULL) { + for (int i = 0; i < model.numberOfLayers; i++) { + Matrix biasResult; + Matrix weightResult; + + weightResult = multiply(model.layers[i].weights, result); + clearMatrix(&result); + biasResult = add(model.layers[i].biases, weightResult); + clearMatrix(&weightResult); + + if (model.layers[i].activation != NULL) + model.layers[i].activation(&biasResult); + result = biasResult; + } + } + + return result; } -void clearModel(NeuralNetwork *model) -{ - if(model != NULL) - { - for(int i = 0; i < model->numberOfLayers; i++) - { - clearLayer(&model->layers[i]); +unsigned char *argmax(const Matrix matrix) { + unsigned char *maxIdx = NULL; + + if (matrix.rows > 0 && matrix.cols > 0) { + maxIdx = (unsigned char *)malloc(sizeof(unsigned char) * matrix.cols); + + if (maxIdx != NULL) { + for (int colIdx = 0; colIdx < matrix.cols; colIdx++) { + maxIdx[colIdx] = 0; + + for (int rowIdx = 1; rowIdx < matrix.rows; rowIdx++) { + if (getMatrixAt(matrix, rowIdx, colIdx) > + getMatrixAt(matrix, maxIdx[colIdx], colIdx)) + maxIdx[colIdx] = rowIdx; } - model->layers = NULL; - model->numberOfLayers = 0; + } } + } + + return maxIdx; +} + +unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[], + unsigned int numberOfImages) { + Matrix inputBatch = imageBatchToMatrixOfImageVectors(images, numberOfImages); + Matrix outputBatch = forward(model, inputBatch); + + unsigned char *result = argmax(outputBatch); + + clearMatrix(&outputBatch); + + return result; +} + +void clearModel(NeuralNetwork *model) { + if (model != NULL) { + for (int i = 0; i < model->numberOfLayers; i++) { + clearLayer(&model->layers[i]); + } + model->layers = NULL; + model->numberOfLayers = 0; + } } \ No newline at end of file diff --git a/neuralNetworkTests.c b/neuralNetworkTests.c index 21ab370..c6a6ae7 100644 --- a/neuralNetworkTests.c +++ b/neuralNetworkTests.c @@ -1,242 +1,331 @@ +#include "neuralNetwork.h" +#include "unity.h" +#include #include #include #include -#include -#include "unity.h" -#include "neuralNetwork.h" - -static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) +/*typedef struct { - // TODO -} + Matrix weights; + Matrix biases; + ActivationFunctionType activation; +} Layer; -void test_loadModelReturnsCorrectNumberOfLayers(void) +typedef struct { - const char *path = "some__nn_test_file.info2"; - MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; - MatrixType buffer2[] = {1, 2, 3, 4, 5, 6}; - Matrix weights1 = {.buffer=buffer1, .rows=3, .cols=2}; - Matrix weights2 = {.buffer=buffer2, .rows=2, .cols=3}; - MatrixType buffer3[] = {1, 2, 3}; - MatrixType buffer4[] = {1, 2}; - Matrix biases1 = {.buffer=buffer3, .rows=3, .cols=1}; - Matrix biases2 = {.buffer=buffer4, .rows=2, .cols=1}; - Layer layers[] = {{.weights=weights1, .biases=biases1}, {.weights=weights2, .biases=biases2}}; + Layer *layers; + unsigned int numberOfLayers; +} NeuralNetwork;*/ - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=2}; - NeuralNetwork netUnderTest; +/*Layer: Ebene im neuronalen Netzwerk, besteht aus mehreren Neuronen +Input-Layer: Eingabedatei +Hidden-Layer: verarbeiten die Daten +Output-Layer: Ergebnis - prepareNeuralNetworkFile(path, expectedNet); - netUnderTest = loadModel(path); - remove(path); +Gewichte: bestimmen, wie stark ein Eingangssignal auf ein Neuron wirkt - TEST_ASSERT_EQUAL_INT(expectedNet.numberOfLayers, netUnderTest.numberOfLayers); - clearModel(&netUnderTest); -} +Dimension: Form der Matrizen für einen Layer*/ -void test_loadModelReturnsCorrectWeightDimensions(void) -{ - const char *path = "some__nn_test_file.info2"; - MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; - Matrix weights = {.buffer=weightBuffer, .rows=3, .cols=2}; - MatrixType biasBuffer[] = {7, 8, 9}; - Matrix biases = {.buffer=biasBuffer, .rows=3, .cols=1}; - Layer layers[] = {{.weights=weights, .biases=biases}}; +// speichert NeuralNetwork nn in binäre Datei->erzeugt Dateiformat +static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { + FILE *fptr = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen + if (fptr == NULL) + return; // file konnte nicht geöffnet werden - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; - NeuralNetwork netUnderTest; + // Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt + // Dateiformat + const char header[] = + "__info2_neural_network_file_format__"; // header vor jedem Layer + fwrite(header, sizeof(char), strlen(header), fptr); - prepareNeuralNetworkFile(path, expectedNet); + // Wenn es keine Layer gibt, 0 eintragen, LoadModel gibt 0 zurück + if (nn.numberOfLayers == 0) { + int zero = 0; + fwrite(&zero, sizeof(int), 1, fptr); + fclose(fptr); + return; + } - netUnderTest = loadModel(path); - remove(path); + // Layer 0, inputDimension: Anzahl Input-Neuronen, outputDimension: Anzahl + // Output-Neuronen + int inputDim = (int)nn.layers[0].weights.cols; + int outputDim = (int)nn.layers[0].weights.rows; + fwrite(&inputDim, sizeof(int), 1, fptr); + fwrite(&outputDim, sizeof(int), 1, fptr); - TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); - TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.rows, netUnderTest.layers[0].weights.rows); - TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.cols, netUnderTest.layers[0].weights.cols); - clearModel(&netUnderTest); -} + /* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output + x 1). Zwischen Layern wird nur die nächste outputDimension (int) + geschrieben. */ + for (int i = 0; i < nn.numberOfLayers; i++) { + Layer layer = nn.layers[i]; -void test_loadModelReturnsCorrectBiasDimensions(void) -{ - const char *path = "some__nn_test_file.info2"; - MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; - Matrix weights = {.buffer=weightBuffer, .rows=3, .cols=2}; - MatrixType biasBuffer[] = {7, 8, 9}; - Matrix biases = {.buffer=biasBuffer, .rows=3, .cols=1}; - Layer layers[] = {{.weights=weights, .biases=biases}}; + int wrows = (int)layer.weights.rows; + int wcols = (int)layer.weights.cols; + int wcount = wrows * wcols; + int bcount = + layer.biases.rows * layer.biases.cols; /* normalerweise rows * 1 */ - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; - NeuralNetwork netUnderTest; - - prepareNeuralNetworkFile(path, expectedNet); - - netUnderTest = loadModel(path); - remove(path); - - TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); - TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].biases.rows, netUnderTest.layers[0].biases.rows); - TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].biases.cols, netUnderTest.layers[0].biases.cols); - clearModel(&netUnderTest); -} - -void test_loadModelReturnsCorrectWeights(void) -{ - const char *path = "some__nn_test_file.info2"; - MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; - Matrix weights = {.buffer=weightBuffer, .rows=3, .cols=2}; - MatrixType biasBuffer[] = {7, 8, 9}; - Matrix biases = {.buffer=biasBuffer, .rows=3, .cols=1}; - Layer layers[] = {{.weights=weights, .biases=biases}}; - - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; - NeuralNetwork netUnderTest; - - prepareNeuralNetworkFile(path, expectedNet); - - netUnderTest = loadModel(path); - remove(path); - - TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); - TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.rows, netUnderTest.layers[0].weights.rows); - TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.cols, netUnderTest.layers[0].weights.cols); - int n = netUnderTest.layers[0].weights.rows * netUnderTest.layers[0].weights.cols; - TEST_ASSERT_EQUAL_INT_ARRAY(expectedNet.layers[0].weights.buffer, netUnderTest.layers[0].weights.buffer, n); - clearModel(&netUnderTest); -} - -void test_loadModelReturnsCorrectBiases(void) -{ - const char *path = "some__nn_test_file.info2"; - MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; - Matrix weights = {.buffer=weightBuffer, .rows=3, .cols=2}; - MatrixType biasBuffer[] = {7, 8, 9}; - Matrix biases = {.buffer=biasBuffer, .rows=3, .cols=1}; - Layer layers[] = {{.weights=weights, .biases=biases}}; - - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; - NeuralNetwork netUnderTest; - - prepareNeuralNetworkFile(path, expectedNet); - - netUnderTest = loadModel(path); - remove(path); - - TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); - TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.rows, netUnderTest.layers[0].weights.rows); - TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.cols, netUnderTest.layers[0].weights.cols); - int n = netUnderTest.layers[0].biases.rows * netUnderTest.layers[0].biases.cols; - TEST_ASSERT_EQUAL_INT_ARRAY(expectedNet.layers[0].biases.buffer, netUnderTest.layers[0].biases.buffer, n); - clearModel(&netUnderTest); -} - -void test_loadModelFailsOnWrongFileTag(void) -{ - const char *path = "some_nn_test_file.info2"; - NeuralNetwork netUnderTest; - FILE *file = fopen(path, "wb"); - - if(file != NULL) - { - const char *fileTag = "info2_neural_network_file_format"; - - fwrite(fileTag, sizeof(char), strlen(fileTag), file); - - fclose(file); + /* Gewichte (MatrixType binär) */ + if (wcount > 0 && layer.weights.buffer != NULL) { + fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, fptr); } - netUnderTest = loadModel(path); - - remove(path); - - TEST_ASSERT_NULL(netUnderTest.layers); - TEST_ASSERT_EQUAL_INT(0, netUnderTest.numberOfLayers); -} - -void test_clearModelSetsMembersToNull(void) -{ - const char *path = "some__nn_test_file.info2"; - MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; - Matrix weights = {.buffer=weightBuffer, .rows=3, .cols=2}; - MatrixType biasBuffer[] = {7, 8, 9}; - Matrix biases = {.buffer=biasBuffer, .rows=3, .cols=1}; - Layer layers[] = {{.weights=weights, .biases=biases}}; - - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; - NeuralNetwork netUnderTest; - - prepareNeuralNetworkFile(path, expectedNet); - - netUnderTest = loadModel(path); - remove(path); - - TEST_ASSERT_NOT_NULL(netUnderTest.layers); - TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); - clearModel(&netUnderTest); - TEST_ASSERT_NULL(netUnderTest.layers); - TEST_ASSERT_EQUAL_INT(0, netUnderTest.numberOfLayers); -} - -static void someActivation(Matrix *matrix) -{ - for(int i = 0; i < matrix->rows * matrix->cols; i++) - { - matrix->buffer[i] = fabs(matrix->buffer[i]); + /* Biases (MatrixType binär) */ + if (bcount > 0 && layer.biases.buffer != NULL) { + fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, fptr); } + + /* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */ + if (i + 1 < nn.numberOfLayers) { + int nextOutput = (int)nn.layers[i + 1].weights.rows; + fwrite(&nextOutput, sizeof(int), 1, fptr); + } else { + /* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0 + schreiben. loadModel liest dann outputDimension = 0 und beendet die + Schleife. */ + int zero = 0; + fwrite(&zero, sizeof(int), 1, fptr); + } + } + + fclose(fptr); } -void test_predictReturnsCorrectLabels(void) -{ - const unsigned char expectedLabels[] = {4, 2}; - GrayScalePixelType imageBuffer1[] = {10, 30, 25, 17}; - GrayScalePixelType imageBuffer2[] = {20, 40, 10, 128}; - GrayScaleImage inputImages[] = {{.buffer=imageBuffer1, .width=2, .height=2}, {.buffer=imageBuffer2, .width=2, .height=2}}; - MatrixType weightsBuffer1[] = {1, -2, 3, -4, 5, -6, 7, -8}; - MatrixType weightsBuffer2[] = {-9, 10, 11, 12, 13, 14}; - MatrixType weightsBuffer3[] = {-15, 16, 17, 18, -19, 20, 21, 22, 23, -24, 25, 26, 27, -28, -29}; - Matrix weights1 = {.buffer=weightsBuffer1, .rows=2, .cols=4}; - Matrix weights2 = {.buffer=weightsBuffer2, .rows=3, .cols=2}; - Matrix weights3 = {.buffer=weightsBuffer3, .rows=5, .cols=3}; - MatrixType biasBuffer1[] = {200, 0}; - MatrixType biasBuffer2[] = {0, -100, 0}; - MatrixType biasBuffer3[] = {0, -1000, 0, 2000, 0}; - Matrix biases1 = {.buffer=biasBuffer1, .rows=2, .cols=1}; - Matrix biases2 = {.buffer=biasBuffer2, .rows=3, .cols=1}; - Matrix biases3 = {.buffer=biasBuffer3, .rows=5, .cols=1}; - Layer layers[] = {{.weights=weights1, .biases=biases1, .activation=someActivation}, \ - {.weights=weights2, .biases=biases2, .activation=someActivation}, \ - {.weights=weights3, .biases=biases3, .activation=someActivation}}; - NeuralNetwork netUnderTest = {.layers=layers, .numberOfLayers=3}; - unsigned char *predictedLabels = predict(netUnderTest, inputImages, 2); - TEST_ASSERT_NOT_NULL(predictedLabels); - int n = (int)(sizeof(expectedLabels) / sizeof(expectedLabels[0])); - TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedLabels, predictedLabels, n); - free(predictedLabels); +void test_loadModelReturnsCorrectNumberOfLayers(void) { + const char *path = "some__nn_test_file.info2"; + MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; + MatrixType buffer2[] = {1, 2, 3, 4, 5, 6}; + Matrix weights1 = {.buffer = buffer1, .rows = 3, .cols = 2}; + Matrix weights2 = {.buffer = buffer2, .rows = 2, .cols = 3}; + MatrixType buffer3[] = {1, 2, 3}; + MatrixType buffer4[] = {1, 2}; + Matrix biases1 = {.buffer = buffer3, .rows = 3, .cols = 1}; + Matrix biases2 = {.buffer = buffer4, .rows = 2, .cols = 1}; + Layer layers[] = {{.weights = weights1, .biases = biases1}, + {.weights = weights2, .biases = biases2}}; + + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 2}; + NeuralNetwork netUnderTest; + + prepareNeuralNetworkFile(path, expectedNet); + + netUnderTest = loadModel(path); + remove(path); + + TEST_ASSERT_EQUAL_INT(expectedNet.numberOfLayers, + netUnderTest.numberOfLayers); + clearModel(&netUnderTest); +} + +void test_loadModelReturnsCorrectWeightDimensions(void) { + const char *path = "some__nn_test_file.info2"; + MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; + Matrix weights = {.buffer = weightBuffer, .rows = 3, .cols = 2}; + MatrixType biasBuffer[] = {7, 8, 9}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; + + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; + NeuralNetwork netUnderTest; + + prepareNeuralNetworkFile(path, expectedNet); + + netUnderTest = loadModel(path); + remove(path); + + TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); + TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.rows, + netUnderTest.layers[0].weights.rows); + TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.cols, + netUnderTest.layers[0].weights.cols); + clearModel(&netUnderTest); +} + +void test_loadModelReturnsCorrectBiasDimensions(void) { + const char *path = "some__nn_test_file.info2"; + MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; + Matrix weights = {.buffer = weightBuffer, .rows = 3, .cols = 2}; + MatrixType biasBuffer[] = {7, 8, 9}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; + + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; + NeuralNetwork netUnderTest; + + prepareNeuralNetworkFile(path, expectedNet); + + netUnderTest = loadModel(path); + remove(path); + + TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); + TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].biases.rows, + netUnderTest.layers[0].biases.rows); + TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].biases.cols, + netUnderTest.layers[0].biases.cols); + clearModel(&netUnderTest); +} + +void test_loadModelReturnsCorrectWeights(void) { + const char *path = "some__nn_test_file.info2"; + MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; + Matrix weights = {.buffer = weightBuffer, .rows = 3, .cols = 2}; + MatrixType biasBuffer[] = {7, 8, 9}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; + + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; + NeuralNetwork netUnderTest; + + prepareNeuralNetworkFile(path, expectedNet); + + netUnderTest = loadModel(path); + remove(path); + + TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); + TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.rows, + netUnderTest.layers[0].weights.rows); + TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.cols, + netUnderTest.layers[0].weights.cols); + int n = + netUnderTest.layers[0].weights.rows * netUnderTest.layers[0].weights.cols; + TEST_ASSERT_EQUAL_INT_ARRAY(expectedNet.layers[0].weights.buffer, + netUnderTest.layers[0].weights.buffer, n); + clearModel(&netUnderTest); +} + +void test_loadModelReturnsCorrectBiases(void) { + const char *path = "some__nn_test_file.info2"; + MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; + Matrix weights = {.buffer = weightBuffer, .rows = 3, .cols = 2}; + MatrixType biasBuffer[] = {7, 8, 9}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; + + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; + NeuralNetwork netUnderTest; + + prepareNeuralNetworkFile(path, expectedNet); + + netUnderTest = loadModel(path); + remove(path); + + TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); + TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.rows, + netUnderTest.layers[0].weights.rows); + TEST_ASSERT_EQUAL_INT(expectedNet.layers[0].weights.cols, + netUnderTest.layers[0].weights.cols); + int n = + netUnderTest.layers[0].biases.rows * netUnderTest.layers[0].biases.cols; + TEST_ASSERT_EQUAL_INT_ARRAY(expectedNet.layers[0].biases.buffer, + netUnderTest.layers[0].biases.buffer, n); + clearModel(&netUnderTest); +} + +void test_loadModelFailsOnWrongFileTag(void) { + const char *path = "some_nn_test_file.info2"; + NeuralNetwork netUnderTest; + FILE *file = fopen(path, "wb"); + + if (file != NULL) { + const char *fileTag = "info2_neural_network_file_format"; + + fwrite(fileTag, sizeof(char), strlen(fileTag), file); + + fclose(file); + } + + netUnderTest = loadModel(path); + + remove(path); + + TEST_ASSERT_NULL(netUnderTest.layers); + TEST_ASSERT_EQUAL_INT(0, netUnderTest.numberOfLayers); +} + +void test_clearModelSetsMembersToNull(void) { + const char *path = "some__nn_test_file.info2"; + MatrixType weightBuffer[] = {1, 2, 3, 4, 5, 6}; + Matrix weights = {.buffer = weightBuffer, .rows = 3, .cols = 2}; + MatrixType biasBuffer[] = {7, 8, 9}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; + + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; + NeuralNetwork netUnderTest; + + prepareNeuralNetworkFile(path, expectedNet); + + netUnderTest = loadModel(path); + remove(path); + + TEST_ASSERT_NOT_NULL(netUnderTest.layers); + TEST_ASSERT_TRUE(netUnderTest.numberOfLayers > 0); + clearModel(&netUnderTest); + TEST_ASSERT_NULL(netUnderTest.layers); + TEST_ASSERT_EQUAL_INT(0, netUnderTest.numberOfLayers); +} + +static void someActivation(Matrix *matrix) { + for (int i = 0; i < matrix->rows * matrix->cols; i++) { + matrix->buffer[i] = fabs(matrix->buffer[i]); + } +} + +void test_predictReturnsCorrectLabels(void) { + const unsigned char expectedLabels[] = {4, 2}; + GrayScalePixelType imageBuffer1[] = {10, 30, 25, 17}; + GrayScalePixelType imageBuffer2[] = {20, 40, 10, 128}; + GrayScaleImage inputImages[] = { + {.buffer = imageBuffer1, .width = 2, .height = 2}, + {.buffer = imageBuffer2, .width = 2, .height = 2}}; + MatrixType weightsBuffer1[] = {1, -2, 3, -4, 5, -6, 7, -8}; + MatrixType weightsBuffer2[] = {-9, 10, 11, 12, 13, 14}; + MatrixType weightsBuffer3[] = {-15, 16, 17, 18, -19, 20, 21, 22, + 23, -24, 25, 26, 27, -28, -29}; + Matrix weights1 = {.buffer = weightsBuffer1, .rows = 2, .cols = 4}; + Matrix weights2 = {.buffer = weightsBuffer2, .rows = 3, .cols = 2}; + Matrix weights3 = {.buffer = weightsBuffer3, .rows = 5, .cols = 3}; + MatrixType biasBuffer1[] = {200, 0}; + MatrixType biasBuffer2[] = {0, -100, 0}; + MatrixType biasBuffer3[] = {0, -1000, 0, 2000, 0}; + Matrix biases1 = {.buffer = biasBuffer1, .rows = 2, .cols = 1}; + Matrix biases2 = {.buffer = biasBuffer2, .rows = 3, .cols = 1}; + Matrix biases3 = {.buffer = biasBuffer3, .rows = 5, .cols = 1}; + Layer layers[] = { + {.weights = weights1, .biases = biases1, .activation = someActivation}, + {.weights = weights2, .biases = biases2, .activation = someActivation}, + {.weights = weights3, .biases = biases3, .activation = someActivation}}; + NeuralNetwork netUnderTest = {.layers = layers, .numberOfLayers = 3}; + unsigned char *predictedLabels = predict(netUnderTest, inputImages, 2); + TEST_ASSERT_NOT_NULL(predictedLabels); + int n = (int)(sizeof(expectedLabels) / sizeof(expectedLabels[0])); + TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedLabels, predictedLabels, n); + free(predictedLabels); } void setUp(void) { - // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden + // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden } void tearDown(void) { - // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden + // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden } -int main() -{ - UNITY_BEGIN(); +int main() { + UNITY_BEGIN(); - printf("\n============================\nNeural network tests\n============================\n"); - RUN_TEST(test_loadModelReturnsCorrectNumberOfLayers); - RUN_TEST(test_loadModelReturnsCorrectWeightDimensions); - RUN_TEST(test_loadModelReturnsCorrectBiasDimensions); - RUN_TEST(test_loadModelReturnsCorrectWeights); - RUN_TEST(test_loadModelReturnsCorrectBiases); - RUN_TEST(test_loadModelFailsOnWrongFileTag); - RUN_TEST(test_clearModelSetsMembersToNull); - RUN_TEST(test_predictReturnsCorrectLabels); + printf("\n============================\nNeural network " + "tests\n============================\n"); + RUN_TEST(test_loadModelReturnsCorrectNumberOfLayers); + RUN_TEST(test_loadModelReturnsCorrectWeightDimensions); + RUN_TEST(test_loadModelReturnsCorrectBiasDimensions); + RUN_TEST(test_loadModelReturnsCorrectWeights); + RUN_TEST(test_loadModelReturnsCorrectBiases); + RUN_TEST(test_loadModelFailsOnWrongFileTag); + RUN_TEST(test_clearModelSetsMembersToNull); + RUN_TEST(test_predictReturnsCorrectLabels); - return UNITY_END(); + return UNITY_END(); } \ No newline at end of file