Compare commits

...

6 Commits

4 changed files with 59 additions and 55 deletions

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "imageInput.h"
@ -7,30 +8,22 @@
#define FILE_HEADER_STRING "__info2_image_file_format__"
/// @brief Gets the next char value from specified and opened file
/// @param openedFile stream FILE, from which to read
/// @param iterations how many chars are being taken from (1 char equals 2 Hexdecimals equals 8bit)
static int getCharValueFromFile(FILE* openedFile, int iterations) {
/// @brief Gets the next char value from specified and opened file in the moment that is only ONE short int.
/// @param openedFile stream FILE, from which to read
static int getCharValueFromFile(FILE* openedFile) {
int addToFile = 0;
if (openedFile == NULL) return 0;
for (int i = 0; i < iterations; i++) {
int tmp = fgetc(openedFile);
//If the File ends, the method returns a '0'
if (openedFile == EOF) return addToFile;
addToFile += tmp;
}
addToFile = fgetc(openedFile);
addToFile += fgetc(openedFile) * 256;
return addToFile;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
GrayScaleImageSeries* readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
// uint16_t
FILE* openFile;
openFile = fopen(path, "rb");
@ -38,78 +31,91 @@ GrayScaleImageSeries *readImages(const char *path)
// file Could not be opened/does not exist
if (openFile != NULL) {
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING) * sizeof(char));
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING)+1 * sizeof(char));
int numberOfImages = 0;
int width = 0;
int height = 0;
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
actualFileTag += fgetc(openFile);
}
char tmpFileChar = fgetc(openFile);
// checks if the files are equal: strcmp should return '0' --> convert it to '1' for 'true'
int fileTagEqual = !strcmp(actualFileTag, FILE_HEADER_STRING);
if (tmpFileChar == EOF) {
fclose(openFile);
return NULL;
}
actualFileTag[i] = tmpFileChar;
}
actualFileTag[strlen(FILE_HEADER_STRING)] = '\0';
// checks if the files are equal
int fileTagEqual = strcmp(actualFileTag, FILE_HEADER_STRING);
//we only need the fileTag to verify its an image for our series.
free(actualFileTag);
actualFileTag = NULL;
if (fileTagEqual) {
numberOfImages = getCharValueFromFile(openFile, 2);
//only if equal, the method continues
if (fileTagEqual == 0) {
numberOfImages = getCharValueFromFile(openFile);
// no Images in series -> No image-series
if (numberOfImages == 0) {
fclose(openFile);
return series;
return NULL;
}
width = getCharValueFromFile(openFile, 2);
height = getCharValueFromFile(openFile, 2);
width = getCharValueFromFile(openFile);
height = getCharValueFromFile(openFile);
// no height/width --> impossible file
if(height == 0 || width == 0) {
fclose(openFile);
return series;
return NULL;
}
//all the starting parameters are set --> the images can be read and stored
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
series = malloc(sizeof(GrayScaleImageSeries));
series->count = 0;
series->images = images;
series->labels = labels;
series->images = malloc(numberOfImages * sizeof(GrayScaleImage));
series->labels = malloc(numberOfImages * sizeof(unsigned char));
for (int i = 0; i < numberOfImages; i++) {
for (int j = 0; j < width * height; j++) {
// allocating the actual matrix image for image
images[i].buffer = malloc(width * height);
series->images[i].height = height;
series->images[i].width = width;
series->images[i].buffer = malloc(width * height);
images[i].height = height;
images[i].width = width;
images[i].buffer[j] = fgetc(openFile);
for (int j = 0; j < (width * height); j++) {
// allocating the actual matrix image for image
series->images[i].buffer[j] = fgetc(openFile);
}
//rest of the values that only affect the image itself
series->labels[i] = fgetc(openFile);
series->count++;
}
}
// the fclose happens here, since every other path closes the file beforehand
fclose (openFile);
}
fclose(openFile);
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
if (series == NULL) return;
for (int i = 0; i < series->count; i++) {
free(series->images[i].buffer);
series->images[i].buffer = NULL;
}
free(series->images);
series->images = NULL;
free(series->labels);
series->labels = NULL;
free(series);
series = NULL;
}

View File

@ -1,9 +1,6 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
#include <stdbool.h>
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(size_t rows, size_t cols)
{
@ -17,7 +14,7 @@ Matrix createMatrix(size_t rows, size_t cols)
return m;
}
// Single allocation for entire matrix
//Allocate Matrix buffer
m.buffer = malloc(rows * cols * sizeof(MatrixType));
if(!m.buffer){
@ -25,7 +22,7 @@ Matrix createMatrix(size_t rows, size_t cols)
return m;
}
// Initialize (optional)
//Initialize matrix with default value
for(unsigned int i = 0; i < rows * cols; i++){
m.buffer[i] = UNDEFINED_MATRIX_VALUE;
}
@ -34,7 +31,7 @@ Matrix createMatrix(size_t rows, size_t cols)
}
void clearMatrix(Matrix *matrix)
void clearMatrix(Matrix* matrix)
{
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols;j++) {
@ -75,7 +72,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
bool doBroadcast = false;
unsigned int doBroadcast = 0;
Matrix larger, smaller;
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
@ -86,13 +83,13 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
{
larger = matrix1;
smaller = matrix2;
doBroadcast = true;
doBroadcast = 1;
}
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
{
larger = matrix2;
smaller = matrix1;
doBroadcast = true;
doBroadcast = 1;
}
else{
Matrix m = {NULL, 0, 0};
@ -101,6 +98,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
if(doBroadcast){
//Broadcasting
for(int i = 0; i < outputMatrix.rows; i++){
MatrixType broadcastValue = smaller.buffer[i];
for(int j = 0; j < outputMatrix.cols; j++){
@ -108,6 +106,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
}
}
} else{
//Classic execution
for (int i = 0; i < matrix1.rows;i++) {
for (int j = 0; j < matrix1.cols; j++) {
// how this should work in normal Matrix version:

View File

@ -164,7 +164,6 @@ NeuralNetwork loadModel(const char *path)
assignActivations(model);
}
printf("%d\n", model.numberOfLayers);
return model;
}

View File

@ -13,30 +13,30 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
if(file != NULL){
const char *fileTag = "__info2_neural_network_file_format__";
// Write file header
//Write header
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
// Write the input dimension of the first layer
//Write the input dimension of the first layer
if(nn.numberOfLayers > 0){
fwrite(&nn.layers[0].weights.cols, sizeof(int), 1, file);
}
// Write dimensions and data for each layer
//Write each layer into file
for(int i = 0; i < nn.numberOfLayers; i++){
// Write output dimension (rows of weights)
//Write output dimension
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
// Write weight matrix data
//Write weight data
int weightSize = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightSize, file);
// Write bias matrix data
//Write bias data
int biasSize = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasSize, file);
}
// Write terminating 0 to signal end of layers
//EOF Terminator
int zero = 0;
fwrite(&zero, sizeof(int), 1, file);