Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f90b74937 |
218
imageInput.c
218
imageInput.c
@ -6,223 +6,17 @@
|
||||
#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
|
||||
|
||||
|
||||
//HilfsFunktionen:
|
||||
|
||||
|
||||
/* Opens the file in binary mode.
|
||||
* path: file path to open (must not be NULL).
|
||||
* returns FILE* or NULL on failure.
|
||||
*/
|
||||
static FILE *openImageFile(const char *path)
|
||||
{
|
||||
if (path == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fopen(path, "rb");
|
||||
}
|
||||
|
||||
/* Reads and checks the file header.
|
||||
* file: opened file to read the header from.
|
||||
* returns 1 if header is valid, otherwise 0.
|
||||
*/
|
||||
static int readAndCheckHeader(FILE *file)
|
||||
{
|
||||
size_t headerLength = strlen(FILE_HEADER_STRING);
|
||||
char buffer[BUFFER_SIZE];
|
||||
|
||||
if (headerLength + 1 > BUFFER_SIZE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fread(buffer, 1, headerLength, file) != headerLength)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
buffer[headerLength] = '\0';
|
||||
|
||||
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1; /* Header ok */
|
||||
}
|
||||
|
||||
|
||||
/* Reads metadata: image count, width, and height.
|
||||
* file: file to read from; count/width/height are outputs.
|
||||
* returns 1 on success or 0 on invalid/zero metadata.
|
||||
*/
|
||||
static int readImageMetaData(FILE *file,
|
||||
unsigned short *count,
|
||||
unsigned short *width,
|
||||
unsigned short *height)
|
||||
{
|
||||
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (*count == 0 || *width == 0 || *height == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
//Hauptfunktion:
|
||||
|
||||
|
||||
/* Loads all images + labels from a .info2 file.
|
||||
* path: file path to load.
|
||||
* returns a new GrayScaleImageSeries or NULL on error.
|
||||
*/
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
// 1. Open the file
|
||||
FILE *file = openImageFile(path);
|
||||
if (file == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 2. Check the header
|
||||
if (!readAndCheckHeader(file))
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 3. Read image metadata
|
||||
unsigned short count = 0;
|
||||
unsigned short width = 0;
|
||||
unsigned short height = 0;
|
||||
|
||||
if (!readImageMetaData(file, &count, &width, &height))
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 4. Allocate memory for image series
|
||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||
if (series == NULL)
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->count = count;
|
||||
series->images = (GrayScaleImage *)calloc(count, sizeof(GrayScaleImage));
|
||||
series->labels = (unsigned char *)malloc(count * sizeof(unsigned char));
|
||||
|
||||
if (series->images == NULL || series->labels == NULL)
|
||||
{
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 5. Read the images and labels
|
||||
for (unsigned int i = 0; i < count; i++)
|
||||
{
|
||||
GrayScaleImage *image = &series->images[i];
|
||||
|
||||
image->width = (unsigned int)width;
|
||||
image->height = (unsigned int)height;
|
||||
|
||||
size_t numPixels = (size_t)width * (size_t)height;
|
||||
|
||||
image->buffer = (GrayScalePixelType *)malloc(numPixels * sizeof(GrayScalePixelType));
|
||||
if (image->buffer == NULL)
|
||||
{
|
||||
for (unsigned int j = 0; j < i; j++)
|
||||
{
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fread(image->buffer, sizeof(GrayScalePixelType), numPixels, file) != numPixels)
|
||||
{
|
||||
for (unsigned int j = 0; j <= i; j++)
|
||||
{
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1)
|
||||
{
|
||||
for (unsigned int j = 0; j <= i; j++)
|
||||
{
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
/* Frees the entire image series.
|
||||
* series: image series to free (NULL-safe).
|
||||
* returns nothing (void).
|
||||
*/
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (series == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (series->images != NULL)
|
||||
{
|
||||
for (unsigned int i = 0; i < series->count; i++)
|
||||
{
|
||||
free(series->images[i].buffer);
|
||||
series->images[i].buffer = NULL;
|
||||
}
|
||||
free(series->images);
|
||||
series->images = NULL;
|
||||
}
|
||||
|
||||
if (series->labels != NULL)
|
||||
{
|
||||
free(series->labels);
|
||||
series->labels = NULL;
|
||||
}
|
||||
|
||||
free(series);
|
||||
}
|
||||
}
|
||||
158
matrix.c
158
matrix.c
@ -2,176 +2,34 @@
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
//Erstellt eine mit Nullen gefuellte Matrix, wenn genug Speicherplatz vorhanden ist und ein out-of-bounds-error durch unpassende Spalten oder Zeilen verhindert werden kann.
|
||||
//Gibt die erstellte Matrix als Matrix-Objekt zurueck, also als eine Kopie der erstellten Matrix.
|
||||
//@Param: rows gibt die Anzahl der Zeilen der neuen Matrix an, cols gibt die Anzahl der Spalten an
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
if(rows && cols){
|
||||
Matrix matrix;
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||
|
||||
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||
if (!matrix.buffer) {
|
||||
matrix.rows = 0;
|
||||
matrix.cols = 0;
|
||||
return matrix; // malloc ist fehlgeschlagen
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < (rows * cols); i++)
|
||||
matrix.buffer[i] = 0;
|
||||
|
||||
return matrix;
|
||||
}
|
||||
Matrix matrix;
|
||||
matrix.rows = 0;
|
||||
matrix.cols = 0;
|
||||
matrix.buffer = 0;
|
||||
return matrix;
|
||||
|
||||
}
|
||||
|
||||
//Loescht die uebergebene Matrix vollstaendig aus dem Speicher.
|
||||
//@Param: *matrix Gibt den Zeiger auf die zu loeschende Matrix wieder. Dadurch wird nicht nur eine Kopie geloescht, sondern tatsaechlich die Matrix.
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
|
||||
}
|
||||
|
||||
//Setzt den Wert einer Matrix an einer bestimmten Stelle im Array.
|
||||
//@Param: value gibt den einzufuegenden Wert an, matrix die Matrix, deren Wert geandert oder gesetzt werden soll,
|
||||
// rowIdx die Anzahl der Zeilen, die im Speicher uebersprungen werden soll, um in die gewunschte Zeile zu kommen,
|
||||
// collIdx die Nummer der Spalte, die geandert werden soll
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||
|
||||
}
|
||||
|
||||
//Gibt den Wert einer Matrix an einer bestimmten Stelle im Array zurueck.
|
||||
//@Param: value gibt den einzufuegenden Wert an, matrix die Matrix, deren Wert geandert oder gesetzt werden soll,
|
||||
//rowIdx die Anzahl der Zeilen, die im Speicher uebersprungen werden soll, um in die gewunschte Zeile zu kommen,
|
||||
// collIdx die Nummer der Spalte, die gelesen werden soll
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
|
||||
}
|
||||
|
||||
//Addiert zwei Matrizen miteinander oder broadcastet die eine Matrix mit einem Vektor.
|
||||
//Ueberpueft, ob broadcasting moeglich ist. Wenn nicht, werden die Matrizen addiert. Prueft zusaetzlich, ob Matrizenaddition moeglich ist.
|
||||
//Ist Broadcasting moeglich, wird die broadcasting-Methode aufgerufen.
|
||||
//Gibt addierte Matrix zurueck, nachdem diese in der Funktion erstellt wurde.
|
||||
//@param: matrix1 und matrix2 sind die Matrizen, deren Spalten und Zeilen ueberprueft werden und von denen moeglicherweise einer ein Vektor ist fuer das Broadcasting.
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
int rows1 = rows(matrix1);
|
||||
int rows2 = rows(matrix2);
|
||||
int cols1 = cols(matrix1);
|
||||
int cols2 = cols(matrix2);
|
||||
if((cols1 == 1 || cols2 == 1) && rows1 == rows2) //Broadcasting
|
||||
{
|
||||
if(cols1 == 1) //Wenn die erste Matrix der Vektor ist
|
||||
{
|
||||
return broadcasting(matrix1, matrix2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return broadcasting(matrix2, matrix1);
|
||||
}
|
||||
}
|
||||
else if(rows1 == rows2 && cols1 == cols2) //Addition nur moeglich, wenn beide Matrizen gleiche ANzahl an Zeilen und Spalten haben
|
||||
{
|
||||
Matrix addition = createMatrix(rows1, cols2); //Matrix erstellt, in die die addierten Werte geschrieben werden
|
||||
MatrixType wert1;
|
||||
MatrixType wert2;
|
||||
for(int i = 0; i < rows1; i++){ //soll fuer jedes Element durchlaufen, sodass alle Werte von beiden Matrizen aufaddiert werden.
|
||||
for(int j = 0; j < cols1; j++){
|
||||
wert1 = getMatrixAt(matrix1, i, j);
|
||||
wert2 = getMatrixAt(matrix2, i, j);
|
||||
MatrixType addierterWert = wert1 + wert2;
|
||||
setMatrixAt(addierterWert, addition, i, j);
|
||||
}
|
||||
}
|
||||
return addition;
|
||||
}
|
||||
else{
|
||||
return createMatrix(0,0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Gibt die Matrix zurueck, die nach dem Broadcasting entstanden ist.
|
||||
//Addiert einen Vektor zu einer Matrix, sodass jede Zelle der Matrix mit der Koordinate des Vektors addiert wird.
|
||||
//@param: vektor ist die einspaltige Matrix, deren Koordinaten auf die Matrix addiert werden, matrix ist die Matrix, zu der der Vektor addiert wird.
|
||||
Matrix broadcasting(const Matrix vektor, const Matrix matrix)
|
||||
{
|
||||
int rowsM = rows(matrix);
|
||||
int colsM = cols(matrix);
|
||||
Matrix result = createMatrix(rowsM, colsM);
|
||||
MatrixType wert;
|
||||
MatrixType koordinate;
|
||||
for(int i = 0; i < rowsM; i++)
|
||||
{
|
||||
for(int j = 0; j < colsM; j++){
|
||||
wert = getMatrixAt(matrix, i, j);
|
||||
koordinate = getMatrixAt(vektor, i, 0);
|
||||
setMatrixAt((koordinate + wert), result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//Prueft, ob Multiplikation zweier Matrizen moeglich ist und gibt fuer den Fall, dass es moeglich ist, eine neue Matrix mit den Ergebnissen zurueck.
|
||||
//@param: matrix1 und matrix2 geben die Matrizen an, die miteinander multipliziert werden sollen
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
int rows1 = rows(matrix1);
|
||||
int cols1 = cols(matrix1);
|
||||
int rows2 = rows(matrix2);
|
||||
int cols2 = cols(matrix2);
|
||||
if(cols1 == rows2) //Bedingung fuer Multiplikation: Spalten der ersten Matrix gleich Zeilen 2. Matrix
|
||||
{
|
||||
Matrix result = createMatrix(rows1, cols2);
|
||||
MatrixType wert1;
|
||||
MatrixType wert2;
|
||||
MatrixType mul;
|
||||
MatrixType add = 0;
|
||||
for(int i = 0; i < rows1; i++)
|
||||
{
|
||||
for(int k = 0; k < cols2; k++)
|
||||
{
|
||||
for(int j = 0; j < cols1; j++)
|
||||
{
|
||||
wert1 = getMatrixAt(matrix1, i, j);
|
||||
wert2 = getMatrixAt(matrix2, j, k);
|
||||
mul = wert1 * wert2;
|
||||
add += mul;
|
||||
}
|
||||
setMatrixAt(add, result, i, k);
|
||||
add = 0;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return createMatrix(0,0);
|
||||
}
|
||||
|
||||
//Gibt die Strukturvariable rows einer Matrix zurueck
|
||||
//@param: matrix gibt an, von welcher Matrix die Strukturvariable uebergeben werden soll
|
||||
int rows(const Matrix matrix)
|
||||
{
|
||||
return matrix.rows;
|
||||
}
|
||||
|
||||
//Gibt die Strukturvariable cols einer Matrix zurueck
|
||||
//@param: matrix gibt an, von welcher Matrix die Strukturvariable uebergeben werden soll
|
||||
int cols(const Matrix matrix)
|
||||
{
|
||||
return matrix.cols;
|
||||
|
||||
}
|
||||
8
matrix.h
8
matrix.h
@ -6,11 +6,6 @@
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
typedef struct{
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType *buffer; //Data wird in einem eindimensionalen Array gespeichert (Spalten und Reihen liegen ja im Speicher hintereinannder)
|
||||
} Matrix;
|
||||
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
@ -18,10 +13,7 @@ void clearMatrix(Matrix *matrix);
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||
Matrix broadcasting(const Matrix matrix1, const Matrix matrix2);
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
int rows(const Matrix matrix);
|
||||
int cols(const Matrix matrix);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
|
||||
|
||||
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
||||
{
|
||||
Matrix matrix = {0,0, NULL};
|
||||
Matrix matrix = {NULL, 0, 0};
|
||||
|
||||
if(count > 0 && images != NULL)
|
||||
{
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
#ifndef NEURALNETWORK_H
|
||||
#define NEURALNETWORK_H
|
||||
|
||||
#define FILE_HEADER_STRING "__info2_neural_network_file_format__"
|
||||
|
||||
#include "imageInput.h"
|
||||
#include "matrix.h"
|
||||
|
||||
|
||||
@ -6,44 +6,11 @@
|
||||
#include "neuralNetwork.h"
|
||||
|
||||
|
||||
//Testdatei schreiben
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||
{
|
||||
// Datei öffnen
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (file == NULL)
|
||||
return;
|
||||
|
||||
// Header schreiben
|
||||
const char *fileTag = "__info2_neural_network_file_format__";
|
||||
fwrite(fileTag, 1, strlen(fileTag), file);
|
||||
|
||||
// input Dimension schreiben
|
||||
int inputDim = nn.layers[0].weights.cols;
|
||||
fwrite(&inputDim, sizeof(int), 1, file);
|
||||
|
||||
// für weiter Layer nur output Dimension schreiben
|
||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||
{
|
||||
int outputDim = nn.layers[i].weights.rows;
|
||||
fwrite(&outputDim, sizeof(int), 1, file);
|
||||
|
||||
int weightCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||
|
||||
int biasesCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasesCount, file);
|
||||
// TODO
|
||||
}
|
||||
|
||||
// Ende: loadModel liest 0 ein
|
||||
int fileEnd = 0;
|
||||
fwrite(&fileEnd, sizeof(int), 1, file);
|
||||
|
||||
// Datei schließen
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
{
|
||||
const char *path = "some__nn_test_file.info2";
|
||||
|
||||
Binary file not shown.
@ -1,20 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.xcode.dsym.runImageInputTests</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>dSYM</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
||||
Binary file not shown.
@ -1,5 +0,0 @@
|
||||
---
|
||||
triple: 'arm64-apple-darwin'
|
||||
binary-path: runImageInputTests
|
||||
relocations: []
|
||||
...
|
||||
Loading…
x
Reference in New Issue
Block a user