Compare commits

...

7 Commits

Author SHA1 Message Date
284a313751 completed prepareNeuralNetworkFile 2025-11-13 23:53:15 +01:00
c7c68a0ce0 fixed setMatrixAt 2025-11-13 23:52:53 +01:00
c0760a6646 fixed error neuralNetworkTests 2025-11-13 23:52:34 +01:00
34a471bda6 brodcasting test hinzugefügt 2025-11-11 18:51:04 +01:00
f8035cc4db funktionalität erweitert add 2025-11-10 20:39:35 +01:00
db7617e046 variablen Namen angepasst 2025-11-10 13:08:14 +01:00
47ff0906cc improved testfile 2025-11-04 18:28:59 +01:00
5 changed files with 161 additions and 26 deletions

View File

@ -6,23 +6,20 @@
#include "imageInput.h"
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label, unsigned short greyScaleTest)
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
{
FILE *file = fopen(path, "wb");
if(file != NULL)
{
const char *fileTag = "__info2_image_file_format__";
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
GrayScalePixelType *buffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
if(zeroBuffer != NULL)
if(buffer != NULL)
{
if(greyScaleTest)
for(unsigned int i = 0; i < (numberOfImages * width * height); i++)
{
for(unsigned int i = 0; i < (numberOfImages * width * height); i++)
{
zeroBuffer[i] = (GrayScalePixelType)i;
}
buffer[i] = (GrayScalePixelType)i;
}
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
@ -32,11 +29,11 @@ static void prepareImageFile(const char *path, unsigned short int width, unsigne
for(int i = 0; i < numberOfImages; i++)
{
fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
fwrite(buffer, sizeof(GrayScalePixelType), width * height, file);
fwrite(&label, sizeof(unsigned char), 1, file);
}
free(zeroBuffer);
free(buffer);
}
fclose(file);
@ -49,7 +46,7 @@ void test_readImagesReturnsCorrectNumberOfImages(void)
GrayScaleImageSeries *series = NULL;
const unsigned short expectedNumberOfImages = 2;
const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1, 0);
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
@ -62,7 +59,7 @@ void test_readImagesReturnsCorrectImageWidth(void)
GrayScaleImageSeries *series = NULL;
const unsigned short expectedWidth = 10;
const char *path = "testFile.info2";
prepareImageFile(path, expectedWidth, 8, 2, 1, 0);
prepareImageFile(path, expectedWidth, 8, 2, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images);
@ -78,7 +75,7 @@ void test_readImagesReturnsCorrectImageHeight(void)
GrayScaleImageSeries *series = NULL;
const unsigned short expectedHeight = 10;
const char *path = "testFile.info2";
prepareImageFile(path, 8, expectedHeight, 2, 1, 0);
prepareImageFile(path, 8, expectedHeight, 2, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images);
@ -95,7 +92,7 @@ void test_readImagesReturnsCorrectLabels(void)
GrayScaleImageSeries *series = NULL;
const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, 2, expectedLabel, 0);
prepareImageFile(path, 8, 8, 2, expectedLabel);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->labels);
@ -132,7 +129,7 @@ void test_readImagesReadsCorrectGrayScales(void)
GrayScaleImageSeries *series = NULL;
const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, 1, 1, 1);
prepareImageFile(path, 8, 8, 1, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);

View File

@ -40,8 +40,8 @@ mnistVisualization.o: mnistVisualization.c
matrixTests: matrix.o matrixTests.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runMatrixTests matrixTests.c matrix.o $(BINARIES)/libunity.a
neuralNetworkTests: neuralNetwork.o neuralNetworkTests.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runNeuralNetworkTests neuralNetworkTests.c neuralNetwork.o $(BINARIES)/libunity.a
neuralNetworkTests: neuralNetwork.o neuralNetworkTests.c matrix.o
$(CC) $(CFLAGS) -I$(unityfolder) -o runNeuralNetworkTests neuralNetworkTests.c neuralNetwork.o matrix.o $(BINARIES)/libunity.a
imageInputTests: imageInput.o imageInputTests.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runImageInputTests imageInputTests.c imageInput.o $(BINARIES)/libunity.a

View File

@ -42,7 +42,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
{
if (matrix.buffer != NULL)
{
if (rowIdx < matrix.rows || colIdx < matrix.cols)
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
@ -63,7 +63,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
{
result.rows = 0;
result.cols = 0;
@ -71,17 +71,51 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return result;
}
result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows; i++)
if (matrix1.cols == matrix2.cols)
{
for (int j = 0; j < matrix1.cols; j++)
result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows; i++)
{
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j);
for (int j = 0; j < matrix1.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j);
}
}
return result;
}
if (matrix1.cols == 1 && matrix2.cols > 1)
{
result = createMatrix(matrix1.rows, matrix2.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j);
}
}
return result;
}
else if (matrix2.cols == 1 && matrix1.cols > 1)
{
result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix1.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
setMatrixAt(value, result, i, j);
}
}
return result;
}
//Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}

View File

@ -141,6 +141,32 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows);
}
void test_addSupportsBroadcasting(void)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
MatrixType buffer2[] = {7, 8};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2};
Matrix result1 = add(matrix1, matrix2);
Matrix result2 = add(matrix2, matrix1);
float expectedResults[] = {8, 9, 10, 12, 13, 14};
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows);
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols);
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result2.rows);
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result2.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result1.rows * result1.cols);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result1.buffer, result1.cols * result1.rows);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result2.rows * result2.cols);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows);
free(result1.buffer);
free(result2.buffer);
}
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
@ -165,6 +191,7 @@ int main()
RUN_TEST(test_getMatrixAtFailsOnIndicesOutOfRange);
RUN_TEST(test_setMatrixAtSetsCorrectValue);
RUN_TEST(test_setMatrixAtFailsOnIndicesOutOfRange);
RUN_TEST(test_addSupportsBroadcasting);
return UNITY_END();
}

View File

@ -5,12 +5,89 @@
#include "unity.h"
#include "neuralNetwork.h"
/*
################
Aufbau Test File
################
HEADER
inputDim
outputDim
-- Layer 1 --
weights (outputDim * inputDim * MatrixType)
biases (outputDim * MatrixType)
outputDim
-- Layer 2 --
weights
biases
...
...
-- Layer n --
weights
biases
outputDim = 0 => Ende
*/
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{
// TODO
FILE *file = fopen(path, "wb");
if (file)
{
const char *fileTag = "__info2_neural_network_file_format__";
fwrite(fileTag, 1, strlen(fileTag), file);
//Stopt loadModel, falls keine Layer vorhanden
if (nn.numberOfLayers == 0)
{
int zero = 0;
fwrite(&zero, sizeof(int), 1, file);
fclose(file);
return;
}
// input und output dimension schreiben
int inputDim = nn.layers[0].weights.cols;
int outputDim = nn.layers[0].weights.rows;
fwrite(&inputDim, sizeof(int), 1, file);
fwrite(&outputDim, sizeof(int), 1, file);
// erstes Layer schreiben
int weightCount = nn.layers[0].weights.rows * nn.layers[0].weights.cols;
fwrite(nn.layers[0].weights.buffer, sizeof(MatrixType), weightCount, file);
int biasesCount = nn.layers[0].biases.rows * nn.layers[0].biases.cols;
fwrite(nn.layers[0].biases.buffer, sizeof(MatrixType), biasesCount, file);
// für weiter Layer nur outputDimension schreiben
for (unsigned int i = 1; i < nn.numberOfLayers; i++)
{
outputDim = nn.layers[i].weights.rows;
fwrite(&outputDim, sizeof(int), 1, file);
weightCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightCount, file);
biasesCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasesCount, file);
}
// loadModel ließt 0 ein -> Stop
int fileEnd = 0;
fwrite(&fileEnd, sizeof(int), 1, file);
}
fclose(file);
}
void test_loadModelReturnsCorrectNumberOfLayers(void)
{
const char *path = "some__nn_test_file.info2";