Author SHA1 Message Date
D2A62006 2b388ab748 fix image input.c 2025-11-27 12:19:07 +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
= a3b3605ff9 fixed error with matrix buffer call 2025-11-20 16:47:37 +01:00
= e0f192f774 added return value to add function in matrix.c 2025-11-20 16:29:02 +01:00
= 4da0a796c1 Added matrix funcionality methods 2025-11-20 16:25:20 +01:00
= 19910ab17f Added addition and multiplication to new version, still no functionality with accessing the matrix information 2025-11-20 14:42:57 +01:00
9 changed files with 384 additions and 49 deletions
+2
View File
@@ -1,5 +1,7 @@
mnist mnist
runTests runTests
runMatrixTests runMatrixTests
runImageInputTests
runNeuralNetworkTests
*.o *.o
*.exe *.exe
+133 -2
View File
@@ -6,17 +6,148 @@
#define BUFFER_SIZE 100 #define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__" #define FILE_HEADER_STRING "__info2_image_file_format__"
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
/// @brief Reads a value in little-endian format from file
/// @param openedFile stream FILE, from which to read
/// @param bytes how many bytes to read
static unsigned int readLittleEndian(FILE* openedFile, int bytes) {
unsigned int value = 0;
if (openedFile == NULL) return 0;
for (int i = 0; i < bytes; i++) {
int tmp = fgetc(openedFile);
if (tmp == EOF) return 0;
value |= ((unsigned int)tmp) << (i * 8);
}
return value;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path) GrayScaleImageSeries *readImages(const char *path)
{ {
GrayScaleImageSeries *series = NULL; FILE* openFile = fopen(path, "rb");
// file Could not be opened/does not exist
if (openFile == NULL) {
return NULL;
}
char actualFileTag[strlen(FILE_HEADER_STRING) + 1];
size_t tagLength = strlen(FILE_HEADER_STRING);
if (fread(actualFileTag, 1, tagLength, openFile) != tagLength) {
fclose(openFile);
return NULL;
}
actualFileTag[tagLength] = '\0';
// checks if the files are equal
if (strcmp(actualFileTag, FILE_HEADER_STRING) != 0) {
fclose(openFile);
return NULL;
}
unsigned int numberOfImages = readLittleEndian(openFile, 2);
// no Images in series -> No image-series
if (numberOfImages == 0) {
fclose(openFile);
return NULL;
}
unsigned int width = readLittleEndian(openFile, 2);
unsigned int height = readLittleEndian(openFile, 2);
// 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
GrayScaleImageSeries* series = malloc(sizeof(GrayScaleImageSeries));
if (series == NULL) {
fclose(openFile);
return NULL;
}
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
if (images == NULL || labels == NULL) {
free(images);
free(labels);
free(series);
fclose(openFile);
return NULL;
}
series->count = 0;
series->images = images;
series->labels = labels;
for (unsigned int i = 0; i < numberOfImages; i++) {
// allocating the actual matrix image for each image
images[i].buffer = malloc(width * height);
if (images[i].buffer == NULL) {
for (unsigned int k = 0; k < i; k++) {
free(images[k].buffer);
}
free(images);
free(labels);
free(series);
fclose(openFile);
return NULL;
}
images[i].height = height;
images[i].width = width;
if (fread(images[i].buffer, 1, width * height, openFile) != width * height) {
for (unsigned int k = 0; k <= i; k++) {
free(images[k].buffer);
}
free(images);
free(labels);
free(series);
fclose(openFile);
return NULL;
}
//rest of the values that only affect the image itself
int label = fgetc(openFile);
if (label == EOF) {
for (unsigned int k = 0; k <= i; k++) {
free(images[k].buffer);
}
free(images);
free(labels);
free(series);
fclose(openFile);
return NULL;
}
series->labels[i] = (unsigned char)label;
series->count++;
}
fclose(openFile);
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)
{ {
if (series == NULL) return;
for (int i = 0; i < series->count; i++) {
free(series->images[i].buffer);
}
free(series->images);
free(series->labels);
free(series);
} }
+115
View File
@@ -0,0 +1,115 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imageInput.h"
#define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__"
/// @brief Gets the next char value from specified and opened file
/// @param openedFile stream FILE, from which to read
/// @param iterations how many chars are being taken from (1 char equals 2 Hexdecimals equals 8bit)
static int getCharValueFromFile(FILE* openedFile, int iterations) {
int addToFile = 0;
if (openedFile == NULL) return 0;
for (int i = 0; i < iterations; i++) {
int tmp = fgetc(openedFile);
//If the File ends, the method returns a '0'
if (openedFile == EOF) return addToFile;
addToFile += tmp;
}
return addToFile;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
FILE* openFile;
openFile = fopen(path, "rb");
// file Could not be opened/does not exist
if (openFile != NULL) {
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING) * sizeof(char));
int numberOfImages = 0;
int width = 0;
int height = 0;
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
actualFileTag += fgetc(openFile);
}
// checks if the files are equal: strcmp should return '0' --> convert it to '1' for 'true'
int fileTagEqual = !strcmp(actualFileTag, FILE_HEADER_STRING);
//we only need the fileTag to verify its an image for our series.
free(actualFileTag);
actualFileTag = NULL;
if (fileTagEqual) {
numberOfImages = getCharValueFromFile(openFile, 2);
// no Images in series -> No image-series
if (numberOfImages == 0) {
fclose(openFile);
return series;
}
width = getCharValueFromFile(openFile, 2);
height = getCharValueFromFile(openFile, 2);
// no height/width --> impossible file
if(height == 0 || width == 0) {
fclose(openFile);
return series;
}
//all the starting parameters are set --> the images can be read and stored
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
series->count = 0;
series->images = images;
series->labels = labels;
for (int i = 0; i < numberOfImages; i++) {
for (int j = 0; j < width * height; j++) {
// allocating the actual matrix image for image
images[i].buffer = malloc(width * height);
images[i].height = height;
images[i].width = width;
images[i].buffer[j] = fgetc(openFile);
}
//rest of the values that only affect the image itself
series->labels[i] = fgetc(openFile);
series->count++;
}
}
}
fclose(openFile);
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
for (int i = 0; i < series->count; i++) {
free(series->images[i].buffer);
}
free(series->images);
series->images = NULL;
free(series->labels);
series->labels = NULL;
}
+95 -42
View File
@@ -1,48 +1,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
#include <stdbool.h>
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
/* Matrix createMatrix(size_t rows, size_t cols)
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 cols, size_t rows)
{ {
Matrix m; Matrix m;
m.rows = rows; m.rows = rows;
@@ -73,25 +36,115 @@ Matrix createMatrix(size_t cols, size_t rows)
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols;j++) {
// 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) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
//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 getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
//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) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
bool doBroadcast = false;
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 = true;
}
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
{
larger = matrix2;
smaller = matrix1;
doBroadcast = true;
}
else{
Matrix m = {NULL, 0, 0};
return m;
}
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
if(doBroadcast){
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{
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.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
}
return outputMatrix;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
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;
} }
+2 -2
View File
@@ -7,9 +7,9 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct Matrix { typedef struct Matrix {
size_t rows;
size_t cols;
MatrixType *buffer; MatrixType *buffer;
size_t rows; //X-Element
size_t cols; //Y-Element
} Matrix; } Matrix;
+1
View File
@@ -0,0 +1 @@
make clean && make matrixTests && ./runMatrixTests
+1 -1
View File
@@ -164,7 +164,7 @@ NeuralNetwork loadModel(const char *path)
assignActivations(model); assignActivations(model);
} }
printf("%d\n", model.numberOfLayers);
return model; return model;
} }
+1
View File
@@ -0,0 +1 @@
make clean && make && make neuralNetworkTests
+34 -2
View File
@@ -8,9 +8,41 @@
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){
const char *fileTag = "__info2_neural_network_file_format__";
// Write file 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 dimensions and data for each layer
for(int i = 0; i < nn.numberOfLayers; i++){
// Write output dimension (rows of weights)
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
// Write weight matrix 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 matrix data
int biasSize = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasSize, file);
}
// Write terminating 0 to signal end of layers
int zero = 0;
fwrite(&zero, sizeof(int), 1, file);
fclose(file);
}
}
void test_loadModelReturnsCorrectNumberOfLayers(void) void test_loadModelReturnsCorrectNumberOfLayers(void)
{ {
const char *path = "some__nn_test_file.info2"; const char *path = "some__nn_test_file.info2";