18 Commits
Author SHA1 Message Date
Simon Wiesend 3a9d8275a8 fix bug 2025-11-28 08:14:41 +01:00
Simon Wiesend bbb0ea1cf5 Merge branch 'main' into neuralNetworkTests 2025-11-25 13:40:23 +01:00
Simon Wiesend 12825cc1d3 Revert "neuralNetwork fixed". The root cause has been fixed in matrix.h
This reverts commit 6ba9ba3195.
2025-11-25 13:38:56 +01:00
Simon Wiesend 633ee723f4 adapt matrix struct to existing tests 2025-11-25 13:33:20 +01:00
Fabrice 7ea80137b0 Header wird gelesen, läuft alles optimal 2025-11-24 10:35:46 +01:00
uhlmannja101588 6ba9ba3195 neuralNetwork fixed 2025-11-24 08:24:37 +00:00
Fabrice 9c3d9f0a40 imageInput implementiert 2025-11-23 20:54:39 +01:00
uhlmannja101588 f4427d2892 unittests bestanden 2025-11-23 16:38:17 +00:00
uhlmannja101588 84b65525a6 Funktion implementiert / nicht getestet 2025-11-23 12:04:25 +00:00
Simon Wiesend 92ad1e1c31 clean up and improve allocation error handling 2025-11-21 09:09:55 +01:00
Simon Wiesend 6137e45bdb Merge branch 'main' into matrix 2025-11-17 18:35:46 +01:00
schroederen 2436240736 Merge pull request 'matrixTests korrigiert.' (#4) from schroederen/info2Praktikum-NeuronalesNetz:main into main
Reviewed-on: freudenreichan/info2Praktikum-NeuronalesNetz#4
2025-11-17 14:07:46 +00:00
schroederen fde82f2d9a matrixTests korrigiert. 2025-11-17 15:07:03 +01:00
Simon Wiesend 0d7f380d87 implement matmul 2025-11-14 09:17:31 +01:00
Simon Wiesend 645d471860 first implementation of new broadcasting addition requirement 2025-11-11 19:05:03 +01:00
wiesendsi102436 721f5cc2d1 merge upstream 2025-11-11 12:55:12 +00: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
7 changed files with 234 additions and 12 deletions
Binary file not shown.
+94 -4
View File
@@ -6,17 +6,107 @@
#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
//Datei öffnen, Header, Anzahl, Höhe und Breite lesen, geöffnete Datei zurückgeben
static FILE* openAndReadShort (const char *path, unsigned short *count, unsigned short *width, unsigned short *height) {
FILE *file = fopen(path, "rb");
if (!file) {
return NULL;
}
size_t headerLength = strlen(FILE_HEADER_STRING);
char *header = malloc (headerLength + 1);
if(!header) {
return NULL;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
if (fread(header, sizeof(char), headerLength, file) != headerLength) {
free (header);
return NULL;
}
header[headerLength] = '\0';
if (strcmp (header, FILE_HEADER_STRING) != 0) {
free(header);
return NULL;
}
free (header);
fread(count, sizeof(unsigned short), 1, file);
fread(width, sizeof(unsigned short), 1, file);
fread(height, sizeof(unsigned short), 1, file);
return file;
}
//Speicher anlegen und Pixel eines Bildes einlesen
static GrayScaleImage* readPixles (FILE *file, unsigned short *width, unsigned short *height) {
GrayScaleImage *image = malloc (sizeof(GrayScaleImage));
image->width = *width;
image->height = *height;
image->buffer = malloc ((*width) * (*height) * sizeof(GrayScalePixelType));
if (!image->buffer) {
free(image);
return NULL;
}
for (unsigned int i = 0; i < (*width) * (*height); i++) {
unsigned char pixel;
if (fread(&pixel, sizeof(unsigned char), 1, file) != 1) {
free(image->buffer);
free(image);
return NULL;
}
image->buffer[i] = pixel;
}
return image;
}
//Ausführen von openAndReadShort, Anlegen des Speichers für Bilderserie, readPixles wird für jedes Bild ausgeführt
//Nach jedem Bild wird das zugehörige Label gelesen, bei sämtlichen Fehlern wird NULL zurückgegeben und Speicher durch clearSeries bereinigt
GrayScaleImageSeries *readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
unsigned short count = 0, width = 0, height = 0;
FILE *file = openAndReadShort(path, &count, &width, &height);
if (file == 0) {
return NULL;
}
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
if (!series) {
fclose(file);
return NULL;
}
series->count = count;
series->images = malloc(count * sizeof(GrayScaleImage));
series->labels = malloc(count* sizeof(unsigned char));
for (unsigned int i = 0; i < series->count; i++) {
GrayScaleImage *image = readPixles(file, &width, &height);
series->images[i] = *image;
free(image);
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
clearSeries(series);
fclose(file);
return NULL;
}
}
fclose(file);
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
//Bereinigt den Speicher
void clearSeries(GrayScaleImageSeries *series)
{
for (unsigned int i = 0; i < series->count; i++) {
free(series->images[i].buffer);
}
free(series->images);
free(series->labels);
free(series);
}
+73 -4
View File
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
#include <stdio.h>
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
@@ -20,6 +21,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
if (mat.buffer == NULL)
{
clearMatrix(&mat);
perror("could not allocate memory");
}
return mat;
@@ -58,15 +60,57 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix resMat = createMatrix(matrix1.rows, matrix1.cols);
Matrix resMat = (matrix1.cols > matrix2.cols) ? createMatrix(matrix1.rows, matrix1.cols) : createMatrix(matrix2.rows, matrix2.cols);
// clear matrix and return if the dimensions of the input matrices differ from each other
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
if (resMat.buffer == NULL)
{
return createMatrix(0, 0);
}
// matrices not compatible
if (matrix1.rows != matrix2.rows)
{
clearMatrix(&resMat);
return resMat;
}
// check if broadcasting is possible
if (matrix1.cols != matrix2.cols)
{
// matrix1 is a vector
if (matrix1.cols == 1)
{
// broadcast vector
for (size_t m = 0; m < matrix2.rows; m++)
{
for (size_t n = 0; n < matrix2.cols; n++)
{
setMatrixAt(getMatrixAt(matrix2, m, n) + getMatrixAt(matrix1, m, 0), resMat, m, n);
}
}
return resMat;
}
// matrix2 is a vector
else if (matrix2.cols == 1)
{
// broadcast vector
for (size_t m = 0; m < matrix1.rows; m++)
{
for (size_t n = 0; n < matrix1.cols; n++)
{
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, 0), resMat, m, n);
}
}
return resMat;
}
// addition not possible
else
{
clearMatrix(&resMat);
return resMat;
}
}
for (size_t m = 0; m < matrix1.rows; m++)
{
for (size_t n = 0; n < matrix1.cols; n++)
@@ -79,7 +123,32 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return resMat;
}
// TODO implement
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL)
{
return createMatrix(0, 0);
}
int rows = matrix1.rows, cols = matrix2.cols;
Matrix resMat = createMatrix(rows, cols);
if (resMat.buffer == NULL)
{
return createMatrix(0, 0);
}
for (size_t rowIdx = 0; rowIdx < rows; rowIdx++)
{
for (size_t colIdx = 0; colIdx < cols; colIdx++)
{
int curCellVal = 0;
for (size_t k = 0; k < matrix1.cols; k++)
{
curCellVal += getMatrixAt(matrix1, rowIdx, k) * getMatrixAt(matrix2, k, colIdx);
}
setMatrixAt(curCellVal, resMat, rowIdx, colIdx);
}
}
return resMat;
}
+1 -2
View File
@@ -8,10 +8,9 @@ typedef float MatrixType;
// Matrixtyp
typedef struct Matrix
{
MatrixType *buffer;
size_t rows;
size_t cols;
MatrixType *buffer;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
+28 -1
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};
@@ -138,7 +164,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
setMatrixAt(-1, matrixToTest, 2, 3);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType));
}
void setUp(void) {
@@ -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);
+37 -1
View File
@@ -5,10 +5,46 @@
#include "unity.h"
#include "neuralNetwork.h"
static void writeLayer(FILE *file, const Matrix weights, const Matrix biases, unsigned int inputDim)
{
unsigned int outputDim = (unsigned int)weights.rows;
fwrite(&outputDim, sizeof(unsigned int), 1, file);
if (weights.buffer != NULL)
fwrite(weights.buffer, sizeof(MatrixType), outputDim * inputDim, file);
if (biases.buffer != NULL)
fwrite(biases.buffer, sizeof(MatrixType), outputDim, file);
}
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{
// TODO
FILE *file = fopen(path, "wb");
if (!file) return;
const char tag[] = "__info2_neural_network_file_format__";
fwrite(tag, sizeof(char), strlen(tag), file);
if (nn.numberOfLayers == 0)
{
unsigned int zero = 0;
fwrite(&zero, sizeof(unsigned int), 1, file);
fclose(file);
return;
}
unsigned int inputDim = (unsigned int)nn.layers[0].weights.cols;
fwrite(&inputDim, sizeof(unsigned int), 1, file);
for (int i = 0; i < nn.numberOfLayers; i++)
{
writeLayer(file, nn.layers[i].weights, nn.layers[i].biases, inputDim);
inputDim = (unsigned int)nn.layers[i].weights.rows;
}
unsigned int zero = 0;
fwrite(&zero, sizeof(unsigned int), 1, file);
fclose(file);
}
void test_loadModelReturnsCorrectNumberOfLayers(void)
+1
View File
@@ -0,0 +1 @@
some_tag