Compare commits

4 Commits
Author SHA1 Message Date
gessnitzerlu98886 8ceb081ffe neuralNetwork.c
neuralNetworkTests.c
check
2025-11-18 14:36:11 +01:00
gessnitzerlu98886 8d4ee4cc4e matrix.c fix 2025-11-18 14:30:41 +01:00
m_kol 7d9b4bc6bf bla bla bla 2025-11-18 13:59:32 +01:00
gessnitzerlu98886 e26690d0d0 matrix.h check
matrix.c angefangen
imageInput.c sollte gehen tests gehen nicht bei mi
2025-11-12 19:53:48 +01:00
39 changed files with 422 additions and 143 deletions
+3 -60
View File
@@ -1,61 +1,4 @@
# ---> C mnist
# Prerequisites runTests
*.d
# Object files
*.o *.o
*.ko *.exe
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
Startcode/mnist
Startcode/runTests
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# IDE folders
.vscode/
.idea/
# macOS
.DS_Store
-22
View File
@@ -1,22 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imageInput.h"
#define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__"
// 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
GrayScaleImageSeries *readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
}
-23
View File
@@ -1,23 +0,0 @@
#ifndef IMAGEINPUT_H
#define IMAGEINPUT_H
typedef unsigned char GrayScalePixelType;
typedef struct
{
GrayScalePixelType *buffer;
unsigned int width;
unsigned int height;
} GrayScaleImage;
typedef struct
{
GrayScaleImage *images;
unsigned char *labels;
unsigned int count;
} GrayScaleImageSeries;
GrayScaleImageSeries *readImages(const char *path);
void clearSeries(GrayScaleImageSeries *series);
#endif
-35
View File
@@ -1,35 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
}
void clearMatrix(Matrix *matrix)
{
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
}
+180
View File
@@ -0,0 +1,180 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imageInput.h"
#define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__"
// 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
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)
{
// 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;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
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);
}
+23
View File
@@ -0,0 +1,23 @@
#ifndef IMAGEINPUT_H
#define IMAGEINPUT_H
typedef unsigned char GrayScalePixelType;
typedef struct
{
GrayScalePixelType *buffer; // Breite in Pixeln
unsigned int width; // Höhe in Pixeln
unsigned int height; // Die Pixelwerte (0-255)
} GrayScaleImage; // EIN Bild
typedef struct
{
GrayScaleImage *images; // Array von Bildern
unsigned char *labels; // Array von Labels (welche Ziffer?)
unsigned int count; // Wie viele Bilder ?
} GrayScaleImageSeries; // Sammlung der Bilder
GrayScaleImageSeries *readImages(const char *path);
void clearSeries(GrayScaleImageSeries *series);
#endif
View File
View File
+173
View File
@@ -0,0 +1,173 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
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)
{
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)
{
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)
{
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 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)
{
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;
}
+5
View File
@@ -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
View File
@@ -164,7 +164,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
setMatrixAt(-1, matrixToTest, 2, 3); setMatrixAt(-1, matrixToTest, 2, 3);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType)); TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows);
} }
void setUp(void) { void setUp(void) {
@@ -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)
{ {
@@ -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)