11 Commits
Author SHA1 Message Date
lukas d63cd86797 Merged branches 2026-11-11 09:11:34 +01:00
lukas 7336d12a54 Merge branch 'gade' 2026-11-11 09:05:51 +01:00
lukas 3fb1537348 Merge branch 'baeuml' 2026-11-11 09:04:54 +01:00
lukas 0e8c2fe05d Added error handling 2026-11-11 08:53:15 +01:00
Baeuml 7414865608 kommentare 2026-11-11 00:41:22 +01:00
Baeuml eb8db3856a verbesserung 2026-11-11 00:37:27 +01:00
Baeuml aba8d54204 Test feinschliff 2026-11-10 22:04:53 +01:00
Baeuml aa8ba9db4a prepareNeuralNetworkFile 2026-11-10 21:28:26 +01:00
Gade 2cf7133422 Bilder einlesen 2026-11-10 19:14:30 +01:00
Gade 2016582360 ImageInput 2026-11-10 18:59:30 +01:00
Baeuml b6ae8d17b1 test 2026-10-29 10:12:16 +01:00
4 changed files with 136 additions and 6 deletions

No files matched your search

+78 -5
View File
@@ -2,21 +2,94 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "imageInput.h" #include "imageInput.h"
//test
#define BUFFER_SIZE 100 #define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__" #define FILE_HEADER_STRING "__info2_image_file_format__"
static int checkHeader(FILE *file) //Headerprüfung
{
char buffer[BUFFER_SIZE];
fread(buffer, //liest Header ein
sizeof(char),
strlen(FILE_HEADER_STRING),
file);
buffer[strlen(FILE_HEADER_STRING)] = '\0';
return strcmp(buffer, FILE_HEADER_STRING) == 0;
}
// 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
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen static GrayScaleImage readImage(FILE *file,unsigned int width,unsigned int height)
GrayScaleImageSeries *readImages(const char *path)
{ {
GrayScaleImageSeries *series = NULL; GrayScaleImage image;
image.width = width;
image.height = height;
image.buffer = malloc(width * height * sizeof(GrayScalePixelType));
fread(image.buffer,sizeof(GrayScalePixelType),width * height,file);
return image;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path) //Bilder einlesen
{
FILE *file = fopen(path, "rb");
if (file == NULL)
{
return NULL;
}
if (!checkHeader(file)) //Prüft Header
{
fclose(file);
return NULL;
}
unsigned short count, width, height; //Anzahl der Bilder, Höhe und Breite
fread(&count,sizeof(unsigned short),1,file);
fread(&width,sizeof(unsigned short),1,file);
fread(&height,sizeof(unsigned short),1,file);
GrayScaleImageSeries *series =malloc(sizeof(GrayScaleImageSeries));
series->count = count;
series->images = malloc(count * sizeof(GrayScaleImage)); //Reservierung der Speicher für alle Bilder
series->labels = malloc(count * sizeof(unsigned char));
for (unsigned short i = 0; i < count; i++)
{
series->images[i] = readImage(file, width, height);
fread(&series->labels[i],sizeof(unsigned char),1,file);
}
fclose(file);
return series; return series;
} }
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series) void clearSeries(GrayScaleImageSeries *series) //Gibt den reservierten Speicher wieder frei
{ {
if (series == NULL) //Falls keine Serie existiert -> Beenden
{
return;
}
for (unsigned short i = 0; i < series->count; i++)
{
free(series->images[i].buffer);
}
free(series->images);
free(series->labels);
free(series);
} }
+4
View File
@@ -41,6 +41,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
newmatrix.cols = matrix1.cols; newmatrix.cols = matrix1.cols;
newmatrix.rows = matrix1.rows; newmatrix.rows = matrix1.rows;
newmatrix.buffer = malloc(matrix1.rows * matrix1.cols * sizeof(MatrixType)); newmatrix.buffer = malloc(matrix1.rows * matrix1.cols * sizeof(MatrixType));
if(newmatrix.buffer == NULL) return newmatrix;
//element addition //element addition
for(int i = 0; i < matrix1.rows; i++){ for(int i = 0; i < matrix1.rows; i++){
for(int j = 0; j < matrix1.cols; j++){ for(int j = 0; j < matrix1.cols; j++){
@@ -53,6 +54,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
newmatrix.cols = matrix1.cols; newmatrix.cols = matrix1.cols;
newmatrix.rows = matrix1.rows; newmatrix.rows = matrix1.rows;
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType)); newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
if(newmatrix.buffer == NULL) return newmatrix;
for(int i = 0; i < matrix1.rows; i++){ for(int i = 0; i < matrix1.rows; i++){
for(int j = 0; j < matrix1.cols; j++){ for(int j = 0; j < matrix1.cols; j++){
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols); *(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols);
@@ -64,6 +66,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
newmatrix.cols = matrix2.cols; newmatrix.cols = matrix2.cols;
newmatrix.rows = matrix2.rows; newmatrix.rows = matrix2.rows;
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType)); newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
if(newmatrix.buffer == NULL) return newmatrix;
for(int i = 0; i < matrix1.rows; i++){ for(int i = 0; i < matrix1.rows; i++){
for(int j = 0; j < matrix2.cols; j++){ for(int j = 0; j < matrix2.cols; j++){
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix2.buffer+i*matrix2.cols+j) + *(matrix1.buffer+i*matrix1.cols); *(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix2.buffer+i*matrix2.cols+j) + *(matrix1.buffer+i*matrix1.cols);
@@ -78,6 +81,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
Matrix newmatrix = {.buffer = NULL, .cols = 0, .rows = 0}; Matrix newmatrix = {.buffer = NULL, .cols = 0, .rows = 0};
if(matrix1.cols == matrix2.rows){ if(matrix1.cols == matrix2.rows){
newmatrix.buffer = malloc(matrix1.rows * matrix2.cols * sizeof(MatrixType)); newmatrix.buffer = malloc(matrix1.rows * matrix2.cols * sizeof(MatrixType));
if(newmatrix.buffer == NULL) return newmatrix;
newmatrix.rows = matrix1.rows; newmatrix.rows = matrix1.rows;
newmatrix.cols = matrix2.cols; newmatrix.cols = matrix2.cols;
for(int i = 0; i < newmatrix.rows; i++){ for(int i = 0; i < newmatrix.rows; i++){
+1 -1
View File
@@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count) static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
{ {
Matrix matrix = {NULL, 0, 0}; Matrix matrix = {0, 0, NULL};
if(count > 0 && images != NULL) if(count > 0 && images != NULL)
{ {
+53
View File
@@ -9,6 +9,59 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{ {
// TODO // TODO
//Datei zum binaeren schreiben oeffnen
FILE *file = fopen(path, "wb");
if (file == NULL)
{
return;
}
//Header schreiben, loadModel prüft nach String
const char *header = "__info2_neural_network_file_format__";
fwrite(header, sizeof(char), strlen(header), file);
//Prüfen ob Layer existieren
if (nn.numberOfLayers > 0)
{
//Format entspricht anzahl der cols der Matrix des 1ten Layers
int inputDimension = nn.layers[0].weights.cols;
fwrite(&inputDimension, sizeof(int), 1, file);
//Layer nacheinander in Datei schreiben
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
{
Layer layer = nn.layers[i];
//outputDimension entspricht Zeilenzahl seiner Matrix
int outputDimension = layer.weights.rows;
//outputDim. speichern
fwrite(&outputDimension, sizeof(int), 1, file);
//weight werte berechnen
int weightCount = layer.weights.rows * layer.weights.cols;
//Matrix speichern
fwrite(layer.weights.buffer, sizeof(MatrixType), weightCount, file);
//bias berechnen
int biasCount = layer.biases.rows * layer.biases.cols;
//bias speichern
fwrite(layer.biases.buffer, sizeof(MatrixType), biasCount, file);
}
//Abbruchkriterium für loadModel
int ende= 0;
fwrite(&ende, sizeof(int), 1, file);
}
//Datei schließen
fclose(file);
} }
void test_loadModelReturnsCorrectNumberOfLayers(void) void test_loadModelReturnsCorrectNumberOfLayers(void)