Compare commits
2 Commits
284a313751
...
8efd1a3bfb
| Author | SHA1 | Date | |
|---|---|---|---|
| 8efd1a3bfb | |||
| 4e238675c8 |
@ -8,15 +8,116 @@
|
|||||||
|
|
||||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||||
|
|
||||||
|
/* ---------------- Hilfsfunktionen ---------------- */
|
||||||
|
|
||||||
|
static int readHeader(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
|
||||||
|
{
|
||||||
|
unsigned short headerlength = strlen(FILE_HEADER_STRING);
|
||||||
|
char buffer[headerlength + 1];
|
||||||
|
if (fread(buffer, 1, headerlength, file) != headerlength)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
buffer[headerlength] = '\0';
|
||||||
|
|
||||||
|
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int readSingleImage(FILE *file, GrayScaleImage *image)
|
||||||
|
{
|
||||||
|
unsigned int pixelCount = image->width * image->height;
|
||||||
|
|
||||||
|
if (fread(image->buffer, sizeof(GrayScalePixelType), pixelCount, file) != pixelCount)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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 *file = fopen(path, "rb");
|
||||||
|
if (!file)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
unsigned short count, width, height;
|
||||||
|
|
||||||
|
if (!readHeader(file, &count, &width, &height)) {
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
series->count = count;
|
||||||
|
series->images = malloc(count * sizeof(GrayScaleImage));
|
||||||
|
series->labels = malloc(count * sizeof(unsigned char));
|
||||||
|
|
||||||
|
if (!series || !series->images || !series->labels)
|
||||||
|
{
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
series->images[i].width = width;
|
||||||
|
series->images[i].height = height;
|
||||||
|
series->images[i].buffer = malloc(width * height * sizeof(GrayScalePixelType));
|
||||||
|
|
||||||
|
//malloc prüfen
|
||||||
|
if (!series->images[i].buffer)
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Image einlesen + prüfen
|
||||||
|
if (!readSingleImage(file, &series->images[i])) {
|
||||||
|
fclose(file);
|
||||||
|
clearSeries(series);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// label einlesen
|
||||||
|
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
|
||||||
|
fclose(file);
|
||||||
|
clearSeries(series);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
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)
|
||||||
|
{
|
||||||
|
for(unsigned int i = 0; i < series->count; i++)
|
||||||
|
{
|
||||||
|
free(series->images[i].buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -13,10 +13,15 @@ static void prepareImageFile(const char *path, unsigned short int width, unsigne
|
|||||||
if(file != NULL)
|
if(file != NULL)
|
||||||
{
|
{
|
||||||
const char *fileTag = "__info2_image_file_format__";
|
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)
|
||||||
{
|
{
|
||||||
|
for(unsigned int i = 0; i < (numberOfImages * width * height); i++)
|
||||||
|
{
|
||||||
|
buffer[i] = (GrayScalePixelType)i;
|
||||||
|
}
|
||||||
|
|
||||||
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
|
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
|
||||||
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
|
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
|
||||||
fwrite(&width, sizeof(width), 1, file);
|
fwrite(&width, sizeof(width), 1, file);
|
||||||
@ -24,11 +29,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(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
|
fwrite(buffer, sizeof(GrayScalePixelType), width * height, file);
|
||||||
fwrite(&label, sizeof(unsigned char), 1, file);
|
fwrite(&label, sizeof(unsigned char), 1, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(zeroBuffer);
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
@ -54,7 +59,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, 8, expectedWidth, 2, 1);
|
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
||||||
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);
|
||||||
@ -70,7 +75,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, expectedHeight, 8, 2, 1);
|
prepareImageFile(path, 8, expectedHeight, 2, 1);
|
||||||
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);
|
||||||
@ -119,6 +124,27 @@ void test_readImagesFailsOnWrongFileTag(void)
|
|||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_readImagesReadsCorrectGrayScales(void)
|
||||||
|
{
|
||||||
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
const char *path = "testFile.info2";
|
||||||
|
|
||||||
|
prepareImageFile(path, 8, 8, 1, 1);
|
||||||
|
series = readImages(path);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
|
TEST_ASSERT_EQUAL_UINT16(1, series->count);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < (8 * 8); i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i, series->images->buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSeries(series);
|
||||||
|
remove(path);
|
||||||
|
}
|
||||||
|
|
||||||
void setUp(void) {
|
void setUp(void) {
|
||||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
}
|
}
|
||||||
@ -138,6 +164,7 @@ int main()
|
|||||||
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
||||||
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
||||||
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
||||||
|
RUN_TEST(test_readImagesReadsCorrectGrayScales);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
@ -6,30 +6,146 @@
|
|||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix m;
|
||||||
|
|
||||||
|
if (rows == 0 || cols == 0)
|
||||||
|
{
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
m.rows = rows;
|
||||||
|
m.cols = cols;
|
||||||
|
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
if (matrix != NULL)
|
||||||
|
{
|
||||||
|
if (matrix->buffer != NULL)
|
||||||
|
{
|
||||||
|
free(matrix->buffer);
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if (matrix.buffer != NULL)
|
||||||
|
{
|
||||||
|
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
|
{
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
|
||||||
|
{
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
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, 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
|
||||||
|
{
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for (int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType sum = 0;
|
||||||
|
|
||||||
|
for (int k = 0; k < matrix1.cols; k++)
|
||||||
|
{
|
||||||
|
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
|
}
|
||||||
|
setMatrixAt(sum, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
@ -7,6 +7,13 @@ typedef float MatrixType;
|
|||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
MatrixType *buffer;
|
||||||
|
int rows;
|
||||||
|
int cols;
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
void clearMatrix(Matrix *matrix);
|
||||||
|
|||||||
@ -5,12 +5,80 @@
|
|||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
################
|
||||||
|
Aufbau Test File
|
||||||
|
################
|
||||||
|
|
||||||
|
|
||||||
|
HEADER
|
||||||
|
|
||||||
|
inputDim
|
||||||
|
outputDim
|
||||||
|
|
||||||
|
-- Layer 1 --
|
||||||
|
weights
|
||||||
|
biases
|
||||||
|
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
// 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;
|
||||||
|
fwrite(&inputDim, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
// für weiter Layer nur outputDimension schreiben
|
||||||
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||||
|
{
|
||||||
|
int outputDim = nn.layers[i].weights.rows;
|
||||||
|
fwrite(&outputDim, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
int weightCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||||
|
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||||
|
|
||||||
|
int 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";
|
||||||
|
|||||||
@ -17,8 +17,8 @@ inputDim
|
|||||||
outputDim
|
outputDim
|
||||||
|
|
||||||
-- Layer 1 --
|
-- Layer 1 --
|
||||||
weights (outputDim * inputDim * MatrixType)
|
weights
|
||||||
biases (outputDim * MatrixType)
|
biases
|
||||||
|
|
||||||
outputDim
|
outputDim
|
||||||
|
|
||||||
@ -55,27 +55,18 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
|||||||
|
|
||||||
// input und output dimension schreiben
|
// input und output dimension schreiben
|
||||||
int inputDim = nn.layers[0].weights.cols;
|
int inputDim = nn.layers[0].weights.cols;
|
||||||
int outputDim = nn.layers[0].weights.rows;
|
|
||||||
fwrite(&inputDim, sizeof(int), 1, file);
|
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
|
// für weiter Layer nur outputDimension schreiben
|
||||||
for (unsigned int i = 1; i < nn.numberOfLayers; i++)
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||||
{
|
{
|
||||||
outputDim = nn.layers[i].weights.rows;
|
int outputDim = nn.layers[i].weights.rows;
|
||||||
fwrite(&outputDim, sizeof(int), 1, file);
|
fwrite(&outputDim, sizeof(int), 1, file);
|
||||||
|
|
||||||
weightCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
int weightCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||||
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightCount, file);
|
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||||
|
|
||||||
biasesCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
int biasesCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
||||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasesCount, file);
|
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasesCount, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,15 +8,116 @@
|
|||||||
|
|
||||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||||
|
|
||||||
|
/* ---------------- Hilfsfunktionen ---------------- */
|
||||||
|
|
||||||
|
static int readHeader(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
|
||||||
|
{
|
||||||
|
unsigned short headerlength = strlen(FILE_HEADER_STRING);
|
||||||
|
char buffer[headerlength + 1];
|
||||||
|
if (fread(buffer, 1, headerlength, file) != headerlength)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
buffer[headerlength] = '\0';
|
||||||
|
|
||||||
|
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int readSingleImage(FILE *file, GrayScaleImage *image)
|
||||||
|
{
|
||||||
|
unsigned int pixelCount = image->width * image->height;
|
||||||
|
|
||||||
|
if (fread(image->buffer, sizeof(GrayScalePixelType), pixelCount, file) != pixelCount)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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 *file = fopen(path, "rb");
|
||||||
|
if (!file)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
unsigned short count, width, height;
|
||||||
|
|
||||||
|
if (!readHeader(file, &count, &width, &height)) {
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
series->count = count;
|
||||||
|
series->images = malloc(count * sizeof(GrayScaleImage));
|
||||||
|
series->labels = malloc(count * sizeof(unsigned char));
|
||||||
|
|
||||||
|
if (!series || !series->images || !series->labels)
|
||||||
|
{
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
series->images[i].width = width;
|
||||||
|
series->images[i].height = height;
|
||||||
|
series->images[i].buffer = malloc(width * height * sizeof(GrayScalePixelType));
|
||||||
|
|
||||||
|
//malloc prüfen
|
||||||
|
if (!series->images[i].buffer)
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Image einlesen + prüfen
|
||||||
|
if (!readSingleImage(file, &series->images[i])) {
|
||||||
|
fclose(file);
|
||||||
|
clearSeries(series);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// label einlesen
|
||||||
|
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
|
||||||
|
fclose(file);
|
||||||
|
clearSeries(series);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
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)
|
||||||
|
{
|
||||||
|
for(unsigned int i = 0; i < series->count; i++)
|
||||||
|
{
|
||||||
|
free(series->images[i].buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -6,30 +6,146 @@
|
|||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix m;
|
||||||
|
|
||||||
|
if (rows == 0 || cols == 0)
|
||||||
|
{
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
m.rows = rows;
|
||||||
|
m.cols = cols;
|
||||||
|
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
if (matrix != NULL)
|
||||||
|
{
|
||||||
|
if (matrix->buffer != NULL)
|
||||||
|
{
|
||||||
|
free(matrix->buffer);
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if (matrix.buffer != NULL)
|
||||||
|
{
|
||||||
|
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
|
{
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
|
||||||
|
{
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
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, 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
|
||||||
|
{
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for (int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType sum = 0;
|
||||||
|
|
||||||
|
for (int k = 0; k < matrix1.cols; k++)
|
||||||
|
{
|
||||||
|
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
|
}
|
||||||
|
setMatrixAt(sum, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
@ -7,6 +7,13 @@ typedef float MatrixType;
|
|||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
MatrixType *buffer;
|
||||||
|
int rows;
|
||||||
|
int cols;
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
void clearMatrix(Matrix *matrix);
|
||||||
|
|||||||
@ -5,12 +5,80 @@
|
|||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
################
|
||||||
|
Aufbau Test File
|
||||||
|
################
|
||||||
|
|
||||||
|
|
||||||
|
HEADER
|
||||||
|
|
||||||
|
inputDim
|
||||||
|
outputDim
|
||||||
|
|
||||||
|
-- Layer 1 --
|
||||||
|
weights
|
||||||
|
biases
|
||||||
|
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
// 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;
|
||||||
|
fwrite(&inputDim, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
// für weiter Layer nur outputDimension schreiben
|
||||||
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||||
|
{
|
||||||
|
int outputDim = nn.layers[i].weights.rows;
|
||||||
|
fwrite(&outputDim, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
int weightCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||||
|
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||||
|
|
||||||
|
int 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