generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
42
Commits
mMRecovery
...
TobiasV2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4272d04f0d | ||
|
|
06782431a4 | ||
|
|
0a8349d722 | ||
|
|
3df6e152d7 | ||
|
|
f52de5e656 | ||
|
|
65da22f564 | ||
|
|
2ee6db1268 | ||
|
|
ec8c735eb2 | ||
|
|
f6a813a635 | ||
|
|
82d72c5e90 | ||
|
|
0a549b7e0d | ||
|
|
c99db0f96c | ||
|
|
6524bf95e4 | ||
|
|
c13c8bd6f6 | ||
|
|
7168681f1d | ||
|
|
04493332f7 | ||
|
|
94a69b0068 | ||
|
|
ed304e3e60 | ||
|
|
8676389e62 | ||
|
|
963eb3b3a4 | ||
|
|
9bd9cd7985 | ||
|
|
3adc90e98c | ||
|
|
ef7062e1fd | ||
|
|
db14708439 | ||
|
|
e4b0aea848 | ||
|
|
926c5c583d | ||
|
|
1dd5d458e6 | ||
|
|
98cbfefedf | ||
|
|
4640908e1b | ||
|
|
0c19c17d68 | ||
|
|
5d31d4e490 | ||
|
|
dfce0ec3aa | ||
|
|
73e3a32c75 | ||
|
|
fb8672e5a1 | ||
|
|
3ff09b0ee6 | ||
|
|
ac8c827b37 | ||
|
|
0ac4b64866 | ||
|
|
abe4ecec65 | ||
|
|
9eba090e45 | ||
|
|
b9b4d926f5 | ||
|
|
7acece0571 | ||
|
|
44b77815b3 |
+115
-5
@@ -6,17 +6,127 @@
|
||||
#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
|
||||
int checkString (FILE *file){ //Checks if String at the start of File equals expected Format (0 for wrong 1 for right)
|
||||
if (file == NULL){ //returns 0 for empty file
|
||||
return 0;
|
||||
}
|
||||
char expectedString[] = FILE_HEADER_STRING;
|
||||
char actualString[BUFFER_SIZE];
|
||||
|
||||
rewind(file);
|
||||
size_t bytesRead = fread(actualString, 1,27 , file); //Stores Bytes of start of File to actualString
|
||||
if (bytesRead != 27){
|
||||
return 0;
|
||||
} //Returns 0 if File is to short
|
||||
actualString[27] = '\0';
|
||||
|
||||
if (strcmp(actualString, expectedString) != 0){
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height);
|
||||
static int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height);
|
||||
|
||||
|
||||
// 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(!(checkString(file))){
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned int width, height; //uninitialised Variables, will be set by setparamters
|
||||
|
||||
|
||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); //Reserving memory for series
|
||||
|
||||
setParameters(file, &(series->count), &width, &height); //setting parameters taken from image file
|
||||
|
||||
if(!allocateMemory(series, series->count, width, height)){
|
||||
clearSeries(series); //If Memory couldnt be correctly allocated a clearing is necessary
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Getting image data
|
||||
for(unsigned int i = 0; i < series->count ; i++){ //Iterating for every image
|
||||
for(unsigned int j = 0; j < width * height; j++){ //Iterating for every pixel
|
||||
fread(&(series->images[i].buffer[j]), sizeof(GrayScalePixelType), 1, file);
|
||||
}
|
||||
fread(&(series->labels[i]), sizeof(unsigned char), 1, file); // TODO Added writing in labels array
|
||||
}
|
||||
fclose(file);
|
||||
return series;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (!series)
|
||||
return;
|
||||
|
||||
//Freeing Pixelbuffer for every image
|
||||
for(unsigned int i = 0; i < series->count; i++)
|
||||
{
|
||||
free(series->images[i].buffer);
|
||||
}
|
||||
free(series->images); //Freeing Images array
|
||||
|
||||
free(series->labels); //Freeing Labels
|
||||
free(series); //Freeing Main Structure
|
||||
|
||||
}
|
||||
static void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height) { // sets the parameters
|
||||
|
||||
unsigned short buffer[3];
|
||||
|
||||
fseek(file, 27, SEEK_SET);
|
||||
if(fread(buffer, sizeof(unsigned short), 3, file) != 3){ // TODO changed some stuff here, used UNSIGNED SHORT instead of UNSIGNED CHAR!!
|
||||
*imageCount = 0;
|
||||
*width = 0;
|
||||
*height = 0;
|
||||
return;
|
||||
} //Stops if file is too short and clean up
|
||||
|
||||
*imageCount = (unsigned int) buffer[0];
|
||||
|
||||
*width = (unsigned int) buffer[1];
|
||||
|
||||
*height = (unsigned int) buffer[2];
|
||||
}
|
||||
|
||||
static int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) {
|
||||
series->count = imageCount; //counts number of images in series
|
||||
|
||||
series->images = malloc (sizeof(GrayScaleImage) * imageCount); //Reserving Memory for Images Array
|
||||
if (series->images == NULL)
|
||||
return 0;
|
||||
|
||||
|
||||
series->labels = malloc (series->count * sizeof(unsigned char)); //One Label for every Image
|
||||
if (series->labels == NULL){
|
||||
return 0;}
|
||||
|
||||
for (int i = 0; i < imageCount; i++) { //Reserving Memory for every Images Pixelbuffer and setting width and height
|
||||
series->images[i].width = width;
|
||||
series->images[i].height = height;
|
||||
|
||||
series->images[i].buffer = malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series
|
||||
if (series->images[i].buffer == NULL){
|
||||
for (int j = 0; j < i; j++){
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -39,12 +39,16 @@ static void prepareImageFile(const char *path, unsigned short int width, unsigne
|
||||
void test_readImagesReturnsCorrectNumberOfImages(void)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
const unsigned short expectedNumberOfImages = 2;
|
||||
const char *path = "testFile.info2";
|
||||
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
||||
|
||||
series = readImages(path);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(series);
|
||||
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
||||
|
||||
clearSeries(series);
|
||||
remove(path);
|
||||
}
|
||||
|
||||
@@ -1,61 +1,147 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
typedef struct Matrix {
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType* buffer;
|
||||
} Matrix;
|
||||
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix newMatrix;
|
||||
if (rows == 0 || cols == 0) {
|
||||
newMatrix.rows = 0;
|
||||
newMatrix.cols = 0;
|
||||
newMatrix.buffer = NULL;
|
||||
return newMatrix;
|
||||
}
|
||||
|
||||
newMatrix.rows = rows;
|
||||
newMatrix.cols = cols;
|
||||
|
||||
newMatrix.buffer = calloc(rows*cols, sizeof(MatrixType));
|
||||
return newMatrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols){
|
||||
printf("Index out of bounds\n"); //Error Message because Index Input exceeds Matrix
|
||||
}
|
||||
else{
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; //Writes Value of value variable in the selected place in Matrix
|
||||
}
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols){
|
||||
printf("Index out of bounds\n");
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
|
||||
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx]; //Stores value of selected place of Matrix in value variable
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
|
||||
//If rows and cols of both Matrixes are the same the normal addition starts
|
||||
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
|
||||
Matrix MatrixErgebnis = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for (unsigned int i = 0; i < matrix1.rows; i++){
|
||||
for (unsigned int j = 0; j < matrix1.cols; j++){
|
||||
//Adding Matrix Elements of same row and col index together and store in new Matrix
|
||||
MatrixErgebnis.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix1.cols + j];
|
||||
|
||||
}
|
||||
}
|
||||
return MatrixErgebnis;
|
||||
}
|
||||
|
||||
//if matrix1 is a Vektor it adds elements to first col of matrix2
|
||||
else if (matrix1.cols == 1 && matrix1.rows == matrix2.rows){
|
||||
Matrix MatrixErgebnis = createMatrix(matrix2.rows, matrix2.cols);
|
||||
for (unsigned int i = 0; i < matrix2.rows; i++) {
|
||||
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
||||
//Copy Matrix2 to ErgebnisMatrix
|
||||
MatrixErgebnis.buffer[i * matrix2.cols + j] = getMatrixAt(matrix2, i, j);
|
||||
//Adding Elements of first col of Matrix 1 (Vector) to all Elements of MatrixErgebnis
|
||||
MatrixErgebnis.buffer[i * matrix2.cols + j] += matrix1.buffer[i];
|
||||
}
|
||||
}
|
||||
return MatrixErgebnis;
|
||||
}
|
||||
//if matrix2 is a Vektor it adds elements to first col of matrix1
|
||||
else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows){
|
||||
Matrix MatrixErgebnis = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
||||
for (unsigned int j = 0; j < matrix1.cols; j++) {
|
||||
//Copy Matrix1 to ErgebnisMatrix
|
||||
MatrixErgebnis.buffer[i * matrix1.cols + j] = getMatrixAt(matrix1, i, j);
|
||||
//Adding Elements of first col of Matrix 2 (Vector) to first col of MatrixErgebnis
|
||||
MatrixErgebnis.buffer[i * matrix1.cols + j] += matrix2.buffer[i];
|
||||
|
||||
}
|
||||
}
|
||||
return MatrixErgebnis;
|
||||
}
|
||||
else{
|
||||
printf("Matrix dimensions do not match\n"); //Error Message if dimensions of Input Matrixes are not identical
|
||||
Matrix MatrixErgebnis = createMatrix(0, 0);
|
||||
clearMatrix(&MatrixErgebnis);
|
||||
return MatrixErgebnis;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
if (matrix1.rows == matrix2.cols) {
|
||||
Matrix result;
|
||||
result.rows = matrix1.rows;
|
||||
result.cols = matrix2.cols;
|
||||
Matrix result;
|
||||
result.rows = 0;
|
||||
result.cols = 0;
|
||||
result.buffer = NULL;
|
||||
|
||||
if (matrix1.cols != matrix2.rows) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result.rows = matrix1.rows;
|
||||
result.cols = matrix2.cols;
|
||||
|
||||
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||
|
||||
for(int i = 0; i < rows; i++) {
|
||||
for(int j = 0; j < cols; j++) {
|
||||
for(int i = 0; i < result.rows; i++) {
|
||||
for(int j = 0; j < result.cols; j++) {
|
||||
MatrixType value = 0;
|
||||
for(int k = 0; k < matrix1.cols; k++) {
|
||||
value += matrix1[i][k] * matrix2[k][j];
|
||||
value += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
||||
}
|
||||
result.buffer[i][i] = value;
|
||||
result.buffer[i * result.cols + j] = value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
printf("Die angegebenen Matrizen haben keine passenden Dimensionen für die Multiplikation");
|
||||
return NULL;
|
||||
return result;
|
||||
}
|
||||
void writeMatrix(Matrix matrix, FILE *file)//Added for neuralNetworkTests
|
||||
{
|
||||
//fprintf(file, "%d%d", matrix.rows, matrix.cols);
|
||||
for(int i = 0; i < matrix.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix.cols; j++)
|
||||
{
|
||||
putw(*(matrix.buffer + (j+i)*sizeof(MatrixType)), file);
|
||||
//fprintf(file, "%f", *(matrix.buffer + (j+i)*sizeof(MatrixType)));
|
||||
printf("%f", *(matrix.buffer + (j+i)*sizeof(MatrixType)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,11 @@ typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct Matrix;
|
||||
typedef struct Matrix{
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType* buffer;
|
||||
}Matrix;
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +21,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
void writeMatrix(Matrix matrix, FILE *file);//Added for neuralNetworkTests
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
+14
-2
@@ -4,11 +4,23 @@
|
||||
#include <math.h>
|
||||
#include "unity.h"
|
||||
#include "neuralNetwork.h"
|
||||
#include "matrix.h"
|
||||
|
||||
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||
{
|
||||
// TODO
|
||||
FILE *testDatei = fopen(path, "w");
|
||||
fprintf(testDatei, "__info2_neural_network_file_format__");
|
||||
fprintf(testDatei, "%d%d", nn.layers->weights.rows, nn.layers->weights.cols);
|
||||
// fprintf(testDatei, (char*) nn.numberOfLayers);
|
||||
for(int i = 0; i < nn.numberOfLayers; i++)
|
||||
{
|
||||
//fprintf(testDatei, "\n");
|
||||
//putw((nn.layers + sizeof(Layer) * i), testDatei);
|
||||
writeMatrix(nn.layers[i].weights, testDatei);
|
||||
writeMatrix(nn.layers[i].biases, testDatei);
|
||||
}
|
||||
fclose(testDatei);
|
||||
}
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
@@ -31,7 +43,7 @@ void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
|
||||
netUnderTest = loadModel(path);
|
||||
remove(path);
|
||||
|
||||
printf("\n%d\n%d\n", netUnderTest.numberOfLayers, expectedNet.numberOfLayers);
|
||||
TEST_ASSERT_EQUAL_INT(expectedNet.numberOfLayers, netUnderTest.numberOfLayers);
|
||||
clearModel(&netUnderTest);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user