Compare commits
No commits in common. "284a313751643cc063230c9d9e1f72543408180a" and "fe0728da3e4293ace0a7804302d002e524deb419" have entirely different histories.
284a313751
...
fe0728da3e
@ -6,20 +6,23 @@
|
|||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
|
|
||||||
|
|
||||||
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
|
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label, unsigned short greyScaleTest)
|
||||||
{
|
{
|
||||||
FILE *file = fopen(path, "wb");
|
FILE *file = fopen(path, "wb");
|
||||||
|
|
||||||
if(file != NULL)
|
if(file != NULL)
|
||||||
{
|
{
|
||||||
const char *fileTag = "__info2_image_file_format__";
|
const char *fileTag = "__info2_image_file_format__";
|
||||||
GrayScalePixelType *buffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
|
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
|
||||||
|
|
||||||
if(buffer != NULL)
|
if(zeroBuffer != NULL)
|
||||||
{
|
{
|
||||||
for(unsigned int i = 0; i < (numberOfImages * width * height); i++)
|
if(greyScaleTest)
|
||||||
{
|
{
|
||||||
buffer[i] = (GrayScalePixelType)i;
|
for(unsigned int i = 0; i < (numberOfImages * width * height); i++)
|
||||||
|
{
|
||||||
|
zeroBuffer[i] = (GrayScalePixelType)i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
|
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
|
||||||
@ -29,11 +32,11 @@ static void prepareImageFile(const char *path, unsigned short int width, unsigne
|
|||||||
|
|
||||||
for(int i = 0; i < numberOfImages; i++)
|
for(int i = 0; i < numberOfImages; i++)
|
||||||
{
|
{
|
||||||
fwrite(buffer, sizeof(GrayScalePixelType), width * height, file);
|
fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
|
||||||
fwrite(&label, sizeof(unsigned char), 1, file);
|
fwrite(&label, sizeof(unsigned char), 1, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(buffer);
|
free(zeroBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
@ -46,7 +49,7 @@ void test_readImagesReturnsCorrectNumberOfImages(void)
|
|||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const unsigned short expectedNumberOfImages = 2;
|
const unsigned short expectedNumberOfImages = 2;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1, 0);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
||||||
@ -59,7 +62,7 @@ void test_readImagesReturnsCorrectImageWidth(void)
|
|||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const unsigned short expectedWidth = 10;
|
const unsigned short expectedWidth = 10;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
prepareImageFile(path, expectedWidth, 8, 2, 1, 0);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series->images);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
@ -75,7 +78,7 @@ void test_readImagesReturnsCorrectImageHeight(void)
|
|||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const unsigned short expectedHeight = 10;
|
const unsigned short expectedHeight = 10;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, 8, expectedHeight, 2, 1);
|
prepareImageFile(path, 8, expectedHeight, 2, 1, 0);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series->images);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
@ -92,7 +95,7 @@ void test_readImagesReturnsCorrectLabels(void)
|
|||||||
|
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, 8, 8, 2, expectedLabel);
|
prepareImageFile(path, 8, 8, 2, expectedLabel, 0);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series->labels);
|
TEST_ASSERT_NOT_NULL(series->labels);
|
||||||
@ -129,7 +132,7 @@ void test_readImagesReadsCorrectGrayScales(void)
|
|||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
|
|
||||||
prepareImageFile(path, 8, 8, 1, 1);
|
prepareImageFile(path, 8, 8, 1, 1, 1);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
|
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
|
|||||||
@ -40,8 +40,8 @@ mnistVisualization.o: mnistVisualization.c
|
|||||||
matrixTests: matrix.o matrixTests.c
|
matrixTests: matrix.o matrixTests.c
|
||||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runMatrixTests matrixTests.c matrix.o $(BINARIES)/libunity.a
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runMatrixTests matrixTests.c matrix.o $(BINARIES)/libunity.a
|
||||||
|
|
||||||
neuralNetworkTests: neuralNetwork.o neuralNetworkTests.c matrix.o
|
neuralNetworkTests: neuralNetwork.o neuralNetworkTests.c
|
||||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runNeuralNetworkTests neuralNetworkTests.c neuralNetwork.o matrix.o $(BINARIES)/libunity.a
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runNeuralNetworkTests neuralNetworkTests.c neuralNetwork.o $(BINARIES)/libunity.a
|
||||||
|
|
||||||
imageInputTests: imageInput.o imageInputTests.c
|
imageInputTests: imageInput.o imageInputTests.c
|
||||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runImageInputTests imageInputTests.c imageInput.o $(BINARIES)/libunity.a
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runImageInputTests imageInputTests.c imageInput.o $(BINARIES)/libunity.a
|
||||||
|
|||||||
@ -42,7 +42,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
|||||||
{
|
{
|
||||||
if (matrix.buffer != NULL)
|
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;
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
Matrix result;
|
Matrix result;
|
||||||
|
|
||||||
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
|
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||||
{
|
{
|
||||||
result.rows = 0;
|
result.rows = 0;
|
||||||
result.cols = 0;
|
result.cols = 0;
|
||||||
@ -71,51 +71,17 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matrix1.cols == matrix2.cols)
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for (int i = 0; i < matrix1.rows; i++)
|
||||||
{
|
{
|
||||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
for (int j = 0; j < matrix1.cols; j++)
|
||||||
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, j);
|
||||||
{
|
setMatrixAt(value, result, i, 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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -141,32 +141,6 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
|
|||||||
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows);
|
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) {
|
void setUp(void) {
|
||||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
}
|
}
|
||||||
@ -191,7 +165,6 @@ int main()
|
|||||||
RUN_TEST(test_getMatrixAtFailsOnIndicesOutOfRange);
|
RUN_TEST(test_getMatrixAtFailsOnIndicesOutOfRange);
|
||||||
RUN_TEST(test_setMatrixAtSetsCorrectValue);
|
RUN_TEST(test_setMatrixAtSetsCorrectValue);
|
||||||
RUN_TEST(test_setMatrixAtFailsOnIndicesOutOfRange);
|
RUN_TEST(test_setMatrixAtFailsOnIndicesOutOfRange);
|
||||||
RUN_TEST(test_addSupportsBroadcasting);
|
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
@ -5,89 +5,12 @@
|
|||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "neuralNetwork.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)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
FILE *file = fopen(path, "wb");
|
// TODO
|
||||||
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)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
{
|
{
|
||||||
const char *path = "some__nn_test_file.info2";
|
const char *path = "some__nn_test_file.info2";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user