Compare commits

17 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
hallerni98888 07f217f1f4 merge upstream 2025-11-11 13:20:47 +00:00
hallerni98888 d4375c31e8 changes? 2025-11-11 14:09:06 +01:00
schroederen 0fc70f982c Merge pull request 'Aufgabenstellung zur Matrixfunktion add() präzisiert. Unittest hinzugefügt.' (#3) from schroederen/info2Praktikum-NeuronalesNetz:main into main
Reviewed-on: freudenreichan/info2Praktikum-NeuronalesNetz#3
2025-11-11 10:09:21 +00:00
schroederen b271c865cb Aufgabenstellung zur Matrixfunktion add() präzisiert. Unittest hinzugefügt. 2025-11-11 11:08:51 +01:00
Simon 67c5110e5c image input prototyp 2025-11-08 18:23:13 +01:00
Simon e40a5cbd7b matrix pass all tests 2025-11-08 15:33:11 +01:00
hallerni98888 42e92f278f Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/hallerni98888/info2Praktikum-NeuronalesNetz 2025-11-08 15:28:39 +01:00
hallerni98888 14388a5637 multiply fix 2025-11-08 15:23:56 +01:00
paulusja 077c6def78 Fix unit tests. 2025-11-05 12:05:29 +01:00
8 changed files with 393 additions and 172 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.
+96 -21
View File
@@ -1,22 +1,97 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
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;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
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;
const unsigned short expectedWidth = 10;
const char *path = "testFile.info2";
prepareImageFile(path, 8, expectedWidth, 2, 1);
prepareImageFile(path, expectedWidth, 8, 2, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images);
@@ -70,7 +70,7 @@ void test_readImagesReturnsCorrectImageHeight(void)
GrayScaleImageSeries *series = NULL;
const unsigned short expectedHeight = 10;
const char *path = "testFile.info2";
prepareImageFile(path, expectedHeight, 8, 2, 1);
prepareImageFile(path, 8, expectedHeight, 2, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images);
+153 -122
View File
@@ -1,123 +1,154 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix m = {0, 0, NULL};
if (rows > 0 && cols > 0)
{
m.rows = rows;
m.cols = cols;
m.buffer = malloc(rows * cols * sizeof(int));
}
return m;
}
void clearMatrix(Matrix *matrix)
{
if (matrix == NULL)
{
return;
}
// Speicher freigeben, falls vorhanden
free(matrix->buffer);
matrix->buffer = NULL;
// Metadaten zurücksetzen
matrix->rows = 0;
matrix->cols = 0;
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // setzte Matrix auf den Wert value am Punkt (row col)
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
MatrixType value = 0;
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col)
}
return value;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix result = {0};
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
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 (result.buffer == NULL)
{
result.rows = result.cols = 0;
return result;
}
// 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];
}
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix result = {0};
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
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 (result.buffer == NULL)
{
result.rows = result.cols = 0;
return result;
}
// Matritzenmultiplikation
for (unsigned int i = 0; i < result.rows; i++)
{
for (unsigned int j = 0; j < result.cols; j++)
{
MatrixType sum = 0;
for (unsigned int k = 0; k < matrix1.cols; k++)
{
sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
}
result.buffer[i * result.cols + j] = sum;
}
}
return result;
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix m = {NULL, 0, 0};
if (rows > 0 && cols > 0)
{
m.buffer = malloc(rows * cols * sizeof(MatrixType));
m.rows = rows;
m.cols = cols;
}
return m;
}
void clearMatrix(Matrix *matrix)
{
if (matrix == NULL)
{
return;
}
// Speicher freigeben, falls vorhanden
free(matrix->buffer);
matrix->buffer = NULL;
// Metadaten zurücksetzen
matrix->rows = 0;
matrix->cols = 0;
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // setzte Matrix auf den Wert value am Punkt (row col)
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
MatrixType value = 0;
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col)
}
return value;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix result = {0};
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;
}
if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) // Broadcasting
{
result = createMatrix(matrix2.rows, matrix2.cols);
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];
}
}
}
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;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix result = {0};
if (matrix1.cols != matrix2.rows)
{
return result;
}
result.rows = matrix1.rows;
result.cols = matrix2.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
// wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
if (result.buffer == NULL)
{
result.rows = result.cols = 0;
return result;
}
// Matritzenmultiplikation
for (int r = 0; r < result.rows; r++) // Zeile in Ergebnis
{
for (int m = 0; m < result.cols; m++) // Spalte in Ergebnis
{
MatrixType sum = 0;
for (int n = 0; n < matrix1.cols; n++)
{
sum += matrix1.buffer[r * matrix1.cols + n] *
matrix2.buffer[n * matrix2.cols + m];
}
result.buffer[r * result.cols + m] = sum;
}
}
return result;
}
+26 -26
View File
@@ -1,26 +1,26 @@
#ifndef MATRIX_H
#define MATRIX_H
#define UNDEFINED_MATRIX_VALUE 0
typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct
{
unsigned int rows; // Anzahl der Zeilen
unsigned int cols; // Anzahl der Spalten
MatrixType *buffer; // Zeiger auf die Matrixdaten
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif
#ifndef MATRIX_H
#define MATRIX_H
#define UNDEFINED_MATRIX_VALUE 0
typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct
{
MatrixType *buffer; // Zeiger auf die Matrixdaten
unsigned int rows; // Anzahl der Zeilen
unsigned int cols; // Anzahl der Spalten
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif
+27
View File
@@ -71,6 +71,32 @@ void test_addFailsOnDifferentInputDimensions(void)
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)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
@@ -159,6 +185,7 @@ int main()
RUN_TEST(test_clearMatrixSetsMembersToNull);
RUN_TEST(test_addReturnsCorrectResult);
RUN_TEST(test_addFailsOnDifferentInputDimensions);
RUN_TEST(test_addSupportsBroadcasting);
RUN_TEST(test_multiplyReturnsCorrectResults);
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
+56 -1
View File
@@ -8,7 +8,62 @@
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)