Compare commits

..

8 Commits
simon ... main

Author SHA1 Message Date
Simon Wiesend
3a9d8275a8
fix bug 2025-11-28 08:14:41 +01:00
Simon Wiesend
bbb0ea1cf5
Merge branch 'main' into neuralNetworkTests 2025-11-25 13:40:23 +01:00
Simon Wiesend
12825cc1d3
Revert "neuralNetwork fixed". The root cause has been fixed in matrix.h
This reverts commit 6ba9ba319592ac3aac15723ad549f1389b5c9718.
2025-11-25 13:38:56 +01:00
Simon Wiesend
633ee723f4
adapt matrix struct to existing tests 2025-11-25 13:33:20 +01:00
Fabrice
7ea80137b0 Header wird gelesen, läuft alles optimal 2025-11-24 10:35:46 +01:00
6ba9ba3195 neuralNetwork fixed 2025-11-24 08:24:37 +00:00
Fabrice
9c3d9f0a40 imageInput implementiert 2025-11-23 20:54:39 +01:00
f4427d2892 unittests bestanden 2025-11-23 16:38:17 +00:00
6 changed files with 141 additions and 185 deletions

3
.gitignore vendored
View File

@ -3,6 +3,3 @@ runTests
*.o *.o
*.exe *.exe
runMatrixTests runMatrixTests
runImageInputTests
runNeuralNetworkTests
.vscode

View File

@ -6,165 +6,107 @@
#define BUFFER_SIZE 100 #define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__" #define FILE_HEADER_STRING "__info2_image_file_format__"
typedef enum //Datei öffnen, Header, Anzahl, Höhe und Breite lesen, geöffnete Datei zurückgeben
{ static FILE* openAndReadShort (const char *path, unsigned short *count, unsigned short *width, unsigned short *height) {
IMG_SUCCESS = 0, FILE *file = fopen(path, "rb");
IMG_ERR_INVALID_HEADER, // Header did not match if (!file) {
IMG_ERR_READ, // Failed to read from file, maybe it's too short return NULL;
IMG_ERR, // General Error }
} ImageError; size_t headerLength = strlen(FILE_HEADER_STRING);
char *header = malloc (headerLength + 1);
if(!header) {
return NULL;
}
static ImageError checkHeader(FILE *file); if (fread(header, sizeof(char), headerLength, file) != headerLength) {
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file); free (header);
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file); return NULL;
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series); }
header[headerLength] = '\0';
if (strcmp (header, FILE_HEADER_STRING) != 0) {
free(header);
return NULL;
}
free (header);
fread(count, sizeof(unsigned short), 1, file);
fread(width, sizeof(unsigned short), 1, file);
fread(height, sizeof(unsigned short), 1, file);
return file;
}
//Speicher anlegen und Pixel eines Bildes einlesen
static GrayScaleImage* readPixles (FILE *file, unsigned short *width, unsigned short *height) {
GrayScaleImage *image = malloc (sizeof(GrayScaleImage));
image->width = *width;
image->height = *height;
image->buffer = malloc ((*width) * (*height) * sizeof(GrayScalePixelType));
if (!image->buffer) {
free(image);
return NULL;
}
for (unsigned int i = 0; i < (*width) * (*height); i++) {
unsigned char pixel;
if (fread(&pixel, sizeof(unsigned char), 1, file) != 1) {
free(image->buffer);
free(image);
return NULL;
}
image->buffer[i] = pixel;
}
return image;
}
//Ausführen von openAndReadShort, Anlegen des Speichers für Bilderserie, readPixles wird für jedes Bild ausgeführt
//Nach jedem Bild wird das zugehörige Label gelesen, bei sämtlichen Fehlern wird NULL zurückgegeben und Speicher durch clearSeries bereinigt
GrayScaleImageSeries *readImages(const char *path) GrayScaleImageSeries *readImages(const char *path)
{ {
// It's very important to call calloc here because otherwise clearSeries() might try to free random memory unsigned short count = 0, width = 0, height = 0;
GrayScaleImageSeries *series = calloc(1, sizeof(GrayScaleImageSeries));
if (series == NULL) FILE *file = openAndReadShort(path, &count, &width, &height);
{ if (file == 0) {
return NULL; return NULL;
} }
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
FILE *file = fopen(path, "rb"); if (!series) {
// If fopen() failed
if (file == NULL)
{
clearSeries(series);
series = NULL;
return NULL;
}
// Try to parse the whole file. If anything fails memory is freed
if (parseImageFile(file, series))
{
clearSeries(series);
series = NULL; // Return NULL after the file is properly closed
}
fclose(file); fclose(file);
return NULL;
}
series->count = count;
series->images = malloc(count * sizeof(GrayScaleImage));
series->labels = malloc(count* sizeof(unsigned char));
for (unsigned int i = 0; i < series->count; i++) {
GrayScaleImage *image = readPixles(file, &width, &height);
series->images[i] = *image;
free(image);
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
clearSeries(series);
fclose(file);
return NULL;
}
}
fclose(file);
return series; return series;
} }
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series) //Bereinigt den Speicher
{
if (checkHeader(file))
{
return IMG_ERR; // header check failed
}
unsigned short imgCNT, width, height;
if (readPictureParams(&imgCNT, &width, &height, file))
{
return IMG_ERR; // read failed
}
size_t pixels = width * height;
// The images contain more pointers, definitely use calloc here
series->images = calloc(imgCNT, sizeof(GrayScaleImage));
if (series->images == NULL)
{
return IMG_ERR;
}
series->labels = malloc(imgCNT * sizeof(unsigned char));
if (series->labels == NULL)
{
return IMG_ERR;
}
series->count = imgCNT;
// Read every image and it's label
for (size_t imageIdx = 0; imageIdx < imgCNT; imageIdx++)
{
GrayScaleImage *curImage = &series->images[imageIdx];
curImage->buffer = malloc(sizeof(GrayScalePixelType) * pixels);
if (curImage->buffer == NULL)
{
return IMG_ERR;
}
curImage->width = width;
curImage->height = height;
if (readImage(pixels, curImage->buffer, &series->labels[imageIdx], file))
{
return IMG_ERR;
}
}
return IMG_SUCCESS;
}
static ImageError checkHeader(FILE *file)
{
size_t len = strlen(FILE_HEADER_STRING);
char headerBuf[len + 1];
size_t charsRead = fread(headerBuf, 1, len, file);
// Check if the file is to short
if (charsRead < len)
{
return IMG_ERR_READ;
}
headerBuf[len] = '\0'; // Terminate string
return strcmp(headerBuf, FILE_HEADER_STRING) == 0 ? IMG_SUCCESS : IMG_ERR_INVALID_HEADER;
}
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file)
{
// number of images
if (1 != fread(number, sizeof(unsigned short), 1, file))
{
return IMG_ERR_READ;
}
// image width
if (1 != fread(width, sizeof(unsigned short), 1, file))
{
return IMG_ERR_READ;
}
// height
if (1 != fread(height, sizeof(unsigned short), 1, file))
{
return IMG_ERR_READ;
}
return IMG_SUCCESS;
}
// Read 1 image and it's label
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file)
{
if (numPixels > fread(pixelBuffer, sizeof(GrayScalePixelType), numPixels, file))
{
return IMG_ERR_READ;
}
if (1 != fread(label, sizeof(unsigned char), 1, file))
{
return IMG_ERR_READ;
}
return IMG_SUCCESS;
}
// Frees memory for each image buffer, image, label and finally series
void clearSeries(GrayScaleImageSeries *series) void clearSeries(GrayScaleImageSeries *series)
{ {
if (series) for (unsigned int i = 0; i < series->count; i++) {
{ free(series->images[i].buffer);
int seriesLen = series->count;
for (size_t imageIdx = 0; imageIdx < seriesLen; imageIdx++)
{
free(series->images[imageIdx].buffer);
} }
free(series->images); free(series->images);
free(series->labels); free(series->labels);
free(series); free(series);
}
} }

View File

@ -67,14 +67,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return createMatrix(0, 0); return createMatrix(0, 0);
} }
if (matrix1.cols != matrix2.cols) // matrices not compatible
{
if (matrix1.rows != matrix2.rows) if (matrix1.rows != matrix2.rows)
{ {
clearMatrix(&resMat); clearMatrix(&resMat);
return resMat; return resMat;
} }
else if (matrix1.cols == 1)
// check if broadcasting is possible
if (matrix1.cols != matrix2.cols)
{
// matrix1 is a vector
if (matrix1.cols == 1)
{ {
// broadcast vector // broadcast vector
for (size_t m = 0; m < matrix2.rows; m++) for (size_t m = 0; m < matrix2.rows; m++)
@ -86,6 +90,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
} }
return resMat; return resMat;
} }
// matrix2 is a vector
else if (matrix2.cols == 1) else if (matrix2.cols == 1)
{ {
// broadcast vector // broadcast vector
@ -98,6 +103,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
} }
return resMat; return resMat;
} }
// addition not possible
else else
{ {
clearMatrix(&resMat); clearMatrix(&resMat);
@ -109,6 +115,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
for (size_t n = 0; n < matrix1.cols; n++) for (size_t n = 0; n < matrix1.cols; n++)
{ {
// this is unnecessarily complicated because at this point we already know that the matrices are compatible
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, n), resMat, m, n); setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, n), resMat, m, n);
} }
} }
@ -116,37 +123,32 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return resMat; return resMat;
} }
Matrix multiply(const Matrix A, const Matrix B) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
if (A.cols != B.rows || A.buffer == NULL || B.buffer == NULL) if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL)
{ {
return createMatrix(0, 0); return createMatrix(0, 0);
} }
int rows = A.rows, cols = B.cols; int rows = matrix1.rows, cols = matrix2.cols;
Matrix C = createMatrix(rows, cols); Matrix resMat = createMatrix(rows, cols);
if (C.buffer == NULL) if (resMat.buffer == NULL)
{ {
return createMatrix(0, 0); return createMatrix(0, 0);
} }
// M = Rows, K = Common Dim, N = Cols for (size_t rowIdx = 0; rowIdx < rows; rowIdx++)
size_t M = A.rows, K = A.cols, N = B.cols;
for (size_t i = 0; i < M; i++)
{ {
for (size_t k = 0; k < K; k++) for (size_t colIdx = 0; colIdx < cols; colIdx++)
{ {
MatrixType valA = A.buffer[i * K + k]; int curCellVal = 0;
for (size_t j = 0; j < N; j++) for (size_t k = 0; k < matrix1.cols; k++)
{ {
// C[i, j] += A[i, k] * B[k, j]; curCellVal += getMatrixAt(matrix1, rowIdx, k) * getMatrixAt(matrix2, k, colIdx);
// M x N, M x K, K x N }
C.buffer[i * N + j] += valA * B.buffer[k * N + j]; setMatrixAt(curCellVal, resMat, rowIdx, colIdx);
} }
} }
} return resMat;
return C;
} }

View File

@ -8,10 +8,9 @@ typedef float MatrixType;
// Matrixtyp // Matrixtyp
typedef struct Matrix typedef struct Matrix
{ {
MatrixType *buffer;
size_t rows; size_t rows;
size_t cols; size_t cols;
MatrixType *buffer;
} Matrix; } Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);

View File

@ -5,29 +5,44 @@
#include "unity.h" #include "unity.h"
#include "neuralNetwork.h" #include "neuralNetwork.h"
static void writeLayer(FILE *file, const Matrix weights, const Matrix biases, unsigned int inputDim)
static void erzeugeMatrix(FILE *file, const Matrix *m)
{ {
fwrite(&m->rows, sizeof(int), 1, file); unsigned int outputDim = (unsigned int)weights.rows;
fwrite(&m->cols, sizeof(int), 1, file); fwrite(&outputDim, sizeof(unsigned int), 1, file);
fwrite(m->buffer, sizeof(MatrixType), m->rows * m->cols, file); if (weights.buffer != NULL)
fwrite(weights.buffer, sizeof(MatrixType), outputDim * inputDim, file);
if (biases.buffer != NULL)
fwrite(biases.buffer, sizeof(MatrixType), outputDim, file);
} }
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{ {
FILE *file = fopen(path, "wb"); FILE *file = fopen(path, "wb");
if (!file) if (!file) return;
return;
const char *header = "__info2_neural_network_file_format__"; const char tag[] = "__info2_neural_network_file_format__";
fwrite(header, sizeof(char), strlen(header), file); fwrite(tag, sizeof(char), strlen(tag), file);
fwrite(&nn.numberOfLayers, sizeof(int), 1, file);
if (nn.numberOfLayers == 0)
{
unsigned int zero = 0;
fwrite(&zero, sizeof(unsigned int), 1, file);
fclose(file);
return;
}
unsigned int inputDim = (unsigned int)nn.layers[0].weights.cols;
fwrite(&inputDim, sizeof(unsigned int), 1, file);
for (int i = 0; i < nn.numberOfLayers; i++) for (int i = 0; i < nn.numberOfLayers; i++)
{ {
erzeugeMatrix(file, &nn.layers[i].weights); writeLayer(file, nn.layers[i].weights, nn.layers[i].biases, inputDim);
erzeugeMatrix(file, &nn.layers[i].biases); inputDim = (unsigned int)nn.layers[i].weights.rows;
} }
unsigned int zero = 0;
fwrite(&zero, sizeof(unsigned int), 1, file);
fclose(file); fclose(file);
} }

1
testFile.info2 Normal file
View File

@ -0,0 +1 @@
some_tag