Compare commits

..

5 Commits
main ... simon

Author SHA1 Message Date
Simon Wiesend
2d12ecee4b
add some comments 2025-11-23 19:59:44 +01:00
Simon Wiesend
adf75b66d2
make matrix multiplication faster 2025-11-23 16:31:18 +01:00
Simon Wiesend
00ac1aa04a
initial prototype 2025-11-23 16:29:59 +01:00
Simon Wiesend
04768db522
update .gitignore 2025-11-23 15:57:18 +01:00
Simon Wiesend
01a13090a5
Merge remote-tracking branch 'origin/neuralNetworkTests' into simon 2025-11-23 15:53:22 +01:00
6 changed files with 184 additions and 140 deletions

5
.gitignore vendored
View File

@ -2,4 +2,7 @@ mnist
runTests
*.o
*.exe
runMatrixTests
runMatrixTests
runImageInputTests
runNeuralNetworkTests
.vscode

View File

@ -6,107 +6,165 @@
#define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__"
//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) {
FILE *file = fopen(path, "rb");
if (!file) {
return NULL;
}
size_t headerLength = strlen(FILE_HEADER_STRING);
char *header = malloc (headerLength + 1);
if(!header) {
return NULL;
}
typedef enum
{
IMG_SUCCESS = 0,
IMG_ERR_INVALID_HEADER, // Header did not match
IMG_ERR_READ, // Failed to read from file, maybe it's too short
IMG_ERR, // General Error
} ImageError;
if (fread(header, sizeof(char), headerLength, file) != headerLength) {
free (header);
return NULL;
}
static ImageError checkHeader(FILE *file);
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file);
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file);
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)
{
unsigned short count = 0, width = 0, height = 0;
FILE *file = openAndReadShort(path, &count, &width, &height);
if (file == 0) {
// It's very important to call calloc here because otherwise clearSeries() might try to free random memory
GrayScaleImageSeries *series = calloc(1, sizeof(GrayScaleImageSeries));
if (series == NULL)
{
return NULL;
}
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
if (!series) {
fclose(file);
FILE *file = fopen(path, "rb");
// If fopen() failed
if (file == NULL)
{
clearSeries(series);
series = NULL;
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;
}
// 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);
return series;
}
//Bereinigt den Speicher
void clearSeries(GrayScaleImageSeries *series)
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series)
{
for (unsigned int i = 0; i < series->count; i++) {
free(series->images[i].buffer);
if (checkHeader(file))
{
return IMG_ERR; // header check failed
}
free(series->images);
free(series->labels);
free(series);
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)
{
if (series)
{
int seriesLen = series->count;
for (size_t imageIdx = 0; imageIdx < seriesLen; imageIdx++)
{
free(series->images[imageIdx].buffer);
}
free(series->images);
free(series->labels);
free(series);
}
}

View File

@ -67,18 +67,14 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return createMatrix(0, 0);
}
// matrices not compatible
if (matrix1.rows != matrix2.rows)
{
clearMatrix(&resMat);
return resMat;
}
// check if broadcasting is possible
if (matrix1.cols != matrix2.cols)
{
// matrix1 is a vector
if (matrix1.cols == 1)
if (matrix1.rows != matrix2.rows)
{
clearMatrix(&resMat);
return resMat;
}
else if (matrix1.cols == 1)
{
// broadcast vector
for (size_t m = 0; m < matrix2.rows; m++)
@ -90,7 +86,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
}
return resMat;
}
// matrix2 is a vector
else if (matrix2.cols == 1)
{
// broadcast vector
@ -103,7 +98,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
}
return resMat;
}
// addition not possible
else
{
clearMatrix(&resMat);
@ -115,7 +109,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
{
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);
}
}
@ -123,32 +116,37 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return resMat;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix A, const Matrix B)
{
if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL)
if (A.cols != B.rows || A.buffer == NULL || B.buffer == NULL)
{
return createMatrix(0, 0);
}
int rows = matrix1.rows, cols = matrix2.cols;
Matrix resMat = createMatrix(rows, cols);
int rows = A.rows, cols = B.cols;
Matrix C = createMatrix(rows, cols);
if (resMat.buffer == NULL)
if (C.buffer == NULL)
{
return createMatrix(0, 0);
}
for (size_t rowIdx = 0; rowIdx < rows; rowIdx++)
// M = Rows, K = Common Dim, N = Cols
size_t M = A.rows, K = A.cols, N = B.cols;
for (size_t i = 0; i < M; i++)
{
for (size_t colIdx = 0; colIdx < cols; colIdx++)
for (size_t k = 0; k < K; k++)
{
int curCellVal = 0;
for (size_t k = 0; k < matrix1.cols; k++)
MatrixType valA = A.buffer[i * K + k];
for (size_t j = 0; j < N; j++)
{
curCellVal += getMatrixAt(matrix1, rowIdx, k) * getMatrixAt(matrix2, k, colIdx);
// C[i, j] += A[i, k] * B[k, j];
// 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,9 +8,10 @@ typedef float MatrixType;
// Matrixtyp
typedef struct Matrix
{
MatrixType *buffer;
size_t rows;
size_t cols;
MatrixType *buffer;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);

View File

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

View File

@ -1 +0,0 @@
some_tag