Compare commits

...
17 Commits
Author SHA1 Message Date
bergerma98875 b5f493bd43 Kommentare überarbeitet 2025-11-17 08:21:02 +01:00
muellerla101249 53a7828cf0 prepareNeuralNetworkFile angepasst, sodass Test PASS läuft 2025-11-16 22:00:43 +01:00
muellerla101249 28a968e2c9 test_clearSeriesFreesMemory 2025-11-16 21:22:56 +01:00
wehnerla99053 86d225f2d8 imageInputTests.c erweitert 2025-11-16 15:53:56 +01:00
wehnerla99053 c212109a27 finale Änderungen 2025-11-15 21:55:42 +01:00
wehnerla99053 5f068f1337 finale Änderungen 2025-11-15 21:51:57 +01:00
wehnerla99053 05fbd80b8c Anpassung neuralNetworkTests 2025-11-15 21:31:11 +01:00
muellerla101249 6d162b313c clearSeries und Kommentare ergänzt 2025-11-13 10:48:09 +01:00
bergerma98875 c8b14cefae Merge remote-tracking branch 'origin/main' 2025-11-10 15:28:45 +01:00
bergerma98875 47a54fb567 Fehler beim Einlesen der Bilddaten behoben 2025-11-10 15:28:39 +01:00
muellerla101249 150cce7f8c unsigned int zu size_t geändert (matrix.c) 2025-11-10 15:06:50 +01:00
muellerla101249 24f547ccec Matrix Struktur: Reihenfolge geändert 2025-11-10 14:51:50 +01:00
bergerma98875 4020cb36a1 Fehler beim Einlesen der Bilddaten behoben 2025-11-10 14:48:45 +01:00
muellerla101249 1e3e0fc0bb Matrix-Tests laufen alle PASS 2025-11-10 14:41:25 +01:00
muellerla101249 444067262e matrix.data zu matrix.buffer umgenannt, da Tests diese Bezeichnung erwarten. Test für matrix.c laufen bis auf test_createMatrixFailsOnZeroDimensions:FAIL. Expected NULL 2025-11-10 14:16:29 +01:00
bergerma98875 13daa63648 Merge remote-tracking branch 'origin/main' 2025-11-10 14:10:09 +01:00
bergerma98875 ee34744fef imageInputTests.c Update aus Moodle 2025-11-10 14:09:42 +01:00
7 changed files with 205 additions and 60 deletions
+29 -20
View File
@@ -6,7 +6,6 @@
#define BUFFER_SIZE 100 #define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__" #define FILE_HEADER_STRING "__info2_image_file_format__"
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
// Lädt eine .info2-Datei und prüft, ob das Dateiformat korrekt ist // Lädt eine .info2-Datei und prüft, ob das Dateiformat korrekt ist
static FILE* openImageFile(const char* path) { static FILE* openImageFile(const char* path) {
FILE* imageFile = NULL; FILE* imageFile = NULL;
@@ -19,7 +18,7 @@ static FILE* openImageFile(const char* path) {
// Prüfe, ob die Datei mit dem richtigen String beginnt // Prüfe, ob die Datei mit dem richtigen String beginnt
char *fileHeaderString = (char*)malloc(BUFFER_SIZE); char *fileHeaderString = (char*)malloc(BUFFER_SIZE);
fread(fileHeaderString, 1, strlen(FILE_HEADER_STRING) + 1, imageFile); fread(fileHeaderString, 1, strlen(FILE_HEADER_STRING), imageFile);
if (strncmp(fileHeaderString, FILE_HEADER_STRING, 27)) { if (strncmp(fileHeaderString, FILE_HEADER_STRING, 27)) {
// Datei hat nicht das korrekte Format // Datei hat nicht das korrekte Format
@@ -42,23 +41,19 @@ static int getInformationOfImages(FILE* imageFile, int dimensionsOfImages[], Gra
// Daten einlesen // Daten einlesen
int itemsRead = fread(bytestream, 2, 3, imageFile); int itemsRead = fread(bytestream, 2, 3, imageFile);
//for (size_t i = 0; i < (itemsRead * 2); i++) {
// printf("%02X ", bytestream[i]);
//}
if (itemsRead != 3) { if (itemsRead != 3) {
// Nicht genügend Daten gelesen // Nicht genügend Daten gelesen
return -1; return -1;
} }
// Anzahl der Bilder auslesen // Anzahl der Bilder auslesen
series->count = (unsigned int)bytestream[0] << 8 | bytestream[1]; series->count = (unsigned int)bytestream[1] << 8 | bytestream[0];
// Breite der Bilder auslesen // Breite der Bilder auslesen
dimensionsOfImages[0] = (unsigned int)bytestream[2] << 8 | bytestream[3]; dimensionsOfImages[0] = (unsigned int)bytestream[3] << 8 | bytestream[2];
// Höhe der Bilder auslesen // Höhe der Bilder auslesen
dimensionsOfImages[1] = (unsigned int)bytestream[4] << 8 | bytestream[5]; dimensionsOfImages[1] = (unsigned int)bytestream[5] << 8 | bytestream[4];
return 0; return 0;
} }
@@ -99,11 +94,9 @@ static char getLabelOfImage(FILE* imageFile, GrayScaleImage* image) {
return label; return label;
} }
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path) GrayScaleImageSeries *readImages(const char *path)
{ {
// Datei laden und Dateiformat prüfen // Datei laden und Dateiformat prüfen
// setzt außerdem den Pointer an der Stelle nach dem Prestring
FILE* imageFile = openImageFile(path); FILE* imageFile = openImageFile(path);
if (imageFile == NULL) { if (imageFile == NULL) {
@@ -184,20 +177,36 @@ GrayScaleImageSeries *readImages(const char *path)
} }
} }
// für jedes Bild gemäß GreyScaleImageSeries.count:
// GreyScaleImage.width = width
// GreyScaleImage.height = height
// GreyScaleImageSeries.buffer = Pixelwerte
// labels = Label des Bilds
// series.labels[i] = (unsigned char)(i % 256);
// Springe in der Datei an die Stelle nach dem i. Bild
fclose(imageFile); fclose(imageFile);
return series; return series;
} }
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series) void clearSeries(GrayScaleImageSeries *series)
{ {
//prüfen, ob series überhaupt bereinigt werden muss
if(series == NULL){
return;
}
//images freigeben und NULL setzen -> jeden Index der Images durchgehen und buffer freigeben
if(series->images != NULL){
for(unsigned int i = 0; i < series->count; i++){
if (series->images[i].buffer != NULL){
free(series->images[i].buffer);
series->images[i].buffer = NULL;
}
}
free(series->images); // wenn alle images bereinigt sind, den Zeiger Images selbst bereinigen
series->images = NULL;
}
//labels freigeben und NULL setzen
if(series->labels != NULL){
free(series->labels);
series->labels = NULL;
}
//series freigeben
free(series);
} }
@@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include "unity.h" #include "unity.h"
#include "imageInput.h" #include "imageInput.h"
#define FILE_HEADER_STRING "__info2_image_file_format__"
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label) static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
@@ -64,6 +65,19 @@ static void prepareImageFileIncorrectTag(const char *path, unsigned short int wi
} }
} }
void test_readImagesFailsOnIncompleteHeader(void) {
const char *path = "testIncompleteHeader.info2";
FILE *file = fopen(path, "wb");
if (file) {
fwrite(FILE_HEADER_STRING, 1, strlen(FILE_HEADER_STRING), file);
unsigned char incompleteHeader[2] = {0x01, 0x00};
fwrite(incompleteHeader, 1, 2, file);
fclose(file);
}
TEST_ASSERT_NULL(readImages(path));
remove(path);
}
void test_readImagesReturnsCorrectNumberOfImages(void) void test_readImagesReturnsCorrectNumberOfImages(void)
{ {
@@ -83,7 +97,7 @@ void test_readImagesReturnsCorrectImageWidth(void)
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
const unsigned short expectedWidth = 10; const unsigned short expectedWidth = 10;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, 8, expectedWidth, 2, 1); prepareImageFile(path, expectedWidth, 8, 2, 1);
series = readImages(path); series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_NOT_NULL(series->images);
@@ -99,7 +113,7 @@ void test_readImagesReturnsCorrectImageHeight(void)
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
const unsigned short expectedHeight = 10; const unsigned short expectedHeight = 10;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, expectedHeight, 8, 2, 1); prepareImageFile(path, 8, expectedHeight, 2, 1);
series = readImages(path); series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_NOT_NULL(series->images);
@@ -159,6 +173,37 @@ void test_openImageFileIncorrectTag(void)
remove(path); remove(path);
} }
void test_clearSeriesFreesMemory(void)
{
// Dummy-Series erstellen
GrayScaleImageSeries *series = (GrayScaleImageSeries*)malloc(sizeof(GrayScaleImageSeries));
series->count = 2;
// Labels allokieren
series->labels = (unsigned char*)malloc(series->count * sizeof(unsigned char));
for (unsigned int i = 0; i < series->count; i++) {
series->labels[i] = (unsigned char)(i + 1);
}
// Images allokieren
series->images = (GrayScaleImage*)malloc(series->count * sizeof(GrayScaleImage));
for (unsigned int i = 0; i < series->count; i++) {
series->images[i].width = 4;
series->images[i].height = 4;
series->images[i].buffer = (GrayScalePixelType*)malloc(series->images[i].width * series->images[i].height);
for (unsigned int j = 0; j < series->images[i].width * series->images[i].height; j++){
series->images[i].buffer[j] = 0;
}
}
// clearSeries aufrufen
clearSeries(series);
// Test
TEST_PASS();
}
void setUp(void) { void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
} }
@@ -179,6 +224,8 @@ int main()
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath); RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
RUN_TEST(test_readImagesFailsOnWrongFileTag); RUN_TEST(test_readImagesFailsOnWrongFileTag);
RUN_TEST(test_openImageFileIncorrectTag); RUN_TEST(test_openImageFileIncorrectTag);
RUN_TEST(test_readImagesFailsOnIncompleteHeader);
RUN_TEST(test_clearSeriesFreesMemory);
return UNITY_END(); return UNITY_END();
} }
+41 -26
View File
@@ -10,78 +10,93 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
Matrix matrix; Matrix matrix;
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
matrix.data = (float *)malloc(rows * cols * sizeof(MatrixType)); if(matrix.rows == 0 || matrix.cols == 0){ // prüfen, ob ungültige Dimensionen übergeben wurden, wenn ja, "error matrix" erstellen
if (matrix.data != NULL) { matrix.rows = 0;
matrix.cols = 0;
matrix.buffer = NULL;
}
else { // für gültige Dimensionen: Speicher allokieren
matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType));
} }
return matrix; return matrix;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix->data != NULL) { if (matrix->buffer != NULL) {
free(matrix->data); free(matrix->buffer);
matrix->data = NULL; matrix->buffer = NULL;
} }
matrix->rows = 0; matrix->rows = 0;
matrix->cols = 0; matrix->cols = 0;
} }
void setMatrixAt(MatrixType value, Matrix* matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(rowIdx >= matrix->rows || colIdx >= matrix->cols){ if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ // Gültigkeit der Dimensionen prüfen
return; return;
} }
matrix->data[rowIdx * matrix->cols + colIdx] = value; matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // value an vorgebenener Stelle setzen
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ // Gültigkeit der Dimensionen prüfen
return 0; return 0;
} }
MatrixType value; MatrixType value;
value = matrix.data[rowIdx * matrix.cols + colIdx]; value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // value an vorgegebener Stelle auslesen
return value; return value;
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { // Matrixen können nur addiert werden, sofern sie jeweils die gleiche Anzahl Spalten und Zeilen haben size_t rows = (matrix1.rows > matrix2.rows) ? matrix1.rows : matrix2.rows;
Matrix errorMatrix = createMatrix(0, 0); size_t cols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols;
errorMatrix.data = NULL;
return errorMatrix; // Prüfen, ob Broadcasting möglich ist
if (!((matrix1.rows == rows || matrix1.rows == 1) &&
(matrix2.rows == rows || matrix2.rows == 1) &&
(matrix1.cols == cols || matrix1.cols == 1) &&
(matrix2.cols == cols || matrix2.cols == 1))) {
Matrix errorMatrix = createMatrix(0, 0);
return errorMatrix;
} }
Matrix result = createMatrix(matrix1.rows, matrix1.cols); Matrix result = createMatrix(rows, cols);
for (unsigned int i = 0; i < matrix1.rows; i++) {
for (unsigned int j = 0; j < matrix1.cols; j++) { for (size_t i = 0; i < rows; i++) {
MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); for (size_t j = 0; j < cols; j++) {
setMatrixAt(sum, &result, i, j); MatrixType val1 = matrix1.buffer[(i % matrix1.rows) * matrix1.cols + (j % matrix1.cols)];
} MatrixType val2 = matrix2.buffer[(i % matrix2.rows) * matrix2.cols + (j % matrix2.cols)];
result.buffer[i * cols + j] = val1 + val2;
}
} }
return result; return result;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
if (matrix1.cols != matrix2.rows){ if (matrix1.cols != matrix2.rows){ // prüfen, ob Matrizen multipliziert werden dürfen
Matrix errorMatrix = createMatrix(0, 0); Matrix errorMatrix = createMatrix(0, 0);
errorMatrix.data = NULL; errorMatrix.buffer = NULL;
return errorMatrix; return errorMatrix;
} }
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols); Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols); // Ergebnis-Matrix erstellen
// Algorithmus für Multiplikation
for( size_t i = 0; i < matrix1.rows; i++){ for( size_t i = 0; i < matrix1.rows; i++){
for(size_t j = 0; j < matrix2.cols; j++){ for(size_t j = 0; j < matrix2.cols; j++){
MatrixType sum = 0; MatrixType sum = 0;
for(size_t k = 0; k < matrix1.cols; k++){ for(size_t k = 0; k < matrix1.cols; k++){
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j); sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
} }
setMatrixAt(sum, &matrix3, i, j); setMatrixAt(sum, matrix3, i, j);
} }
} }
+1 -1
View File
@@ -4,9 +4,9 @@
#define UNDEFINED_MATRIX_VALUE 0 #define UNDEFINED_MATRIX_VALUE 0
typedef struct{ typedef struct{
float* buffer;
size_t rows; size_t rows;
size_t cols; size_t cols;
float* buffer;
} Matrix; } Matrix;
+30 -3
View File
@@ -21,7 +21,7 @@ void test_createMatrixReturnsCorrectMatrixDimensions(void)
{ {
const unsigned int expectedRows = 15; const unsigned int expectedRows = 15;
const unsigned int expectedCols = 10; const unsigned int expectedCols = 10;
Matrix matrixToTest = createMatrix(expectedRows, expectedCols); Matrix matrixToTest = createMatrix(expectedRows, expectedCols);
TEST_ASSERT_EQUAL_UINT32(expectedRows, matrixToTest.rows); TEST_ASSERT_EQUAL_UINT32(expectedRows, matrixToTest.rows);
TEST_ASSERT_EQUAL_UINT32(expectedCols, matrixToTest.cols); TEST_ASSERT_EQUAL_UINT32(expectedCols, matrixToTest.cols);
@@ -63,7 +63,7 @@ void test_addFailsOnDifferentInputDimensions(void)
MatrixType buffer2[] = {7, 8, 9, 10, 11, 12}; MatrixType buffer2[] = {7, 8, 9, 10, 11, 12};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=3, .cols=2, .buffer=buffer2}; Matrix matrix2 = {.rows=3, .cols=2, .buffer=buffer2};
Matrix result = add(matrix1, matrix2); Matrix result = add(matrix1, matrix2);
TEST_ASSERT_NULL(result.buffer); TEST_ASSERT_NULL(result.buffer);
@@ -71,13 +71,39 @@ void test_addFailsOnDifferentInputDimensions(void)
TEST_ASSERT_EQUAL_UINT32(0, result.cols); TEST_ASSERT_EQUAL_UINT32(0, result.cols);
} }
void test_addSupportsBroadcasting(void)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
MatrixType buffer2[] = {7, 8};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2};
Matrix result1 = add(matrix1, matrix2);
Matrix result2 = add(matrix2, matrix1);
float expectedResults[] = {8, 9, 10, 12, 13, 14};
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows);
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols);
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result2.rows);
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result2.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result1.rows * result1.cols);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result1.buffer, result1.cols * result1.rows);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result2.rows * result2.cols);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows);
free(result1.buffer);
free(result2.buffer);
}
void test_multiplyReturnsCorrectResults(void) void test_multiplyReturnsCorrectResults(void)
{ {
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
MatrixType buffer2[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; MatrixType buffer2[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=3, .cols=4, .buffer=buffer2}; Matrix matrix2 = {.rows=3, .cols=4, .buffer=buffer2};
Matrix result = multiply(matrix1, matrix2); Matrix result = multiply(matrix1, matrix2);
float expectedResults[] = {74, 80, 86, 92, 173, 188, 203, 218}; float expectedResults[] = {74, 80, 86, 92, 173, 188, 203, 218};
@@ -159,6 +185,7 @@ int main()
RUN_TEST(test_clearMatrixSetsMembersToNull); RUN_TEST(test_clearMatrixSetsMembersToNull);
RUN_TEST(test_addReturnsCorrectResult); RUN_TEST(test_addReturnsCorrectResult);
RUN_TEST(test_addFailsOnDifferentInputDimensions); RUN_TEST(test_addFailsOnDifferentInputDimensions);
RUN_TEST(test_addSupportsBroadcasting);
RUN_TEST(test_multiplyReturnsCorrectResults); RUN_TEST(test_multiplyReturnsCorrectResults);
RUN_TEST(test_multiplyFailsOnWrongInputDimensions); RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
RUN_TEST(test_getMatrixAtReturnsCorrectResult); RUN_TEST(test_getMatrixAtReturnsCorrectResult);
+13 -7
View File
@@ -67,14 +67,14 @@ static unsigned int readDimension(FILE *file)
if(fread(&dimension, sizeof(int), 1, file) != 1) if(fread(&dimension, sizeof(int), 1, file) != 1)
dimension = 0; dimension = 0;
return dimension; return dimension;
} }
static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols) static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols)
{ {
Matrix matrix = createMatrix(rows, cols); Matrix matrix = createMatrix(rows, cols);
if(matrix.buffer != NULL) 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)
@@ -118,6 +118,10 @@ static void assignActivations(NeuralNetwork model)
if(model.numberOfLayers > 0) if(model.numberOfLayers > 0)
model.layers[model.numberOfLayers-1].activation = softmax; model.layers[model.numberOfLayers-1].activation = softmax;
} }
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "neuralNetwork.h"
NeuralNetwork loadModel(const char *path) NeuralNetwork loadModel(const char *path)
{ {
@@ -128,7 +132,7 @@ NeuralNetwork loadModel(const char *path)
{ {
if(checkFileHeader(file)) if(checkFileHeader(file))
{ {
unsigned int inputDimension = readDimension(file); unsigned int inputDimension = readDimension(file);
unsigned int outputDimension = readDimension(file); unsigned int outputDimension = readDimension(file);
while(inputDimension > 0 && outputDimension > 0) while(inputDimension > 0 && outputDimension > 0)
@@ -142,7 +146,7 @@ NeuralNetwork loadModel(const char *path)
clearModel(&model); clearModel(&model);
break; break;
} }
layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer)); layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer));
if(layerBuffer != NULL) if(layerBuffer != NULL)
@@ -168,6 +172,7 @@ NeuralNetwork loadModel(const char *path)
return model; return model;
} }
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count) static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
{ {
Matrix matrix = {NULL, 0, 0}; Matrix matrix = {NULL, 0, 0};
@@ -201,7 +206,7 @@ static Matrix forward(const NeuralNetwork model, Matrix inputBatch)
{ {
Matrix biasResult; Matrix biasResult;
Matrix weightResult; Matrix weightResult;
weightResult = multiply(model.layers[i].weights, result); weightResult = multiply(model.layers[i].weights, result);
clearMatrix(&result); clearMatrix(&result);
biasResult = add(model.layers[i].biases, weightResult); biasResult = add(model.layers[i].biases, weightResult);
@@ -248,12 +253,13 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[],
Matrix outputBatch = forward(model, inputBatch); Matrix outputBatch = forward(model, inputBatch);
unsigned char *result = argmax(outputBatch); unsigned char *result = argmax(outputBatch);
clearMatrix(&outputBatch); clearMatrix(&outputBatch);
return result; return result;
} }
void clearModel(NeuralNetwork *model) void clearModel(NeuralNetwork *model)
{ {
if(model != NULL) if(model != NULL)
@@ -8,7 +8,48 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{ {
// TODO FILE *file = fopen(path, "wb");
if (!file) {
perror("Fehler beim Öffnen der Datei");
return;
}
// Header schreiben
const char *header = "__info2_neural_network_file_format__";
fwrite(header, sizeof(char), strlen(header), file);
// Alle Layer schreiben
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
Layer layer = nn.layers[i];
if(i == 0){
// beim ersten Layer inputDim und outputDim schreiben
unsigned int inputDim = layer.weights.cols; // Anzahl Eingänge
fwrite(&inputDim, sizeof(unsigned int), 1, file);
unsigned int outputDim = layer.weights.rows; // Anzahl Ausgänge
fwrite(&outputDim, sizeof(unsigned int), 1, file);
}
else{
// bei allen anderen Layern nur outputDim schreiben
unsigned int outputDim = layer.weights.rows; // Anzahl Ausgänge
fwrite(&outputDim, sizeof(unsigned int), 1, file);
}
// Gewichte schreiben
size_t weightCount = layer.weights.rows * layer.weights.cols;
fwrite(layer.weights.buffer, sizeof(MatrixType), weightCount, file);
// Biases schreiben
size_t biasCount = layer.biases.rows * layer.biases.cols;
fwrite(layer.biases.buffer, sizeof(MatrixType), biasCount, file);
}
// End-Marker (outputDim = 0)
unsigned int zero = 0;
fwrite(&zero, sizeof(unsigned int), 1, file);
fclose(file);
} }
void test_loadModelReturnsCorrectNumberOfLayers(void) void test_loadModelReturnsCorrectNumberOfLayers(void)