Compare commits

8 Commits
Author SHA1 Message Date
Simon May 06109225f8 debugged 2025-11-18 15:13:23 +01:00
hallerni98888 7837be1c3e done? 2025-11-14 17:25:07 +01:00
Simon 44f0bfc16d sync 2025-11-14 13:01:03 +01:00
Simon May ff21ecab21 nochmal? 2025-11-14 12:36:47 +01:00
Simon May 7f5291deca Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/hallerni98888/info2Praktikum-NeuronalesNetz 2025-11-11 15:58:58 +01:00
Simon May dadcdc873d pass 2025-11-11 15:58:53 +01:00
hallerni98888 9e7fcca725 matrix.c unitTest PASS 2025-11-11 15:53:35 +01:00
hallerni98888 da8dc2a9ef matrixAdd check 2025-11-11 15:34:34 +01:00
6 changed files with 163 additions and 47 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
}
]
}
]
}
+25 -24
View File
@@ -11,23 +11,20 @@
// 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;
// return series;
FILE *file = fopen(path, "rb"); FILE *file = fopen(path, "rb");
if (file == NULL) { if (file == NULL)
return NULL; // Datei existiert nicht oder konnte nicht geöffnet werden {
return NULL;
} }
// Überprüfe den Dateitag // Überprüfe den Header
char fileTag[25]; char fileTag[strlen(FILE_HEADER_STRING)];
fread(fileTag, sizeof(fileTag[0]), 24, file); fread(fileTag, sizeof(fileTag[0]), strlen(FILE_HEADER_STRING), file);
fileTag[24] = '\0'; // Null-Terminierung des Strings
if (strcmp(fileTag, "__info2_image_file_format__") != 0) { if (strcmp(fileTag, FILE_HEADER_STRING) != 0)
{
fclose(file); fclose(file);
return NULL; // Ungültiges Dateiformat return NULL;
} }
// Lese die Metadaten: Anzahl der Bilder, Breite und Höhe // Lese die Metadaten: Anzahl der Bilder, Breite und Höhe
@@ -36,31 +33,33 @@ GrayScaleImageSeries *readImages(const char *path)
fread(&width, sizeof(width), 1, file); fread(&width, sizeof(width), 1, file);
fread(&height, sizeof(height), 1, file); fread(&height, sizeof(height), 1, file);
// Speicher für die Bildserie und die Bilddaten allozieren
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries)); GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
if (series == NULL) { if (series == NULL)
{
fclose(file); fclose(file);
return NULL; // Speicher konnte nicht allokiert werden return NULL;
} }
series->count = numberOfImages; series->count = numberOfImages;
series->images = (GrayScaleImage *)malloc(numberOfImages * sizeof(GrayScaleImage)); series->images = (GrayScaleImage *)malloc(numberOfImages * sizeof(GrayScaleImage));
series->labels = (unsigned char *)malloc(numberOfImages * sizeof(unsigned char)); series->labels = (unsigned char *)malloc(numberOfImages * sizeof(unsigned char));
if (series->images == NULL || series->labels == NULL) { if (series->images == NULL || series->labels == NULL)
{
free(series); free(series);
fclose(file); fclose(file);
return NULL; // Speicher konnte nicht allokiert werden return NULL;
} }
// Lese die Bilddaten und die Labels for (int i = 0; i < numberOfImages; i++)
for (int i = 0; i < numberOfImages; i++) { {
series->images[i].width = width; series->images[i].width = width;
series->images[i].height = height; series->images[i].height = height;
series->images[i].buffer = (GrayScalePixelType *)malloc(width * height * sizeof(GrayScalePixelType)); series->images[i].buffer = (GrayScalePixelType *)malloc(width * height * sizeof(GrayScalePixelType));
if (series->images[i].buffer == NULL) { if (series->images[i].buffer == NULL)
// Fehlerbehandlung: Speicher freigeben, wenn Allocation fehlschlägt {
// Fehlerbehandlung: Speicher freigeben, wenn malloc fehlschlägt
for (int j = 0; j < i; j++) for (int j = 0; j < i; j++)
{ {
free(series->images[j].buffer); free(series->images[j].buffer);
@@ -84,13 +83,15 @@ GrayScaleImageSeries *readImages(const char *path)
// 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; if (series == NULL)
return;
for (int i = 0; i < series->count; i++) { for (int i = 0; i < series->count; i++)
{
free(series->images[i].buffer); // Speicher für das Bild freigeben free(series->images[i].buffer); // Speicher für das Bild freigeben
} }
free(series->images); // Speicher für die Bild-Array freigeben free(series->images); // Speicher für die Bild-Array freigeben
free(series->labels); // Speicher für die Labels freigeben free(series->labels); // Speicher für die Labels freigeben
free(series); free(series); // Speicher freigeben
} }
+38 -11
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,24 +56,48 @@ 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++)
{
for (unsigned int j = 0; j < result.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j];
}
} }
// Matritzenaddition
}
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 i = 0; i < result.rows; i++)
{ {
for (unsigned int j = 0; j < result.cols; j++) for (unsigned int j = 0; j < result.cols; j++)
@@ -81,6 +105,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
} }
} }
}
return result; return result;
} }
+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;
+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)
BIN
View File
Binary file not shown.