6 changed files with 322 additions and 116 deletions
+133 -2
View File
@@ -6,17 +6,148 @@
#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
/// @brief Reads a value in little-endian format from file
/// @param openedFile stream FILE, from which to read
/// @param bytes how many bytes to read
static unsigned int readLittleEndian(FILE* openedFile, int bytes) {
unsigned int value = 0;
if (openedFile == NULL) return 0;
for (int i = 0; i < bytes; i++) {
int tmp = fgetc(openedFile);
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 *series = NULL;
FILE* openFile = fopen(path, "rb");
// file Could not be opened/does not exist
if (openFile == NULL) {
return NULL;
}
char actualFileTag[strlen(FILE_HEADER_STRING) + 1];
size_t tagLength = strlen(FILE_HEADER_STRING);
if (fread(actualFileTag, 1, tagLength, openFile) != tagLength) {
fclose(openFile);
return NULL;
}
actualFileTag[tagLength] = '\0';
// checks if the files are equal
if (strcmp(actualFileTag, FILE_HEADER_STRING) != 0) {
fclose(openFile);
return NULL;
}
unsigned int numberOfImages = readLittleEndian(openFile, 2);
// no Images in series -> No image-series
if (numberOfImages == 0) {
fclose(openFile);
return NULL;
}
unsigned int width = readLittleEndian(openFile, 2);
unsigned int height = readLittleEndian(openFile, 2);
// no height/width --> impossible file
if(height == 0 || width == 0) {
fclose(openFile);
return NULL;
}
//all the starting parameters are set --> the images can be read and stored
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->images = images;
series->labels = labels;
for (unsigned int i = 0; i < numberOfImages; i++) {
// allocating the actual matrix image for each image
images[i].buffer = malloc(width * height);
if (images[i].buffer == NULL) {
for (unsigned int k = 0; k < i; k++) {
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
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++;
}
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);
}
free(series->images);
free(series->labels);
free(series);
}
+115
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;
}
+36 -109
View File
@@ -1,47 +1,10 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
#include <stdbool.h>
// TODO Matrix-Funktionen implementieren
/*
Alte Funktion
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix m;
m.rows = rows;
m.cols = cols;
m.data = NULL;
if(rows == 0 || cols == 0){
m.rows = m.cols = 0;
return m;
}
m.data = malloc(rows * sizeof *m.data);
if(!m.data){
m.rows = m.cols = 0;
return m;
}
for(unsigned int i = 0; i < rows; i++){
m.data[i] = malloc(cols * sizeof *m.data[i]);
if(!m.data[i]){
for(unsigned int j = 0; j < i; j++){
free(m.data[j]);
}
free(m.data);
m.data = NULL;
m.rows = m.cols = 0;
return m;
}
}
return m;
}
*/
Matrix createMatrix(size_t rows, size_t cols)
{
Matrix m;
@@ -109,90 +72,54 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
else return 0;
}
/*
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
//check if the matrices are able to be added (same size)
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
//size of the matrices should be the same, if the addition is supposed to happen
// Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
bool doBroadcast = false;
Matrix larger, smaller;
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
larger = matrix1;
smaller = matrix2;
}
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
{
larger = matrix1;
smaller = matrix2;
doBroadcast = true;
}
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
{
larger = matrix2;
smaller = matrix1;
doBroadcast = true;
}
else{
Matrix m = {NULL, 0, 0};
return m;
}
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
if(doBroadcast){
for(int i = 0; i < outputMatrix.rows; i++){
MatrixType broadcastValue = smaller.buffer[i];
for(int j = 0; j < outputMatrix.cols; j++){
outputMatrix.buffer[i * outputMatrix.cols + j] = larger.buffer[i * larger.cols + j] + broadcastValue;
}
}
} else{
for (int i = 0; i < matrix1.rows;i++) {
for (int j = 0; j < matrix1.cols; j++) {
// how this should work in normal Matrix version:
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
outputMatrix.buffer[i + outputMatrix.rows* j] = matrix1.buffer[i + matrix1.rows* j] + matrix2.buffer[i + matrix2.rows * j];
outputMatrix.buffer[i * outputMatrix.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
return outputMatrix;
} else {
//the matrix could not be added, since the matrix sizes are not set correct.
Matrix m;
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
}
*/
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
//check if the matrices are able to be added (same size)
if (matrix1.cols != matrix2.cols && matrix1.rows != matrix2.rows){
Matrix m = {NULL, 0, 0};
return m;
}
//size of the matrices should be the same, if the addition is supposed to happen
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
if(!outputMatrix.buffer){
Matrix m = {NULL, 0, 0};
return m;
}
for (int i = 0; i < matrix1.rows;i++) {
for (int j = 0; j < matrix1.cols; j++) {
// how this should work in normal Matrix version:
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
outputMatrix.buffer[i * outputMatrix.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
return outputMatrix;
}
/*
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
//check, if the matrices can be multiplied
if (matrix1.rows == matrix2.cols) {
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
//Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
for(int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix2.cols; j++) {
for (int k = 0; k < matrix2.rows; k++) {
// how this should work in normal Matrix version:
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
outputMatrix.buffer[i + outputMatrix.rows * j] += matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
}
}
}
return outputMatrix;
} else {
//the matrix could not be added, since the matrix sizes are not set correct.
Matrix m;
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
}
*/
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
+1 -1
View File
@@ -164,7 +164,7 @@ NeuralNetwork loadModel(const char *path)
assignActivations(model);
}
printf("%d\n", model.numberOfLayers);
return model;
}
+1
View File
@@ -0,0 +1 @@
make clean && make && make neuralNetworkTests
+34 -2
View File
@@ -8,9 +8,41 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{
// TODO
}
FILE *file = fopen(path, "wb");
if(file != NULL){
const char *fileTag = "__info2_neural_network_file_format__";
// Write file header
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
// 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
for(int i = 0; i < nn.numberOfLayers; i++){
// Write output dimension (rows of weights)
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
// Write weight matrix 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
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
int zero = 0;
fwrite(&zero, sizeof(int), 1, file);
fclose(file);
}
}
void test_loadModelReturnsCorrectNumberOfLayers(void)
{
const char *path = "some__nn_test_file.info2";