Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4dc5ca572f | ||
|
|
d9f7a2ab3f | ||
|
|
57708089a4 | ||
|
|
0d41f5e631 | ||
|
|
e1284e9efc | ||
|
|
bea1545564 | ||
|
|
0ee879ba20 | ||
|
|
4a31d6388d | ||
|
|
240cc77288 | ||
|
|
b6069a0a73 | ||
|
|
fc2b14d0e0 | ||
|
|
954318593c |
No files matched your search
+160
-1
@@ -8,15 +8,174 @@
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
|
||||
static int readHeader(FILE *file)
|
||||
{
|
||||
char buffer[BUFFER_SIZE];
|
||||
size_t len = strlen(FILE_HEADER_STRING);
|
||||
|
||||
// Genau so viele Zeichen lesen, wie der Tag lang ist
|
||||
if(fread(buffer, sizeof(char), len, file) != len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// In der Datei steht kein '\0', daher selbst terminieren
|
||||
buffer[len] = '\0';
|
||||
|
||||
return strcmp(buffer, FILE_HEADER_STRING) == 0;
|
||||
}
|
||||
|
||||
// Liest Anzahl der Bilder, Breite und Höhe (je ein unsigned short) aus der Datei.
|
||||
// Rückgabe: 1 = erfolgreich, 0 = Datei zu kurz oder ungültige Werte
|
||||
static int readDimensions(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
|
||||
{
|
||||
// Reihenfolge in der Datei: Anzahl, Breite, Höhe
|
||||
if(fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Eine Serie ohne Bilder oder Bilder ohne Pixel sind ungültig
|
||||
if(*count == 0 || *width == 0 || *height == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Reserviert den kompletten Speicher für eine Bildserie (ohne Daten zu lesen).
|
||||
// Rückgabe: Zeiger auf die neue Serie oder NULL, falls kein Speicher verfügbar ist
|
||||
static GrayScaleImageSeries *createSeries(unsigned short count, unsigned short width, unsigned short height)
|
||||
{
|
||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||
if(series == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Sofort initialisieren, damit clearSeries im Fehlerfall sicher aufräumen kann
|
||||
series->count = count;
|
||||
series->images = NULL;
|
||||
series->labels = NULL;
|
||||
|
||||
series->labels = malloc(count * sizeof(unsigned char));
|
||||
// calloc setzt alle buffer-Zeiger der Bilder auf NULL
|
||||
series->images = calloc(count, sizeof(GrayScaleImage));
|
||||
if(series->labels == NULL || series->images == NULL)
|
||||
{
|
||||
clearSeries(series);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Für jedes Bild Größe setzen und eigenen Pixelpuffer reservieren
|
||||
for(unsigned int i = 0; i < count; i++)
|
||||
{
|
||||
series->images[i].width = width;
|
||||
series->images[i].height = height;
|
||||
series->images[i].buffer = malloc((size_t)width * height * sizeof(GrayScalePixelType));
|
||||
if(series->images[i].buffer == NULL)
|
||||
{
|
||||
clearSeries(series);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
// Liest die Pixel eines einzelnen Bildes und das zugehörige Label aus der Datei.
|
||||
// Der Pixelpuffer des Bildes muss bereits reserviert sein (siehe createSeries).
|
||||
// Rückgabe: 1 = erfolgreich, 0 = Datei zu kurz
|
||||
static int readSingleImage(FILE *file, GrayScaleImage *image, unsigned char *label)
|
||||
{
|
||||
size_t pixels = (size_t)image->width * image->height;
|
||||
|
||||
// Erst alle Pixel des Bildes ...
|
||||
if(fread(image->buffer, sizeof(GrayScalePixelType), pixels, file) != pixels)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ... dann das Label (bewusst keine Prüfung auf 0-9)
|
||||
if(fread(label, sizeof(unsigned char), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
unsigned short count, width, height;
|
||||
|
||||
// 1. Datei im Binärmodus öffnen
|
||||
FILE *file = fopen(path, "rb");
|
||||
if(file == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 2. Tag prüfen und 3. Dimensionen lesen
|
||||
if(!readHeader(file) || !readDimensions(file, &count, &width, &height))
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 4. Speicher für die komplette Serie reservieren
|
||||
series = createSeries(count, width, height);
|
||||
if(series == NULL)
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 5. Alle Bilder samt Label nacheinander einlesen
|
||||
for(unsigned int i = 0; i < count; i++)
|
||||
{
|
||||
if(!readSingleImage(file, &series->images[i], &series->labels[i]))
|
||||
{
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return series;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if(series == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Zuerst die Pixelpuffer der einzelnen Bilder freigeben
|
||||
if(series->images != NULL)
|
||||
{
|
||||
for(unsigned int i = 0; i < series->count; i++)
|
||||
{
|
||||
free(series->images[i].buffer);
|
||||
}
|
||||
}
|
||||
|
||||
// Danach die Arrays und zuletzt die Serie selbst
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
}
|
||||
@@ -119,6 +119,95 @@ void test_readImagesFailsOnWrongFileTag(void)
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// Prüft readSingleImage: Pixelwerte müssen exakt so ankommen, wie sie geschrieben wurden
|
||||
void test_readImagesReadsCorrectPixelValues(void)
|
||||
{
|
||||
const char *path = "testFile.info2";
|
||||
const char *fileTag = "__info2_image_file_format__";
|
||||
unsigned short numberOfImages = 1;
|
||||
unsigned short width = 4;
|
||||
unsigned short height = 3;
|
||||
unsigned char label = 7;
|
||||
GrayScalePixelType pixels[4 * 3];
|
||||
|
||||
// Pixel mit bekannten, unterschiedlichen Werten füllen: 0, 1, 2, ...
|
||||
for(int i = 0; i < width * height; i++)
|
||||
{
|
||||
pixels[i] = (GrayScalePixelType)i;
|
||||
}
|
||||
|
||||
FILE *file = fopen(path, "wb");
|
||||
TEST_ASSERT_NOT_NULL(file);
|
||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
||||
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
|
||||
fwrite(&width, sizeof(width), 1, file);
|
||||
fwrite(&height, sizeof(height), 1, file);
|
||||
fwrite(pixels, sizeof(GrayScalePixelType), width * height, file);
|
||||
fwrite(&label, sizeof(label), 1, file);
|
||||
fclose(file);
|
||||
|
||||
GrayScaleImageSeries *series = readImages(path);
|
||||
TEST_ASSERT_NOT_NULL(series);
|
||||
TEST_ASSERT_NOT_NULL(series->images);
|
||||
TEST_ASSERT_NOT_NULL(series->images[0].buffer);
|
||||
TEST_ASSERT_EQUAL_UINT8_ARRAY(pixels, series->images[0].buffer, width * height);
|
||||
TEST_ASSERT_EQUAL_UINT8(label, series->labels[0]);
|
||||
|
||||
clearSeries(series);
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// Prüft readDimensions: Datei enthält nur den korrekten Tag, aber keine Dimensionen
|
||||
void test_readImagesFailsOnMissingDimensions(void)
|
||||
{
|
||||
const char *path = "testFile.info2";
|
||||
const char *fileTag = "__info2_image_file_format__";
|
||||
|
||||
FILE *file = fopen(path, "wb");
|
||||
TEST_ASSERT_NOT_NULL(file);
|
||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
||||
fclose(file);
|
||||
|
||||
TEST_ASSERT_NULL(readImages(path));
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// Prüft readDimensions: Eine Serie mit 0 Bildern ist ungültig
|
||||
void test_readImagesFailsOnZeroImages(void)
|
||||
{
|
||||
const char *path = "testFile.info2";
|
||||
prepareImageFile(path, 8, 8, 0, 1);
|
||||
TEST_ASSERT_NULL(readImages(path));
|
||||
remove(path);
|
||||
}
|
||||
|
||||
// Prüft readSingleImage: Tag und Dimensionen korrekt, aber Pixeldaten fehlen teilweise
|
||||
void test_readImagesFailsOnTruncatedImageData(void)
|
||||
{
|
||||
const char *path = "testFile.info2";
|
||||
const char *fileTag = "__info2_image_file_format__";
|
||||
unsigned short numberOfImages = 2;
|
||||
unsigned short width = 8;
|
||||
unsigned short height = 8;
|
||||
GrayScalePixelType pixels[8 * 8] = {0};
|
||||
unsigned char label = 1;
|
||||
|
||||
FILE *file = fopen(path, "wb");
|
||||
TEST_ASSERT_NOT_NULL(file);
|
||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
||||
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
|
||||
fwrite(&width, sizeof(width), 1, file);
|
||||
fwrite(&height, sizeof(height), 1, file);
|
||||
// Nur das erste Bild vollständig, vom zweiten nur die Hälfte der Pixel
|
||||
fwrite(pixels, sizeof(GrayScalePixelType), width * height, file);
|
||||
fwrite(&label, sizeof(label), 1, file);
|
||||
fwrite(pixels, sizeof(GrayScalePixelType), (width * height) / 2, file);
|
||||
fclose(file);
|
||||
|
||||
TEST_ASSERT_NULL(readImages(path));
|
||||
remove(path);
|
||||
}
|
||||
|
||||
void setUp(void) {
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
@@ -138,6 +227,10 @@ int main()
|
||||
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
||||
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
||||
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
||||
RUN_TEST(test_readImagesReadsCorrectPixelValues);
|
||||
RUN_TEST(test_readImagesFailsOnMissingDimensions);
|
||||
RUN_TEST(test_readImagesFailsOnZeroImages);
|
||||
RUN_TEST(test_readImagesFailsOnTruncatedImageData);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
+66
-7
@@ -1,35 +1,94 @@
|
||||
#include "matrix.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
|
||||
Matrix matrix;
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
matrix.buffer = NULL;
|
||||
if (matrix.rows == 0 || matrix.cols == 0)
|
||||
{
|
||||
matrix.rows = 0;
|
||||
matrix.cols = 0;
|
||||
return matrix;
|
||||
}
|
||||
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if ((rowIdx < matrix.rows) && (colIdx < matrix.cols))
|
||||
{
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if ((rowIdx < matrix.rows) || (colIdx < matrix.cols))
|
||||
{
|
||||
MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
return MatrixAt;
|
||||
}
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
unsigned int resRows = (matrix1.rows >= matrix2.rows) ? matrix1.rows : matrix2.rows;
|
||||
unsigned int resCols = (matrix1.cols >= matrix2.cols) ? matrix1.cols : matrix2.cols;
|
||||
|
||||
Matrix MatrixAdd = createMatrix(resRows, resCols);
|
||||
if ((matrix1.rows == matrix2.rows || matrix1.rows == 1 || matrix2.rows == 1) && (matrix1.cols == matrix2.cols || (matrix1.cols == 1 || matrix2.cols == 1)) && matrix1.rows > 0 && matrix1.cols > 0 && matrix2.rows > 0 && matrix2.cols > 0)
|
||||
{
|
||||
for (int i = 0; i < resRows; i++)
|
||||
{
|
||||
for (int j = 0; j < resCols; j++)
|
||||
{
|
||||
MatrixType val1 = getMatrixAt(matrix1, i % matrix1.rows, j % matrix1.cols);
|
||||
MatrixType val2 = getMatrixAt(matrix2, i % matrix2.rows, j % matrix2.cols);
|
||||
setMatrixAt(val1 + val2, MatrixAdd, i, j);
|
||||
}
|
||||
}
|
||||
return MatrixAdd;
|
||||
}
|
||||
return createMatrix(0, 0);
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols);
|
||||
if (matrix1.cols == matrix2.rows)
|
||||
{
|
||||
for (int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < matrix2.cols; j++)
|
||||
{
|
||||
MatrixType skalar = 0;
|
||||
for (int k = 0; k < matrix1.cols; k++)
|
||||
{
|
||||
MatrixType val1 = getMatrixAt(matrix1, i, k);
|
||||
MatrixType val2 = getMatrixAt(matrix2, k, j);
|
||||
skalar += val1 * val2;
|
||||
}
|
||||
setMatrixAt(skalar, MatrixMult, i, j);
|
||||
}
|
||||
}
|
||||
return MatrixMult;
|
||||
}
|
||||
return createMatrix(0, 0);
|
||||
}
|
||||
+6
-2
@@ -6,7 +6,12 @@
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct Matrix
|
||||
{
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType *buffer;
|
||||
} Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
@@ -15,5 +20,4 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
|
||||
|
||||
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
||||
{
|
||||
Matrix matrix = {NULL, 0, 0};
|
||||
Matrix matrix = createMatrix(0, 0);
|
||||
|
||||
if(count > 0 && images != NULL)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,31 @@
|
||||
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||
{
|
||||
// TODO
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (file != NULL) {
|
||||
const char *fileHeader = "__info2_neural_network_file_format__";
|
||||
fwrite(fileHeader, sizeof(char), strlen(fileHeader), file);
|
||||
|
||||
if (nn.numberOfLayers > 0 && nn.layers != NULL) {
|
||||
|
||||
int inputDimension = nn.layers[0].weights.cols;
|
||||
fwrite(&inputDimension, sizeof(int), 1, file);
|
||||
|
||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
||||
int outputDimension = nn.layers[i].weights.rows;
|
||||
fwrite(&outputDimension, sizeof(int), 1, file);
|
||||
|
||||
int weightsCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightsCount, file);
|
||||
|
||||
int biasesCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasesCount, file);
|
||||
}
|
||||
int endSignal = 0;
|
||||
fwrite(&endSignal, sizeof(int), 1, file);
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
|
||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user