Compare commits

...

26 Commits

Author SHA1 Message Date
=
2a4ff89df7 last fix 2025-11-27 16:32:06 +01:00
=
7d30feef54 added some more comments 2025-11-27 15:40:57 +01:00
=
4e2dd7b4d4 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/bruennerda98937/info2NeuronalesNetzBruennnerKobNew 2025-11-27 15:28:29 +01:00
=
22b79b5cd3 Added functionality to imageInput.c 2025-11-27 15:28:27 +01:00
D2A62006
1f1a7a23b1 Cleanup and edit comments neuralNetwork FIles 2025-11-27 13:23:21 +01:00
D2A62006
0c85fb19bc refractor and cleanup matrix.c 2025-11-27 13:20:06 +01:00
D2A62006
e0ded7b738 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/bruennerda98937/info2NeuronalesNetzBruennnerKobNew 2025-11-27 11:54:50 +01:00
D2A62006
880514b55f implement prepareNeuralNetworkFile() 2025-11-27 11:54:47 +01:00
=
419296af9e last short fix for the day in imageInput.c 2025-11-26 21:55:17 +01:00
=
4d1908ed27 added outline for imageInput.c 2025-11-26 21:44:58 +01:00
D2A62006
ce371e4228 Clean up Matrix.c 2025-11-26 18:37:26 +01:00
D2A62006
98aa5f354a Merge remote-tracking branch 'origin/devKob' 2025-11-26 18:35:55 +01:00
=
15d74d972d added shellscript for matrixTests 2025-11-26 18:34:53 +01:00
D2A62006
5ce0982e17 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/bruennerda98937/info2NeuronalesNetzBruennnerKobNew 2025-11-26 18:33:33 +01:00
D2A62006
cfac9ae60e Matrix add broadcasting support 2025-11-26 18:33:30 +01:00
=
e7373fe73a fixed some spacing 2025-11-26 18:28:45 +01:00
=
9bd18d3681 Fixed editing of matrix values by defenition 2025-11-26 18:22:30 +01:00
D2A62006
04d9b35c36 Refractor Matrix add 2025-11-26 18:04:17 +01:00
D2A62006
57bf46bfc5 Fix matrix multiply 2025-11-26 17:43:41 +01:00
D2A62006
2783663ab9 fix minor issues 2025-11-26 11:10:28 +01:00
=
a2f2be5592 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/bruennerda98937/info2NeuronalesNetzBruennnerKobNew 2025-11-25 15:01:53 +01:00
=
82c335ca89 Fixed UnitTest Errorrs in matrix.c 2025-11-25 15:01:21 +01:00
D2A62006
f8456a7d5e Update gitignore 2025-11-20 17:04:09 +01:00
=
05a29b50c6 fixed previously missed error 2025-11-20 16:52:31 +01:00
D2A62006
8ef0d7688f Merge branch 'dev_kob' 2025-11-20 16:50:33 +01:00
D2A62006
32cf681534 matrix createMatrix working 2025-11-20 16:47:34 +01:00
8 changed files with 227 additions and 90 deletions

2
.gitignore vendored
View File

@ -1,5 +1,7 @@
mnist
runTests
runMatrixTests
runImageInputTests
runNeuralNetworkTests
*.o
*.exe

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "imageInput.h"
@ -6,17 +7,115 @@
#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)
/// @brief Gets the next char value from specified and opened file in the moment that is only ONE short int.
/// @param openedFile stream FILE, from which to read
static int getCharValueFromFile(FILE* openedFile) {
int addToFile = 0;
if (openedFile == NULL) return 0;
addToFile = fgetc(openedFile);
addToFile += fgetc(openedFile) * 256;
return addToFile;
}
GrayScaleImageSeries* readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
// uint16_t
FILE* openFile;
openFile = fopen(path, "rb");
// file Could not be opened/does not exist
if (openFile != NULL) {
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING)+1 * sizeof(char));
int numberOfImages = 0;
int width = 0;
int height = 0;
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
char tmpFileChar = fgetc(openFile);
if (tmpFileChar == EOF) {
fclose(openFile);
return NULL;
}
actualFileTag[i] = tmpFileChar;
}
actualFileTag[strlen(FILE_HEADER_STRING)] = '\0';
// checks if the files are equal
int fileTagEqual = strcmp(actualFileTag, FILE_HEADER_STRING);
//we only need the fileTag to verify its an image for our series.
free(actualFileTag);
actualFileTag = NULL;
//only if equal, the method continues
if (fileTagEqual == 0) {
numberOfImages = getCharValueFromFile(openFile);
// no Images in series -> No image-series
if (numberOfImages == 0) {
fclose(openFile);
return NULL;
}
width = getCharValueFromFile(openFile);
height = getCharValueFromFile(openFile);
// no height/width --> impossible file
if(height == 0 || width == 0) {
fclose(openFile);
return NULL;
}
//all the starting parameters are set --> the images can be read and stored
series = malloc(sizeof(GrayScaleImageSeries));
series->count = 0;
series->images = malloc(numberOfImages * sizeof(GrayScaleImage));
series->labels = malloc(numberOfImages * sizeof(unsigned char));
for (int i = 0; i < numberOfImages; i++) {
series->images[i].height = height;
series->images[i].width = width;
series->images[i].buffer = malloc(width * height);
for (int j = 0; j < (width * height); j++) {
// allocating the actual matrix image for image
series->images[i].buffer[j] = fgetc(openFile);
}
//rest of the values that only affect the image itself
series->labels[i] = fgetc(openFile);
series->count++;
}
}
// the fclose happens here, since every other path closes the file beforehand
fclose (openFile);
}
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
if (series == NULL) return;
for (int i = 0; i < series->count; i++) {
free(series->images[i].buffer);
series->images[i].buffer = NULL;
}
free(series->images);
series->images = NULL;
free(series->labels);
series->labels = NULL;
free(series);
series = NULL;
}

165
matrix.c
View File

@ -2,46 +2,6 @@
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
/*
Alte Funktion
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix m;
m.rows = rows;
m.cols = cols;
m.data = NULL;
if(rows == 0 || cols == 0){
m.rows = m.cols = 0;
return m;
}
m.data = malloc(rows * sizeof *m.data);
if(!m.data){
m.rows = m.cols = 0;
return m;
}
for(unsigned int i = 0; i < rows; i++){
m.data[i] = malloc(cols * sizeof *m.data[i]);
if(!m.data[i]){
for(unsigned int j = 0; j < i; j++){
free(m.data[j]);
}
free(m.data);
m.data = NULL;
m.rows = m.cols = 0;
return m;
}
}
return m;
}
*/
Matrix createMatrix(size_t rows, size_t cols)
{
Matrix m;
@ -54,7 +14,7 @@ Matrix createMatrix(size_t rows, size_t cols)
return m;
}
// Single allocation for entire matrix
//Allocate Matrix buffer
m.buffer = malloc(rows * cols * sizeof(MatrixType));
if(!m.buffer){
@ -62,7 +22,7 @@ Matrix createMatrix(size_t rows, size_t cols)
return m;
}
// Initialize (optional)
//Initialize matrix with default value
for(unsigned int i = 0; i < rows * cols; i++){
m.buffer[i] = UNDEFINED_MATRIX_VALUE;
}
@ -70,77 +30,120 @@ Matrix createMatrix(size_t rows, size_t cols)
return m;
}
void clearMatrix(Matrix *matrix)
void clearMatrix(Matrix* matrix)
{
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols;j++) {
matrix->buffer[i-1 + matrix->rows*(j-1)] = UNDEFINED_MATRIX_VALUE;
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE;
matrix->buffer[j + matrix->cols * i] = UNDEFINED_MATRIX_VALUE;
}
}
free(matrix->buffer);
matrix->rows = 0;
matrix->cols = 0;
matrix->buffer = NULL;
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)] = value;
//checks, if the given incies are allowed
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value;
matrix.buffer[colIdx + matrix.cols * rowIdx] = value;
}
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
MatrixType returnVal;
returnVal = matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)];
return returnVal;
//checks, if the given indices are allowed
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
MatrixType returnVal;
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)];
returnVal = matrix.buffer[colIdx + rowIdx * matrix.cols];
return returnVal;
}
else return 0;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
//check if the matrices are able to be added (same size)
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
//size of the matrices should be the same, if the addition is supposed to happen
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
unsigned int doBroadcast = 0;
Matrix larger, smaller;
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
larger = matrix1;
smaller = matrix2;
}
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
{
larger = matrix1;
smaller = matrix2;
doBroadcast = 1;
}
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
{
larger = matrix2;
smaller = matrix1;
doBroadcast = 1;
}
else{
Matrix m = {NULL, 0, 0};
return m;
}
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
if(doBroadcast){
//Broadcasting
for(int i = 0; i < outputMatrix.rows; i++){
MatrixType broadcastValue = smaller.buffer[i];
for(int j = 0; j < outputMatrix.cols; j++){
outputMatrix.buffer[i * outputMatrix.cols + j] = larger.buffer[i * larger.cols + j] + broadcastValue;
}
}
} else{
//Classic execution
for (int i = 0; i < matrix1.rows;i++) {
for (int j = 0; j < matrix1.cols; j++) {
// how this should work in normal Matrix version:
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
outputMatrix.buffer[i + outputMatrix.rows* j] = matrix1.buffer[i + matrix1.rows* j] + matrix2.buffer[i + matrix2.rows * j];
outputMatrix.buffer[i * outputMatrix.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
return outputMatrix;
} else {
//the matrix could not be added, since the matrix sizes are not set correct.
Matrix m;
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
return outputMatrix;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
//check, if the matrices can be multiplied
if (matrix1.rows == matrix2.cols) {
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
for(int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix2.cols; j++) {
for (int k = 0; k < matrix2.rows; k++) {
// how this should work in normal Matrix version:
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
outputMatrix.buffer[i + outputMatrix.rows* j] = matrix1.buffer[i + matrix1.rows* k] * matrix2.buffer[k + matrix2.rows*j];
}
}
}
return outputMatrix;
} else {
//the matrix could not be added, since the matrix sizes are not set correct.
Matrix m;
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
if(matrix1.cols != matrix2.rows){
Matrix m = {NULL, 0, 0};
return m;
}
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
if(!outputMatrix.buffer){
Matrix m = {NULL, 0, 0};
return m;
}
//Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
for(int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix2.cols; j++) {
for (int k = 0; k < matrix1.cols; k++) {
// how this should work in normal Matrix version:
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
//outputMatrix.buffer[i + outputMatrix.rows * j] += matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
outputMatrix.buffer[i * outputMatrix.cols + j] += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[j + matrix2.cols * k];
}
}
}
return outputMatrix;
}

View File

@ -7,9 +7,9 @@ typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct Matrix {
MatrixType *buffer;
size_t rows; //X-Element
size_t cols; //Y-Element
MatrixType *buffer;
} Matrix;

1
matrixTests.sh Normal file
View File

@ -0,0 +1 @@
make clean && make matrixTests && ./runMatrixTests

View File

@ -164,7 +164,6 @@ NeuralNetwork loadModel(const char *path)
assignActivations(model);
}
return model;
}

1
neuralNetwork.sh Normal file
View File

@ -0,0 +1 @@
make clean && make && make neuralNetworkTests

View File

@ -8,9 +8,41 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{
// TODO
}
FILE *file = fopen(path, "wb");
if(file != NULL){
const char *fileTag = "__info2_neural_network_file_format__";
//Write header
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
//Write the input dimension of the first layer
if(nn.numberOfLayers > 0){
fwrite(&nn.layers[0].weights.cols, sizeof(int), 1, file);
}
//Write each layer into file
for(int i = 0; i < nn.numberOfLayers; i++){
//Write output dimension
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
//Write weight data
int weightSize = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightSize, file);
//Write bias data
int biasSize = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasSize, file);
}
//EOF Terminator
int zero = 0;
fwrite(&zero, sizeof(int), 1, file);
fclose(file);
}
}
void test_loadModelReturnsCorrectNumberOfLayers(void)
{
const char *path = "some__nn_test_file.info2";