Compare commits

..

1 Commits

Author SHA1 Message Date
=
306c1e09e2 fixed previously missed bug 2025-11-20 16:50:13 +01:00
8 changed files with 89 additions and 226 deletions

2
.gitignore vendored
View File

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

View File

@ -1,5 +1,4 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "imageInput.h"
@ -7,115 +6,17 @@
#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
/// @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)
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
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;
}

163
matrix.c
View File

@ -2,6 +2,46 @@
#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;
@ -14,7 +54,7 @@ Matrix createMatrix(size_t rows, size_t cols)
return m;
}
//Allocate Matrix buffer
// Single allocation for entire matrix
m.buffer = malloc(rows * cols * sizeof(MatrixType));
if(!m.buffer){
@ -22,7 +62,7 @@ Matrix createMatrix(size_t rows, size_t cols)
return m;
}
//Initialize matrix with default value
// Initialize (optional)
for(unsigned int i = 0; i < rows * cols; i++){
m.buffer[i] = UNDEFINED_MATRIX_VALUE;
}
@ -30,120 +70,77 @@ 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++) {
// 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;
matrix->buffer[i + matrix->rows* j] = 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)
{
//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;
}
matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)] = value;
}
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;
MatrixType returnVal;
returnVal = matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)];
return returnVal;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
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
//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);
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];
outputMatrix.buffer[i + outputMatrix.rows* j] = matrix1.buffer[i + matrix1.rows* j] + matrix2.buffer[i + 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;
return m;
}
return outputMatrix;
}
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];
//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;
return m;
}
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;

View File

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

View File

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

View File

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

View File

@ -8,41 +8,9 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{
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);
}
// TODO
}
void test_loadModelReturnsCorrectNumberOfLayers(void)
{
const char *path = "some__nn_test_file.info2";