Compare commits

..

1 Commits

Author SHA1 Message Date
D2A62006
2b388ab748 fix image input.c 2025-11-27 12:19:07 +01:00
5 changed files with 256 additions and 107 deletions

View File

@ -1,5 +1,4 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "imageInput.h" #include "imageInput.h"
@ -8,56 +7,50 @@
#define FILE_HEADER_STRING "__info2_image_file_format__" #define FILE_HEADER_STRING "__info2_image_file_format__"
/// @brief Gets the next char value from specified and opened file in the moment that is only ONE short int. /// @brief Reads a value in little-endian format from file
/// @param openedFile stream FILE, from which to read /// @param openedFile stream FILE, from which to read
static int getCharValueFromFile(FILE* openedFile) { /// @param bytes how many bytes to read
int addToFile = 0; static unsigned int readLittleEndian(FILE* openedFile, int bytes) {
unsigned int value = 0;
if (openedFile == NULL) return 0; if (openedFile == NULL) return 0;
addToFile = fgetc(openedFile); for (int i = 0; i < bytes; i++) {
addToFile += fgetc(openedFile) * 256; int tmp = fgetc(openedFile);
return addToFile; if (tmp == EOF) return 0;
value |= ((unsigned int)tmp) << (i * 8);
} }
return value;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path) GrayScaleImageSeries *readImages(const char *path)
{ {
GrayScaleImageSeries *series = NULL; FILE* openFile = fopen(path, "rb");
// uint16_t
FILE* openFile;
openFile = fopen(path, "rb");
// file Could not be opened/does not exist // file Could not be opened/does not exist
if (openFile != NULL) { if (openFile == NULL) {
return NULL;
}
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING)+1 * sizeof(char)); char actualFileTag[strlen(FILE_HEADER_STRING) + 1];
int numberOfImages = 0;
int width = 0;
int height = 0;
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) { size_t tagLength = strlen(FILE_HEADER_STRING);
char tmpFileChar = fgetc(openFile); if (fread(actualFileTag, 1, tagLength, openFile) != tagLength) {
if (tmpFileChar == EOF) {
fclose(openFile); fclose(openFile);
return NULL; return NULL;
} }
actualFileTag[i] = tmpFileChar; actualFileTag[tagLength] = '\0';
}
actualFileTag[strlen(FILE_HEADER_STRING)] = '\0';
// checks if the files are equal // checks if the files are equal
int fileTagEqual = strcmp(actualFileTag, FILE_HEADER_STRING); if (strcmp(actualFileTag, FILE_HEADER_STRING) != 0) {
fclose(openFile);
return NULL;
}
//we only need the fileTag to verify its an image for our series. unsigned int numberOfImages = readLittleEndian(openFile, 2);
free(actualFileTag);
actualFileTag = NULL;
//only if equal, the method continues
if (fileTagEqual == 0) {
numberOfImages = getCharValueFromFile(openFile);
// no Images in series -> No image-series // no Images in series -> No image-series
if (numberOfImages == 0) { if (numberOfImages == 0) {
@ -65,8 +58,8 @@ GrayScaleImageSeries* readImages(const char *path)
return NULL; return NULL;
} }
width = getCharValueFromFile(openFile); unsigned int width = readLittleEndian(openFile, 2);
height = getCharValueFromFile(openFile); unsigned int height = readLittleEndian(openFile, 2);
// no height/width --> impossible file // no height/width --> impossible file
if(height == 0 || width == 0) { if(height == 0 || width == 0) {
@ -75,47 +68,86 @@ GrayScaleImageSeries* readImages(const char *path)
} }
//all the starting parameters are set --> the images can be read and stored //all the starting parameters are set --> the images can be read and stored
series = malloc(sizeof(GrayScaleImageSeries)); GrayScaleImageSeries* series = malloc(sizeof(GrayScaleImageSeries));
if (series == NULL) {
fclose(openFile);
return NULL;
}
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
if (images == NULL || labels == NULL) {
free(images);
free(labels);
free(series);
fclose(openFile);
return NULL;
}
series->count = 0; series->count = 0;
series->images = malloc(numberOfImages * sizeof(GrayScaleImage)); series->images = images;
series->labels = malloc(numberOfImages * sizeof(unsigned char)); series->labels = labels;
for (int i = 0; i < numberOfImages; i++) { for (unsigned int i = 0; i < numberOfImages; i++) {
series->images[i].height = height; // allocating the actual matrix image for each image
series->images[i].width = width; images[i].buffer = malloc(width * height);
series->images[i].buffer = malloc(width * height);
for (int j = 0; j < (width * height); j++) { if (images[i].buffer == NULL) {
// allocating the actual matrix image for image for (unsigned int k = 0; k < i; k++) {
series->images[i].buffer[j] = fgetc(openFile); free(images[k].buffer);
} }
free(images);
free(labels);
free(series);
fclose(openFile);
return NULL;
}
images[i].height = height;
images[i].width = width;
if (fread(images[i].buffer, 1, width * height, openFile) != width * height) {
for (unsigned int k = 0; k <= i; k++) {
free(images[k].buffer);
}
free(images);
free(labels);
free(series);
fclose(openFile);
return NULL;
}
//rest of the values that only affect the image itself //rest of the values that only affect the image itself
series->labels[i] = fgetc(openFile); int label = fgetc(openFile);
if (label == EOF) {
for (unsigned int k = 0; k <= i; k++) {
free(images[k].buffer);
}
free(images);
free(labels);
free(series);
fclose(openFile);
return NULL;
}
series->labels[i] = (unsigned char)label;
series->count++; series->count++;
} }
}
// the fclose happens here, since every other path closes the file beforehand
fclose(openFile); fclose(openFile);
}
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 == NULL) return; if (series == NULL) return;
for (int i = 0; i < series->count; i++) { for (int i = 0; i < series->count; i++) {
free(series->images[i].buffer); free(series->images[i].buffer);
series->images[i].buffer = NULL;
} }
free(series->images); free(series->images);
series->images = NULL;
free(series->labels); free(series->labels);
series->labels = NULL;
free(series); free(series);
series = NULL;
} }

115
imageInput_old.c Normal file
View File

@ -0,0 +1,115 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imageInput.h"
#define BUFFER_SIZE 100
#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) {
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;
}
return addToFile;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
FILE* openFile;
openFile = fopen(path, "rb");
// file Could not be opened/does not exist
if (openFile != NULL) {
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING) * sizeof(char));
int numberOfImages = 0;
int width = 0;
int height = 0;
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
actualFileTag += 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);
//we only need the fileTag to verify its an image for our series.
free(actualFileTag);
actualFileTag = NULL;
if (fileTagEqual) {
numberOfImages = getCharValueFromFile(openFile, 2);
// no Images in series -> No image-series
if (numberOfImages == 0) {
fclose(openFile);
return series;
}
width = getCharValueFromFile(openFile, 2);
height = getCharValueFromFile(openFile, 2);
// no height/width --> impossible file
if(height == 0 || width == 0) {
fclose(openFile);
return series;
}
//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->count = 0;
series->images = images;
series->labels = labels;
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);
images[i].height = height;
images[i].width = width;
images[i].buffer[j] = fgetc(openFile);
}
//rest of the values that only affect the image itself
series->labels[i] = fgetc(openFile);
series->count++;
}
}
}
fclose(openFile);
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
for (int i = 0; i < series->count; i++) {
free(series->images[i].buffer);
}
free(series->images);
series->images = NULL;
free(series->labels);
series->labels = NULL;
}

View File

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

View File

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

View File

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