forked from freudenreichan/info2Praktikum-NeuronalesNetz
neu
This commit is contained in:
parent
0f0f2f19c3
commit
fd1bc886a7
@ -28,6 +28,7 @@ static int readMeta(FILE *file, unsigned short *count, unsigned short *width,
|
|||||||
return 0;
|
return 0;
|
||||||
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,9 +75,11 @@ GrayScaleImageSeries *readImages(const char *path) {
|
|||||||
|
|
||||||
unsigned short count, width, height;
|
unsigned short count, width, height;
|
||||||
if (!readMeta(file, &count, &width, &height)) {
|
if (!readMeta(file, &count, &width, &height)) {
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
// printf("%d, %d, %d", count, width, height);
|
||||||
|
|
||||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
if (!series) {
|
if (!series) {
|
||||||
|
|||||||
26
matrix.c
26
matrix.c
@ -3,32 +3,38 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
|
||||||
/*typedef struct {
|
/*typedef struct {
|
||||||
unsigned int rows; //Zeilen
|
unsigned int rows; //Zeilen
|
||||||
unsigned int cols; //Spalten
|
unsigned int cols; //Spalten
|
||||||
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
||||||
} Matrix;*/
|
} Matrix;*/
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
||||||
Matrix matrix;
|
|
||||||
Matrix errorMatrix = {0, 0, NULL};
|
Matrix matrix; // neue Metrix Struktur erstellen
|
||||||
if (rows == 0 || cols == 0) {
|
|
||||||
|
Matrix errorMatrix = {0, 0, NULL}; // Fehlermatrix zum zurückgeben
|
||||||
|
|
||||||
|
if (rows == 0 || cols == 0) { // fehlerhafte Parameter übergeben
|
||||||
|
|
||||||
return errorMatrix;
|
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.cols = cols;
|
||||||
|
|
||||||
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
|
matrix.buffer =
|
||||||
if (matrix.buffer == NULL) {
|
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren
|
||||||
|
if (matrix.buffer == NULL) { // Prüfen, ob Speicher reserviert werden konnte
|
||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
matrix.cols = 0;
|
matrix.cols = 0;
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < rows; i++) {
|
for (int r = 0; r < rows; r++) {
|
||||||
for (int j = 0; j < cols; j++) {
|
for (int c = 0; c < cols; c++) {
|
||||||
matrix.buffer[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
|
matrix.buffer[r * matrix.cols + c] = UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matrix;
|
return matrix;
|
||||||
|
|||||||
@ -30,20 +30,21 @@ Dimension: Form der Matrizen für einen Layer*/
|
|||||||
|
|
||||||
// speichert NeuralNetwork nn in binäre Datei->erzeugt Dateiformat
|
// speichert NeuralNetwork nn in binäre Datei->erzeugt Dateiformat
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
|
||||||
FILE *f = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
|
FILE *fptr = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
|
||||||
if (f == NULL)
|
if (fptr == NULL)
|
||||||
return;
|
return; // file konnte nicht geöffnet werden
|
||||||
|
|
||||||
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
|
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
|
||||||
// Dateiformat
|
// Dateiformat
|
||||||
const char header[] = "__info2_neural_network_file_format__";
|
const char header[] =
|
||||||
fwrite(header, sizeof(char), strlen(header), f);
|
"__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
|
// Wenn es keine Layer gibt, 0 eintragen, LoadModel gibt 0 zurück
|
||||||
if (nn.numberOfLayers == 0) {
|
if (nn.numberOfLayers == 0) {
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
fwrite(&zero, sizeof(int), 1, f);
|
fwrite(&zero, sizeof(int), 1, fptr);
|
||||||
fclose(f);
|
fclose(fptr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,8 +52,8 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
|
|||||||
// Output-Neuronen
|
// Output-Neuronen
|
||||||
int inputDim = (int)nn.layers[0].weights.cols;
|
int inputDim = (int)nn.layers[0].weights.cols;
|
||||||
int outputDim = (int)nn.layers[0].weights.rows;
|
int outputDim = (int)nn.layers[0].weights.rows;
|
||||||
fwrite(&inputDim, sizeof(int), 1, f);
|
fwrite(&inputDim, sizeof(int), 1, fptr);
|
||||||
fwrite(&outputDim, sizeof(int), 1, f);
|
fwrite(&outputDim, sizeof(int), 1, fptr);
|
||||||
|
|
||||||
/* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output
|
/* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output
|
||||||
x 1). Zwischen Layern wird nur die nächste outputDimension (int)
|
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) */
|
/* Gewichte (MatrixType binär) */
|
||||||
if (wcount > 0 && layer.weights.buffer != NULL) {
|
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) */
|
/* Biases (MatrixType binär) */
|
||||||
if (bcount > 0 && layer.biases.buffer != NULL) {
|
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 */
|
/* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */
|
||||||
if (i + 1 < nn.numberOfLayers) {
|
if (i + 1 < nn.numberOfLayers) {
|
||||||
int nextOutput = (int)nn.layers[i + 1].weights.rows;
|
int nextOutput = (int)nn.layers[i + 1].weights.rows;
|
||||||
fwrite(&nextOutput, sizeof(int), 1, f);
|
fwrite(&nextOutput, sizeof(int), 1, fptr);
|
||||||
} else {
|
} else {
|
||||||
/* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
|
/* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
|
||||||
schreiben. loadModel liest dann outputDimension = 0 und beendet die
|
schreiben. loadModel liest dann outputDimension = 0 und beendet die
|
||||||
Schleife. */
|
Schleife. */
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
fwrite(&zero, sizeof(int), 1, f);
|
fwrite(&zero, sizeof(int), 1, fptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
fclose(fptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void) {
|
void test_loadModelReturnsCorrectNumberOfLayers(void) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user