diff --git a/.gitignore b/.gitignore index e9476da..617b31d 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,12 @@ *.hex Startcode/mnist Startcode/runTests +Startcode/runMatrixTests +Startcode/runImageInputTests +Startcode/runNeuralNetworkTests +Startcode/testFile.info2 +Startcode/testFile.txt +Startcode/some*nn_test_file.info2 # Debug files *.dSYM/ diff --git a/Startcode/imageInput.c b/Startcode/imageInput.c index f9432c2..ef1b692 100644 --- a/Startcode/imageInput.c +++ b/Startcode/imageInput.c @@ -6,88 +6,143 @@ #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 +// Liest den Identifikationstag am Dateianfang und vergleicht ihn mit FILE_HEADER_STRING +static int checkFileHeader(FILE *file) +{ + int isValid = 0; + size_t fileHeaderLen = strlen(FILE_HEADER_STRING); + char buffer[BUFFER_SIZE] = {0}; + + if(fileHeaderLen > BUFFER_SIZE-1) + fileHeaderLen = BUFFER_SIZE-1; + + if(fread(buffer, sizeof(char), fileHeaderLen, file) == fileHeaderLen) + isValid = strcmp(buffer, FILE_HEADER_STRING) == 0; + + return isValid; +} + +// Liest den naechsten unsigned short (Anzahl, Breite bzw. Hoehe); 0 im Fehlerfall. +// Kein fseek noetig: fread setzt den Dateizeiger automatisch hinter die gelesenen Bytes. +static unsigned short readUnsignedShort(FILE *file) +{ + unsigned short value = 0; + + if(fread(&value, sizeof(unsigned short), 1, file) != 1) + value = 0; + + return value; +} + static unsigned short readNumberOfImages(FILE *file) { - unsigned short numberOfImages = 0; - fseek(file, strlen(FILE_HEADER_STRING), SEEK_SET); - fread(&numberOfImages, sizeof(unsigned short), 1, file); - return numberOfImages; + return readUnsignedShort(file); } static unsigned short readImageWidth(FILE *file) { - unsigned short ImageWidth = 0; - fseek(file, sizeof(unsigned short), SEEK_CUR); - fread(&ImageWidth, sizeof(unsigned short), 1, file); - - return ImageWidth; + return readUnsignedShort(file); } static unsigned short readImageHeight(FILE *file) { - unsigned short ImageHeight = 0; - fseek(file, sizeof(unsigned short), SEEK_CUR); - fread(&ImageHeight, sizeof(unsigned short), 1, file); - return ImageHeight; + return readUnsignedShort(file); } -static GrayScalePixelType *readImage(FILE *file, unsigned short ImageWidth, unsigned short ImageHeight) +// Liest die Pixel eines Bildes in einen neuen Puffer; NULL bei Speicher- oder Lesefehler +static GrayScalePixelType *readImage(FILE *file, unsigned short imageWidth, unsigned short imageHeight) { - GrayScalePixelType *image = malloc(ImageHeight*ImageWidth*sizeof(GrayScalePixelType)); - fseek(file, sizeof(unsigned short), SEEK_CUR); - fread(image, sizeof(char), ImageHeight*ImageWidth, file); + size_t numberOfPixels = (size_t)imageWidth * imageHeight; + GrayScalePixelType *image = (GrayScalePixelType *)malloc(numberOfPixels * sizeof(GrayScalePixelType)); + + if(image != NULL && fread(image, sizeof(GrayScalePixelType), numberOfPixels, file) != numberOfPixels) + { + free(image); + image = NULL; + } + return image; - } -static GrayScalePixelType readLabel(FILE *file) +// Liest das Label, das direkt auf die Pixel eines Bildes folgt; 0 bei Erfolg, sonst -1 +static int readLabel(FILE *file, unsigned char *label) { - unsigned char label; - fseek(file, sizeof(unsigned short), SEEK_CUR); - fread(&label, sizeof(unsigned char), 1, file); - return label; + return fread(label, sizeof(unsigned char), 1, file) == 1 ? 0 : -1; } - -// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen -GrayScaleImageSeries *readImages(const char *path) +// Legt eine leere Serie fuer numberOfImages Bilder an (Bildpuffer werden spaeter von readImage geliefert) +static GrayScaleImageSeries *createSeries(unsigned short numberOfImages) { - FILE *file = fopen(path, "rb"); - if (file == NULL) + GrayScaleImageSeries *series = (GrayScaleImageSeries *)calloc(1, sizeof(GrayScaleImageSeries)); + + if(series != NULL) { - return NULL; + // calloc: alle Bildpuffer starten als NULL, damit clearSeries jederzeit gefahrlos aufraeumen kann + series->images = (GrayScaleImage *)calloc(numberOfImages, sizeof(GrayScaleImage)); + series->labels = (unsigned char *)calloc(numberOfImages, sizeof(unsigned char)); + + if(series->images == NULL || series->labels == NULL) + { + clearSeries(series); + series = NULL; + } + else + series->count = numberOfImages; + } + + return series; +} + +GrayScaleImageSeries *readImages(const char *path) +{ + GrayScaleImageSeries *series = NULL; + FILE *file = fopen(path, "rb"); + + if(file != NULL) + { + if(checkFileHeader(file)) + { + unsigned short numberOfImages = readNumberOfImages(file); + unsigned short imageWidth = readImageWidth(file); + unsigned short imageHeight = readImageHeight(file); + + if(numberOfImages > 0 && imageWidth > 0 && imageHeight > 0) + series = createSeries(numberOfImages); + + for(unsigned int i = 0; series != NULL && i < series->count; i++) + { + series->images[i].width = imageWidth; + series->images[i].height = imageHeight; + series->images[i].buffer = readImage(file, imageWidth, imageHeight); + + if(series->images[i].buffer == NULL || readLabel(file, &series->labels[i]) != 0) + { + clearSeries(series); + series = NULL; + } + } + } + + fclose(file); } - unsigned short numberOfImages = readNumberOfImages(file); - unsigned short imageWidth = readImageWidth(file); - unsigned short imageHeight = readImageHeight(file); - - GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); - if(series == NULL) - { - fclose(file); - return NULL; - } - - series->count = numberOfImages; - series->images = malloc(numberOfImages * sizeof(GrayScaleImage)); - series->labels = malloc(numberOfImages * sizeof(unsigned char)); - - - for(int i = 0; i < numberOfImages; i++) - { - series->images[i].width = imageWidth; - series->images[i].height = imageHeight; - series->images[i].buffer = readImage(file, imageWidth, imageHeight); - series->labels[i] = readLabel(file); - } - - fclose(file); return series; } -// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt void clearSeries(GrayScaleImageSeries *series) { -} \ No newline at end of file + if(series != NULL) + { + if(series->images != NULL) + { + for(unsigned int i = 0; i < series->count; i++) + { + free(series->images[i].buffer); + } + } + + free(series->images); + free(series->labels); + free(series); + } +} diff --git a/Startcode/imageInputTests.c b/Startcode/imageInputTests.c index 03240ab..8dfe059 100644 --- a/Startcode/imageInputTests.c +++ b/Startcode/imageInputTests.c @@ -119,6 +119,97 @@ void test_readImagesFailsOnWrongFileTag(void) remove(path); } +static void writeRawFile(const char *path, const void *data, size_t size) +{ + FILE *file = fopen(path, "wb"); + + if(file != NULL) + { + fwrite(data, 1, size, file); + fclose(file); + } +} + +void test_readImagesReturnsCorrectPixelValues(void) +{ + const char *path = "testFile.info2"; + const char *fileTag = "__info2_image_file_format__"; + const unsigned short header[] = {2, 2, 2}; + const GrayScalePixelType expectedPixels1[] = {0, 64, 128, 255}; + const GrayScalePixelType expectedPixels2[] = {1, 2, 3, 4}; + const unsigned char expectedLabels[] = {7, 3}; + GrayScaleImageSeries *series = NULL; + FILE *file = fopen(path, "wb"); + + TEST_ASSERT_NOT_NULL(file); + fwrite(fileTag, sizeof(char), strlen(fileTag), file); + fwrite(header, sizeof(header[0]), 3, file); + fwrite(expectedPixels1, sizeof(GrayScalePixelType), 4, file); + fwrite(&expectedLabels[0], sizeof(unsigned char), 1, file); + fwrite(expectedPixels2, sizeof(GrayScalePixelType), 4, file); + fwrite(&expectedLabels[1], sizeof(unsigned char), 1, file); + fclose(file); + + series = readImages(path); + remove(path); + + TEST_ASSERT_NOT_NULL(series); + TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedPixels1, series->images[0].buffer, 4); + TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedPixels2, series->images[1].buffer, 4); + TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedLabels, series->labels, 2); + clearSeries(series); +} + +void test_readImagesFailsOnTruncatedImageData(void) +{ + const char *path = "testFile.info2"; + long fullSize = 0; + char *content = NULL; + FILE *file = NULL; + + prepareImageFile(path, 8, 8, 3, 1); + + // Datei einlesen und ohne das letzte Byte (Label des letzten Bildes) neu schreiben + file = fopen(path, "rb"); + TEST_ASSERT_NOT_NULL(file); + fseek(file, 0, SEEK_END); + fullSize = ftell(file); + rewind(file); + content = (char *)malloc(fullSize); + TEST_ASSERT_NOT_NULL(content); + TEST_ASSERT_EQUAL_INT(fullSize, fread(content, 1, fullSize, file)); + fclose(file); + writeRawFile(path, content, fullSize - 1); + free(content); + + TEST_ASSERT_NULL(readImages(path)); + remove(path); +} + +void test_readImagesFailsOnIncompleteHeader(void) +{ + const char *path = "testFile.info2"; + const char content[] = "__info2_image_file_format__\x02\x00\x08"; + + writeRawFile(path, content, sizeof(content) - 1); + TEST_ASSERT_NULL(readImages(path)); + remove(path); +} + +void test_readImagesFailsOnZeroImages(void) +{ + const char *path = "testFile.info2"; + prepareImageFile(path, 8, 8, 0, 1); + TEST_ASSERT_NULL(readImages(path)); + remove(path); +} + +void test_clearSeriesAcceptsNull(void) +{ + clearSeries(NULL); + TEST_PASS(); +} + void setUp(void) { // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden } @@ -138,6 +229,11 @@ int main() RUN_TEST(test_readImagesReturnsCorrectLabels); RUN_TEST(test_readImagesReturnsNullOnNotExistingPath); RUN_TEST(test_readImagesFailsOnWrongFileTag); + RUN_TEST(test_readImagesReturnsCorrectPixelValues); + RUN_TEST(test_readImagesFailsOnTruncatedImageData); + RUN_TEST(test_readImagesFailsOnIncompleteHeader); + RUN_TEST(test_readImagesFailsOnZeroImages); + RUN_TEST(test_clearSeriesAcceptsNull); return UNITY_END(); } \ No newline at end of file diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 0ce51fb..8f1422d 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -8,7 +8,8 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { Matrix m; - if (rows == 0 || cols == 0) //Ueberprüfen, ob ungültige Werte für Matrix vorhanden + if (rows == 0 || cols == 0) + //Ueberprüfen, ob ungültige Werte für Matrix vorhanden { m.rows = 0; m.cols = 0; @@ -18,21 +19,40 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) m.rows = rows; m.cols = cols; - m.buffer = (MatrixType*)calloc(rows*cols, sizeof(MatrixType)); //Bei Fehlschalg gibt calloc entweder NULL oder Zeiger verschieden von NULL aus + m.buffer = (MatrixType*)calloc(rows*cols, sizeof(MatrixType)); + //Bei Fehlschalg gibt calloc entweder NULL oder Zeiger verschieden von NULL aus + + if (m.buffer == NULL) + //Kein Speicher: leere Matrix zurueckgeben, damit Groesse und Puffer zusammenpassen + { + m.rows = 0; + m.cols = 0; + } return m; } void clearMatrix(Matrix *matrix) { - matrix->buffer = 0; - matrix->rows = 0; - matrix->cols = 0; + if (matrix != NULL) + { + free(matrix->buffer); + //Speicher leeren + matrix->buffer = NULL; + matrix->rows = 0; + matrix->cols = 0; + } +} + +static int isValidIndex(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) +{ + //Zeile und Spalte einzeln pruefen: sonst wuerde z.B. (0, cols) faelschlich auf (1, 0) zugreifen + return matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols)) + if(isValidIndex(matrix, rowIdx, colIdx)) { matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } @@ -40,8 +60,8 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - MatrixType value = 0; - if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols)) + MatrixType value = UNDEFINED_MATRIX_VALUE; + if(isValidIndex(matrix, rowIdx, colIdx)) { value = matrix.buffer[rowIdx * matrix.cols + colIdx]; } @@ -53,28 +73,38 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) typedef enum {elemt_add, broadcast} Addiermodi; Addiermodi modus; - if((matrix1.cols == 1 || matrix2.cols ==1) && (matrix1.rows == matrix2.rows)) + Matrix m = {NULL, 0, 0}; + + if(matrix1.buffer == NULL || matrix2.buffer == NULL) { - modus = broadcast; + return m; } - else if((matrix1.rows == matrix2.rows) && (matrix1.cols == matrix2.cols)) + + //Elementweise zuerst pruefen: zwei Spaltenvektoren gleicher Groesse sind ein normaler elementweiser Fall + if((matrix1.rows == matrix2.rows) && (matrix1.cols == matrix2.cols)) { modus = elemt_add; } + else if((matrix1.cols == 1 || matrix2.cols ==1) && (matrix1.rows == matrix2.rows)) + { + modus = broadcast; + } else { - Matrix failure = {NULL, 0, 0}; - return failure; + return m; } - Matrix m; if(modus == elemt_add) { m = createMatrix(matrix1.rows, matrix1.cols); - - for(int i = 0; i< matrix1.rows; i++) + if(m.buffer == NULL) { - for(int j = 0; j < matrix1.cols ; j++) + return m; + } + + for(unsigned int i = 0; i< matrix1.rows; i++) + { + for(unsigned int j = 0; j < matrix1.cols ; j++) { m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix1.cols+j]; } @@ -87,9 +117,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) if(matrix1.cols == 1) { m = createMatrix(matrix2.rows, matrix2.cols); - for(int i = 0; i< matrix2.rows; i++) + for(unsigned int i = 0; i< m.rows; i++) { - for(int j = 0; j < matrix2.cols ; j++) + for(unsigned int j = 0; j < m.cols ; j++) { m.buffer[i*matrix2.cols+j] = matrix2.buffer[i*matrix2.cols+j] + matrix1.buffer[i*matrix1.cols]; } @@ -98,9 +128,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) else { m = createMatrix(matrix1.rows, matrix1.cols); - for(int i = 0; i< matrix1.rows; i++) + for(unsigned int i = 0; i< m.rows; i++) { - for(int j = 0; j < matrix1.cols ; j++) + for(unsigned int j = 0; j < m.cols ; j++) { m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix2.cols]; } @@ -112,7 +142,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - if(matrix1.cols != matrix2.rows) + if(matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows) { Matrix failure = {NULL, 0, 0}; return failure; @@ -120,16 +150,18 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix m = createMatrix(matrix1.rows, matrix2.cols); - for(int i = 0; i < matrix1.rows; i++) + //Reihenfolge i-k-j statt i-j-k: matrix2 und m werden zeilenweise am Stueck durchlaufen. + //m startet dank calloc mit 0, daher wird direkt aufaddiert. + for(unsigned int i = 0; i < m.rows; i++) { - for(int j = 0; j < matrix2.cols ; j++) + for(unsigned int k = 0; k < matrix1.cols; k++) { - int value = 0; - for(int k = 0; k < matrix1.cols; k++) + MatrixType factor = matrix1.buffer[i*matrix1.cols+k]; + //MatrixType statt int, hat die zahlen gerundet. + for(unsigned int j = 0; j < m.cols ; j++) { - value += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k , j); + m.buffer[i*m.cols+j] += factor * matrix2.buffer[k*matrix2.cols+j]; } - m.buffer[i*matrix2.cols+j] = value; } } return m; diff --git a/Startcode/matrixTests.c b/Startcode/matrixTests.c index 6d56e7a..07d72ef 100644 --- a/Startcode/matrixTests.c +++ b/Startcode/matrixTests.c @@ -130,6 +130,28 @@ void test_multiplyFailsOnWrongInputDimensions(void) TEST_ASSERT_EQUAL_UINT32(0, result.cols); } +void test_multiplyKeepsFractionalValues(void) +{ + MatrixType buffer1[] = {0.5f, 0.25f}; + MatrixType buffer2[] = {0.5f, 0.5f}; + Matrix matrix1 = {.rows=1, .cols=2, .buffer=buffer1}; + Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2}; + + Matrix result = multiply(matrix1, matrix2); + + TEST_ASSERT_NOT_NULL(result.buffer); + TEST_ASSERT_EQUAL_FLOAT(0.375f, result.buffer[0]); + free(result.buffer); +} + +void test_getMatrixAtFailsOnColumnIndexOutOfRange(void) +{ + MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; + Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; + + TEST_ASSERT_EQUAL_INT(UNDEFINED_MATRIX_VALUE, getMatrixAt(matrixToTest, 0, 3)); +} + void test_getMatrixAtReturnsCorrectResult(void) { MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; @@ -188,6 +210,8 @@ int main() RUN_TEST(test_addSupportsBroadcasting); RUN_TEST(test_multiplyReturnsCorrectResults); RUN_TEST(test_multiplyFailsOnWrongInputDimensions); + RUN_TEST(test_multiplyKeepsFractionalValues); + RUN_TEST(test_getMatrixAtFailsOnColumnIndexOutOfRange); RUN_TEST(test_getMatrixAtReturnsCorrectResult); RUN_TEST(test_getMatrixAtFailsOnIndicesOutOfRange); RUN_TEST(test_setMatrixAtSetsCorrectValue); diff --git a/Startcode/neuralNetwork.c b/Startcode/neuralNetwork.c index bd8f164..85711d0 100644 --- a/Startcode/neuralNetwork.c +++ b/Startcode/neuralNetwork.c @@ -262,6 +262,7 @@ void clearModel(NeuralNetwork *model) { clearLayer(&model->layers[i]); } + free(model->layers); model->layers = NULL; model->numberOfLayers = 0; } diff --git a/Startcode/neuralNetworkTests.c b/Startcode/neuralNetworkTests.c index 21ab370..9392a30 100644 --- a/Startcode/neuralNetworkTests.c +++ b/Startcode/neuralNetworkTests.c @@ -8,7 +8,33 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { - // TODO + FILE *file = fopen(path, "wb"); + + if(file != NULL) + { + const char *fileTag = "__info2_neural_network_file_format__"; + + fwrite(fileTag, sizeof(char), strlen(fileTag), file); + + if(nn.numberOfLayers > 0) + { + // Eingangsdimension nur einmal: jede weitere Schicht nutzt die Ausgangsdimension der vorherigen + int inputDimension = (int)nn.layers[0].weights.cols; + fwrite(&inputDimension, sizeof(int), 1, file); + } + + for(unsigned int i = 0; i < nn.numberOfLayers; i++) + { + const Layer layer = nn.layers[i]; + int outputDimension = (int)layer.weights.rows; + + fwrite(&outputDimension, sizeof(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); + } + + fclose(file); + } } void test_loadModelReturnsCorrectNumberOfLayers(void) diff --git a/Startcode/testFile.info2 b/Startcode/testFile.info2 deleted file mode 100644 index 1ea3b4c..0000000 --- a/Startcode/testFile.info2 +++ /dev/null @@ -1 +0,0 @@ -some_tag \ No newline at end of file