4 Commits
Author SHA1 Message Date
Max-R fb72278d55 Merge branch 'main' into RMax 2025-11-25 11:12:36 +01:00
Kristin f1af6c1e4a Merge branch 'Krisp2' 2025-11-25 10:49:32 +01:00
Kristin 9606b5a03e kp 2025-11-25 10:48:24 +01:00
Max-R 4a1b6cbb40 Fehler 0 Matrix bei add abfangen 2025-11-25 10:13:01 +01:00
12 changed files with 206 additions and 66 deletions
+1 -4
View File
@@ -2,9 +2,6 @@ mnist
runTests
*.o
*.exe
.vscode/settings.json
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/settings.json
.vscode/settings.json
runImageInputTests
testFile.info2
+18
View File
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "C:/ProgramData/mingw64/mingw64/bin/gcc.exe",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
+24
View File
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/Max-R/I2Pr/repoKachelto/I2-Pr_neuronalesNetz/info2Praktikum-NeuronalesNetz",
"program": "c:/Users/Max-R/I2Pr/repoKachelto/I2-Pr_neuronalesNetz/info2Praktikum-NeuronalesNetz/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
+57 -1
View File
@@ -1,3 +1,59 @@
{
"makefile.configureOnOpen": false
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
+2 -4
View File
@@ -4,8 +4,6 @@
#include <string.h>
#define FILE_HEADER_STRING "__info2_image_file_format__"
// define BUFFER 100
// 10x10 pixel
/* ----------------------------------------------------------
1. Header prüfen
@@ -42,14 +40,14 @@ static int readSingleImage(FILE *file, GrayScaleImage *img,
img->width = width;
img->height = height;
size_t numPixels = (size_t)width * (size_t)height; // anzahl an pixeln
size_t numPixels = (size_t)width * (size_t)height;
img->buffer = malloc(numPixels);
if (!img->buffer)
return 0;
if (fread(img->buffer, 1, numPixels, file) != numPixels) {
free(img->buffer);
img->buffer = NULL; // fehler bei ungültiger eingabe
img->buffer = NULL;
return 0;
}
return 1;
+7 -13
View File
@@ -126,25 +126,19 @@ void test_readImagesFailsOnWrongFileTag(void) {
remove(path);
}
// Test
void test_read_GrayScale_Pixel(
void) { // testet das einlesen eines graustufenbildes von readImages()
GrayScaleImageSeries *series = NULL; // enthält später das Bild
void test_read_GrayScale_Pixel(void) {
GrayScaleImageSeries *series = NULL;
const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, 1,
1); // Höhe x Breite in Pixel, Anzahl Bilder und Kategorie
prepareImageFile(path, 8, 8, 1, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series); // Speicher reservieren
TEST_ASSERT_NOT_NULL(series->images); // Inhalt ist da
TEST_ASSERT_EQUAL_UINT(1, series->count); // Anzahl der Bilder stimmt
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images);
TEST_ASSERT_EQUAL_UINT(1, series->count);
for (int i = 0; i < (8 * 8); i++) {
TEST_ASSERT_EQUAL_UINT8(
(GrayScalePixelType)i,
series->images[0].buffer[i]); // alle Pixelwerte prüfen
TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i, series->images[0].buffer[i]);
}
clearSeries(series);
+2 -2
View File
@@ -59,8 +59,8 @@ imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c
# --------------------------
clean:
ifeq ($(OS),Windows_NT)
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
else
del /f *.o *.exe
else
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
endif
+68 -14
View File
@@ -9,7 +9,7 @@
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
} Matrix;*/
Matrix createMatrix(const unsigned int rows, const unsigned int cols) {
Matrix createMatrix(unsigned int rows, unsigned int cols) {
if (cols == 0 || rows == 0) {
Matrix errorMatrix = {0, 0, NULL};
return errorMatrix;
@@ -42,10 +42,9 @@ void setMatrixAt(const MatrixType value, Matrix matrix,
// rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
// innerhalb der Zeile
}
MatrixType
getMatrixAt(const Matrix matrix,
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
const unsigned int colIdx) {
MatrixType getMatrixAt(const Matrix matrix,
unsigned int rowIdx, // Kopie der Matrix wird übergeben
unsigned int colIdx) {
if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
matrix.buffer == NULL) { // Speichergröße nicht überschreiten
return UNDEFINED_MATRIX_VALUE;
@@ -55,7 +54,7 @@ getMatrixAt(const Matrix matrix,
return value;
}
Matrix broadCastCols(const Matrix matrix, const unsigned int cols) {
Matrix broadcastingCols(const Matrix matrix, const unsigned int cols) {
Matrix copy1 = createMatrix(matrix.rows, cols);
for (int r = 0; r < matrix.rows; r++) {
MatrixType valueMatrix1 = getMatrixAt(matrix, r, 0);
@@ -65,7 +64,7 @@ Matrix broadCastCols(const Matrix matrix, const unsigned int cols) {
}
return copy1;
}
Matrix broadCastRows(const Matrix matrix, const unsigned int rows) {
Matrix broadcastingRows(const Matrix matrix, const unsigned int rows) {
Matrix copy1 = createMatrix(rows, matrix.cols);
for (int c = 0; c < matrix.cols; c++) {
MatrixType valueMatrix1 = getMatrixAt(matrix, 0, c);
@@ -91,6 +90,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
// Matrix
if (rowsEqual == 1 && colsEqual == 1) {
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
@@ -98,6 +98,15 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
for (int j= 0; j< cols1; j++){
int valueM1= getMatrixAt(matrix1, i, j);
int valueM2= getMatrixAt(matrix2, i, j);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
int valueM1 = getMatrixAt(matrix1, i, j);
int valueM2 = getMatrixAt(matrix2, i, j);
>>>>>>> main
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
@@ -105,9 +114,10 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
return result;
} else if (rowsEqual == 1 && (cols1 == 1 || cols2 == 1)) {
if (cols1 == 1) { // broadcasting von vektor 1 zu matrix 1, add
Matrix newMatrix = broadCastCols(matrix1, cols2);
Matrix newMatrix = broadcastingCols(matrix1, cols2);
// add
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
@@ -117,14 +127,25 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
int valueM2= getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
>>>>>>> main
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
int valueM1 = getMatrixAt(newMatrix, i, j);
int valueM2 = getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
clearMatrix(&newMatrix);
return result;
} else {
Matrix newMatrix2 = broadCastCols(matrix2, cols1);
Matrix newMatrix2 = broadcastingCols(matrix2, cols1);
// add
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
@@ -134,18 +155,29 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
int valueM2= getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
>>>>>>> main
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
int valueM1 = getMatrixAt(matrix1, i, j);
int valueM2 = getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
return result;
}
}
else if ((rows1 == 1 || rows2 == 1) && colsEqual == 1) {
if (rows1 == 1) {
Matrix newMatrix = broadCastRows(matrix1, rows2);
Matrix newMatrix = broadcastingRows(matrix1, rows2);
// add
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
@@ -155,13 +187,25 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
int valueM2= getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
>>>>>>> main
}
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols1; j++) {
int valueM1 = getMatrixAt(newMatrix, i, j);
int valueM2 = getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
return result;
} else {
Matrix newMatrix2 = broadCastRows(matrix2, rows1);
Matrix newMatrix2 = broadcastingRows(matrix2, rows1);
// add
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
@@ -171,9 +215,19 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
int valueM2= getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
>>>>>>> main
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
int valueM1 = getMatrixAt(matrix1, i, j);
int valueM2 = getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
clearMatrix(&newMatrix2);
return result;
}
} else {
+9 -7
View File
@@ -13,15 +13,17 @@ typedef struct {
} Matrix;
Matrix createMatrix(const unsigned int rows, const unsigned int cols);
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);
void setMatrixAt(const MatrixType value, Matrix matrix,
const unsigned int rowIdx, const unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, const unsigned int rowIdx,
const unsigned int colIdx);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx,
unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
unsigned int colIdx);
Matrix broadCastCols(const Matrix matrix, const unsigned int cols);
Matrix broadCastRows(const Matrix matrix, const unsigned int rows);
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
const unsigned int cols);
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
const unsigned int cols);
Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
+18 -21
View File
@@ -28,11 +28,7 @@ Gewichte: bestimmen, wie stark ein Eingangssignal auf ein Neuron wirkt
Dimension: Form der Matrizen für einen Layer*/
/* Gewichtsmatrix der Layer:
*/
// speichert NeuralNetwork nn in binäre Datei->später kann es wieder geöffnet
// werden
// speichert NeuralNetwork nn in binäre Datei->erzeugt Dateiformat
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
FILE *fptr = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
if (fptr == NULL)
@@ -40,12 +36,11 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
// Dateiformat
const char header[] = "__info2_neural_network_file_format__"; // header string
fwrite(header, sizeof(char), strlen(header),
fptr); // der header wird am Anfang der Datei platziert
const char header[] =
"__info2_neural_network_file_format__"; // header vor jedem Layer
fwrite(header, sizeof(char), strlen(header), fptr);
// Wenn es keine Layer gibt, 0 eintragen, LoadModel erkennt, dass Datei leer
// ist
// Wenn es keine Layer gibt, 0 eintragen, LoadModel gibt 0 zurück
if (nn.numberOfLayers == 0) {
int zero = 0;
fwrite(&zero, sizeof(int), 1, fptr);
@@ -54,7 +49,7 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
}
// Layer 0, inputDimension: Anzahl Input-Neuronen, outputDimension: Anzahl
// Output-Neuronen wird in Datei eingefügt
// Output-Neuronen
int inputDim = (int)nn.layers[0].weights.cols;
int outputDim = (int)nn.layers[0].weights.rows;
fwrite(&inputDim, sizeof(int), 1, fptr);
@@ -64,36 +59,38 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
x 1). Zwischen Layern wird nur die nächste outputDimension (int)
geschrieben. */
for (int i = 0; i < nn.numberOfLayers; i++) {
Layer layer = nn.layers[i]; // kürzer, durch alle layer iterieren
Layer layer = nn.layers[i];
int wrows = (int)layer.weights.rows;
int wcols = (int)layer.weights.cols;
int wcount = wrows * wcols; // Anzahl Gewichtseinträge
int wcount = wrows * wcols;
int bcount =
layer.biases.rows * layer.biases.cols; // Anzahl der Bias-Einträge
layer.biases.rows * layer.biases.cols; /* normalerweise rows * 1 */
/* Gewichte */
/* Gewichte (MatrixType binär) */
if (wcount > 0 && layer.weights.buffer != NULL) {
fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, fptr);
} // Gewichte werden als Matrix gespeichert
}
/* Biases */
/* Biases (MatrixType binär) */
if (bcount > 0 && layer.biases.buffer != NULL) {
fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, fptr);
} // Biases werden als Vektor gespeichert
}
/* outputDimensionen der nächsten Layer */
/* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */
if (i + 1 < nn.numberOfLayers) {
int nextOutput = (int)nn.layers[i + 1].weights.rows;
fwrite(&nextOutput, sizeof(int), 1, fptr);
} else {
// loadModel erkennt 0 als Ende der Datei
/* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
schreiben. loadModel liest dann outputDimension = 0 und beendet die
Schleife. */
int zero = 0;
fwrite(&zero, sizeof(int), 1, fptr);
}
}
fclose(fptr); // Datei schließen
fclose(fptr);
}
void test_loadModelReturnsCorrectNumberOfLayers(void) {