Compare commits

15 Commits
8 changed files with 393 additions and 175 deletions
+33
View File
@@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/mnist.exe",
"args": ["mnist_test.info2","mnist_model.info2"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
Binary file not shown.
+76 -1
View File
@@ -11,12 +11,87 @@
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path) GrayScaleImageSeries *readImages(const char *path)
{ {
GrayScaleImageSeries *series = NULL; FILE *file = fopen(path, "rb");
if (file == NULL)
{
return NULL;
}
// Überprüfe den Header
char fileTag[strlen(FILE_HEADER_STRING)];
fread(fileTag, sizeof(fileTag[0]), strlen(FILE_HEADER_STRING), file);
if (strcmp(fileTag, FILE_HEADER_STRING) != 0)
{
fclose(file);
return NULL;
}
// Lese die Metadaten: Anzahl der Bilder, Breite und Höhe
unsigned short numberOfImages, width, height;
fread(&numberOfImages, sizeof(numberOfImages), 1, file);
fread(&width, sizeof(width), 1, file);
fread(&height, sizeof(height), 1, file);
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
if (series == NULL)
{
fclose(file);
return NULL;
}
series->count = numberOfImages;
series->images = (GrayScaleImage *)malloc(numberOfImages * sizeof(GrayScaleImage));
series->labels = (unsigned char *)malloc(numberOfImages * sizeof(unsigned char));
if (series->images == NULL || series->labels == NULL)
{
free(series);
fclose(file);
return NULL;
}
for (int i = 0; i < numberOfImages; i++)
{
series->images[i].width = width;
series->images[i].height = height;
series->images[i].buffer = (GrayScalePixelType *)malloc(width * height * sizeof(GrayScalePixelType));
if (series->images[i].buffer == NULL)
{
// Fehlerbehandlung: Speicher freigeben, wenn malloc fehlschlägt
for (int j = 0; j < i; j++)
{
free(series->images[j].buffer);
}
free(series->images);
free(series->labels);
free(series);
fclose(file);
return NULL;
}
// Lese die Pixel-Daten und das Label
fread(series->images[i].buffer, sizeof(GrayScalePixelType), width * height, file);
fread(&series->labels[i], sizeof(unsigned char), 1, file);
}
fclose(file);
return series; return series;
} }
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series) void clearSeries(GrayScaleImageSeries *series)
{ {
if (series == NULL)
return;
for (int i = 0; i < series->count; i++)
{
free(series->images[i].buffer); // Speicher für das Bild freigeben
}
free(series->images); // Speicher für die Bild-Array freigeben
free(series->labels); // Speicher für die Labels freigeben
free(series); // Speicher freigeben
} }
+2 -2
View File
@@ -54,7 +54,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);
@@ -70,7 +70,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);
+46 -18
View File
@@ -6,13 +6,13 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m = {0, 0, NULL}; Matrix m = {NULL, 0, 0};
if (rows > 0 && cols > 0) if (rows > 0 && cols > 0)
{ {
m.buffer = malloc(rows * cols * sizeof(MatrixType));
m.rows = rows; m.rows = rows;
m.cols = cols; m.cols = cols;
m.buffer = malloc(rows * cols * sizeof(int));
} }
return m; return m;
@@ -56,31 +56,58 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0}; Matrix result = {0};
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) int broadcast_case =
(matrix1.cols == 1 && matrix1.rows == matrix2.rows) ||
(matrix2.cols == 1 && matrix1.rows == matrix2.rows);
if (!broadcast_case && (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols))
{ {
return result; return result;
} }
result.rows = matrix1.rows;
result.cols = matrix1.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
// wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) // Broadcasting
if (result.buffer == NULL)
{ {
result.rows = result.cols = 0; result = createMatrix(matrix2.rows, matrix2.cols);
return result; for (unsigned int i = 0; i < matrix1.rows; i++)
}
// Matritzenaddition
for (unsigned int i = 0; i < result.rows; i++)
{
for (unsigned int j = 0; j < result.cols; j++)
{ {
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; for (unsigned int j = 0; j < result.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j];
}
}
}
else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows)
{
result = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix2.rows; i++)
{
for (unsigned int j = 0; j < result.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i];
}
} }
} }
else
{
result = createMatrix(matrix1.rows, matrix1.cols);
// Elementweise Addition
for (unsigned int i = 0; i < result.rows; i++)
{
for (unsigned int j = 0; j < result.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
}
return result; return result;
} }
@@ -88,7 +115,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0}; Matrix result = {0};
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) if (matrix1.cols != matrix2.rows)
{ {
return result; return result;
} }
@@ -106,6 +133,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
} }
// Matritzenmultiplikation // Matritzenmultiplikation
for (int r = 0; r < result.rows; r++) // Zeile in Ergebnis for (int r = 0; r < result.rows; r++) // Zeile in Ergebnis
{ {
for (int m = 0; m < result.cols; m++) // Spalte in Ergebnis for (int m = 0; m < result.cols; m++) // Spalte in Ergebnis
+1 -1
View File
@@ -9,9 +9,9 @@ typedef float MatrixType;
typedef struct typedef struct
{ {
MatrixType *buffer; // Zeiger auf die Matrixdaten
unsigned int rows; // Anzahl der Zeilen unsigned int rows; // Anzahl der Zeilen
unsigned int cols; // Anzahl der Spalten unsigned int cols; // Anzahl der Spalten
MatrixType *buffer; // Zeiger auf die Matrixdaten
} Matrix; } Matrix;
+27
View File
@@ -71,6 +71,32 @@ 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};
@@ -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);
+56 -1
View File
@@ -8,7 +8,62 @@
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 Erstellen der Testdatei");
exit(EXIT_FAILURE);
}
// File header
const char *fileTag = "__info2_neural_network_file_format__";
fwrite(fileTag, strlen(fileTag), 1, file);
if (nn.numberOfLayers == 0)
{
unsigned int zero = 0;
fwrite(&zero, sizeof(unsigned int), 1, file);
fclose(file);
return;
}
// first layer dimension
unsigned int in = nn.layers[0].weights.cols;
unsigned int out = nn.layers[0].weights.rows;
fwrite(&in, sizeof(unsigned int), 1, file);
fwrite(&out, sizeof(unsigned int), 1, file);
// do all layers
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
{
const Layer *L = &nn.layers[i];
// Write weights matrix
fwrite(L->weights.buffer,
sizeof(MatrixType),
L->weights.rows * L->weights.cols,
file);
// Write biases matrix
fwrite(L->biases.buffer,
sizeof(MatrixType),
L->biases.rows * L->biases.cols,
file);
// After layer i, write dimension of next layer
if (i + 1 < nn.numberOfLayers)
{
unsigned int nextOut = nn.layers[i+1].weights.rows;
fwrite(&nextOut, sizeof(unsigned int), 1, file);
}
}
// --- 5. Write terminating zero ---
unsigned int zero = 0;
fwrite(&zero, sizeof(unsigned int), 1, file);
fclose(file);
} }
void test_loadModelReturnsCorrectNumberOfLayers(void) void test_loadModelReturnsCorrectNumberOfLayers(void)