forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
10
Commits
934ba2d06e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e17eaf4542 | ||
|
|
6c26652744 | ||
|
|
5d8dbd548b | ||
|
|
225dbac29f | ||
|
|
c476386ff3 | ||
|
|
a031bb0b7a | ||
|
|
545acd0356 | ||
|
|
807bbbd375 | ||
|
|
9817c87e7a | ||
|
|
c1db7c612f |
+41
-11
@@ -46,24 +46,54 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
unsigned short image_count, width, height;
|
unsigned short image_count, width, height;
|
||||||
fread(&image_count,1,sizeof(unsigned short),datei);
|
fread(&image_count,sizeof(unsigned short),1,datei);
|
||||||
fread(&width,1,sizeof(unsigned short),datei);
|
fread(&width,sizeof(unsigned short),1,datei);
|
||||||
fread(&height,1,sizeof(unsigned short),datei);
|
fread(&height,sizeof(unsigned short),1,datei);
|
||||||
//printf("%u Bilder und %u mal %u",image_count,width,height);
|
//printf("%u Bilder und %u mal %u",image_count,width,height);
|
||||||
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
series->count = image_count;
|
||||||
|
series->images = malloc(image_count*sizeof(GrayScaleImage));
|
||||||
|
series->labels = malloc(image_count*sizeof(unsigned char));
|
||||||
|
for(unsigned short i = 0;i<image_count;i++)
|
||||||
|
{
|
||||||
|
series->images[i].width = width;
|
||||||
|
series->images[i].height = height;
|
||||||
|
series->images[i].buffer = malloc(width*height);
|
||||||
|
}
|
||||||
|
for(unsigned short i = 0;i<image_count;i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j=0;j<(width*height);j++)
|
||||||
|
{
|
||||||
|
fread(&series->images[i].buffer[j],1,1,datei);
|
||||||
|
}
|
||||||
|
fread(&series->labels[i],1,1,datei);
|
||||||
|
//printf("%d\n",series->labels[i]);
|
||||||
|
}
|
||||||
fclose(datei);
|
fclose(datei);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
GrayScaleImageSeries *series = NULL;
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
if(series == NULL)
|
||||||
|
{
|
||||||
|
printf("Serie nicht vorhanden\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned short anzahl = series->count;
|
||||||
|
for(unsigned short i = 0;i<anzahl;i++)
|
||||||
|
{
|
||||||
|
free(series->images[i].buffer );
|
||||||
|
}
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
printf("Serie freigegeben\n");
|
||||||
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
+2
-2
@@ -54,7 +54,7 @@ void test_readImagesReturnsCorrectImageWidth(void)
|
|||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const unsigned short expectedWidth = 10;
|
const unsigned short expectedWidth = 10;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, 8, expectedWidth, 2, 1);
|
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series->images);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
@@ -70,7 +70,7 @@ void test_readImagesReturnsCorrectImageHeight(void)
|
|||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const unsigned short expectedHeight = 10;
|
const unsigned short expectedHeight = 10;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, expectedHeight, 8, 2, 1);
|
prepareImageFile(path,8, expectedHeight, 2, 1);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series->images);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
//#include "mnistVisualization.h"
|
#include "mnistVisualization.h"
|
||||||
//#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
readImages("mnist_test.info2");
|
//readImages("mnist_test.info2");
|
||||||
/*
|
|
||||||
const unsigned int windowWidth = 800;
|
const unsigned int windowWidth = 800;
|
||||||
const unsigned int windowHeight = 600;
|
const unsigned int windowHeight = 600;
|
||||||
|
|
||||||
@@ -67,5 +68,5 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
return exitCode;
|
return exitCode;
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
|
||||||
|
|
||||||
// Matrix erzeugen
|
// Matrix erzeugen
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
@@ -12,28 +12,32 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
matrix.cols = 0;
|
matrix.cols = 0;
|
||||||
|
|
||||||
|
if (rows == 0 || cols == 0)
|
||||||
|
return matrix; // leere Matrix
|
||||||
|
|
||||||
|
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
||||||
|
if (!matrix.buffer)
|
||||||
|
return matrix; // Speicher konnte nicht reserviert werden
|
||||||
|
|
||||||
|
matrix.rows = rows;
|
||||||
|
matrix.cols = cols;
|
||||||
|
|
||||||
|
// Initialisiere alle Werte auf UNDEFINED_MATRIX_VALUE
|
||||||
|
for (unsigned int i = 0; i < rows * cols; i++)
|
||||||
|
matrix.buffer[i] = UNDEFINED_MATRIX_VALUE;
|
||||||
|
|
||||||
// Wenn die Dimensionen gültig sind, Speicher reservieren
|
|
||||||
if (rows > 0 && cols > 0)
|
|
||||||
{
|
|
||||||
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
|
||||||
if (matrix.buffer != NULL)
|
|
||||||
{
|
|
||||||
matrix.rows = rows;
|
|
||||||
matrix.cols = cols;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matrix Speicher freigeben
|
// Matrix Speicher freigeben
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
if (matrix->buffer != NULL)
|
if (!matrix) return;
|
||||||
{
|
|
||||||
|
if (matrix->buffer)
|
||||||
free(matrix->buffer);
|
free(matrix->buffer);
|
||||||
matrix->buffer = NULL;
|
|
||||||
}
|
matrix->buffer = NULL;
|
||||||
matrix->rows = 0;
|
matrix->rows = 0;
|
||||||
matrix->cols = 0;
|
matrix->cols = 0;
|
||||||
}
|
}
|
||||||
@@ -41,67 +45,84 @@ void clearMatrix(Matrix *matrix)
|
|||||||
// Wert setzen
|
// Wert setzen
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
if (!matrix.buffer) return;
|
||||||
{
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return;
|
||||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
|
||||||
}
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wert auslesen
|
// Wert auslesen
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
if (!matrix.buffer) return UNDEFINED_MATRIX_VALUE;
|
||||||
{
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return UNDEFINED_MATRIX_VALUE;
|
||||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
|
||||||
}
|
|
||||||
return 0; // Fallback
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
|
}
|
||||||
// Matrizen addieren
|
// Matrizen addieren
|
||||||
Matrix add(const Matrix m1, const Matrix m2)
|
Matrix add(const Matrix m1, const Matrix m2)
|
||||||
{
|
{
|
||||||
if (m1.rows != m2.rows || m1.cols != m2.cols)
|
if (!m1.buffer || !m2.buffer) return createMatrix(0,0);
|
||||||
|
|
||||||
|
// gleiche Dimension
|
||||||
|
if (m1.rows == m2.rows && m1.cols == m2.cols)
|
||||||
{
|
{
|
||||||
return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
|
Matrix result = createMatrix(m1.rows, m1.cols);
|
||||||
|
if (!result.buffer) return result;
|
||||||
|
|
||||||
|
for (unsigned int r = 0; r < m1.rows; r++)
|
||||||
|
for (unsigned int c = 0; c < m1.cols; c++)
|
||||||
|
result.buffer[r * result.cols + c] = m1.buffer[r * m1.cols + c] + m2.buffer[r * m2.cols + c];
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix result = createMatrix(m1.rows, m1.cols);
|
// Matrix2 ist ein Spaltenvektor
|
||||||
if (result.buffer == NULL) return result;
|
if (m1.rows == m2.rows && m2.cols == 1)
|
||||||
|
|
||||||
for (unsigned int r = 0; r < m1.rows; r++)
|
|
||||||
{
|
{
|
||||||
for (unsigned int c = 0; c < m1.cols; c++)
|
Matrix result = createMatrix(m1.rows, m1.cols);
|
||||||
{
|
if (!result.buffer) return result;
|
||||||
result.buffer[r * m1.cols + c] =
|
|
||||||
getMatrixAt(m1, r, c) + getMatrixAt(m2, r, c);
|
for (unsigned int r = 0; r < m1.rows; r++)
|
||||||
}
|
for (unsigned int c = 0; c < m1.cols; c++)
|
||||||
|
result.buffer[r * result.cols + c] = m1.buffer[r * m1.cols + c] + m2.buffer[r];
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
|
// Matrix1 ist ein Spaltenvektor
|
||||||
|
if (m1.rows == m2.rows && m1.cols == 1)
|
||||||
|
{
|
||||||
|
Matrix result = createMatrix(m2.rows, m2.cols);
|
||||||
|
if (!result.buffer) return result;
|
||||||
|
|
||||||
|
for (unsigned int r = 0; r < m2.rows; r++)
|
||||||
|
for (unsigned int c = 0; c < m2.cols; c++)
|
||||||
|
result.buffer[r * result.cols + c] = m1.buffer[r] + m2.buffer[r * m2.cols + c];
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// passt nicht
|
||||||
|
return createMatrix(0,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matrizen multiplizieren
|
// Matrizen multiplizieren
|
||||||
Matrix multiply(const Matrix m1, const Matrix m2)
|
Matrix multiply(const Matrix m1, const Matrix m2)
|
||||||
{
|
{
|
||||||
if (m1.cols != m2.rows)
|
if (!m1.buffer || !m2.buffer) return createMatrix(0,0);
|
||||||
{
|
if (m1.cols != m2.rows) return createMatrix(0,0);
|
||||||
return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix result = createMatrix(m1.rows, m2.cols);
|
Matrix result = createMatrix(m1.rows, m2.cols);
|
||||||
if (result.buffer == NULL) return result;
|
if (!result.buffer) return result;
|
||||||
|
|
||||||
for (unsigned int r = 0; r < m1.rows; r++)
|
for (unsigned int r = 0; r < m1.rows; r++)
|
||||||
{
|
|
||||||
for (unsigned int c = 0; c < m2.cols; c++)
|
for (unsigned int c = 0; c < m2.cols; c++)
|
||||||
{
|
{
|
||||||
MatrixType sum = 0;
|
MatrixType sum = 0;
|
||||||
for (unsigned int k = 0; k < m1.cols; k++)
|
for (unsigned int k = 0; k < m1.cols; k++)
|
||||||
{
|
sum += m1.buffer[r * m1.cols + k] * m2.buffer[k * m2.cols + c];
|
||||||
sum += getMatrixAt(m1, r, k) * getMatrixAt(m2, k, c);
|
result.buffer[r * result.cols + c] = sum;
|
||||||
}
|
|
||||||
result.buffer[r * m2.cols + c] = sum;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
+1
-1
@@ -197,7 +197,7 @@ static Matrix forward(const NeuralNetwork model, Matrix inputBatch)
|
|||||||
|
|
||||||
if(result.buffer != NULL)
|
if(result.buffer != NULL)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < model.numberOfLayers; i++)
|
for(int i = 0; i < model.numberOfLayers; i++)
|
||||||
{
|
{
|
||||||
Matrix biasResult;
|
Matrix biasResult;
|
||||||
Matrix weightResult;
|
Matrix weightResult;
|
||||||
|
|||||||
Reference in New Issue
Block a user