This commit is contained in:
Kristin 2025-11-25 10:38:40 +01:00
parent 0f0f2f19c3
commit fd1bc886a7
3 changed files with 34 additions and 24 deletions

View File

@ -28,6 +28,7 @@ static int readMeta(FILE *file, unsigned short *count, unsigned short *width,
return 0;
if (fread(height, sizeof(unsigned short), 1, file) != 1)
return 0;
return 1;
}
@ -74,9 +75,11 @@ GrayScaleImageSeries *readImages(const char *path) {
unsigned short count, width, height;
if (!readMeta(file, &count, &width, &height)) {
fclose(file);
return NULL;
}
// printf("%d, %d, %d", count, width, height);
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
if (!series) {

View File

@ -3,32 +3,38 @@
#include <stdlib.h>
#include <string.h>
// TODO Matrix-Funktionen implementieren
/*typedef struct {
unsigned int rows; //Zeilen
unsigned int cols; //Spalten
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
} Matrix;*/
Matrix createMatrix(unsigned int rows, unsigned int cols) {
Matrix matrix;
Matrix errorMatrix = {0, 0, NULL};
if (rows == 0 || cols == 0) {
Matrix matrix; // neue Metrix Struktur erstellen
Matrix errorMatrix = {0, 0, NULL}; // Fehlermatrix zum zurückgeben
if (rows == 0 || cols == 0) { // fehlerhafte Parameter übergeben
return errorMatrix;
}
matrix.rows = rows;
matrix.rows = rows; // nach dem Prüfen auf unmögliche Angaben kann die Matrix
// erstellt werden
matrix.cols = cols;
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
if (matrix.buffer == NULL) {
matrix.buffer =
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren
if (matrix.buffer == NULL) { // Prüfen, ob Speicher reserviert werden konnte
matrix.rows = 0;
matrix.cols = 0;
return matrix;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix.buffer[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
matrix.buffer[r * matrix.cols + c] = UNDEFINED_MATRIX_VALUE;
}
}
return matrix;

View File

@ -30,20 +30,21 @@ Dimension: Form der Matrizen für einen Layer*/
// speichert NeuralNetwork nn in binäre Datei->erzeugt Dateiformat
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
FILE *f = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
if (f == NULL)
return;
FILE *fptr = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
if (fptr == NULL)
return; // file konnte nicht geöffnet werden
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
// Dateiformat
const char header[] = "__info2_neural_network_file_format__";
fwrite(header, sizeof(char), strlen(header), f);
const char header[] =
"__info2_neural_network_file_format__"; // header vor jedem Layer
fwrite(header, sizeof(char), strlen(header), fptr);
// Wenn es keine Layer gibt, 0 eintragen, LoadModel gibt 0 zurück
if (nn.numberOfLayers == 0) {
int zero = 0;
fwrite(&zero, sizeof(int), 1, f);
fclose(f);
fwrite(&zero, sizeof(int), 1, fptr);
fclose(fptr);
return;
}
@ -51,8 +52,8 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
// Output-Neuronen
int inputDim = (int)nn.layers[0].weights.cols;
int outputDim = (int)nn.layers[0].weights.rows;
fwrite(&inputDim, sizeof(int), 1, f);
fwrite(&outputDim, sizeof(int), 1, f);
fwrite(&inputDim, sizeof(int), 1, fptr);
fwrite(&outputDim, sizeof(int), 1, fptr);
/* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output
x 1). Zwischen Layern wird nur die nächste outputDimension (int)
@ -68,28 +69,28 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
/* Gewichte (MatrixType binär) */
if (wcount > 0 && layer.weights.buffer != NULL) {
fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, f);
fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, fptr);
}
/* Biases (MatrixType binär) */
if (bcount > 0 && layer.biases.buffer != NULL) {
fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, f);
fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, fptr);
}
/* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */
if (i + 1 < nn.numberOfLayers) {
int nextOutput = (int)nn.layers[i + 1].weights.rows;
fwrite(&nextOutput, sizeof(int), 1, f);
fwrite(&nextOutput, sizeof(int), 1, fptr);
} else {
/* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
schreiben. loadModel liest dann outputDimension = 0 und beendet die
Schleife. */
int zero = 0;
fwrite(&zero, sizeof(int), 1, f);
fwrite(&zero, sizeof(int), 1, fptr);
}
}
fclose(f);
fclose(fptr);
}
void test_loadModelReturnsCorrectNumberOfLayers(void) {