5 changed files with 252 additions and 93 deletions

No files matched your search

+92 -3
View File
@@ -8,15 +8,104 @@
// 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
/* Liest den Dateitag sowie Anzahl und Abmessungen der Bilder. */
static int readFileHeader(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
{
char tag[sizeof(FILE_HEADER_STRING) - 1];
return fread(tag, sizeof(tag), 1, file) == 1 &&
memcmp(tag, FILE_HEADER_STRING, sizeof(tag)) == 0 &&
fread(count, sizeof(*count), 1, file) == 1 &&
fread(width, sizeof(*width), 1, file) == 1 &&
fread(height, sizeof(*height), 1, file) == 1 &&
*count > 0 && *width > 0 && *height > 0;
}
/* Liest die Pixel und das unmittelbar folgende Label eines Bildes. */
static int readImage(FILE *file, GrayScaleImage *image, unsigned char *label, size_t pixelCount)
{
image->buffer = (GrayScalePixelType *)malloc(pixelCount);
if (image->buffer == NULL || fread(image->buffer, sizeof(*image->buffer), pixelCount, file) != pixelCount ||
fread(label, sizeof(*label), 1, file) != 1)
{
free(image->buffer);
image->buffer = NULL;
return 0;
}
return 1;
}
/* Gibt auch bereits teilweise geladene Serien und deren Pixelpuffer frei. */
static void freeImages(GrayScaleImageSeries *series)
{
if (series == NULL)
return;
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);
}
// 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)
{ {
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
unsigned short count, width, height;
size_t pixelCount;
FILE *file;
if (path == NULL || (file = fopen(path, "rb")) == NULL)
return 0;
/* Zuerst den Header validieren, bevor Speicher reserviert wird */
if (!readFileHeader(file, &count, &width, &height))
{
fclose(file);
freeImages(series);
return 0;
}
pixelCount = (size_t)width * height;
series = (GrayScaleImageSeries *)calloc(1, sizeof(*series));
if (series == NULL)
{
fclose(file);
freeImages(series);
return 0;
}
series->count = count;
series->images = (GrayScaleImage *)calloc(count, sizeof(*series->images));
series->labels = (unsigned char *)malloc(count * sizeof(*series->labels));
if (series->images == NULL || series->labels == NULL)
{
fclose(file);
freeImages(series);
return 0;
}
/* Ein Datensatz besteht jeweils aus Pixeln, gefolgt von einem Labelbyte. */
for (unsigned int i = 0; i < count; ++i)
{
series->images[i].width = width;
series->images[i].height = height;
if (!readImage(file, &series->images[i], &series->labels[i], pixelCount))
{
fclose(file);
freeImages(series);
return 0;
}
}
fclose(file);
return series; return series;
} }
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series) void clearSeries(GrayScaleImageSeries *series)
{ {
} freeImages(series);
}
+86 -88
View File
@@ -6,102 +6,96 @@
#include "imageInput.h" #include "imageInput.h"
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label) static void writeHeader(FILE *file, unsigned short count, unsigned short width, unsigned short height)
{ {
FILE *file = fopen(path, "wb"); const char *tag = "__info2_image_file_format__";
fwrite(tag, 1, strlen(tag), file);
if(file != NULL) fwrite(&count, sizeof(count), 1, file);
{ fwrite(&width, sizeof(width), 1, file);
const char *fileTag = "__info2_image_file_format__"; fwrite(&height, sizeof(height), 1, file);
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
if(zeroBuffer != NULL)
{
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
fwrite(&width, sizeof(width), 1, file);
fwrite(&height, sizeof(height), 1, file);
for(int i = 0; i < numberOfImages; i++)
{
fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
fwrite(&label, sizeof(unsigned char), 1, file);
}
free(zeroBuffer);
}
fclose(file);
}
} }
static void prepareImageFile(const char *path, unsigned short width, unsigned short height,
unsigned short count, unsigned char label)
{
FILE *file = fopen(path, "wb");
if (file == NULL) return;
writeHeader(file, count, width, height);
size_t pixelCount = (size_t)width * height;
GrayScalePixelType *pixels = (GrayScalePixelType *)calloc(pixelCount, sizeof(*pixels));
if (pixels != NULL)
{
for (unsigned int i = 0; i < count; ++i)
{
fwrite(pixels, sizeof(*pixels), pixelCount, file);
fwrite(&label, sizeof(label), 1, file);
}
free(pixels);
}
fclose(file);
}
void test_readImagesReturnsCorrectNumberOfImages(void) void test_readImagesReturnsCorrectNumberOfImages(void)
{ {
GrayScaleImageSeries *series = NULL;
const unsigned short expectedNumberOfImages = 2;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1); const unsigned short expectedCount = 2;
series = readImages(path); prepareImageFile(path, 8, 8, expectedCount, 1);
GrayScaleImageSeries *series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count); TEST_ASSERT_EQUAL_UINT16(expectedCount, series->count);
clearSeries(series); clearSeries(series);
remove(path); remove(path);
} }
void test_readImagesReturnsCorrectImageWidth(void) void test_readImagesReturnsCorrectImageDimensions(void)
{ {
GrayScaleImageSeries *series = NULL;
const unsigned short expectedWidth = 10;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, expectedWidth, 8, 2, 1); prepareImageFile(path, 10, 9, 2, 1);
series = readImages(path); GrayScaleImageSeries *series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_EQUAL_UINT16(10, series->images[0].width);
TEST_ASSERT_EQUAL_UINT16(2, series->count); TEST_ASSERT_EQUAL_UINT16(10, series->images[1].width);
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width); TEST_ASSERT_EQUAL_UINT16(9, series->images[0].height);
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width); TEST_ASSERT_EQUAL_UINT16(9, series->images[1].height);
clearSeries(series);
remove(path);
}
void test_readImagesReturnsCorrectImageHeight(void)
{
GrayScaleImageSeries *series = NULL;
const unsigned short expectedHeight = 10;
const char *path = "testFile.info2";
prepareImageFile(path, 8, expectedHeight, 2, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images);
TEST_ASSERT_EQUAL_UINT16(2, series->count);
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[0].height);
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[1].height);
clearSeries(series); clearSeries(series);
remove(path); remove(path);
} }
void test_readImagesReturnsCorrectLabels(void) void test_readImagesReturnsCorrectLabels(void)
{ {
const unsigned char expectedLabel = 15;
GrayScaleImageSeries *series = NULL;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, 2, expectedLabel); prepareImageFile(path, 8, 8, 2, 15);
series = readImages(path); GrayScaleImageSeries *series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->labels); TEST_ASSERT_EQUAL_UINT8(15, series->labels[0]);
TEST_ASSERT_EQUAL_UINT16(2, series->count); TEST_ASSERT_EQUAL_UINT8(15, series->labels[1]);
for (int i = 0; i < 2; i++) {
TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]);
}
clearSeries(series); clearSeries(series);
remove(path); remove(path);
} }
void test_readImagesReturnsNullOnNotExistingPath(void) void test_readImagesReturnsPixelData(void)
{ {
const char *path = "testFile.txt"; const char *path = "testFile.info2";
const unsigned char pixels[] = { 0, 17, 128, 255 };
const unsigned char label = 7;
FILE *file = fopen(path, "wb");
TEST_ASSERT_NOT_NULL(file);
writeHeader(file, 1, 2, 2);
fwrite(pixels, sizeof(pixels), 1, file);
fwrite(&label, sizeof(label), 1, file);
fclose(file);
GrayScaleImageSeries *series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_EQUAL_UINT8_ARRAY(pixels, series->images[0].buffer, sizeof(pixels));
clearSeries(series);
remove(path);
}
void test_readImagesReturnsNullOnMissingPath(void)
{
const char *path = "missingTestFile.info2";
remove(path); remove(path);
TEST_ASSERT_NULL(readImages(path)); TEST_ASSERT_NULL(readImages(path));
} }
@@ -109,35 +103,39 @@ void test_readImagesReturnsNullOnNotExistingPath(void)
void test_readImagesFailsOnWrongFileTag(void) void test_readImagesFailsOnWrongFileTag(void)
{ {
const char *path = "testFile.info2"; const char *path = "testFile.info2";
FILE *file = fopen(path, "w"); FILE *file = fopen(path, "wb");
if(file != NULL) TEST_ASSERT_NOT_NULL(file);
{ fputs("wrong tag", file);
fprintf(file, "some_tag "); fclose(file);
fclose(file); TEST_ASSERT_NULL(readImages(path));
TEST_ASSERT_NULL(readImages(path));
}
remove(path); remove(path);
} }
void setUp(void) { void test_readImagesFailsOnTruncatedImage(void)
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden {
const char *path = "testFile.info2";
const unsigned char partialPixels[] = { 1, 2 };
FILE *file = fopen(path, "wb");
TEST_ASSERT_NOT_NULL(file);
writeHeader(file, 1, 2, 2);
fwrite(partialPixels, sizeof(partialPixels), 1, file);
fclose(file);
TEST_ASSERT_NULL(readImages(path));
remove(path);
} }
void tearDown(void) { void setUp(void) {}
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden void tearDown(void) {}
}
int main() int main(void)
{ {
UNITY_BEGIN(); UNITY_BEGIN();
printf("\n============================\nImage input tests\n============================\n");
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages); RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
RUN_TEST(test_readImagesReturnsCorrectImageWidth); RUN_TEST(test_readImagesReturnsCorrectImageDimensions);
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
RUN_TEST(test_readImagesReturnsCorrectLabels); RUN_TEST(test_readImagesReturnsCorrectLabels);
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath); RUN_TEST(test_readImagesReturnsPixelData);
RUN_TEST(test_readImagesReturnsNullOnMissingPath);
RUN_TEST(test_readImagesFailsOnWrongFileTag); RUN_TEST(test_readImagesFailsOnWrongFileTag);
RUN_TEST(test_readImagesFailsOnTruncatedImage);
return UNITY_END(); return UNITY_END();
} }
+39
View File
@@ -31,5 +31,44 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
<<<<<<< Updated upstream
=======
>>>>>>> Stashed changes
// Überprüfen, ob die Matrizen gültig sind und ob ihre Dimensionen eine Multiplikation erlauben
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
{
return (Matrix){0};
}
// Ergebnismatrix erstellen
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
// Überprüfen, ob die Speicherreservierung erfolgreich war
if (result.buffer == NULL)
{
return (Matrix){0};
}
// Jede Zeile der ersten Matrix durchlaufen
for (unsigned int i = 0; i < matrix1.rows; i++)
{
// Jede Spalte der zweiten Matrix durchlaufen
for (unsigned int j = 0; j < matrix2.cols; j++)
{
MatrixType sum = 0;
// Summe der Produkte berechnen
for (unsigned int k = 0; k < matrix1.cols; k++)
{
sum += matrix1.buffer[i * matrix1.cols + k] *
matrix2.buffer[k * matrix2.cols + j];
}
// Berechneten Wert in der Ergebnismatrix speichern
result.buffer[i * result.cols + j] = sum;
}
}
return result;
} }
+2 -2
View File
@@ -9,7 +9,7 @@ typedef float MatrixType;
typedef struct { typedef struct {
unsigned int rows; unsigned int rows;
unsigned int cols; unsigned int cols;
MatrixType *buffer; // Speichert die Matrixwerte in einem Array, zeigt die Speicheradresse des ersten Matrixwerts im Array MatrixType *buffer; // Speichert die Matrixwerte in einem Array, die Speicheradresse des ersten Matrixwerts
} Matrix; } Matrix;
@@ -21,4 +21,4 @@ Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif #endif
+33
View File
@@ -9,6 +9,39 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{ {
// TODO // TODO
FILE *file = fopen(path, "wb");
if (file == NULL)
{
return;
}
const char *fileTag = "__info2_neural_network_file_format__";
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
if (nn.numberOfLayers > 0)
{
fwrite(&nn.layers[0].weights.cols, sizeof(int), 1, file);
fwrite(&nn.layers[0].weights.rows, sizeof(int), 1, file);
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
{
if (i > 0)
{
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
}
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType),
nn.layers[i].weights.rows * nn.layers[i].weights.cols, file);
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType),
nn.layers[i].biases.rows * nn.layers[i].biases.cols, file);
}
}
int endDimension = 0;
fwrite(&endDimension, sizeof(int), 1, file);
fclose(file);
} }
void test_loadModelReturnsCorrectNumberOfLayers(void) void test_loadModelReturnsCorrectNumberOfLayers(void)