Compare commits

..

No commits in common. "2a1ff310dbc3408dc1a6613c39cae38ed1f4f258" and "0f0f2f19c3bc297ddad9cb7375a03525f4be5645" have entirely different histories.

9 changed files with 228 additions and 182 deletions

6
.gitignore vendored
View File

@ -2,9 +2,3 @@ mnist
runTests runTests
*.o *.o
*.exe *.exe
.vscode/settings.json
.vscode/launch.json
.vscode/settings.json
.vscode/settings.json
runImageInputTests
testFile.info2

View File

@ -4,8 +4,6 @@
#include <string.h> #include <string.h>
#define FILE_HEADER_STRING "__info2_image_file_format__" #define FILE_HEADER_STRING "__info2_image_file_format__"
// define BUFFER 100
// 10x10 pixel
/* ---------------------------------------------------------- /* ----------------------------------------------------------
1. Header prüfen 1. Header prüfen
@ -30,7 +28,6 @@ static int readMeta(FILE *file, unsigned short *count, unsigned short *width,
return 0; return 0;
if (fread(height, sizeof(unsigned short), 1, file) != 1) if (fread(height, sizeof(unsigned short), 1, file) != 1)
return 0; return 0;
return 1; return 1;
} }
@ -42,14 +39,14 @@ static int readSingleImage(FILE *file, GrayScaleImage *img,
img->width = width; img->width = width;
img->height = height; img->height = height;
size_t numPixels = (size_t)width * (size_t)height; // anzahl an pixeln size_t numPixels = (size_t)width * (size_t)height;
img->buffer = malloc(numPixels); img->buffer = malloc(numPixels);
if (!img->buffer) if (!img->buffer)
return 0; return 0;
if (fread(img->buffer, 1, numPixels, file) != numPixels) { if (fread(img->buffer, 1, numPixels, file) != numPixels) {
free(img->buffer); free(img->buffer);
img->buffer = NULL; // fehler bei ungültiger eingabe img->buffer = NULL;
return 0; return 0;
} }
return 1; return 1;
@ -77,11 +74,9 @@ GrayScaleImageSeries *readImages(const char *path) {
unsigned short count, width, height; unsigned short count, width, height;
if (!readMeta(file, &count, &width, &height)) { if (!readMeta(file, &count, &width, &height)) {
fclose(file); fclose(file);
return NULL; return NULL;
} }
// printf("%d, %d, %d", count, width, height);
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
if (!series) { if (!series) {

View File

@ -126,25 +126,19 @@ void test_readImagesFailsOnWrongFileTag(void) {
remove(path); remove(path);
} }
// Test void test_read_GrayScale_Pixel(void) {
GrayScaleImageSeries *series = NULL;
void test_read_GrayScale_Pixel(
void) { // testet das einlesen eines graustufenbildes von readImages()
GrayScaleImageSeries *series = NULL; // enthält später das Bild
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, 1, prepareImageFile(path, 8, 8, 1, 1);
1); // Höhe x Breite in Pixel, Anzahl Bilder und Kategorie
series = readImages(path); series = readImages(path);
TEST_ASSERT_NOT_NULL(series); // Speicher reservieren TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images); // Inhalt ist da TEST_ASSERT_NOT_NULL(series->images);
TEST_ASSERT_EQUAL_UINT(1, series->count); // Anzahl der Bilder stimmt TEST_ASSERT_EQUAL_UINT(1, series->count);
for (int i = 0; i < (8 * 8); i++) { for (int i = 0; i < (8 * 8); i++) {
TEST_ASSERT_EQUAL_UINT8( TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i, series->images[0].buffer[i]);
(GrayScalePixelType)i,
series->images[0].buffer[i]); // alle Pixelwerte prüfen
} }
clearSeries(series); clearSeries(series);

View File

@ -59,8 +59,7 @@ imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c
# -------------------------- # --------------------------
clean: clean:
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
else
del /f *.o *.exe del /f *.o *.exe
else
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
endif endif

280
matrix.c
View File

@ -3,22 +3,35 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// TODO Matrix-Funktionen implementieren
/*typedef struct { /*typedef struct {
unsigned int rows; //Zeilen unsigned int rows; //Zeilen
unsigned int cols; //Spalten unsigned int cols; //Spalten
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
} Matrix;*/ } Matrix;*/
Matrix createMatrix(unsigned int rows, unsigned int cols) {
Matrix createMatrix(const unsigned int rows, const unsigned int cols) { Matrix matrix;
if (cols == 0 || rows == 0) {
Matrix errorMatrix = {0, 0, NULL}; Matrix errorMatrix = {0, 0, NULL};
if (rows == 0 || cols == 0) {
return errorMatrix; return errorMatrix;
} }
MatrixType *buffer = matrix.rows = rows;
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc matrix.cols = cols;
// liefert Zeiger auf Speicher
Matrix newMatrix = {rows, cols, buffer}; // neue Matrix nach struct matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
return newMatrix; if (matrix.buffer == NULL) {
matrix.rows = 0;
matrix.cols = 0;
return matrix;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix.buffer[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
}
}
return matrix;
} }
void clearMatrix(Matrix *matrix) { void clearMatrix(Matrix *matrix) {
@ -34,18 +47,18 @@ void setMatrixAt(const MatrixType value, Matrix matrix,
const unsigned int rowIdx, // Kopie der Matrix wird übergeben const unsigned int rowIdx, // Kopie der Matrix wird übergeben
const unsigned int colIdx) { const unsigned int colIdx) {
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) { if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
// Speichergröße nicht überschreiten matrix.buffer == NULL) { // Speichergröße nicht überschreiten
return; return;
} }
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
// rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte matrix.buffer[rowIdx * matrix.cols + colIdx] =
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
// innerhalb der Zeile // innerhalb der Zeile
} }
MatrixType MatrixType getMatrixAt(const Matrix matrix,
getMatrixAt(const Matrix matrix, unsigned int rowIdx, // Kopie der Matrix wird übergeben
const unsigned int rowIdx, // Kopie der Matrix wird übergeben unsigned int colIdx) {
const unsigned int colIdx) {
if (rowIdx >= matrix.rows || colIdx >= matrix.cols || if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
matrix.buffer == NULL) { // Speichergröße nicht überschreiten matrix.buffer == NULL) { // Speichergröße nicht überschreiten
return UNDEFINED_MATRIX_VALUE; return UNDEFINED_MATRIX_VALUE;
@ -55,134 +68,187 @@ getMatrixAt(const Matrix matrix,
return value; return value;
} }
Matrix broadCastCols(const Matrix matrix, const unsigned int cols) {
Matrix copy1 = createMatrix(matrix.rows, cols); Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
for (int r = 0; r < matrix.rows; r++) { const unsigned int cols) {
MatrixType valueMatrix1 = getMatrixAt(matrix, r, 0);
for (int c = 0; c < cols; c++) { Matrix copy = createMatrix(
setMatrixAt(valueMatrix1, copy1, r, c); rows, cols); // Matrix 1 Kopie erstellen mit Dimensionen von Matrix2
}
}
return copy1;
}
Matrix broadCastRows(const Matrix matrix, const unsigned int rows) {
Matrix copy1 = createMatrix(rows, matrix.cols);
for (int c = 0; c < matrix.cols; c++) {
MatrixType valueMatrix1 = getMatrixAt(matrix, 0, c);
for (int r = 0; r < rows; r++) { for (int r = 0; r < rows; r++) {
setMatrixAt(valueMatrix1, copy1, r, c);
MatrixType value = getMatrixAt(matrix, r, 0);
for (int c = 0; c < cols; c++) {
setMatrixAt(value, copy, r, c);
} }
} }
return copy1;
return copy;
} }
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
const unsigned int cols) {
Matrix copy = createMatrix(rows, cols);
for (int c = 0; c < cols; c++) {
MatrixType value = getMatrixAt(matrix, 0, c);
for (int r = 0; r < rows; r++) {
setMatrixAt(value, copy, r, c);
}
}
return copy;
}
Matrix add(const Matrix matrix1, const Matrix matrix2) { Matrix add(const Matrix matrix1, const Matrix matrix2) {
// Ergebnismatrix const unsigned int rows1 = matrix1.rows;
Matrix result; const unsigned int rows2 = matrix2.rows;
const int cols1 = matrix1.cols; const unsigned int cols1 = matrix1.cols;
const int rows1 = matrix1.rows; const unsigned int cols2 = matrix2.cols;
const int cols2 = matrix2.cols;
const int rows2 = matrix2.rows;
const int rowsEqual = (matrix1.rows == matrix2.rows) ? 1 : 0; const int rowsEqual = ((rows1 == rows2) ? 1 : 0);
const int colsEqual = (matrix1.cols == matrix2.cols) ? 1 : 0;
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassender const int colsEqual = ((cols1 == cols2) ? 1 : 0);
// Matrix
if (rowsEqual == 1 && colsEqual == 1) { if (rowsEqual && colsEqual) // addieren
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
{
Matrix result = createMatrix(rows1, cols1); // Speicher reservieren
if (result.buffer == NULL) { if (result.buffer == NULL) {
return (Matrix){0, 0, NULL}; return (Matrix){0, 0, NULL};
} }
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) { for (int i = 0; i < (rows1 * cols1); i++) { // addieren
int valueM1 = getMatrixAt(matrix1, i, j);
int valueM2 = getMatrixAt(matrix2, i, j); result.buffer[i] =
int sum = valueM1 + valueM2; (matrix1.buffer[i] +
setMatrixAt(sum, result, i, j); matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
// Startadresse + (i * sizeof(MatrixType))
} }
return result; // zurückgeben
} }
return result;
} else if (rowsEqual == 1 && (cols1 == 1 || cols2 == 1)) { else if (rowsEqual && !colsEqual) {
if (cols1 == 1) { // broadcasting von vektor 1 zu matrix 1, add
Matrix newMatrix = broadCastCols(matrix1, cols2); if (cols1 == 1) {
// add
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols); Matrix result = createMatrix(rows2, cols2);
if (result.buffer == NULL) { if (result.buffer == NULL) {
return (Matrix){0, 0, NULL}; return (Matrix){0, 0, NULL};
} }
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) { Matrix copy1 = broadCastCols(matrix1, rows2, cols2);
int valueM1 = getMatrixAt(newMatrix, i, j); if (!copy1.buffer) {
int valueM2 = getMatrixAt(matrix2, i, j); clearMatrix(&result);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
clearMatrix(&newMatrix);
return result;
} else {
Matrix newMatrix2 = broadCastCols(matrix2, cols1);
// add
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL}; return (Matrix){0, 0, NULL};
} }
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) { for (unsigned int i = 0; i < rows2 * cols2; i++) {
int valueM1 = getMatrixAt(matrix1, i, j); result.buffer[i] = copy1.buffer[i] + matrix2.buffer[i];
int valueM2 = getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
} }
/* freigeben, weil nicht mehr benötigt */
clearMatrix(&copy1);
return result;
// add und return
} else if (cols2 == 1) {
Matrix result = createMatrix(rows1, cols1);
if (result.buffer == NULL) {
Matrix error = {0, 0, NULL};
return error;
} }
// Matrix2 hat nur eine Spalte -> horizontal broadcasten
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
for (unsigned int i = 0; i < rows1 * cols1; i++) {
result.buffer[i] = matrix1.buffer[i] + copy2.buffer[i];
}
// Optional: Speicher von copy2 freigeben
clearMatrix(&copy2);
return result; return result;
} }
else {
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
// Programmabbruch
Matrix error = {0, 0, NULL};
return error;
} }
else if ((rows1 == 1 || rows2 == 1) && colsEqual == 1) { }
else if (!rowsEqual && colsEqual) {
if (rows1 == 1) { if (rows1 == 1) {
Matrix newMatrix = broadCastRows(matrix1, rows2);
// add Matrix result = createMatrix(rows2, cols2);
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
if (result.buffer == NULL) { if (result.buffer == NULL) {
return (Matrix){0, 0, NULL}; return (Matrix){0, 0, NULL};
} }
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols1; j++) { Matrix copy1 = broadCastRows(matrix1, rows2, cols2);
int valueM1 = getMatrixAt(newMatrix, i, j);
int valueM2 = getMatrixAt(matrix2, i, j); for (int i = 0; i < (rows2 * cols2); i++) { // addieren
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j); result.buffer[i] =
} (copy1.buffer[i] +
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
// Startadresse + (i * sizeof(MatrixType))
} }
return result; return result;
} else {
Matrix newMatrix2 = broadCastRows(matrix2, rows1); // add und return
// add
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols); } else if (rows2 == 1) {
Matrix result = createMatrix(rows1, cols1);
if (result.buffer == NULL) { if (result.buffer == NULL) {
return (Matrix){0, 0, NULL}; return (Matrix){0, 0, NULL};
} }
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) { Matrix copy2 = broadCastRows(matrix2, rows1, cols1);
int valueM1 = getMatrixAt(matrix1, i, j);
int valueM2 = getMatrixAt(newMatrix2, i, j); // add und return
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j); for (int i = 0; i < (rows1 * cols1); i++) { // addieren
}
} result.buffer[i] =
clearMatrix(&newMatrix2); (matrix1.buffer[i] +
return result; copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
} // Startadresse + (i * sizeof(MatrixType))
} else {
// kein add möglich
Matrix errorMatrix = {0, 0, NULL};
return errorMatrix;
} }
return result; return result;
} }
else {
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
// Programmabbruch
Matrix error = {0, 0, NULL};
return error;
}
}
else {
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
// Programmabbruch
Matrix error = {0, 0, NULL};
return error;
}
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2) { Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
// Spalten1 müssen gleich zeilen2 sein! dann multiplizieren // Spalten1 müssen gleich zeilen2 sein! dann multiplizieren
if (matrix1.cols == matrix2.rows) { if (matrix1.cols == matrix2.rows) {

View File

@ -13,15 +13,17 @@ typedef struct {
} Matrix; } Matrix;
Matrix createMatrix(const unsigned int rows, const unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix); void clearMatrix(Matrix *matrix);
void setMatrixAt(const MatrixType value, Matrix matrix, void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx,
const unsigned int rowIdx, const unsigned int colIdx); unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, const unsigned int rowIdx, MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
const unsigned int colIdx); unsigned int colIdx);
Matrix broadCastCols(const Matrix matrix, const unsigned int cols); Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
Matrix broadCastRows(const Matrix matrix, const unsigned int rows); const unsigned int cols);
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
const unsigned int cols);
Matrix add(const Matrix matrix1, const Matrix matrix2); Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2);

View File

@ -28,72 +28,68 @@ Gewichte: bestimmen, wie stark ein Eingangssignal auf ein Neuron wirkt
Dimension: Form der Matrizen für einen Layer*/ Dimension: Form der Matrizen für einen Layer*/
/* Gewichtsmatrix der Layer: // speichert NeuralNetwork nn in binäre Datei->erzeugt Dateiformat
*/
// speichert NeuralNetwork nn in binäre Datei->später kann es wieder geöffnet
// werden
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
FILE *fptr = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen FILE *f = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
if (fptr == NULL) if (f == NULL)
return; // file konnte nicht geöffnet werden return;
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt // Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
// Dateiformat // Dateiformat
const char header[] = "__info2_neural_network_file_format__"; // header string const char header[] = "__info2_neural_network_file_format__";
fwrite(header, sizeof(char), strlen(header), fwrite(header, sizeof(char), strlen(header), f);
fptr); // der header wird am Anfang der Datei platziert
// Wenn es keine Layer gibt, 0 eintragen, LoadModel erkennt, dass Datei leer // Wenn es keine Layer gibt, 0 eintragen, LoadModel gibt 0 zurück
// ist
if (nn.numberOfLayers == 0) { if (nn.numberOfLayers == 0) {
int zero = 0; int zero = 0;
fwrite(&zero, sizeof(int), 1, fptr); fwrite(&zero, sizeof(int), 1, f);
fclose(fptr); fclose(f);
return; return;
} }
// Layer 0, inputDimension: Anzahl Input-Neuronen, outputDimension: Anzahl // Layer 0, inputDimension: Anzahl Input-Neuronen, outputDimension: Anzahl
// Output-Neuronen wird in Datei eingefügt // Output-Neuronen
int inputDim = (int)nn.layers[0].weights.cols; int inputDim = (int)nn.layers[0].weights.cols;
int outputDim = (int)nn.layers[0].weights.rows; int outputDim = (int)nn.layers[0].weights.rows;
fwrite(&inputDim, sizeof(int), 1, fptr); fwrite(&inputDim, sizeof(int), 1, f);
fwrite(&outputDim, sizeof(int), 1, fptr); fwrite(&outputDim, sizeof(int), 1, f);
/* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output /* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output
x 1). Zwischen Layern wird nur die nächste outputDimension (int) x 1). Zwischen Layern wird nur die nächste outputDimension (int)
geschrieben. */ geschrieben. */
for (int i = 0; i < nn.numberOfLayers; i++) { for (int i = 0; i < nn.numberOfLayers; i++) {
Layer layer = nn.layers[i]; // kürzer, durch alle layer iterieren Layer layer = nn.layers[i];
int wrows = (int)layer.weights.rows; int wrows = (int)layer.weights.rows;
int wcols = (int)layer.weights.cols; int wcols = (int)layer.weights.cols;
int wcount = wrows * wcols; // Anzahl Gewichtseinträge int wcount = wrows * wcols;
int bcount = int bcount =
layer.biases.rows * layer.biases.cols; // Anzahl der Bias-Einträge layer.biases.rows * layer.biases.cols; /* normalerweise rows * 1 */
/* Gewichte */ /* Gewichte (MatrixType binär) */
if (wcount > 0 && layer.weights.buffer != NULL) { if (wcount > 0 && layer.weights.buffer != NULL) {
fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, fptr); fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, f);
} // Gewichte werden als Matrix gespeichert }
/* Biases */ /* Biases (MatrixType binär) */
if (bcount > 0 && layer.biases.buffer != NULL) { if (bcount > 0 && layer.biases.buffer != NULL) {
fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, fptr); fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, f);
} // Biases werden als Vektor gespeichert }
/* outputDimensionen der nächsten Layer */ /* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */
if (i + 1 < nn.numberOfLayers) { if (i + 1 < nn.numberOfLayers) {
int nextOutput = (int)nn.layers[i + 1].weights.rows; int nextOutput = (int)nn.layers[i + 1].weights.rows;
fwrite(&nextOutput, sizeof(int), 1, fptr); fwrite(&nextOutput, sizeof(int), 1, f);
} else { } else {
// loadModel erkennt 0 als Ende der Datei /* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
schreiben. loadModel liest dann outputDimension = 0 und beendet die
Schleife. */
int zero = 0; int zero = 0;
fwrite(&zero, sizeof(int), 1, fptr); fwrite(&zero, sizeof(int), 1, f);
} }
} }
fclose(fptr); // Datei schließen fclose(f);
} }
void test_loadModelReturnsCorrectNumberOfLayers(void) { void test_loadModelReturnsCorrectNumberOfLayers(void) {