Compare commits
5
Commits
3e3254ed38
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf36733274 | ||
|
|
5dfce43fa1 | ||
|
|
42aa64d260 | ||
|
|
30b5586a1f | ||
|
|
432e6f9513 |
No files matched your search
+23
-5
@@ -16,18 +16,19 @@ static FILE* openFileCheckHeader(const char *path)
|
|||||||
return NULL;
|
return NULL;
|
||||||
char headerbuffer[BUFFER_SIZE];
|
char headerbuffer[BUFFER_SIZE];
|
||||||
size_t headerlength = strlen(FILE_HEADER_STRING);
|
size_t headerlength = strlen(FILE_HEADER_STRING);
|
||||||
if((fread(headerbuffer, sizeof(char), headerlength, fp)) < headerbuffer)
|
if((fread(headerbuffer, sizeof(char), headerlength, fp)) != headerlength)
|
||||||
{
|
{
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if(strncmp(FILE_HEADER_STRING, headerbuffer, headerlength) != 0)
|
if(strncmp(FILE_HEADER_STRING, headerbuffer, headerlength) != 0)
|
||||||
{
|
{
|
||||||
flcose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return fp;
|
return fp;
|
||||||
}//Oeffnet die Datei. 2. liest aus Datei headerlaenge laenge aus speichert in array vergleicht mit gewolltem header.
|
}
|
||||||
|
//Oeffnet die Datei. 2. liest aus Datei headerlaenge laenge aus speichert in array vergleicht mit gewolltem header.
|
||||||
///Gibt bei Erfolg (header laenge richtig, richtiger header) den FILE* Zeiger zurück welcher nun gespeichert hat
|
///Gibt bei Erfolg (header laenge richtig, richtiger header) den FILE* Zeiger zurück welcher nun gespeichert hat
|
||||||
///dass nun der header Teil gelesen wurde und somit die nächste Funktion ab dieser Stelle weiter liest.
|
///dass nun der header Teil gelesen wurde und somit die nächste Funktion ab dieser Stelle weiter liest.
|
||||||
///NULL, wenn falscher header.
|
///NULL, wenn falscher header.
|
||||||
@@ -61,7 +62,7 @@ static GrayScaleImageSeries* allocateSeries(unsigned int count)
|
|||||||
|
|
||||||
// 2. Prüfen, ob Speicherreservierung nicht funktioniert hat
|
// 2. Prüfen, ob Speicherreservierung nicht funktioniert hat
|
||||||
if (series == NULL) {
|
if (series == NULL) {
|
||||||
return NULL;
|
return NULL;//Wir müssen nichts freen, da wir keinen Speicherplatz reservieren konnten! Gibt nix zu freen
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Initialiseren der Struktur.
|
// 3. Initialiseren der Struktur.
|
||||||
@@ -164,7 +165,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
unsigned int count = 0, width = 0, height = 0;
|
unsigned int count = 0, width = 0, height = 0;
|
||||||
|
|
||||||
// --- Schritt 1: Datei öffnen und Header prüfen ---
|
// --- Schritt 1: Datei öffnen und Header prüfen ---
|
||||||
fp = openAndValidateHeader(path);
|
fp = openFileCheckHeader(path);
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
fprintf(stderr, "Fehler: Datei konnte nicht geöffnet oder validiert werden.\n");
|
fprintf(stderr, "Fehler: Datei konnte nicht geöffnet oder validiert werden.\n");
|
||||||
return NULL; // Kein Aufräumen nötig, da nichts reserviert/geöffnet wurde
|
return NULL; // Kein Aufräumen nötig, da nichts reserviert/geöffnet wurde
|
||||||
@@ -202,4 +203,21 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
//von innen nach außen freen
|
||||||
|
if(series == NULL)
|
||||||
|
{
|
||||||
|
return;//beendet Funktion. Fall series == NULL würde das ausführen der folgenden zu einem segmentation fault führen.
|
||||||
|
}
|
||||||
|
if(series->images != NULL)
|
||||||
|
{
|
||||||
|
for(unsigned int i = 0; i < series->count; i++)
|
||||||
|
{
|
||||||
|
if(series->images[i].buffer != NULL)
|
||||||
|
free(series->images[i].buffer);
|
||||||
|
}
|
||||||
|
free(series->images);
|
||||||
|
}
|
||||||
|
if(series->labels != NULL)
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
}
|
}
|
||||||
Binary file not shown.
@@ -4,32 +4,157 @@
|
|||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
|
//Matrix dimensionieren
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix m; //Struktur anlegen, Varibale m von Typ Matrix
|
||||||
|
|
||||||
|
//Sonderfall aus Unit-Test, wenn rows == 0 oder cols == 0, darf kein Speicher allokiert werden
|
||||||
|
if(rows==0 || cols==0){
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Normalfall
|
||||||
|
m.rows = rows; // strukurvariable.feldname --> Struktur-Zugriffsoperator
|
||||||
|
m.cols = cols;
|
||||||
|
m.buffer = malloc((rows * cols) * sizeof(MatrixType)); //Speicher reserviert für Elemente
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
// falls Speicher existiert (buffer NICHT NULL ist): freigeben
|
||||||
|
if(matrix->buffer != NULL)
|
||||||
|
{
|
||||||
|
free(matrix->buffer);// Speicher freigegeben aber zeigt irgendwo hin (dangling pointer)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Matrix in definierten leeren Zustand setzen
|
||||||
|
matrix->buffer = NULL; // dangling pointer zurücksetzen
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Wert in Matrix schreiben und wo genau
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
// Sicherheit: wenn buffer == NULL (kein gültiger Speicher) oder Index außerhalb der Matrix --> return
|
||||||
|
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//index = Zeile * Anzahl_Spalten + Spalte
|
||||||
|
unsigned int index = rowIdx * matrix.cols + colIdx;
|
||||||
|
|
||||||
|
// Schreibt value direkt an die berechnete Position im Matrixspeicher
|
||||||
|
matrix.buffer[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int index = rowIdx * matrix.cols + colIdx;
|
||||||
|
|
||||||
|
return matrix.buffer[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
//Zeilen müssen gleich sein
|
||||||
|
if (matrix1.rows != matrix2.rows)
|
||||||
|
{
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Spalten müssen gleich sein (mit broadcasting)
|
||||||
|
//Fälle: gleiche Spalten ok, matrix1 hat 1 Spalte, matrix2 hat 1 Spalte
|
||||||
|
//sonst inkompatibel
|
||||||
|
if (matrix1.cols != matrix2.cols && matrix1.cols != 1 && matrix2.cols != 1)
|
||||||
|
{
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.rows = matrix1.rows;
|
||||||
|
result.cols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols;
|
||||||
|
|
||||||
|
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||||
|
|
||||||
|
for (unsigned int r = 0; r < result.rows; r++)
|
||||||
|
{
|
||||||
|
for (unsigned int c = 0; c < result.cols; c++)
|
||||||
|
{
|
||||||
|
// Bestimme Spalte für matrix1:
|
||||||
|
// Wenn nur 1 Spalte -> immer Spalte 0 benutzen
|
||||||
|
unsigned int c1 = (matrix1.cols == 1) ? 0 : c;
|
||||||
|
|
||||||
|
// Bestimme Spalte für matrix2:
|
||||||
|
unsigned int c2 = (matrix2.cols == 1) ? 0 : c;
|
||||||
|
|
||||||
|
MatrixType v1 = getMatrixAt(matrix1, r, c1);
|
||||||
|
MatrixType v2 = getMatrixAt(matrix2, r, c2);
|
||||||
|
|
||||||
|
setMatrixAt(v1 + v2, result, r, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
if(matrix1.cols != matrix2.rows)
|
||||||
|
{
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
result.rows = matrix1.rows;
|
||||||
|
result.cols = matrix2.cols;
|
||||||
|
|
||||||
|
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||||
|
|
||||||
|
for (unsigned int r = 0; r < result.rows; r++)
|
||||||
|
{
|
||||||
|
for (unsigned int c = 0; c < result.cols; c++)
|
||||||
|
{
|
||||||
|
MatrixType sum = 0;
|
||||||
|
|
||||||
|
// gemeinsame Dimension = matrix1.cols = matrix2.rows
|
||||||
|
for (unsigned int i = 0; i < matrix1.cols; i++)
|
||||||
|
{
|
||||||
|
MatrixType a = getMatrixAt(matrix1, r, i);
|
||||||
|
MatrixType b = getMatrixAt(matrix2, i, c);
|
||||||
|
|
||||||
|
sum += a * b;
|
||||||
|
}
|
||||||
|
|
||||||
|
setMatrixAt(sum, result, r, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6,6 +6,11 @@
|
|||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
|
typedef struct {
|
||||||
|
unsigned int rows;
|
||||||
|
unsigned int cols;
|
||||||
|
MatrixType* buffer; //buffer Pointer zeigt auf Heap, mit malloc dort dann Speicher reservieren
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
+1
-1
@@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
|
|
||||||
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
||||||
{
|
{
|
||||||
Matrix matrix = {NULL, 0, 0};
|
Matrix matrix = (Matrix){0, 0, NULL};
|
||||||
|
|
||||||
if(count > 0 && images != NULL)
|
if(count > 0 && images != NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
+36
-1
@@ -6,11 +6,46 @@
|
|||||||
#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO
|
//öffnet die Datei in Binär zum schreiben
|
||||||
|
FILE *file = fopen(path, "wb");
|
||||||
|
if (!file) return;
|
||||||
|
//Fester Headerstring
|
||||||
|
const char *tag = "__info2_neural_network_file_format__";
|
||||||
|
fwrite(tag, sizeof(char), strlen(tag), file);
|
||||||
|
//Bestimmt die Eingabedimension
|
||||||
|
int inputDim = nn.layers[0].weights.cols;
|
||||||
|
fwrite(&inputDim, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
||||||
|
|
||||||
|
const Layer *L = &nn.layers[i];
|
||||||
|
const Matrix *W = &L->weights;
|
||||||
|
const Matrix *B = &L->biases;
|
||||||
|
|
||||||
|
int outputDim = W->rows;
|
||||||
|
fwrite(&outputDim, sizeof(int), 1, file);
|
||||||
|
//Anzahl der Weight-Werte
|
||||||
|
size_t weightCount = (size_t)(W->rows * W->cols);
|
||||||
|
fwrite(&weightCount, sizeof(size_t), 1, file);
|
||||||
|
//Anzahl der Bias-Werte
|
||||||
|
size_t biasCount = (size_t)(B->rows * B->cols);
|
||||||
|
fwrite(B->buffer, sizeof(MatrixType), biasCount, file);
|
||||||
|
|
||||||
|
inputDim = outputDim;
|
||||||
|
}
|
||||||
|
|
||||||
|
int zero = 0;
|
||||||
|
fwrite(&zero, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
{
|
{
|
||||||
const char *path = "some__nn_test_file.info2";
|
const char *path = "some__nn_test_file.info2";
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user