Compare commits

...

18 Commits
main ... main

Author SHA1 Message Date
Björn
e17eaf4542 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/turtschinba100320/info2Praktikum-NeuronalesNetzBastiBjoern 2025-11-18 01:53:20 +01:00
Björn
6c26652744 matrix fehler behoben 2025-11-18 01:52:43 +01:00
Bastian
5d8dbd548b Merging Problem gelöst 2025-11-18 01:47:38 +01:00
Bastian
225dbac29f Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/turtschinba100320/info2Praktikum-NeuronalesNetzBastiBjoern 2025-11-18 01:46:01 +01:00
Bastian
c476386ff3 Fehler gefunden und Ausgebessert 2025-11-18 01:43:20 +01:00
Björn
a031bb0b7a main aktiv 2025-11-17 23:35:58 +01:00
Bastian
545acd0356 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/turtschinba100320/info2Praktikum-NeuronalesNetzBastiBjoern 2025-11-17 23:29:55 +01:00
Björn
934ba2d06e NeuralNetworkTest fertig? 2025-11-17 23:02:54 +01:00
Björn
8c2ed38abf NNTest nur noch predict-Fehler 2025-11-17 20:19:35 +01:00
Björn
86359bb37f matrix vollständig 2025-11-17 18:30:06 +01:00
Bastian
807bbbd375 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/turtschinba100320/info2Praktikum-NeuronalesNetzBastiBjoern 2025-11-17 17:37:43 +01:00
Björn
8ac27e20c5 Matrix Funktion an Test angepasst 2025-11-17 13:46:22 +01:00
Björn
e3349f7f68 Matrix überarbeitet 2025-11-17 13:31:48 +01:00
Bastian
9817c87e7a Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/turtschinba100320/info2Praktikum-NeuronalesNetzBastiBjoern 2025-11-16 15:20:25 +01:00
Björn
8c98197327 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/turtschinba100320/info2Praktikum-NeuronalesNetzBastiBjoern 2025-11-14 19:25:20 +01:00
Björn
8368439db1 Grundkonzept Matrix 2025-11-14 19:25:16 +01:00
Bastian
c1db7c612f Datei wird follständig ausgelesen 2025-11-13 17:07:20 +01:00
Bastian
97bf884e59 Angefangen bildanzahl und groese aus binaerdatei zu lesen 2025-11-12 21:02:20 +01:00
8 changed files with 254 additions and 22 deletions

View File

@ -5,18 +5,95 @@
#define BUFFER_SIZE 100 #define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__" #define FILE_HEADER_STRING "__info2_image_file_format__"
#define FILE_HEADER_SIZE (sizeof(FILE_HEADER_STRING)-1)
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei // TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
FILE *checkFile(const char *path)
{
FILE *datei = fopen(path,"rb");
if (datei == NULL)
{
perror("Datei konnte nicht geoeffnet werden");
return NULL;
}
char buffer[FILE_HEADER_SIZE+1];
if (fread(buffer,1,FILE_HEADER_SIZE,datei)!=FILE_HEADER_SIZE)
{
perror("Header konnte nicht eingelessen werden");
fclose(datei);
return NULL;
}
buffer[FILE_HEADER_SIZE] = '\0';
if (strcmp(buffer,FILE_HEADER_STRING)!=0)
{
printf("Falscher Dateikopf");
//printf("\n%s",buffer);
//printf("\n%s",FILE_HEADER_STRING);
//printf("\n%d",strcmp(buffer,FILE_HEADER_STRING));
fclose(datei);
return NULL;
}
return datei;
}
// 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)
{ {
FILE *datei = checkFile(path);
if (datei==NULL)
{
return NULL;
}
unsigned short image_count, width, height;
fread(&image_count,sizeof(unsigned short),1,datei);
fread(&width,sizeof(unsigned short),1,datei);
fread(&height,sizeof(unsigned short),1,datei);
//printf("%u Bilder und %u mal %u",image_count,width,height);
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
series = malloc(sizeof(GrayScaleImageSeries));
series->count = image_count;
series->images = malloc(image_count*sizeof(GrayScaleImage));
series->labels = malloc(image_count*sizeof(unsigned char));
for(unsigned short i = 0;i<image_count;i++)
{
series->images[i].width = width;
series->images[i].height = height;
series->images[i].buffer = malloc(width*height);
}
for(unsigned short i = 0;i<image_count;i++)
{
for (unsigned int j=0;j<(width*height);j++)
{
fread(&series->images[i].buffer[j],1,1,datei);
}
fread(&series->labels[i],1,1,datei);
//printf("%d\n",series->labels[i]);
}
fclose(datei);
return series; return series;
} }
// 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)
{
printf("Serie nicht vorhanden\n");
return;
}
unsigned short anzahl = series->count;
for(unsigned short i = 0;i<anzahl;i++)
{
free(series->images[i].buffer );
}
free(series->images);
free(series->labels);
free(series);
printf("Serie freigegeben\n");
return;
} }

View File

@ -19,5 +19,5 @@ typedef struct
GrayScaleImageSeries *readImages(const char *path); GrayScaleImageSeries *readImages(const char *path);
void clearSeries(GrayScaleImageSeries *series); void clearSeries(GrayScaleImageSeries *series);
FILE *checkFile(const char *path);
#endif #endif

View File

@ -54,7 +54,7 @@ void test_readImagesReturnsCorrectImageWidth(void)
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
const unsigned short expectedWidth = 10; const unsigned short expectedWidth = 10;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, 8, expectedWidth, 2, 1); prepareImageFile(path, expectedWidth, 8, 2, 1);
series = readImages(path); series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_NOT_NULL(series->images);
@ -70,7 +70,7 @@ void test_readImagesReturnsCorrectImageHeight(void)
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
const unsigned short expectedHeight = 10; const unsigned short expectedHeight = 10;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, expectedHeight, 8, 2, 1); prepareImageFile(path,8, expectedHeight, 2, 1);
series = readImages(path); series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_NOT_NULL(series->images);

4
main.c
View File

@ -1,3 +1,4 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "imageInput.h" #include "imageInput.h"
@ -6,6 +7,8 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
//readImages("mnist_test.info2");
const unsigned int windowWidth = 800; const unsigned int windowWidth = 800;
const unsigned int windowHeight = 600; const unsigned int windowHeight = 600;
@ -65,4 +68,5 @@ int main(int argc, char *argv[])
} }
return exitCode; return exitCode;
} }

105
matrix.c
View File

@ -1,35 +1,128 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
#include <stdio.h>
// TODO Matrix-Funktionen implementieren
// Matrix erzeugen
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix matrix;
matrix.buffer = NULL;
matrix.rows = 0;
matrix.cols = 0;
if (rows == 0 || cols == 0)
return matrix; // leere Matrix
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (!matrix.buffer)
return matrix; // Speicher konnte nicht reserviert werden
matrix.rows = rows;
matrix.cols = cols;
// Initialisiere alle Werte auf UNDEFINED_MATRIX_VALUE
for (unsigned int i = 0; i < rows * cols; i++)
matrix.buffer[i] = UNDEFINED_MATRIX_VALUE;
return matrix;
} }
// Matrix Speicher freigeben
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (!matrix) return;
if (matrix->buffer)
free(matrix->buffer);
matrix->buffer = NULL;
matrix->rows = 0;
matrix->cols = 0;
} }
// Wert setzen
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (!matrix.buffer) return;
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return;
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
} }
// Wert auslesen
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (!matrix.buffer) return UNDEFINED_MATRIX_VALUE;
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return UNDEFINED_MATRIX_VALUE;
return matrix.buffer[rowIdx * matrix.cols + colIdx];
} }
// Matrizen addieren
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix m1, const Matrix m2)
{ {
if (!m1.buffer || !m2.buffer) return createMatrix(0,0);
} // gleiche Dimension
if (m1.rows == m2.rows && m1.cols == m2.cols)
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = createMatrix(m1.rows, m1.cols);
if (!result.buffer) return result;
for (unsigned int r = 0; r < m1.rows; r++)
for (unsigned int c = 0; c < m1.cols; c++)
result.buffer[r * result.cols + c] = m1.buffer[r * m1.cols + c] + m2.buffer[r * m2.cols + c];
return result;
}
// Matrix2 ist ein Spaltenvektor
if (m1.rows == m2.rows && m2.cols == 1)
{
Matrix result = createMatrix(m1.rows, m1.cols);
if (!result.buffer) return result;
for (unsigned int r = 0; r < m1.rows; r++)
for (unsigned int c = 0; c < m1.cols; c++)
result.buffer[r * result.cols + c] = m1.buffer[r * m1.cols + c] + m2.buffer[r];
return result;
}
// Matrix1 ist ein Spaltenvektor
if (m1.rows == m2.rows && m1.cols == 1)
{
Matrix result = createMatrix(m2.rows, m2.cols);
if (!result.buffer) return result;
for (unsigned int r = 0; r < m2.rows; r++)
for (unsigned int c = 0; c < m2.cols; c++)
result.buffer[r * result.cols + c] = m1.buffer[r] + m2.buffer[r * m2.cols + c];
return result;
}
// passt nicht
return createMatrix(0,0);
}
// Matrizen multiplizieren
Matrix multiply(const Matrix m1, const Matrix m2)
{
if (!m1.buffer || !m2.buffer) return createMatrix(0,0);
if (m1.cols != m2.rows) return createMatrix(0,0);
Matrix result = createMatrix(m1.rows, m2.cols);
if (!result.buffer) return result;
for (unsigned int r = 0; r < m1.rows; r++)
for (unsigned int c = 0; c < m2.cols; c++)
{
MatrixType sum = 0;
for (unsigned int k = 0; k < m1.cols; k++)
sum += m1.buffer[r * m1.cols + k] * m2.buffer[k * m2.cols + c];
result.buffer[r * result.cols + c] = sum;
}
return result;
} }

View File

@ -6,6 +6,13 @@
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct
{
MatrixType *buffer;
unsigned int rows;
unsigned int cols;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);

View File

@ -8,7 +8,58 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{ {
// TODO // TODO : Fehlerbehandlung
// Öffne die Datei zum Schreiben im Binärmodus
FILE *file = fopen(path, "wb");
if (!file) return;
// Schreibe den Datei-Tag
const char *tag = "__info2_neural_network_file_format__";
fwrite(tag, 1, strlen(tag), file);
// Schreibe die Anzahl der Layer
if (nn.numberOfLayers == 0) {
fclose(file);
return;
}
// Schreibe die Eingabe- und Ausgabegrößen des Netzwerks
int input = nn.layers[0].weights.cols;
int output = nn.layers[0].weights.rows;
fwrite(&input, sizeof(int), 1, file);
fwrite(&output, sizeof(int), 1, file);
// Schreibe die Layer-Daten
for (int i = 0; i < nn.numberOfLayers; i++)
{
const Layer *layer = &nn.layers[i];
int out = layer->weights.rows;
int in = layer->weights.cols;
fwrite(layer->weights.buffer, sizeof(MatrixType), out * in, file);
fwrite(layer->biases.buffer, sizeof(MatrixType), out * 1, file);
if (i + 1 < nn.numberOfLayers)
{
int nextOut = nn.layers[i + 1].weights.rows;
fwrite(&nextOut, sizeof(int), 1, file);
}
}
fclose(file);
// Debuging-Ausgabe
printf("prepareNeuralNetworkFile: Datei '%s' erstellt mit %u Layer(n)\n", path, nn.numberOfLayers);
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
Layer layer = nn.layers[i];
printf("Layer %u: weights (%u x %u), biases (%u x %u)\n",
i, layer.weights.rows, layer.weights.cols, layer.biases.rows, layer.biases.cols);
}
} }
void test_loadModelReturnsCorrectNumberOfLayers(void) void test_loadModelReturnsCorrectNumberOfLayers(void)