generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e544da140e | ||
|
|
0aa42abd43 | ||
|
|
0f28ee3f02 | ||
|
|
bb96c8f78a | ||
|
|
cfb3848fe2 | ||
|
|
0a8349d722 | ||
|
|
3df6e152d7 | ||
|
|
f52de5e656 | ||
|
|
65da22f564 | ||
|
|
2ee6db1268 | ||
|
|
ec8c735eb2 | ||
|
|
f6a813a635 | ||
|
|
82d72c5e90 | ||
|
|
0a549b7e0d | ||
|
|
c99db0f96c |
+80
-35
@@ -6,8 +6,6 @@
|
|||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#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)
|
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
|
if (file == NULL){ //returns 0 for empty file
|
||||||
return 0;
|
return 0;
|
||||||
@@ -30,58 +28,105 @@ int checkString (FILE *file){ //Checks if String at the start of File equals exp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height);
|
static void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height);
|
||||||
void allocateMemory(GrayScaleImageSeries *s, const int imageCount, const int width, const 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 *readImages(const char *path)
|
||||||
{
|
{
|
||||||
FILE* file = fopen(path, "rb");
|
FILE* file = fopen(path, "rb");
|
||||||
// TODO open binary file from file name
|
if(!(checkString(file))){
|
||||||
GrayScaleImageSeries *series = NULL;
|
fclose(file);
|
||||||
if(!(checkString(file)))
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int* imageCount = &(series->count); // sets a pointer for int variable count in struct
|
unsigned int width, height; //uninitialised Variables, will be set by setparamters
|
||||||
|
|
||||||
setParameters(file, imageCount, &(series->images->width), &(series->images->height));
|
|
||||||
allocateMemory(series, *imageCount, series->images->width, series->images->height);
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); //Reserving memory for series
|
||||||
//allocateMemoryLabels();
|
|
||||||
for(int i = (series->images->width * series->images->height) - 1; i <= 0; i--)
|
setParameters(file, &(series->count), &width, &height); //setting parameters taken from image file
|
||||||
{
|
|
||||||
fread(&(series->images->buffer[i]), sizeof(GrayScalePixelType), 1, 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);
|
fclose(file);
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
for(size_t i = series->count - 1; i >= 0; i--)
|
if (!series)
|
||||||
|
return;
|
||||||
|
|
||||||
|
//Freeing Pixelbuffer for every image
|
||||||
|
for(unsigned int i = 0; i < series->count; i++)
|
||||||
{
|
{
|
||||||
free(series->images+series->count*sizeof(GrayScaleImage)*i);
|
free(series->images[i].buffer);
|
||||||
free(series->labels+series->count*sizeof(unsigned char)*i);
|
|
||||||
}
|
}
|
||||||
series->images = NULL;
|
free(series->images); //Freeing Images array
|
||||||
series->labels = NULL;
|
|
||||||
|
free(series->labels); //Freeing Labels
|
||||||
|
free(series); //Freeing Main Structure
|
||||||
|
|
||||||
}
|
}
|
||||||
void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height) { // sets the parameters
|
static void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height) { // sets the parameters
|
||||||
char buffer[3];
|
|
||||||
|
unsigned short buffer[3];
|
||||||
|
|
||||||
fseek(file, 27, SEEK_SET);
|
fseek(file, 27, SEEK_SET);
|
||||||
fread(buffer, 1, 3, file);
|
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 = (int) buffer[0];
|
*imageCount = (unsigned int) buffer[0];
|
||||||
*width = (int) buffer[1];
|
|
||||||
*height = (int) buffer[2];
|
*width = (unsigned int) buffer[1];
|
||||||
// TODO allocate memory for labels array (in imageInput.h) -> Done in allocate Memory
|
|
||||||
|
*height = (unsigned int) buffer[2];
|
||||||
// TODO read from file and write in 2d arry, write label in labels array, repeat for imageCount -> done in readImages
|
|
||||||
}
|
}
|
||||||
// change this to createMatrix?
|
|
||||||
void allocateMemory(GrayScaleImageSeries* s, const int imageCount, const int width, const int height) {
|
static int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) {
|
||||||
for (int i = 0; i < imageCount; i++) {
|
series->count = imageCount; //counts number of images in series
|
||||||
s->images[i].buffer = (unsigned char *) malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series
|
|
||||||
s->labels = malloc(width * height * sizeof(unsigned char));
|
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)
|
void test_readImagesReturnsCorrectNumberOfImages(void)
|
||||||
{
|
{
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
|
||||||
const unsigned short expectedNumberOfImages = 2;
|
const unsigned short expectedNumberOfImages = 2;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
||||||
|
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
|
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
||||||
|
|
||||||
clearSeries(series);
|
clearSeries(series);
|
||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,11 +108,17 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
Matrix result;
|
Matrix result;
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
|
||||||
|
if (matrix1.cols != matrix2.rows) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
result.rows = matrix1.rows;
|
result.rows = matrix1.rows;
|
||||||
result.cols = matrix2.cols;
|
result.cols = matrix2.cols;
|
||||||
|
|
||||||
if (matrix1.rows == matrix2.cols) {
|
|
||||||
|
|
||||||
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||||
|
|
||||||
for(int i = 0; i < result.rows; i++) {
|
for(int i = 0; i < result.rows; i++) {
|
||||||
@@ -121,11 +127,8 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
for(int k = 0; k < matrix1.cols; k++) {
|
for(int k = 0; k < matrix1.cols; k++) {
|
||||||
value += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
value += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
||||||
}
|
}
|
||||||
result.buffer[i * matrix1.cols + j] = value;
|
result.buffer[i * result.cols + j] = value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
printf("Die angegebenen Matrizen haben keine passenden Dimensionen für die Multiplikation");
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
+1
-2
@@ -155,7 +155,6 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
|
|
||||||
model.layers[model.numberOfLayers] = layer;
|
model.layers[model.numberOfLayers] = layer;
|
||||||
model.numberOfLayers++;
|
model.numberOfLayers++;
|
||||||
|
|
||||||
inputDimension = outputDimension;
|
inputDimension = outputDimension;
|
||||||
outputDimension = readDimension(file);
|
outputDimension = readDimension(file);
|
||||||
}
|
}
|
||||||
@@ -170,7 +169,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}; // TODO changed this line to fit our functionality
|
||||||
|
|
||||||
if(count > 0 && images != NULL)
|
if(count > 0 && images != NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,6 +9,29 @@
|
|||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
|
FILE* file = fopen(path, "wb");
|
||||||
|
if(file == NULL) {
|
||||||
|
printf("Failed to open file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf("\nLayers in pNNF: %d\n", nn.numberOfLayers);
|
||||||
|
const char* header = "__info2_neural_network_file_format__";
|
||||||
|
fwrite(header, sizeof(const char), strlen(header), file);
|
||||||
|
|
||||||
|
for (int i = 0; i < nn.numberOfLayers; i++) {
|
||||||
|
|
||||||
|
fwrite(&(nn.layers[i].weights.cols), sizeof(unsigned int), 1, file);
|
||||||
|
fwrite(&(nn.layers[i].weights.rows), sizeof(unsigned int), 1, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < nn.numberOfLayers; i++) {
|
||||||
|
//write everything to do with weights
|
||||||
|
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), nn.layers[i].weights.rows * nn.layers[i].weights.cols, file);
|
||||||
|
|
||||||
|
//write everything to do with biases
|
||||||
|
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), nn.layers[i].biases.rows * nn.layers[i].biases.cols, file);
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
@@ -25,6 +48,7 @@ void test_loadModelReturnsCorrectNumberOfLayers(void)
|
|||||||
Layer layers[] = {{.weights=weights1, .biases=biases1}, {.weights=weights2, .biases=biases2}};
|
Layer layers[] = {{.weights=weights1, .biases=biases1}, {.weights=weights2, .biases=biases2}};
|
||||||
|
|
||||||
NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=2};
|
NeuralNetwork expectedNet = {.layers=layers, .numberOfLayers=2};
|
||||||
|
printf("\nexpectedNetLayers: %d", expectedNet.numberOfLayers);
|
||||||
NeuralNetwork netUnderTest;
|
NeuralNetwork netUnderTest;
|
||||||
|
|
||||||
prepareNeuralNetworkFile(path, expectedNet);
|
prepareNeuralNetworkFile(path, expectedNet);
|
||||||
|
|||||||
Reference in New Issue
Block a user