Compare commits

8 Commits
6 changed files with 191 additions and 11 deletions
+47
View File
@@ -5,12 +5,59 @@
#define BUFFER_SIZE 100
#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
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
GrayScaleImageSeries *readImages(const char *path)
{
FILE *datei = checkFile(path);
if (datei==NULL)
{
return NULL;
}
unsigned short image_count, width, height;
fread(&image_count,1,sizeof(unsigned short),datei);
fread(&width,1,sizeof(unsigned short),datei);
fread(&height,1,sizeof(unsigned short),datei);
//printf("%u Bilder und %u mal %u",image_count,width,height);
fclose(datei);
GrayScaleImageSeries *series = NULL;
return series;
+1 -1
View File
@@ -19,5 +19,5 @@ typedef struct
GrayScaleImageSeries *readImages(const char *path);
void clearSeries(GrayScaleImageSeries *series);
FILE *checkFile(const char *path);
#endif
+5 -2
View File
@@ -1,11 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include "imageInput.h"
#include "mnistVisualization.h"
#include "neuralNetwork.h"
//#include "mnistVisualization.h"
//#include "neuralNetwork.h"
int main(int argc, char *argv[])
{
readImages("mnist_test.info2");
/*
const unsigned int windowWidth = 800;
const unsigned int windowHeight = 600;
@@ -65,4 +67,5 @@ int main(int argc, char *argv[])
}
return exitCode;
*/
}
+80 -8
View File
@@ -4,32 +4,104 @@
// TODO Matrix-Funktionen implementieren
// Matrix erzeugen
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix matrix;
matrix.buffer = NULL;
matrix.rows = 0;
matrix.cols = 0;
// Wenn die Dimensionen gültig sind, Speicher reservieren
if (rows > 0 && cols > 0)
{
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.buffer != NULL)
{
matrix.rows = rows;
matrix.cols = cols;
}
}
return matrix;
}
// Matrix Speicher freigeben
void clearMatrix(Matrix *matrix)
{
if (matrix->buffer != NULL)
{
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)
{
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
}
// Wert auslesen
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
return 0; // Fallback
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
// Matrizen addieren
Matrix add(const Matrix m1, const Matrix m2)
{
if (m1.rows != m2.rows || m1.cols != m2.cols)
{
return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
}
Matrix result = createMatrix(m1.rows, m1.cols);
if (result.buffer == NULL) return result;
for (unsigned int r = 0; r < m1.rows; r++)
{
for (unsigned int c = 0; c < m1.cols; c++)
{
result.buffer[r * m1.cols + c] =
getMatrixAt(m1, r, c) + getMatrixAt(m2, r, c);
}
}
return result;
}
// Matrizen multiplizieren
Matrix multiply(const Matrix m1, const Matrix m2)
{
if (m1.cols != m2.rows)
{
return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
}
Matrix result = createMatrix(m1.rows, m2.cols);
if (result.buffer == NULL) 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 += getMatrixAt(m1, r, k) * getMatrixAt(m2, k, c);
}
result.buffer[r * m2.cols + c] = sum;
}
}
return result;
}
+7
View File
@@ -6,6 +6,13 @@
typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct
{
MatrixType *buffer;
unsigned int rows;
unsigned int cols;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
+52 -1
View File
@@ -8,7 +8,58 @@
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)