From 1aaec0e5f20ff037f609153d54f0f3d4a11c313a Mon Sep 17 00:00:00 2001 From: Tobias Linz Date: Wed, 4 Nov 2026 19:34:28 +0100 Subject: [PATCH] Implement prepareNeuralNetworkFile --- Startcode/neuralNetwork.c | 79 +++++++++++++++++++++++++----- Startcode/neuralNetwork.h | 1 + Startcode/neuralNetworkTests.c | 89 +++++++++++++++++----------------- 3 files changed, 112 insertions(+), 57 deletions(-) diff --git a/Startcode/neuralNetwork.c b/Startcode/neuralNetwork.c index bd8f164..4d860ba 100644 --- a/Startcode/neuralNetwork.c +++ b/Startcode/neuralNetwork.c @@ -52,8 +52,8 @@ static int checkFileHeader(FILE *file) int fileHeaderLen = strlen(FILE_HEADER_STRING); char buffer[BUFFER_SIZE] = {0}; - if(BUFFER_SIZE-1 < fileHeaderLen) - fileHeaderLen = BUFFER_SIZE-1; + if(BUFFER_SIZE - 1 < fileHeaderLen) + fileHeaderLen = BUFFER_SIZE - 1; if(fread(buffer, sizeof(char), fileHeaderLen, file) == fileHeaderLen) isValid = strcmp(buffer, FILE_HEADER_STRING) == 0; @@ -67,17 +67,17 @@ static unsigned int readDimension(FILE *file) 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) + if(fread(matrix.buffer, sizeof(MatrixType), rows * cols, file) != rows * cols) clearMatrix(&matrix); } @@ -110,13 +110,13 @@ static void clearLayer(Layer *layer) static void assignActivations(NeuralNetwork model) { - for(int i = 0; i < (int)model.numberOfLayers-1; i++) + 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; + model.layers[model.numberOfLayers - 1].activation = softmax; } NeuralNetwork loadModel(const char *path) @@ -128,7 +128,7 @@ NeuralNetwork loadModel(const char *path) { if(checkFileHeader(file)) { - unsigned int inputDimension = readDimension(file); + unsigned int inputDimension = readDimension(file); unsigned int outputDimension = readDimension(file); while(inputDimension > 0 && outputDimension > 0) @@ -142,7 +142,7 @@ NeuralNetwork loadModel(const char *path) clearModel(&model); break; } - + layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer)); if(layerBuffer != NULL) @@ -168,6 +168,59 @@ NeuralNetwork loadModel(const char *path) return model; } +static void writeFileHeader(FILE *file) +{ + fwrite(FILE_HEADER_STRING, sizeof(char), strlen(FILE_HEADER_STRING), file); +} + +static void writeDimension(FILE *file, const unsigned int dimension) +{ + fwrite(&dimension, sizeof(unsigned int), 1, file); +} + +static void writeMatrix(FILE *file, const Matrix matrix) +{ + fwrite(matrix.buffer, sizeof(MatrixType), matrix.rows * matrix.cols, file); +} + +static void writeLayer(FILE *file, const Layer layer) +{ + writeMatrix(file, layer.weights); + writeMatrix(file, layer.biases); +} + +void saveModel(const char *path, const NeuralNetwork nn) +{ + FILE *file = fopen(path, "wb"); + if(file == NULL) + { + return; + } + + writeFileHeader(file); + + if(nn.numberOfLayers > 0) + { + const unsigned int inputDimension = nn.layers[0].weights.cols; + writeDimension(file, inputDimension); + + for(unsigned int i = 0; i < nn.numberOfLayers; i++) + { + const Layer layer = nn.layers[i]; + + const unsigned int outputDimension = layer.weights.rows; + + writeDimension(file, outputDimension); + + writeLayer(file, layer); + } + } + + writeDimension(file, 0); + + fclose(file); +} + static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count) { Matrix matrix = {NULL, 0, 0}; @@ -201,7 +254,7 @@ static Matrix forward(const NeuralNetwork model, Matrix inputBatch) { Matrix biasResult; Matrix weightResult; - + weightResult = multiply(model.layers[i].weights, result); clearMatrix(&result); biasResult = add(model.layers[i].biases, weightResult); @@ -248,9 +301,9 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[], Matrix outputBatch = forward(model, inputBatch); unsigned char *result = argmax(outputBatch); - + clearMatrix(&outputBatch); - + return result; } @@ -265,4 +318,4 @@ void clearModel(NeuralNetwork *model) model->layers = NULL; model->numberOfLayers = 0; } -} \ No newline at end of file +} diff --git a/Startcode/neuralNetwork.h b/Startcode/neuralNetwork.h index 7f06607..6b97a60 100644 --- a/Startcode/neuralNetwork.h +++ b/Startcode/neuralNetwork.h @@ -20,6 +20,7 @@ typedef struct } NeuralNetwork; NeuralNetwork loadModel(const char *path); +void saveModel(const char *path, const NeuralNetwork nn); unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[], unsigned int numberOfImages); void clearModel(NeuralNetwork *model); diff --git a/Startcode/neuralNetworkTests.c b/Startcode/neuralNetworkTests.c index 21ab370..e99cfdf 100644 --- a/Startcode/neuralNetworkTests.c +++ b/Startcode/neuralNetworkTests.c @@ -5,10 +5,9 @@ #include "unity.h" #include "neuralNetwork.h" - static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { - // TODO + saveModel(path, nn); } void test_loadModelReturnsCorrectNumberOfLayers(void) @@ -16,15 +15,15 @@ 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}; + 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}}; + 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 expectedNet = {.layers = layers, .numberOfLayers = 2}; NeuralNetwork netUnderTest; prepareNeuralNetworkFile(path, expectedNet); @@ -40,12 +39,12 @@ 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}; + 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}}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; NeuralNetwork netUnderTest; prepareNeuralNetworkFile(path, expectedNet); @@ -63,12 +62,12 @@ 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}; + 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}}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; NeuralNetwork netUnderTest; prepareNeuralNetworkFile(path, expectedNet); @@ -86,12 +85,12 @@ 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}; + 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}}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; NeuralNetwork netUnderTest; prepareNeuralNetworkFile(path, expectedNet); @@ -111,12 +110,12 @@ 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}; + 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}}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; NeuralNetwork netUnderTest; prepareNeuralNetworkFile(path, expectedNet); @@ -159,12 +158,12 @@ 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}; + 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}}; + Matrix biases = {.buffer = biasBuffer, .rows = 3, .cols = 1}; + Layer layers[] = {{.weights = weights, .biases = biases}}; - NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=1}; + NeuralNetwork expectedNet = {.layers = layers, .numberOfLayers = 1}; NeuralNetwork netUnderTest; prepareNeuralNetworkFile(path, expectedNet); @@ -192,23 +191,23 @@ 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}}; + 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}; + 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}; + 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])); @@ -216,12 +215,14 @@ void test_predictReturnsCorrectLabels(void) free(predictedLabels); } -void setUp(void) { - // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden +void setUp(void) +{ + // TODO: Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden } -void tearDown(void) { - // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden +void tearDown(void) +{ + // TODO: Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden } int main() @@ -239,4 +240,4 @@ int main() RUN_TEST(test_predictReturnsCorrectLabels); return UNITY_END(); -} \ No newline at end of file +}