Compare commits
5
Commits
main
...
7bffe1fdad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bffe1fdad | ||
|
|
8ceb081ffe | ||
|
|
8d4ee4cc4e | ||
|
|
7d9b4bc6bf | ||
|
|
e26690d0d0 |
+159
-1
@@ -9,14 +9,172 @@
|
|||||||
// 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
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
|
|
||||||
|
|
||||||
|
static int checkFileHeader(FILE *file)
|
||||||
|
{
|
||||||
|
char buffer[BUFFER_SIZE];
|
||||||
|
int length = strlen(FILE_HEADER_STRING);
|
||||||
|
|
||||||
|
// Prüfen ob fread erfolgreich war
|
||||||
|
if (fread(buffer, sizeof(char), length, file) != length) {
|
||||||
|
return 0; // Lesefehler
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[length] = '\0';
|
||||||
|
|
||||||
|
if (strcmp(buffer, FILE_HEADER_STRING) == 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int readDimensions(FILE *file, unsigned short * count, unsigned short *width, unsigned short *height)
|
||||||
|
{
|
||||||
|
// Anzahl lesen
|
||||||
|
if (fread(count, sizeof(unsigned short), 1, file) != 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// Breite lesen
|
||||||
|
if (fread(width, sizeof(unsigned short), 1, file) != 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// Höhe lesen
|
||||||
|
if (fread(height, sizeof(unsigned short), 1, file) != 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; // Alles ok
|
||||||
|
}
|
||||||
|
|
||||||
|
static int readSingleImage(FILE *file, GrayScaleImage *image, unsigned char *label, unsigned short width, unsigned short height)
|
||||||
|
{
|
||||||
|
// Schritt 1: Gesamtzahl Pixel berechnen
|
||||||
|
int totalPixels = width * height;
|
||||||
|
|
||||||
|
// Schritt 2: Speicher allokieren
|
||||||
|
image->buffer = (unsigned char *)malloc(totalPixels * sizeof(unsigned char));
|
||||||
|
if (image->buffer == NULL) {
|
||||||
|
return 0; // Fehler: kein Speicher verfügbar
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 3: Breite und Höhe setzen
|
||||||
|
image->width = width;
|
||||||
|
image->height = height;
|
||||||
|
|
||||||
|
// Schritt 4: Pixel lesen
|
||||||
|
if (fread(image->buffer, sizeof(unsigned char), totalPixels, file) != totalPixels) {
|
||||||
|
free(image->buffer); // Aufräumen!
|
||||||
|
return 0; // Fehler beim Lesen
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 5: Label lesen
|
||||||
|
if (fread(label, sizeof(unsigned char), 1, file) != 1) {
|
||||||
|
free(image->buffer); // Aufräumen!
|
||||||
|
return 0; // Fehler beim Lesen
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; // Erfolg!
|
||||||
|
}
|
||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
GrayScaleImageSeries *series = NULL;
|
// Schritt 1: Datei öffnen
|
||||||
|
FILE *file = fopen(path, "rb");
|
||||||
|
if (file == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 2: Header prüfen
|
||||||
|
if (!checkFileHeader(file)) {
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 3: Dimensionen lesen
|
||||||
|
unsigned short count, width, height;
|
||||||
|
if (!readDimensions(file, &count, &width, &height)) {
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 4: Speicher für die Serie allokieren
|
||||||
|
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
if (series == NULL) {
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 5: Speicher für das images-Array allokieren
|
||||||
|
series->images = (GrayScaleImage *)malloc(count * sizeof(GrayScaleImage));
|
||||||
|
if (series->images == NULL) {
|
||||||
|
free(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 6: Speicher für das labels-Array allokieren
|
||||||
|
series->labels = (unsigned char *)malloc(count * sizeof(unsigned char));
|
||||||
|
if (series->labels == NULL) {
|
||||||
|
free(series->images);
|
||||||
|
free(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 7: count setzen
|
||||||
|
series->count = count;
|
||||||
|
|
||||||
|
// Schritt 8: Alle Bilder in einer Schleife einlesen
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
if (!readSingleImage(file, &series->images[i], &series->labels[i], width, height)) {
|
||||||
|
// Bei Fehler: Aufräumen!
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
free(series->images[j].buffer);
|
||||||
|
}
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 9: Datei schließen
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
// Schritt 10: Fertige Serie zurückgeben
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
// Schritt 0: Prüfen ob series überhaupt existiert
|
||||||
|
if (series == NULL) {
|
||||||
|
return; // Nichts zu tun
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 1: Alle Pixel-Buffer freigeben (für jedes Bild)
|
||||||
|
if (series->images != NULL) {
|
||||||
|
for (int i = 0; i < series->count; i++) {
|
||||||
|
if (series->images[i].buffer != NULL) {
|
||||||
|
free(series->images[i].buffer); // ← Buffer von Bild i freigeben
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 2: Das images-Array freigeben
|
||||||
|
if (series->images != NULL) {
|
||||||
|
free(series->images);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 3: Das labels-Array freigeben
|
||||||
|
if (series->labels != NULL) {
|
||||||
|
free(series->labels);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schritt 4: Die Serie-Struktur selbst freigeben
|
||||||
|
free(series);
|
||||||
}
|
}
|
||||||
+8
-8
@@ -5,17 +5,17 @@ typedef unsigned char GrayScalePixelType;
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
GrayScalePixelType *buffer;
|
GrayScalePixelType *buffer; // Breite in Pixeln
|
||||||
unsigned int width;
|
unsigned int width; // Höhe in Pixeln
|
||||||
unsigned int height;
|
unsigned int height; // Die Pixelwerte (0-255)
|
||||||
} GrayScaleImage;
|
} GrayScaleImage; // EIN Bild
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
GrayScaleImage *images;
|
GrayScaleImage *images; // Array von Bildern
|
||||||
unsigned char *labels;
|
unsigned char *labels; // Array von Labels (welche Ziffer?)
|
||||||
unsigned int count;
|
unsigned int count; // Wie viele Bilder ?
|
||||||
} GrayScaleImageSeries;
|
} GrayScaleImageSeries; // Sammlung der Bilder
|
||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path);
|
GrayScaleImageSeries *readImages(const char *path);
|
||||||
void clearSeries(GrayScaleImageSeries *series);
|
void clearSeries(GrayScaleImageSeries *series);
|
||||||
|
|||||||
@@ -1,35 +1,173 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix m;
|
||||||
|
m.rows = rows;
|
||||||
|
m.cols = cols;
|
||||||
|
|
||||||
|
m.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols);
|
||||||
|
// Prüfe auf ungültige Dimensionen
|
||||||
|
if (rows == 0 || cols == 0) {
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
if (m.buffer == NULL){
|
||||||
|
fprintf(stderr, "Error: Memory allocation failed in createMatrix!.\n");
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < rows * cols; i++){
|
||||||
|
m.buffer[i] = 0.0f;
|
||||||
|
}
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if (matrix == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Speicher freigeben falls vorhanden
|
||||||
|
if (matrix->buffer != NULL) {
|
||||||
|
free(matrix->buffer);
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dimensionen zurücksetzen
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (rowIdx >= matrix.rows || colIdx >= matrix.cols) {
|
||||||
|
fprintf(stderr, "Error: setMatrixAt index out of bounds.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {
|
||||||
|
fprintf(stderr, "Error: getMatrixAt index out of bounds.\n");
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
// Fall 1: Normale elementweise Addition (gleiche Dimensionen)
|
||||||
|
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
||||||
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++) {
|
||||||
|
setMatrixAt(
|
||||||
|
getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j),
|
||||||
|
result,
|
||||||
|
i, j
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall 2: Broadcasting - matrix2 ist Spaltenvektor (cols=1)
|
||||||
|
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1) {
|
||||||
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++) {
|
||||||
|
// matrix2 hat nur 1 Spalte (Index 0), wird über alle Spalten verteilt
|
||||||
|
setMatrixAt(
|
||||||
|
getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0),
|
||||||
|
result,
|
||||||
|
i, j
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall 3: Broadcasting - matrix1 ist Spaltenvektor (cols=1)
|
||||||
|
else if (matrix2.rows == matrix1.rows && matrix1.cols == 1) {
|
||||||
|
result = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix2.rows; i++) {
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
||||||
|
// matrix1 hat nur 1 Spalte (Index 0), wird über alle Spalten verteilt
|
||||||
|
setMatrixAt(
|
||||||
|
getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j),
|
||||||
|
result,
|
||||||
|
i, j
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall 4: Ungültige Dimensionen
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "Error: Matrix dimensions do not match for addition.\n");
|
||||||
|
Matrix empty = {0, 0, NULL};
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
if (matrix1.cols != matrix2.rows) {
|
||||||
|
fprintf(stderr, "Error: Invalid matrix dimensions for multiplication.\n");
|
||||||
|
Matrix empty = {0, 0, NULL};
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
||||||
|
MatrixType sum = 0.0f;
|
||||||
|
|
||||||
|
for (unsigned int k = 0; k < matrix1.cols; k++) {
|
||||||
|
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
setMatrixAt(sum, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,12 @@
|
|||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
|
typedef struct{
|
||||||
|
unsigned int rows;
|
||||||
|
unsigned int cols;
|
||||||
|
MatrixType* buffer;
|
||||||
|
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
void clearMatrix(Matrix *matrix);
|
||||||
|
|||||||
+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 = {0, 0, NULL}; //hier evtl Null auf int casten?
|
||||||
|
|
||||||
if(count > 0 && images != NULL)
|
if(count > 0 && images != NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
+36
-1
@@ -8,7 +8,42 @@
|
|||||||
|
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO
|
FILE *file = fopen(path, "wb");
|
||||||
|
|
||||||
|
if (file == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Header schreiben
|
||||||
|
const char *fileTag = "__info2_neural_network_file_format__";
|
||||||
|
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
||||||
|
|
||||||
|
// 2. Alle Schichten schreiben
|
||||||
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
||||||
|
// NUR bei der ERSTEN Schicht: Input-Dimension schreiben
|
||||||
|
if (i == 0) {
|
||||||
|
int inputDim = nn.layers[i].weights.cols;
|
||||||
|
fwrite(&inputDim, sizeof(int), 1, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output-Dimension (= Anzahl Zeilen der Gewichtsmatrix)
|
||||||
|
int outputDim = nn.layers[i].weights.rows;
|
||||||
|
fwrite(&outputDim, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
// Gewichtsmatrix schreiben (alle Werte)
|
||||||
|
int weightCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||||
|
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||||
|
|
||||||
|
// Bias-Matrix schreiben (alle Werte)
|
||||||
|
int biasCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
||||||
|
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasCount, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Terminator schreiben (outputDimension = 0 zum Stoppen)
|
||||||
|
int terminator = 0;
|
||||||
|
fwrite(&terminator, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
|
|||||||
Reference in New Issue
Block a user