forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
6
Commits
9e7fcca725
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06109225f8 | ||
|
|
7837be1c3e | ||
|
|
44f0bfc16d | ||
|
|
ff21ecab21 | ||
|
|
7f5291deca | ||
|
|
dadcdc873d |
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
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
+29
-28
@@ -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,32 +33,34 @@ 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
|
||||
for (int j = 0; j < i; j++)
|
||||
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
|
||||
}
|
||||
@@ -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;
|
||||
@@ -65,23 +65,11 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
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;
|
||||
}
|
||||
|
||||
if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) // Broadcasting
|
||||
{
|
||||
result.rows = matrix2.rows;
|
||||
result.cols = matrix2.cols;
|
||||
|
||||
result = createMatrix(matrix2.rows, matrix2.cols);
|
||||
|
||||
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < result.cols; j++)
|
||||
@@ -95,8 +83,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows)
|
||||
{
|
||||
result.rows = matrix1.rows;
|
||||
result.cols = matrix1.cols;
|
||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
for (unsigned int i = 0; i < matrix2.rows; i++)
|
||||
{
|
||||
@@ -109,6 +96,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
else
|
||||
{
|
||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
// Elementweise Addition
|
||||
for (unsigned int i = 0; i < result.rows; i++)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
|
||||
+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)
|
||||
|
||||
Reference in New Issue
Block a user