Compare commits

..
15 Commits
7 changed files with 203 additions and 58 deletions
+29 -20
View File
@@ -6,7 +6,6 @@
#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
// Lädt eine .info2-Datei und prüft, ob das Dateiformat korrekt ist
static FILE* openImageFile(const char* path) {
FILE* imageFile = NULL;
@@ -19,7 +18,7 @@ static FILE* openImageFile(const char* path) {
// Prüfe, ob die Datei mit dem richtigen String beginnt
char *fileHeaderString = (char*)malloc(BUFFER_SIZE);
fread(fileHeaderString, 1, strlen(FILE_HEADER_STRING) + 1, imageFile);
fread(fileHeaderString, 1, strlen(FILE_HEADER_STRING), imageFile);
if (strncmp(fileHeaderString, FILE_HEADER_STRING, 27)) {
// Datei hat nicht das korrekte Format
@@ -42,23 +41,19 @@ static int getInformationOfImages(FILE* imageFile, int dimensionsOfImages[], Gra
// Daten einlesen
int itemsRead = fread(bytestream, 2, 3, imageFile);
//for (size_t i = 0; i < (itemsRead * 2); i++) {
// printf("%02X ", bytestream[i]);
//}
if (itemsRead != 3) {
// Nicht genügend Daten gelesen
return -1;
}
// Anzahl der Bilder auslesen
series->count = (unsigned int)bytestream[0] << 8 | bytestream[1];
series->count = (unsigned int)bytestream[1] << 8 | bytestream[0];
// Breite der Bilder auslesen
dimensionsOfImages[0] = (unsigned int)bytestream[2] << 8 | bytestream[3];
dimensionsOfImages[0] = (unsigned int)bytestream[3] << 8 | bytestream[2];
// Höhe der Bilder auslesen
dimensionsOfImages[1] = (unsigned int)bytestream[4] << 8 | bytestream[5];
dimensionsOfImages[1] = (unsigned int)bytestream[5] << 8 | bytestream[4];
return 0;
}
@@ -99,11 +94,9 @@ static char getLabelOfImage(FILE* imageFile, GrayScaleImage* image) {
return label;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
// Datei laden und Dateiformat prüfen
// setzt außerdem den Pointer an der Stelle nach dem Prestring
FILE* imageFile = openImageFile(path);
if (imageFile == NULL) {
@@ -184,20 +177,36 @@ GrayScaleImageSeries *readImages(const char *path)
}
}
// für jedes Bild gemäß GreyScaleImageSeries.count:
// GreyScaleImage.width = width
// GreyScaleImage.height = height
// GreyScaleImageSeries.buffer = Pixelwerte
// labels = Label des Bilds
// series.labels[i] = (unsigned char)(i % 256);
// Springe in der Datei an die Stelle nach dem i. Bild
fclose(imageFile);
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
//prüfen, ob series überhaupt bereinigt werden muss
if(series == NULL){
return;
}
//images freigeben und NULL setzen -> jeden Index der Images durchgehen und buffer freigeben
if(series->images != NULL){
for(unsigned int i = 0; i < series->count; i++){
if (series->images[i].buffer != NULL){
free(series->images[i].buffer);
series->images[i].buffer = NULL;
}
}
free(series->images); // wenn alle images bereinigt sind, den Zeiger Images selbst bereinigen
series->images = NULL;
}
//labels freigeben und NULL setzen
if(series->labels != NULL){
free(series->labels);
series->labels = NULL;
}
//series freigeben
free(series);
}
@@ -4,6 +4,7 @@
#include <string.h>
#include "unity.h"
#include "imageInput.h"
#define FILE_HEADER_STRING "__info2_image_file_format__"
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
@@ -64,6 +65,19 @@ static void prepareImageFileIncorrectTag(const char *path, unsigned short int wi
}
}
void test_readImagesFailsOnIncompleteHeader(void) {
const char *path = "testIncompleteHeader.info2";
FILE *file = fopen(path, "wb");
if (file) {
fwrite(FILE_HEADER_STRING, 1, strlen(FILE_HEADER_STRING), file);
unsigned char incompleteHeader[2] = {0x01, 0x00};
fwrite(incompleteHeader, 1, 2, file);
fclose(file);
}
TEST_ASSERT_NULL(readImages(path));
remove(path);
}
void test_readImagesReturnsCorrectNumberOfImages(void)
{
@@ -159,6 +173,37 @@ void test_openImageFileIncorrectTag(void)
remove(path);
}
void test_clearSeriesFreesMemory(void)
{
// Dummy-Series erstellen
GrayScaleImageSeries *series = (GrayScaleImageSeries*)malloc(sizeof(GrayScaleImageSeries));
series->count = 2;
// Labels allokieren
series->labels = (unsigned char*)malloc(series->count * sizeof(unsigned char));
for (unsigned int i = 0; i < series->count; i++) {
series->labels[i] = (unsigned char)(i + 1);
}
// Images allokieren
series->images = (GrayScaleImage*)malloc(series->count * sizeof(GrayScaleImage));
for (unsigned int i = 0; i < series->count; i++) {
series->images[i].width = 4;
series->images[i].height = 4;
series->images[i].buffer = (GrayScalePixelType*)malloc(series->images[i].width * series->images[i].height);
for (unsigned int j = 0; j < series->images[i].width * series->images[i].height; j++){
series->images[i].buffer[j] = 0;
}
}
// clearSeries aufrufen
clearSeries(series);
// Test
TEST_PASS();
}
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
@@ -179,6 +224,8 @@ int main()
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
RUN_TEST(test_readImagesFailsOnWrongFileTag);
RUN_TEST(test_openImageFileIncorrectTag);
RUN_TEST(test_readImagesFailsOnIncompleteHeader);
RUN_TEST(test_clearSeriesFreesMemory);
return UNITY_END();
}
+41 -26
View File
@@ -10,78 +10,93 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
Matrix matrix;
matrix.rows = rows;
matrix.cols = cols;
matrix.data = (float *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.data != NULL) {
if(matrix.rows == 0 || matrix.cols == 0){ // prüfen, ob ungültige Dimensionen übergeben wurden, wenn ja, "error matrix" erstellen
matrix.rows = 0;
matrix.cols = 0;
matrix.buffer = NULL;
}
else { // für gültige Dimensionen: Speicher allokieren
matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType));
}
return matrix;
}
void clearMatrix(Matrix *matrix)
{
if (matrix->data != NULL) {
free(matrix->data);
matrix->data = 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(rowIdx >= matrix->rows || colIdx >= matrix->cols){
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ // Gültigkeit der Dimensionen prüfen
return;
}
matrix->data[rowIdx * matrix->cols + colIdx] = value;
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // value an vorgebenener Stelle setzen
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ // Gültigkeit der Dimensionen prüfen
return 0;
}
MatrixType value;
value = matrix.data[rowIdx * matrix.cols + colIdx];
value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // value an vorgegebener Stelle auslesen
return value;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { // Matrixen können nur addiert werden, sofern sie jeweils die gleiche Anzahl Spalten und Zeilen haben
Matrix errorMatrix = createMatrix(0, 0);
errorMatrix.data = NULL;
return errorMatrix;
size_t rows = (matrix1.rows > matrix2.rows) ? matrix1.rows : matrix2.rows;
size_t cols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols;
// Prüfen, ob Broadcasting möglich ist
if (!((matrix1.rows == rows || matrix1.rows == 1) &&
(matrix2.rows == rows || matrix2.rows == 1) &&
(matrix1.cols == cols || matrix1.cols == 1) &&
(matrix2.cols == cols || matrix2.cols == 1))) {
Matrix errorMatrix = createMatrix(0, 0);
return errorMatrix;
}
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.rows; i++) {
for (unsigned int j = 0; j < matrix1.cols; j++) {
MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(sum, &result, i, j);
}
Matrix result = createMatrix(rows, cols);
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
MatrixType val1 = matrix1.buffer[(i % matrix1.rows) * matrix1.cols + (j % matrix1.cols)];
MatrixType val2 = matrix2.buffer[(i % matrix2.rows) * matrix2.cols + (j % matrix2.cols)];
result.buffer[i * cols + j] = val1 + val2;
}
}
return result;
}
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.rows){
if (matrix1.cols != matrix2.rows){ // prüfen, ob Matrizen multipliziert werden dürfen
Matrix errorMatrix = createMatrix(0, 0);
errorMatrix.data = NULL;
errorMatrix.buffer = NULL;
return errorMatrix;
}
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols);
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols); // Ergebnis-Matrix erstellen
// Algorithmus für Multiplikation
for( size_t i = 0; i < matrix1.rows; i++){
for(size_t j = 0; j < matrix2.cols; j++){
MatrixType sum = 0;
for(size_t k = 0; k < matrix1.cols; k++){
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
setMatrixAt(sum, &matrix3, i, j);
setMatrixAt(sum, matrix3, i, j);
}
}
+1 -1
View File
@@ -4,9 +4,9 @@
#define UNDEFINED_MATRIX_VALUE 0
typedef struct{
float* buffer;
size_t rows;
size_t cols;
float* buffer;
} Matrix;
+30 -3
View File
@@ -21,7 +21,7 @@ void test_createMatrixReturnsCorrectMatrixDimensions(void)
{
const unsigned int expectedRows = 15;
const unsigned int expectedCols = 10;
Matrix matrixToTest = createMatrix(expectedRows, expectedCols);
TEST_ASSERT_EQUAL_UINT32(expectedRows, matrixToTest.rows);
TEST_ASSERT_EQUAL_UINT32(expectedCols, matrixToTest.cols);
@@ -63,7 +63,7 @@ void test_addFailsOnDifferentInputDimensions(void)
MatrixType buffer2[] = {7, 8, 9, 10, 11, 12};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=3, .cols=2, .buffer=buffer2};
Matrix result = add(matrix1, matrix2);
TEST_ASSERT_NULL(result.buffer);
@@ -71,13 +71,39 @@ 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};
MatrixType buffer2[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=3, .cols=4, .buffer=buffer2};
Matrix result = multiply(matrix1, matrix2);
float expectedResults[] = {74, 80, 86, 92, 173, 188, 203, 218};
@@ -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);
+13 -7
View File
@@ -67,14 +67,14 @@ static unsigned int readDimension(FILE *file)
if(fread(&dimension, sizeof(int), 1, file) != 1)
dimension = 0;
return dimension;
}
static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols)
{
Matrix matrix = createMatrix(rows, cols);
if(matrix.buffer != NULL)
{
if(fread(matrix.buffer, sizeof(MatrixType), rows*cols, file) != rows*cols)
@@ -118,6 +118,10 @@ static void assignActivations(NeuralNetwork model)
if(model.numberOfLayers > 0)
model.layers[model.numberOfLayers-1].activation = softmax;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "neuralNetwork.h"
NeuralNetwork loadModel(const char *path)
{
@@ -128,7 +132,7 @@ NeuralNetwork loadModel(const char *path)
{
if(checkFileHeader(file))
{
unsigned int inputDimension = readDimension(file);
unsigned int inputDimension = readDimension(file);
unsigned int outputDimension = readDimension(file);
while(inputDimension > 0 && outputDimension > 0)
@@ -142,7 +146,7 @@ NeuralNetwork loadModel(const char *path)
clearModel(&model);
break;
}
layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer));
if(layerBuffer != NULL)
@@ -168,6 +172,7 @@ NeuralNetwork loadModel(const char *path)
return model;
}
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
{
Matrix matrix = {NULL, 0, 0};
@@ -201,7 +206,7 @@ static Matrix forward(const NeuralNetwork model, Matrix inputBatch)
{
Matrix biasResult;
Matrix weightResult;
weightResult = multiply(model.layers[i].weights, result);
clearMatrix(&result);
biasResult = add(model.layers[i].biases, weightResult);
@@ -248,12 +253,13 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[],
Matrix outputBatch = forward(model, inputBatch);
unsigned char *result = argmax(outputBatch);
clearMatrix(&outputBatch);
return result;
}
void clearModel(NeuralNetwork *model)
{
if(model != NULL)
@@ -8,7 +8,48 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{
// TODO
FILE *file = fopen(path, "wb");
if (!file) {
perror("Fehler beim Öffnen der Datei");
return;
}
// Header schreiben
const char *header = "__info2_neural_network_file_format__";
fwrite(header, sizeof(char), strlen(header), file);
// Alle Layer schreiben
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
Layer layer = nn.layers[i];
if(i == 0){
// beim ersten Layer inputDim und outputDim schreiben
unsigned int inputDim = layer.weights.cols; // Anzahl Eingänge
fwrite(&inputDim, sizeof(unsigned int), 1, file);
unsigned int outputDim = layer.weights.rows; // Anzahl Ausgänge
fwrite(&outputDim, sizeof(unsigned int), 1, file);
}
else{
// bei allen anderen Layern nur outputDim schreiben
unsigned int outputDim = layer.weights.rows; // Anzahl Ausgänge
fwrite(&outputDim, sizeof(unsigned int), 1, file);
}
// Gewichte schreiben
size_t weightCount = layer.weights.rows * layer.weights.cols;
fwrite(layer.weights.buffer, sizeof(MatrixType), weightCount, file);
// Biases schreiben
size_t biasCount = layer.biases.rows * layer.biases.cols;
fwrite(layer.biases.buffer, sizeof(MatrixType), biasCount, file);
}
// End-Marker (outputDim = 0)
unsigned int zero = 0;
fwrite(&zero, sizeof(unsigned int), 1, file);
fclose(file);
}
void test_loadModelReturnsCorrectNumberOfLayers(void)