Compare commits

..
10 Commits
6 changed files with 186 additions and 42 deletions
+25 -12
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;
@@ -95,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) {
@@ -180,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();
}
+26 -16
View File
@@ -10,12 +10,12 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
Matrix matrix;
matrix.rows = rows;
matrix.cols = cols;
if(matrix.rows == 0 || matrix.cols == 0){
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 {
else { // für gültige Dimensionen: Speicher allokieren
matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType));
}
@@ -36,50 +36,60 @@ void clearMatrix(Matrix *matrix)
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.buffer[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.buffer[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
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);
errorMatrix.buffer = NULL;
return errorMatrix;
}
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
for (size_t i = 0; i < matrix1.rows; i++) {
for (size_t 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.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;
@@ -71,6 +71,32 @@ 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};
@@ -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);
@@ -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)
{
@@ -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};
@@ -254,6 +259,7 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[],
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)