forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6053f61bc | ||
|
|
d5753d1510 | ||
|
|
31ceca47ee | ||
|
|
92b3a22ac4 | ||
|
|
2760d2f7b3 | ||
|
|
8ae983ee89 | ||
|
|
0595406dbd | ||
|
|
e062f848f8 | ||
|
|
cdeeb6b1a9 | ||
|
|
c99205afc2 | ||
|
|
e4c7ec1a59 |
No files matched your search
+10
-11
@@ -8,13 +8,13 @@
|
|||||||
|
|
||||||
// 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. */
|
/* Liest den Datei-Header sowie Anzahl und Abmessungen der Bilder. */
|
||||||
static int readFileHeader(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
|
static int readFileHeader(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
|
||||||
{
|
{
|
||||||
char tag[sizeof(FILE_HEADER_STRING) - 1];
|
char tag[sizeof(FILE_HEADER_STRING) - 1];
|
||||||
|
|
||||||
return fread(tag, sizeof(tag), 1, file) == 1 &&
|
return fread(tag, sizeof(tag), 1, file) == 1 &&
|
||||||
memcmp(tag, FILE_HEADER_STRING, sizeof(tag)) == 0 &&
|
memcmp(tag, FILE_HEADER_STRING, sizeof(tag)) == 0 && /* vergleicht File-Header mit dem erwarteten Wert */
|
||||||
fread(count, sizeof(*count), 1, file) == 1 &&
|
fread(count, sizeof(*count), 1, file) == 1 &&
|
||||||
fread(width, sizeof(*width), 1, file) == 1 &&
|
fread(width, sizeof(*width), 1, file) == 1 &&
|
||||||
fread(height, sizeof(*height), 1, file) == 1 &&
|
fread(height, sizeof(*height), 1, file) == 1 &&
|
||||||
@@ -25,8 +25,7 @@ static int readFileHeader(FILE *file, unsigned short *count, unsigned short *wid
|
|||||||
static int readImage(FILE *file, GrayScaleImage *image, unsigned char *label, size_t pixelCount)
|
static int readImage(FILE *file, GrayScaleImage *image, unsigned char *label, size_t pixelCount)
|
||||||
{
|
{
|
||||||
image->buffer = (GrayScalePixelType *)malloc(pixelCount);
|
image->buffer = (GrayScalePixelType *)malloc(pixelCount);
|
||||||
if (image->buffer == NULL || fread(image->buffer, sizeof(*image->buffer), pixelCount, file) != pixelCount ||
|
if (image->buffer == NULL || fread(image->buffer, sizeof(*image->buffer), pixelCount, file) != pixelCount || fread(label, sizeof(*label), 1, file) != 1) /*Prüft ob Speicher korrekt reserviert wurde */
|
||||||
fread(label, sizeof(*label), 1, file) != 1)
|
|
||||||
{
|
{
|
||||||
free(image->buffer);
|
free(image->buffer);
|
||||||
image->buffer = NULL;
|
image->buffer = NULL;
|
||||||
@@ -38,11 +37,11 @@ static int readImage(FILE *file, GrayScaleImage *image, unsigned char *label, si
|
|||||||
/* Gibt auch bereits teilweise geladene Serien und deren Pixelpuffer frei. */
|
/* Gibt auch bereits teilweise geladene Serien und deren Pixelpuffer frei. */
|
||||||
static void freeImages(GrayScaleImageSeries *series)
|
static void freeImages(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
if (series == NULL)
|
if (series == NULL) /*Prüft ob Serie übergeben Wurde*/
|
||||||
return;
|
return;
|
||||||
if (series->images != NULL)
|
if (series->images != NULL)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < series->count; ++i)
|
for (unsigned int i = 0; i < series->count; ++i) /* Prüft jedes bIld der Serie*/
|
||||||
free(series->images[i].buffer);
|
free(series->images[i].buffer);
|
||||||
}
|
}
|
||||||
free(series->images);
|
free(series->images);
|
||||||
@@ -57,10 +56,10 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
size_t pixelCount;
|
size_t pixelCount;
|
||||||
FILE *file;
|
FILE *file;
|
||||||
|
|
||||||
if (path == NULL || (file = fopen(path, "rb")) == NULL)
|
if (path == NULL || (file = fopen(path, "rb")) == NULL) /*prüft ob sich Pfad öffnen lässt*/
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Zuerst den Header validieren, bevor Speicher reserviert wird */
|
/* Zuerst den Header prüfen, bevor Speicher reserviert wird */
|
||||||
if (!readFileHeader(file, &count, &width, &height))
|
if (!readFileHeader(file, &count, &width, &height))
|
||||||
{
|
{
|
||||||
fclose(file);
|
fclose(file);
|
||||||
@@ -69,7 +68,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pixelCount = (size_t)width * height;
|
pixelCount = (size_t)width * height;
|
||||||
series = (GrayScaleImageSeries *)calloc(1, sizeof(*series));
|
series = (GrayScaleImageSeries *)calloc(1, sizeof(*series)); /*Speicher für Serie reservieren*/
|
||||||
if (series == NULL)
|
if (series == NULL)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -78,7 +77,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
series->count = count;
|
series->count = count; /*speicher die anzahl der Bilder*/
|
||||||
series->images = (GrayScaleImage *)calloc(count, sizeof(*series->images));
|
series->images = (GrayScaleImage *)calloc(count, sizeof(*series->images));
|
||||||
series->labels = (unsigned char *)malloc(count * sizeof(*series->labels));
|
series->labels = (unsigned char *)malloc(count * sizeof(*series->labels));
|
||||||
if (series->images == NULL || series->labels == NULL)
|
if (series->images == NULL || series->labels == NULL)
|
||||||
@@ -88,7 +87,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Ein Datensatz besteht jeweils aus Pixeln, gefolgt von einem Labelbyte. */
|
/*Parameter jedes Bildes einlesen*/
|
||||||
for (unsigned int i = 0; i < count; ++i)
|
for (unsigned int i = 0; i < count; ++i)
|
||||||
{
|
{
|
||||||
series->images[i].width = width;
|
series->images[i].width = width;
|
||||||
|
|||||||
+90
-76
@@ -6,96 +6,102 @@
|
|||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
|
|
||||||
|
|
||||||
static void writeHeader(FILE *file, unsigned short count, unsigned short width, unsigned short height)
|
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
|
||||||
{
|
|
||||||
const char *tag = "__info2_image_file_format__";
|
|
||||||
fwrite(tag, 1, strlen(tag), file);
|
|
||||||
fwrite(&count, sizeof(count), 1, file);
|
|
||||||
fwrite(&width, sizeof(width), 1, file);
|
|
||||||
fwrite(&height, sizeof(height), 1, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void prepareImageFile(const char *path, unsigned short width, unsigned short height,
|
|
||||||
unsigned short count, unsigned char label)
|
|
||||||
{
|
{
|
||||||
FILE *file = fopen(path, "wb");
|
FILE *file = fopen(path, "wb");
|
||||||
if (file == NULL) return;
|
|
||||||
|
|
||||||
writeHeader(file, count, width, height);
|
if(file != NULL)
|
||||||
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)
|
const char *fileTag = "__info2_image_file_format__";
|
||||||
|
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
|
||||||
|
|
||||||
|
if(zeroBuffer != NULL)
|
||||||
{
|
{
|
||||||
fwrite(pixels, sizeof(*pixels), pixelCount, file);
|
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
|
||||||
fwrite(&label, sizeof(label), 1, 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);
|
||||||
}
|
}
|
||||||
free(pixels);
|
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
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";
|
||||||
const unsigned short expectedCount = 2;
|
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
||||||
prepareImageFile(path, 8, 8, expectedCount, 1);
|
series = readImages(path);
|
||||||
GrayScaleImageSeries *series = readImages(path);
|
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedCount, series->count);
|
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
||||||
clearSeries(series);
|
clearSeries(series);
|
||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectImageDimensions(void)
|
void test_readImagesReturnsCorrectImageWidth(void)
|
||||||
{
|
{
|
||||||
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
const unsigned short expectedWidth = 10;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, 10, 9, 2, 1);
|
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
||||||
GrayScaleImageSeries *series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_EQUAL_UINT16(10, series->images[0].width);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
TEST_ASSERT_EQUAL_UINT16(10, series->images[1].width);
|
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
||||||
TEST_ASSERT_EQUAL_UINT16(9, series->images[0].height);
|
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width);
|
||||||
TEST_ASSERT_EQUAL_UINT16(9, series->images[1].height);
|
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width);
|
||||||
|
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, 15);
|
prepareImageFile(path, 8, 8, 2, expectedLabel);
|
||||||
GrayScaleImageSeries *series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_EQUAL_UINT8(15, series->labels[0]);
|
TEST_ASSERT_NOT_NULL(series->labels);
|
||||||
TEST_ASSERT_EQUAL_UINT8(15, series->labels[1]);
|
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
||||||
|
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_readImagesReturnsPixelData(void)
|
void test_readImagesReturnsNullOnNotExistingPath(void)
|
||||||
{
|
{
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.txt";
|
||||||
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));
|
||||||
}
|
}
|
||||||
@@ -103,39 +109,47 @@ void test_readImagesReturnsNullOnMissingPath(void)
|
|||||||
void test_readImagesFailsOnWrongFileTag(void)
|
void test_readImagesFailsOnWrongFileTag(void)
|
||||||
{
|
{
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
FILE *file = fopen(path, "wb");
|
FILE *file = fopen(path, "w");
|
||||||
TEST_ASSERT_NOT_NULL(file);
|
if(file != NULL)
|
||||||
fputs("wrong tag", file);
|
{
|
||||||
fclose(file);
|
fprintf(file, "some_tag ");
|
||||||
TEST_ASSERT_NULL(readImages(path));
|
fclose(file);
|
||||||
|
TEST_ASSERT_NULL(readImages(path));
|
||||||
|
}
|
||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesFailsOnTruncatedImage(void)
|
void test_readImagesFailsOnZeroWidth(void)
|
||||||
{
|
{
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
const unsigned char partialPixels[] = { 1, 2 };
|
|
||||||
FILE *file = fopen(path, "wb");
|
prepareImageFile(path, 0, 8, 1, 1);
|
||||||
TEST_ASSERT_NOT_NULL(file);
|
|
||||||
writeHeader(file, 1, 2, 2);
|
|
||||||
fwrite(partialPixels, sizeof(partialPixels), 1, file);
|
|
||||||
fclose(file);
|
|
||||||
TEST_ASSERT_NULL(readImages(path));
|
TEST_ASSERT_NULL(readImages(path));
|
||||||
|
|
||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUp(void) {}
|
void setUp(void) {
|
||||||
void tearDown(void) {}
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
void tearDown(void) {
|
||||||
|
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nImage input tests\n============================\n");
|
||||||
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
|
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
|
||||||
RUN_TEST(test_readImagesReturnsCorrectImageDimensions);
|
RUN_TEST(test_readImagesReturnsCorrectImageWidth);
|
||||||
|
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
|
||||||
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
||||||
RUN_TEST(test_readImagesReturnsPixelData);
|
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
||||||
RUN_TEST(test_readImagesReturnsNullOnMissingPath);
|
|
||||||
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
||||||
RUN_TEST(test_readImagesFailsOnTruncatedImage);
|
RUN_TEST(test_readImagesFailsOnZeroWidth);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
@@ -6,30 +6,158 @@
|
|||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
if (rows == 0 || cols == 0) {
|
||||||
|
return (Matrix){0}; // Setzt rows, cols und den Puffer auf 0/NULL
|
||||||
|
}
|
||||||
|
Matrix m;
|
||||||
|
m.rows = rows;
|
||||||
|
m.cols = cols;
|
||||||
|
|
||||||
|
// Speicher reservieren und mit 0 initialisieren
|
||||||
|
m.buffer = (MatrixType *)calloc((size_t)rows * cols, sizeof(MatrixType));
|
||||||
|
|
||||||
|
// Falls Arbeitsspeicher voll ist
|
||||||
|
if (m.buffer == NULL) {
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
//prüfen ob pointer exisitert und ob es einen buffer gibt
|
||||||
|
if (matrix != NULL && matrix->buffer != NULL)
|
||||||
|
{
|
||||||
|
// Speicher freigeben
|
||||||
|
free(matrix->buffer);
|
||||||
|
|
||||||
|
// Werte zurücksetzen
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
// Sind Idx innerhalb der Matrix?
|
||||||
|
if (matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
// Umrechnung von 2D auf 1D
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
// Sicherheitsprüfung wie beim setzen
|
||||||
|
if (matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fehlerwert, falls außerhalb der grenzen
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
// Prüfen ob Zeilenanzahl gleich ist und ob buffer existieren
|
||||||
|
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
|
||||||
|
{
|
||||||
|
return (Matrix){0};
|
||||||
|
}
|
||||||
|
// Wenn beide Matrixen gleich groß sind:
|
||||||
|
if (matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
if (result.buffer == NULL) return (Matrix){0};
|
||||||
|
|
||||||
|
unsigned int totalElements = matrix1.rows * matrix1.cols;
|
||||||
|
for (unsigned int i = 0; i < totalElements; i++)
|
||||||
|
{
|
||||||
|
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
//Wenn matrix2 ein Spaltenvektor ist
|
||||||
|
else if (matrix2.cols == 1)
|
||||||
|
{
|
||||||
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
if (result.buffer == NULL) return (Matrix){0};
|
||||||
|
|
||||||
|
for (unsigned int r = 0; r < matrix1.rows; r++)
|
||||||
|
{
|
||||||
|
for (unsigned int c = 0; c < matrix1.cols; c++)
|
||||||
|
{
|
||||||
|
// Wert mit r und c aus m1 + Wert mit r aus m2
|
||||||
|
result.buffer[r * matrix1.cols + c] = matrix1.buffer[r * matrix1.cols + c] + matrix2.buffer[r];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
//Wenn matrix1 ein Spaltenvektor ist
|
||||||
|
else if (matrix1.cols == 1)
|
||||||
|
{
|
||||||
|
Matrix result = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
if (result.buffer == NULL) return (Matrix){0};
|
||||||
|
|
||||||
|
for (unsigned int r = 0; r < matrix2.rows; r++)
|
||||||
|
{
|
||||||
|
for (unsigned int c = 0; c < matrix2.cols; c++)
|
||||||
|
{
|
||||||
|
//gleich wie davor nur m1 und m2 getauscht
|
||||||
|
result.buffer[r * matrix2.cols + c] = matrix2.buffer[r * matrix2.cols + c] + matrix1.buffer[r];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (Matrix){0};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
// Ü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;
|
||||||
}
|
}
|
||||||
+1
-1
@@ -7,9 +7,9 @@ typedef float MatrixType;
|
|||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
MatrixType *buffer;
|
||||||
unsigned int rows;
|
unsigned int rows;
|
||||||
unsigned int cols;
|
unsigned int cols;
|
||||||
MatrixType *buffer;
|
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "mnistVisualization.h"
|
|
||||||
|
#define Matrix RaylibMatrix
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
#undef Matrix
|
||||||
|
|
||||||
|
#include "mnistVisualization.h"
|
||||||
|
|
||||||
#define MAX_TEXT_LEN 100
|
#define MAX_TEXT_LEN 100
|
||||||
|
|
||||||
@@ -13,6 +17,9 @@ typedef struct
|
|||||||
const unsigned char *predictions;
|
const unsigned char *predictions;
|
||||||
unsigned int currentIdx;
|
unsigned int currentIdx;
|
||||||
Vector2 pixelSize;
|
Vector2 pixelSize;
|
||||||
|
int isDrawing;
|
||||||
|
GrayScaleImage canvas;
|
||||||
|
NeuralNetwork model;
|
||||||
} MnistVisualization;
|
} MnistVisualization;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
@@ -55,16 +62,23 @@ static MnistVisualization *createVisualizationContainer(const GrayScaleImageSeri
|
|||||||
|
|
||||||
if(container != NULL)
|
if(container != NULL)
|
||||||
{
|
{
|
||||||
|
container->canvas.width = series->images[0].width;
|
||||||
|
container->canvas.height = series->images[0].height;
|
||||||
|
container->canvas.buffer = (unsigned char *)calloc(container->canvas.width * container->canvas.height, sizeof(unsigned char));
|
||||||
|
container->model = loadModel("mnist_model.info2");
|
||||||
|
|
||||||
Vector2 pixelSize = {(int)(size.x / series->images[0].width), (int)(size.y / series->images[0].height)};
|
Vector2 pixelSize = {(int)(size.x / series->images[0].width), (int)(size.y / series->images[0].height)};
|
||||||
container->pixelSize = pixelSize;
|
container->pixelSize = pixelSize;
|
||||||
container->series = series;
|
container->series = series;
|
||||||
container->predictions = predictions;
|
container->predictions = predictions;
|
||||||
|
container->isDrawing = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void drawTextLabel(const TextLabel *label)
|
static void drawTextLabel(const TextLabel *label)
|
||||||
{
|
{
|
||||||
DrawText(label->text, label->position.x, label->position.y, label->fontSize, label->color);
|
DrawText(label->text, label->position.x, label->position.y, label->fontSize, label->color);
|
||||||
@@ -99,7 +113,11 @@ static void drawAll(const MnistVisualization *container, const TextLabel *naviga
|
|||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
|
|
||||||
drawDigit(container->series->images[container->currentIdx], container->position, container->pixelSize);
|
if (container->isDrawing) {
|
||||||
|
drawDigit(container->canvas, container->position, container->pixelSize);
|
||||||
|
} else {
|
||||||
|
drawDigit(container->series->images[container->currentIdx], container->position, container->pixelSize);
|
||||||
|
}
|
||||||
drawTextLabel(navigationLabel);
|
drawTextLabel(navigationLabel);
|
||||||
drawTextLabel(predictionLabel);
|
drawTextLabel(predictionLabel);
|
||||||
|
|
||||||
@@ -114,6 +132,10 @@ static int checkUserInput()
|
|||||||
inputResult = -1;
|
inputResult = -1;
|
||||||
else if(IsKeyReleased(KEY_RIGHT))
|
else if(IsKeyReleased(KEY_RIGHT))
|
||||||
inputResult = 1;
|
inputResult = 1;
|
||||||
|
else if(IsKeyReleased(KEY_UP))
|
||||||
|
inputResult = 2;
|
||||||
|
else if(IsKeyReleased(KEY_DOWN))
|
||||||
|
inputResult = 3;
|
||||||
|
|
||||||
return inputResult;
|
return inputResult;
|
||||||
}
|
}
|
||||||
@@ -137,8 +159,41 @@ static void updatePredictionLabel(TextLabel *predictionLabel, unsigned char true
|
|||||||
|
|
||||||
static void update(MnistVisualization *container, TextLabel *predictionLabel, int updateDirection)
|
static void update(MnistVisualization *container, TextLabel *predictionLabel, int updateDirection)
|
||||||
{
|
{
|
||||||
updateDisplayContainer(container, updateDirection);
|
if (updateDirection == 2)
|
||||||
updatePredictionLabel(predictionLabel, container->series->labels[container->currentIdx], container->predictions[container->currentIdx]);
|
{
|
||||||
|
container->isDrawing = !container->isDrawing;
|
||||||
|
}
|
||||||
|
else if (updateDirection == 3)
|
||||||
|
{
|
||||||
|
memset(container->canvas.buffer, 0, container->canvas.width * container->canvas.height);
|
||||||
|
}
|
||||||
|
else if (!container->isDrawing)
|
||||||
|
{
|
||||||
|
updateDisplayContainer(container, updateDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (container->isDrawing == 1 && IsMouseButtonDown(MOUSE_BUTTON_LEFT))
|
||||||
|
{
|
||||||
|
int col = GetMouseX() / (int)container->pixelSize.x;
|
||||||
|
int row = GetMouseY() / (int)container->pixelSize.y;
|
||||||
|
|
||||||
|
if (col >= 0 && col < container->canvas.width && row >= 0 && row < container->canvas.height)
|
||||||
|
{
|
||||||
|
container->canvas.buffer[row * container->canvas.width + col] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (container->isDrawing)
|
||||||
|
{
|
||||||
|
unsigned char *liveResult = predict(container->model, &container->canvas, 1);
|
||||||
|
unsigned char predictedDigit = liveResult[0];
|
||||||
|
free(liveResult);
|
||||||
|
updatePredictionLabel(predictionLabel, 0, predictedDigit);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
updatePredictionLabel(predictionLabel, container->series->labels[container->currentIdx], container->predictions[container->currentIdx]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GrayScaleImageSeries *series, const unsigned char predictions[])
|
void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GrayScaleImageSeries *series, const unsigned char predictions[])
|
||||||
@@ -146,7 +201,7 @@ void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GraySc
|
|||||||
const Vector2 windowSize = {windowWidth, windowHeight};
|
const Vector2 windowSize = {windowWidth, windowHeight};
|
||||||
|
|
||||||
MnistVisualization *container = createVisualizationContainer(series, predictions, windowSize);
|
MnistVisualization *container = createVisualizationContainer(series, predictions, windowSize);
|
||||||
TextLabel *navigationLabel = createTextLabel("Use left and right key to navigate ...", 20, WHITE);
|
TextLabel *navigationLabel = createTextLabel("Use left and right key to navigate\n Use up and down key to enable free-Drawing and reset", 20, WHITE);
|
||||||
TextLabel *predictionLabel = createTextLabel("", 20, WHITE);
|
TextLabel *predictionLabel = createTextLabel("", 20, WHITE);
|
||||||
predictionLabel->position.x = 10;
|
predictionLabel->position.x = 10;
|
||||||
predictionLabel->position.y = windowSize.y - 50;
|
predictionLabel->position.y = windowSize.y - 50;
|
||||||
@@ -167,6 +222,11 @@ void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GraySc
|
|||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
|
||||||
|
if(container != NULL)
|
||||||
|
{
|
||||||
|
free(container->canvas.buffer);
|
||||||
|
clearModel(&container->model);
|
||||||
|
}
|
||||||
free(container);
|
free(container);
|
||||||
free(navigationLabel);
|
free(navigationLabel);
|
||||||
free(predictionLabel);
|
free(predictionLabel);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define MNISTVISUALIZATION_H
|
#define MNISTVISUALIZATION_H
|
||||||
|
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GrayScaleImageSeries *series, const unsigned char predictions[]);
|
void showMnist(unsigned int windowWidth, unsigned int windowHeight, const GrayScaleImageSeries *series, const unsigned char predictions[]);
|
||||||
|
|
||||||
|
|||||||
+110
@@ -0,0 +1,110 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "imageInput.h"
|
||||||
|
|
||||||
|
#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 Datei-Header 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 && /* vergleicht File-Header mit dem erwarteten Wert */
|
||||||
|
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) /*Prüft ob Speicher korrekt reserviert wurde */
|
||||||
|
{
|
||||||
|
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) /*Prüft ob Serie übergeben Wurde*/
|
||||||
|
return;
|
||||||
|
if (series->images != NULL)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < series->count; ++i) /* Prüft jedes bIld der Serie*/
|
||||||
|
free(series->images[i].buffer);
|
||||||
|
}
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
}
|
||||||
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
|
{
|
||||||
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
unsigned short count, width, height;
|
||||||
|
size_t pixelCount;
|
||||||
|
FILE *file;
|
||||||
|
|
||||||
|
if (path == NULL || (file = fopen(path, "rb")) == NULL) /*prüft ob sich Pfad öffnen lässt*/
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Zuerst den Header prüfen, 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)); /*Speicher für Serie reservieren*/
|
||||||
|
if (series == NULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
freeImages(series);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->count = count; /*speicher die anzahl der Bilder*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Parameter jedes Bildes einlesen*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
|
{
|
||||||
|
freeImages(series);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user