forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
12
Commits
d4375c31e8
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06109225f8 | ||
|
|
7837be1c3e | ||
|
|
44f0bfc16d | ||
|
|
ff21ecab21 | ||
|
|
7f5291deca | ||
|
|
dadcdc873d | ||
|
|
9e7fcca725 | ||
|
|
da8dc2a9ef | ||
|
|
07f217f1f4 | ||
|
|
0fc70f982c | ||
|
|
b271c865cb | ||
|
|
077c6def78 |
Vendored
+33
@@ -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.
+28
-27
@@ -11,23 +11,20 @@
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
// GrayScaleImageSeries *series = NULL;
|
||||
|
||||
// return series;
|
||||
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (file == NULL) {
|
||||
return NULL; // Datei existiert nicht oder konnte nicht geöffnet werden
|
||||
if (file == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Überprüfe den Dateitag
|
||||
char fileTag[25];
|
||||
fread(fileTag, sizeof(fileTag[0]), 24, file);
|
||||
fileTag[24] = '\0'; // Null-Terminierung des Strings
|
||||
// Überprüfe den Header
|
||||
char fileTag[strlen(FILE_HEADER_STRING)];
|
||||
fread(fileTag, sizeof(fileTag[0]), strlen(FILE_HEADER_STRING), file);
|
||||
|
||||
if (strcmp(fileTag, "__info2_image_file_format__") != 0) {
|
||||
if (strcmp(fileTag, FILE_HEADER_STRING) != 0)
|
||||
{
|
||||
fclose(file);
|
||||
return NULL; // Ungültiges Dateiformat
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 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(&height, sizeof(height), 1, file);
|
||||
|
||||
// Speicher für die Bildserie und die Bilddaten allozieren
|
||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||
if (series == NULL) {
|
||||
if (series == NULL)
|
||||
{
|
||||
fclose(file);
|
||||
return NULL; // Speicher konnte nicht allokiert werden
|
||||
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) {
|
||||
if (series->images == NULL || series->labels == NULL)
|
||||
{
|
||||
free(series);
|
||||
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].height = height;
|
||||
series->images[i].buffer = (GrayScalePixelType *)malloc(width * height * sizeof(GrayScalePixelType));
|
||||
|
||||
if (series->images[i].buffer == NULL) {
|
||||
// Fehlerbehandlung: Speicher freigeben, wenn Allocation fehlschlägt
|
||||
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);
|
||||
@@ -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
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (series == NULL) return;
|
||||
if (series == NULL)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < series->count; i++) {
|
||||
free(series->images[i].buffer); // Speicher für das Bild freigeben
|
||||
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);
|
||||
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
@@ -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);
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix m = {0, 0, NULL};
|
||||
Matrix m = {NULL, 0, 0};
|
||||
|
||||
if (rows > 0 && cols > 0)
|
||||
{
|
||||
m.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||
m.rows = rows;
|
||||
m.cols = cols;
|
||||
m.buffer = malloc(rows * cols * sizeof(int));
|
||||
}
|
||||
|
||||
return m;
|
||||
@@ -56,31 +56,58 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) // Broadcasting
|
||||
{
|
||||
result.rows = result.cols = 0;
|
||||
result = createMatrix(matrix2.rows, matrix2.cols);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Matritzenaddition
|
||||
for (unsigned int i = 0; i < result.rows; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < result.cols; j++)
|
||||
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ typedef float MatrixType;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
MatrixType *buffer; // Zeiger auf die Matrixdaten
|
||||
unsigned int rows; // Anzahl der Zeilen
|
||||
unsigned int cols; // Anzahl der Spalten
|
||||
MatrixType *buffer; // Zeiger auf die Matrixdaten
|
||||
} Matrix;
|
||||
|
||||
|
||||
|
||||
@@ -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
@@ -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)
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user