forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
15
Commits
79ad1285b5
...
simon
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d12ecee4b
|
||
|
|
adf75b66d2
|
||
|
|
00ac1aa04a
|
||
|
|
04768db522
|
||
|
|
01a13090a5
|
||
|
|
84b65525a6 | ||
|
|
92ad1e1c31
|
||
|
|
6137e45bdb
|
||
|
|
2436240736 | ||
|
|
fde82f2d9a | ||
|
|
0d7f380d87
|
||
|
|
645d471860
|
||
|
|
721f5cc2d1 | ||
|
|
0fc70f982c | ||
|
|
b271c865cb |
+4
-1
@@ -2,4 +2,7 @@ mnist
|
||||
runTests
|
||||
*.o
|
||||
*.exe
|
||||
runMatrixTests
|
||||
runMatrixTests
|
||||
runImageInputTests
|
||||
runNeuralNetworkTests
|
||||
.vscode
|
||||
Binary file not shown.
+153
-5
@@ -6,17 +6,165 @@
|
||||
#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
|
||||
typedef enum
|
||||
{
|
||||
IMG_SUCCESS = 0,
|
||||
IMG_ERR_INVALID_HEADER, // Header did not match
|
||||
IMG_ERR_READ, // Failed to read from file, maybe it's too short
|
||||
IMG_ERR, // General Error
|
||||
} ImageError;
|
||||
|
||||
static ImageError checkHeader(FILE *file);
|
||||
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file);
|
||||
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file);
|
||||
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series);
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
// It's very important to call calloc here because otherwise clearSeries() might try to free random memory
|
||||
GrayScaleImageSeries *series = calloc(1, sizeof(GrayScaleImageSeries));
|
||||
if (series == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FILE *file = fopen(path, "rb");
|
||||
// If fopen() failed
|
||||
if (file == NULL)
|
||||
{
|
||||
clearSeries(series);
|
||||
series = NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Try to parse the whole file. If anything fails memory is freed
|
||||
if (parseImageFile(file, series))
|
||||
{
|
||||
clearSeries(series);
|
||||
series = NULL; // Return NULL after the file is properly closed
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return series;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series)
|
||||
{
|
||||
if (checkHeader(file))
|
||||
{
|
||||
return IMG_ERR; // header check failed
|
||||
}
|
||||
|
||||
unsigned short imgCNT, width, height;
|
||||
if (readPictureParams(&imgCNT, &width, &height, file))
|
||||
{
|
||||
return IMG_ERR; // read failed
|
||||
}
|
||||
size_t pixels = width * height;
|
||||
|
||||
// The images contain more pointers, definitely use calloc here
|
||||
series->images = calloc(imgCNT, sizeof(GrayScaleImage));
|
||||
if (series->images == NULL)
|
||||
{
|
||||
return IMG_ERR;
|
||||
}
|
||||
|
||||
series->labels = malloc(imgCNT * sizeof(unsigned char));
|
||||
if (series->labels == NULL)
|
||||
{
|
||||
return IMG_ERR;
|
||||
}
|
||||
|
||||
series->count = imgCNT;
|
||||
// Read every image and it's label
|
||||
for (size_t imageIdx = 0; imageIdx < imgCNT; imageIdx++)
|
||||
{
|
||||
GrayScaleImage *curImage = &series->images[imageIdx];
|
||||
|
||||
curImage->buffer = malloc(sizeof(GrayScalePixelType) * pixels);
|
||||
if (curImage->buffer == NULL)
|
||||
{
|
||||
return IMG_ERR;
|
||||
}
|
||||
|
||||
curImage->width = width;
|
||||
curImage->height = height;
|
||||
|
||||
if (readImage(pixels, curImage->buffer, &series->labels[imageIdx], file))
|
||||
{
|
||||
return IMG_ERR;
|
||||
}
|
||||
}
|
||||
return IMG_SUCCESS;
|
||||
}
|
||||
|
||||
static ImageError checkHeader(FILE *file)
|
||||
{
|
||||
size_t len = strlen(FILE_HEADER_STRING);
|
||||
char headerBuf[len + 1];
|
||||
size_t charsRead = fread(headerBuf, 1, len, file);
|
||||
// Check if the file is to short
|
||||
if (charsRead < len)
|
||||
{
|
||||
return IMG_ERR_READ;
|
||||
}
|
||||
headerBuf[len] = '\0'; // Terminate string
|
||||
return strcmp(headerBuf, FILE_HEADER_STRING) == 0 ? IMG_SUCCESS : IMG_ERR_INVALID_HEADER;
|
||||
}
|
||||
|
||||
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file)
|
||||
{
|
||||
// number of images
|
||||
if (1 != fread(number, sizeof(unsigned short), 1, file))
|
||||
{
|
||||
return IMG_ERR_READ;
|
||||
}
|
||||
|
||||
// image width
|
||||
if (1 != fread(width, sizeof(unsigned short), 1, file))
|
||||
{
|
||||
return IMG_ERR_READ;
|
||||
}
|
||||
|
||||
// height
|
||||
if (1 != fread(height, sizeof(unsigned short), 1, file))
|
||||
{
|
||||
return IMG_ERR_READ;
|
||||
}
|
||||
|
||||
return IMG_SUCCESS;
|
||||
}
|
||||
|
||||
// Read 1 image and it's label
|
||||
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file)
|
||||
{
|
||||
if (numPixels > fread(pixelBuffer, sizeof(GrayScalePixelType), numPixels, file))
|
||||
{
|
||||
return IMG_ERR_READ;
|
||||
}
|
||||
|
||||
if (1 != fread(label, sizeof(unsigned char), 1, file))
|
||||
{
|
||||
return IMG_ERR_READ;
|
||||
}
|
||||
|
||||
return IMG_SUCCESS;
|
||||
}
|
||||
|
||||
// Frees memory for each image buffer, image, label and finally series
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (series)
|
||||
{
|
||||
|
||||
int seriesLen = series->count;
|
||||
for (size_t imageIdx = 0; imageIdx < seriesLen; imageIdx++)
|
||||
{
|
||||
free(series->images[imageIdx].buffer);
|
||||
}
|
||||
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
#include <stdio.h>
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
@@ -20,6 +21,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
if (mat.buffer == NULL)
|
||||
{
|
||||
clearMatrix(&mat);
|
||||
perror("could not allocate memory");
|
||||
}
|
||||
|
||||
return mat;
|
||||
@@ -58,20 +60,55 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
Matrix resMat = createMatrix(matrix1.rows, matrix1.cols);
|
||||
Matrix resMat = (matrix1.cols > matrix2.cols) ? createMatrix(matrix1.rows, matrix1.cols) : createMatrix(matrix2.rows, matrix2.cols);
|
||||
|
||||
// clear matrix and return if the dimensions of the input matrices differ from each other
|
||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||
if (resMat.buffer == NULL)
|
||||
{
|
||||
clearMatrix(&resMat);
|
||||
return resMat;
|
||||
return createMatrix(0, 0);
|
||||
}
|
||||
|
||||
if (matrix1.cols != matrix2.cols)
|
||||
{
|
||||
if (matrix1.rows != matrix2.rows)
|
||||
{
|
||||
clearMatrix(&resMat);
|
||||
return resMat;
|
||||
}
|
||||
else if (matrix1.cols == 1)
|
||||
{
|
||||
// broadcast vector
|
||||
for (size_t m = 0; m < matrix2.rows; m++)
|
||||
{
|
||||
for (size_t n = 0; n < matrix2.cols; n++)
|
||||
{
|
||||
setMatrixAt(getMatrixAt(matrix2, m, n) + getMatrixAt(matrix1, m, 0), resMat, m, n);
|
||||
}
|
||||
}
|
||||
return resMat;
|
||||
}
|
||||
else if (matrix2.cols == 1)
|
||||
{
|
||||
// broadcast vector
|
||||
for (size_t m = 0; m < matrix1.rows; m++)
|
||||
{
|
||||
for (size_t n = 0; n < matrix1.cols; n++)
|
||||
{
|
||||
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, 0), resMat, m, n);
|
||||
}
|
||||
}
|
||||
return resMat;
|
||||
}
|
||||
else
|
||||
{
|
||||
clearMatrix(&resMat);
|
||||
return resMat;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t m = 0; m < matrix1.rows; m++)
|
||||
{
|
||||
for (size_t n = 0; n < matrix1.cols; n++)
|
||||
{
|
||||
// this is unnecessarily complicated because at this point we already know that the matrices are compatible
|
||||
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, n), resMat, m, n);
|
||||
}
|
||||
}
|
||||
@@ -79,7 +116,37 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
return resMat;
|
||||
}
|
||||
|
||||
// TODO implement
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
Matrix multiply(const Matrix A, const Matrix B)
|
||||
{
|
||||
if (A.cols != B.rows || A.buffer == NULL || B.buffer == NULL)
|
||||
{
|
||||
return createMatrix(0, 0);
|
||||
}
|
||||
|
||||
int rows = A.rows, cols = B.cols;
|
||||
Matrix C = createMatrix(rows, cols);
|
||||
|
||||
if (C.buffer == NULL)
|
||||
{
|
||||
return createMatrix(0, 0);
|
||||
}
|
||||
|
||||
// M = Rows, K = Common Dim, N = Cols
|
||||
size_t M = A.rows, K = A.cols, N = B.cols;
|
||||
|
||||
for (size_t i = 0; i < M; i++)
|
||||
{
|
||||
for (size_t k = 0; k < K; k++)
|
||||
|
||||
{
|
||||
MatrixType valA = A.buffer[i * K + k];
|
||||
for (size_t j = 0; j < N; j++)
|
||||
{
|
||||
// C[i, j] += A[i, k] * B[k, j];
|
||||
// M x N, M x K, K x N
|
||||
C.buffer[i * N + j] += valA * B.buffer[k * N + j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return C;
|
||||
}
|
||||
+28
-1
@@ -71,6 +71,32 @@ void test_addFailsOnDifferentInputDimensions(void)
|
||||
TEST_ASSERT_EQUAL_UINT32(0, result.cols);
|
||||
}
|
||||
|
||||
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 test_multiplyReturnsCorrectResults(void)
|
||||
{
|
||||
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
||||
@@ -138,7 +164,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
|
||||
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
|
||||
|
||||
setMatrixAt(-1, matrixToTest, 2, 3);
|
||||
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows);
|
||||
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType));
|
||||
}
|
||||
|
||||
void setUp(void) {
|
||||
@@ -159,6 +185,7 @@ int main()
|
||||
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
||||
RUN_TEST(test_addReturnsCorrectResult);
|
||||
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
||||
RUN_TEST(test_addSupportsBroadcasting);
|
||||
RUN_TEST(test_multiplyReturnsCorrectResults);
|
||||
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
||||
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
||||
|
||||
+22
-1
@@ -6,9 +6,30 @@
|
||||
#include "neuralNetwork.h"
|
||||
|
||||
|
||||
static void erzeugeMatrix(FILE *file, const Matrix *m)
|
||||
{
|
||||
fwrite(&m->rows, sizeof(int), 1, file);
|
||||
fwrite(&m->cols, sizeof(int), 1, file);
|
||||
fwrite(m->buffer, sizeof(MatrixType), m->rows * m->cols, file);
|
||||
}
|
||||
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||
{
|
||||
// TODO
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (!file)
|
||||
return;
|
||||
|
||||
const char *header = "__info2_neural_network_file_format__";
|
||||
fwrite(header, sizeof(char), strlen(header), file);
|
||||
fwrite(&nn.numberOfLayers, sizeof(int), 1, file);
|
||||
|
||||
for (int i = 0; i < nn.numberOfLayers; i++)
|
||||
{
|
||||
erzeugeMatrix(file, &nn.layers[i].weights);
|
||||
erzeugeMatrix(file, &nn.layers[i].biases);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
|
||||
Reference in New Issue
Block a user