forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2436240736 | |||
| fde82f2d9a | |||
| 0fc70f982c | |||
| b271c865cb | |||
|
|
077c6def78 |
Binary file not shown.
47
imageInput.c
47
imageInput.c
@ -5,59 +5,12 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
#define FILE_HEADER_SIZE (sizeof(FILE_HEADER_STRING)-1)
|
|
||||||
|
|
||||||
|
|
||||||
// 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
|
||||||
FILE *checkFile(const char *path)
|
|
||||||
{
|
|
||||||
FILE *datei = fopen(path,"rb");
|
|
||||||
if (datei == NULL)
|
|
||||||
{
|
|
||||||
perror("Datei konnte nicht geoeffnet werden");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
char buffer[FILE_HEADER_SIZE+1];
|
|
||||||
if (fread(buffer,1,FILE_HEADER_SIZE,datei)!=FILE_HEADER_SIZE)
|
|
||||||
{
|
|
||||||
perror("Header konnte nicht eingelessen werden");
|
|
||||||
fclose(datei);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
buffer[FILE_HEADER_SIZE] = '\0';
|
|
||||||
if (strcmp(buffer,FILE_HEADER_STRING)!=0)
|
|
||||||
{
|
|
||||||
printf("Falscher Dateikopf");
|
|
||||||
//printf("\n%s",buffer);
|
|
||||||
//printf("\n%s",FILE_HEADER_STRING);
|
|
||||||
//printf("\n%d",strcmp(buffer,FILE_HEADER_STRING));
|
|
||||||
fclose(datei);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return datei;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
FILE *datei = checkFile(path);
|
|
||||||
if (datei==NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
unsigned short image_count, width, height;
|
|
||||||
fread(&image_count,1,sizeof(unsigned short),datei);
|
|
||||||
fread(&width,1,sizeof(unsigned short),datei);
|
|
||||||
fread(&height,1,sizeof(unsigned short),datei);
|
|
||||||
//printf("%u Bilder und %u mal %u",image_count,width,height);
|
|
||||||
|
|
||||||
fclose(datei);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
|
||||||
return series;
|
return series;
|
||||||
|
|||||||
@ -19,5 +19,5 @@ typedef struct
|
|||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path);
|
GrayScaleImageSeries *readImages(const char *path);
|
||||||
void clearSeries(GrayScaleImageSeries *series);
|
void clearSeries(GrayScaleImageSeries *series);
|
||||||
FILE *checkFile(const char *path);
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -54,7 +54,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 +70,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);
|
||||||
|
|||||||
7
main.c
7
main.c
@ -1,13 +1,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
//#include "mnistVisualization.h"
|
#include "mnistVisualization.h"
|
||||||
//#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
readImages("mnist_test.info2");
|
|
||||||
/*
|
|
||||||
const unsigned int windowWidth = 800;
|
const unsigned int windowWidth = 800;
|
||||||
const unsigned int windowHeight = 600;
|
const unsigned int windowHeight = 600;
|
||||||
|
|
||||||
@ -67,5 +65,4 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
return exitCode;
|
return exitCode;
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
82
matrix.c
82
matrix.c
@ -4,104 +4,32 @@
|
|||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
// Matrix erzeugen
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
Matrix matrix;
|
|
||||||
matrix.buffer = NULL;
|
|
||||||
matrix.rows = 0;
|
|
||||||
matrix.cols = 0;
|
|
||||||
|
|
||||||
|
|
||||||
// Wenn die Dimensionen gültig sind, Speicher reservieren
|
|
||||||
if (rows > 0 && cols > 0)
|
|
||||||
{
|
|
||||||
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
|
||||||
if (matrix.buffer != NULL)
|
|
||||||
{
|
|
||||||
matrix.rows = rows;
|
|
||||||
matrix.cols = cols;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return matrix;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matrix Speicher freigeben
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
if (matrix->buffer != NULL)
|
|
||||||
{
|
|
||||||
free(matrix->buffer);
|
|
||||||
matrix->buffer = NULL;
|
|
||||||
}
|
|
||||||
matrix->rows = 0;
|
|
||||||
matrix->cols = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wert setzen
|
|
||||||
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 (rowIdx < matrix.rows && colIdx < matrix.cols)
|
|
||||||
{
|
|
||||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wert auslesen
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
|
||||||
{
|
|
||||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
|
||||||
}
|
|
||||||
return 0; // Fallback
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matrizen addieren
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
Matrix add(const Matrix m1, const Matrix m2)
|
|
||||||
{
|
{
|
||||||
if (m1.rows != m2.rows || m1.cols != m2.cols)
|
|
||||||
{
|
|
||||||
return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix result = createMatrix(m1.rows, m1.cols);
|
|
||||||
if (result.buffer == NULL) return result;
|
|
||||||
|
|
||||||
for (unsigned int r = 0; r < m1.rows; r++)
|
|
||||||
{
|
|
||||||
for (unsigned int c = 0; c < m1.cols; c++)
|
|
||||||
{
|
|
||||||
result.buffer[r * m1.cols + c] =
|
|
||||||
getMatrixAt(m1, r, c) + getMatrixAt(m2, r, c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matrizen multiplizieren
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
Matrix multiply(const Matrix m1, const Matrix m2)
|
|
||||||
{
|
{
|
||||||
if (m1.cols != m2.rows)
|
|
||||||
{
|
|
||||||
return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix result = createMatrix(m1.rows, m2.cols);
|
|
||||||
if (result.buffer == NULL) return result;
|
|
||||||
|
|
||||||
for (unsigned int r = 0; r < m1.rows; r++)
|
|
||||||
{
|
|
||||||
for (unsigned int c = 0; c < m2.cols; c++)
|
|
||||||
{
|
|
||||||
MatrixType sum = 0;
|
|
||||||
for (unsigned int k = 0; k < m1.cols; k++)
|
|
||||||
{
|
|
||||||
sum += getMatrixAt(m1, r, k) * getMatrixAt(m2, k, c);
|
|
||||||
}
|
|
||||||
result.buffer[r * m2.cols + c] = sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
7
matrix.h
7
matrix.h
@ -6,13 +6,6 @@
|
|||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
MatrixType *buffer;
|
|
||||||
unsigned int rows;
|
|
||||||
unsigned int cols;
|
|
||||||
|
|
||||||
} Matrix;
|
|
||||||
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
@ -71,6 +71,32 @@ void test_addFailsOnDifferentInputDimensions(void)
|
|||||||
TEST_ASSERT_EQUAL_UINT32(0, result.cols);
|
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)
|
void test_multiplyReturnsCorrectResults(void)
|
||||||
{
|
{
|
||||||
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
||||||
@ -138,7 +164,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
|
|||||||
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
|
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
|
||||||
|
|
||||||
setMatrixAt(-1, matrixToTest, 2, 3);
|
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) {
|
void setUp(void) {
|
||||||
@ -159,6 +185,7 @@ int main()
|
|||||||
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
||||||
RUN_TEST(test_addReturnsCorrectResult);
|
RUN_TEST(test_addReturnsCorrectResult);
|
||||||
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
||||||
|
RUN_TEST(test_addSupportsBroadcasting);
|
||||||
RUN_TEST(test_multiplyReturnsCorrectResults);
|
RUN_TEST(test_multiplyReturnsCorrectResults);
|
||||||
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
||||||
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
||||||
|
|||||||
@ -8,47 +8,7 @@
|
|||||||
|
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO : Fehlerbehandlung
|
// TODO
|
||||||
FILE *file = fopen(path, "wb");
|
|
||||||
if (!file) {
|
|
||||||
perror("Fehler beim Öffnen der Datei");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *header = "__info2_neural_network_file_format__";
|
|
||||||
fwrite(header, sizeof(char), strlen(header), file);
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
|
||||||
Layer layer = nn.layers[i];
|
|
||||||
|
|
||||||
unsigned int inputDim = (i == 0) ? layer.weights.cols : 0; // nur beim ersten Layer
|
|
||||||
unsigned int outputDim = layer.weights.rows;
|
|
||||||
|
|
||||||
if (i == 0) {
|
|
||||||
// erstes Layer: inputDim und outputDim schreiben
|
|
||||||
fwrite(&inputDim, sizeof(unsigned int), 1, file);
|
|
||||||
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
|
||||||
} else {
|
|
||||||
// nur outputDim für weitere Layer
|
|
||||||
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
fwrite(layer.weights.buffer, sizeof(MatrixType), layer.weights.rows * layer.weights.cols, file);
|
|
||||||
fwrite(layer.biases.buffer, sizeof(MatrixType), layer.biases.rows * layer.biases.cols, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int zero = 0;
|
|
||||||
fwrite(&zero, sizeof(unsigned int), 1, file);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
// Debug
|
|
||||||
printf("prepareNeuralNetworkFile: Datei '%s' erstellt mit %u Layer(n)\n", path, nn.numberOfLayers);
|
|
||||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
|
||||||
Layer layer = nn.layers[i];
|
|
||||||
printf("Layer %u: weights (%u x %u), biases (%u x %u)\n",
|
|
||||||
i, layer.weights.rows, layer.weights.cols, layer.biases.rows, layer.biases.cols);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user