generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
570e3fa930 | ||
|
|
127f93301f | ||
|
|
2b84bdac23 | ||
|
|
cfe95a6221 | ||
|
|
e15c89bb1d | ||
|
|
86b35bb735 | ||
|
|
b8adf7f0d6 | ||
|
|
1271805bb5 | ||
|
|
bc64e2d6fb | ||
|
|
2e76e7b8d9 |
No files matched your search
@@ -1,6 +1,6 @@
|
||||
# Neuronales Netz
|
||||
|
||||
## To-Dos
|
||||
* Matrixtyp definieren in `matrix.h`
|
||||
* `Matrix.c` implementieren
|
||||
* `imageInput.c` implementieren
|
||||
* [ ] Matrixtyp definieren in `matrix.h`
|
||||
* [ ] `Matrix.c` implementieren
|
||||
* [ ] `imageInput.c` implementieren
|
||||
+73
-2
@@ -7,12 +7,83 @@
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
static unsigned short readNumberOfImages(FILE *file)
|
||||
{
|
||||
unsigned short numberOfImages = 0;
|
||||
fseek(file, strlen(FILE_HEADER_STRING), SEEK_SET);
|
||||
fread(&numberOfImages, sizeof(unsigned short), 1, file);
|
||||
return numberOfImages;
|
||||
}
|
||||
|
||||
static unsigned short readImageWidth(FILE *file)
|
||||
{
|
||||
unsigned short ImageWidth = 0;
|
||||
fseek(file, sizeof(unsigned short), SEEK_CUR);
|
||||
fread(&ImageWidth, sizeof(unsigned short), 1, file);
|
||||
|
||||
return ImageWidth;
|
||||
}
|
||||
|
||||
static unsigned short readImageHeight(FILE *file)
|
||||
{
|
||||
unsigned short ImageHeight = 0;
|
||||
fseek(file, sizeof(unsigned short), SEEK_CUR);
|
||||
fread(&ImageHeight, sizeof(unsigned short), 1, file);
|
||||
return ImageHeight;
|
||||
}
|
||||
|
||||
static GrayScalePixelType *readImage(FILE *file, unsigned short ImageWidth, unsigned short ImageHeight)
|
||||
{
|
||||
GrayScalePixelType *image = malloc(ImageHeight*ImageWidth*sizeof(GrayScalePixelType));
|
||||
fseek(file, sizeof(unsigned short), SEEK_CUR);
|
||||
fread(image, sizeof(char), ImageHeight*ImageWidth, file);
|
||||
return image;
|
||||
|
||||
}
|
||||
|
||||
static GrayScalePixelType readLabel(FILE *file)
|
||||
{
|
||||
unsigned char label;
|
||||
fseek(file, sizeof(unsigned short), SEEK_CUR);
|
||||
fread(&label, sizeof(unsigned char), 1, file);
|
||||
return label;
|
||||
}
|
||||
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (file == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned short numberOfImages = readNumberOfImages(file);
|
||||
unsigned short imageWidth = readImageWidth(file);
|
||||
unsigned short imageHeight = readImageHeight(file);
|
||||
|
||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||
if(series == NULL)
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->count = numberOfImages;
|
||||
series->images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
||||
series->labels = malloc(numberOfImages * sizeof(unsigned char));
|
||||
|
||||
|
||||
for(int i = 0; i < numberOfImages; i++)
|
||||
{
|
||||
series->images[i].width = imageWidth;
|
||||
series->images[i].height = imageHeight;
|
||||
series->images[i].buffer = readImage(file, imageWidth, imageHeight);
|
||||
series->labels[i] = readLabel(file);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return series;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,8 @@ neuralNetworkTests: neuralNetwork.o matrix.o neuralNetworkTests.c $(unityfolder)
|
||||
imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runImageInputTests imageInputTests.c imageInput.o $(unityfolder)/unity.c
|
||||
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
|
||||
+107
-6
@@ -6,30 +6,131 @@
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix m;
|
||||
|
||||
if (rows == 0 || cols == 0) //Ueberprüfen, ob ungültige Werte für Matrix vorhanden
|
||||
{
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
m.buffer = NULL;
|
||||
return m;
|
||||
}
|
||||
|
||||
m.rows = rows;
|
||||
m.cols = cols;
|
||||
m.buffer = (MatrixType*)calloc(rows*cols, sizeof(MatrixType)); //Bei Fehlschalg gibt calloc entweder NULL oder Zeiger verschieden von NULL aus
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
matrix->buffer = 0;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols))
|
||||
{
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||
}
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
MatrixType value = 0;
|
||||
if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols))
|
||||
{
|
||||
value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
typedef enum {elemt_add, broadcast} Addiermodi;
|
||||
Addiermodi modus;
|
||||
|
||||
if((matrix1.cols == 1 || matrix2.cols ==1) && (matrix1.rows == matrix2.rows))
|
||||
{
|
||||
modus = broadcast;
|
||||
}
|
||||
else if((matrix1.rows == matrix2.rows) && (matrix1.cols == matrix2.cols))
|
||||
{
|
||||
modus = elemt_add;
|
||||
}
|
||||
else
|
||||
{
|
||||
Matrix failure = {NULL, 0, 0};
|
||||
return failure;
|
||||
}
|
||||
|
||||
Matrix m;
|
||||
if(modus == elemt_add)
|
||||
{
|
||||
m = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
for(int i = 0; i< matrix1.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix1.cols ; j++)
|
||||
{
|
||||
m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix1.cols+j];
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
else if(modus == broadcast)
|
||||
{
|
||||
|
||||
if(matrix1.cols == 1)
|
||||
{
|
||||
m = createMatrix(matrix2.rows, matrix2.cols);
|
||||
for(int i = 0; i< matrix2.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix2.cols ; j++)
|
||||
{
|
||||
m.buffer[i*matrix2.cols+j] = matrix2.buffer[i*matrix2.cols+j] + matrix1.buffer[i*matrix1.cols];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for(int i = 0; i< matrix1.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix1.cols ; j++)
|
||||
{
|
||||
m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix2.cols];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
}
|
||||
if(matrix1.cols != matrix2.rows)
|
||||
{
|
||||
Matrix failure = {NULL, 0, 0};
|
||||
return failure;
|
||||
}
|
||||
|
||||
Matrix m = createMatrix(matrix1.rows, matrix2.cols);
|
||||
|
||||
for(int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix2.cols ; j++)
|
||||
{
|
||||
int value = 0;
|
||||
for(int k = 0; k < matrix1.cols; k++)
|
||||
{
|
||||
value += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k , j);
|
||||
}
|
||||
m.buffer[i*matrix2.cols+j] = value;
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
+5
-1
@@ -6,7 +6,11 @@
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct Matrix{
|
||||
MatrixType *buffer;
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
}Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
some_tag
|
||||
Reference in new issue
Block a user