Compare commits

...
4 changed files with 38 additions and 27 deletions
+13 -22
View File
@@ -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,9 +28,8 @@ 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);
int allocateMemory(GrayScaleImageSeries *series, 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)
@@ -42,7 +39,7 @@ GrayScaleImageSeries *readImages(const char *path)
fclose(file); fclose(file);
return NULL; return NULL;
} }
// TODO open binary file from file name
unsigned int width, height; //uninitialised Variables, will be set by setparamters unsigned int width, height; //uninitialised Variables, will be set by setparamters
@@ -63,12 +60,12 @@ GrayScaleImageSeries *readImages(const char *path)
for(unsigned int j = 0; j < width * height; j++){ //Iterating for every pixel 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->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)
{ {
if (!series) if (!series)
@@ -85,32 +82,26 @@ void clearSeries(GrayScaleImageSeries *series)
free(series); //Freeing Main Structure 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!!
if(fread(buffer, 1, 3, file) != 3){
*imageCount = 0; *imageCount = 0;
*width = 0; *width = 0;
*height = 0; *height = 0;
return;
} //Stops if file is too short and clean up
*imageCount = (unsigned int) buffer[0];
return;} //Stops if file is too short and clean up *width = (unsigned int) buffer[1];
*imageCount = (int) buffer[0]; *height = (unsigned int) buffer[2];
*width = (int) buffer[1];
*height = (int) buffer[2];
// TODO allocate memory for labels array (in imageInput.h) -> Done in allocate Memory
// TODO read from file and write in 2d arry, write label in labels array, repeat for imageCount -> done in readImages
} }
int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) { static int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) {
series->count = imageCount; //counts number of images in series series->count = imageCount; //counts number of images in series
series->images = malloc (sizeof(GrayScaleImage) * imageCount); //Reserving Memory for Images Array series->images = malloc (sizeof(GrayScaleImage) * imageCount); //Reserving Memory for Images Array
-3
View File
@@ -50,9 +50,7 @@ void test_readImagesReturnsCorrectNumberOfImages(void)
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count); TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
clearSeries(series); clearSeries(series);
printf("Test 1 BP5\n");
remove(path); remove(path);
printf("Test 1 BP6\n");
} }
void test_readImagesReturnsCorrectImageWidth(void) void test_readImagesReturnsCorrectImageWidth(void)
@@ -139,7 +137,6 @@ int main()
printf("\n============================\nImage input tests\n============================\n"); printf("\n============================\nImage input tests\n============================\n");
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages); RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
printf("BP Check1 complete");
RUN_TEST(test_readImagesReturnsCorrectImageWidth); RUN_TEST(test_readImagesReturnsCorrectImageWidth);
RUN_TEST(test_readImagesReturnsCorrectImageHeight); RUN_TEST(test_readImagesReturnsCorrectImageHeight);
RUN_TEST(test_readImagesReturnsCorrectLabels); RUN_TEST(test_readImagesReturnsCorrectLabels);
+1 -2
View File
@@ -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)
{ {
+24
View File
@@ -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);