forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
5 Commits
92ad1e1c31
...
adf75b66d2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
adf75b66d2 | ||
|
|
00ac1aa04a | ||
|
|
04768db522 | ||
|
|
01a13090a5 | ||
| 84b65525a6 |
5
.gitignore
vendored
5
.gitignore
vendored
@ -2,4 +2,7 @@ mnist
|
|||||||
runTests
|
runTests
|
||||||
*.o
|
*.o
|
||||||
*.exe
|
*.exe
|
||||||
runMatrixTests
|
runMatrixTests
|
||||||
|
runImageInputTests
|
||||||
|
runNeuralNetworkTests
|
||||||
|
.vscode
|
||||||
149
imageInput.c
149
imageInput.c
@ -6,17 +6,156 @@
|
|||||||
#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
|
typedef enum
|
||||||
|
{
|
||||||
|
IMG_SUCCESS = 0,
|
||||||
|
IMG_ERR_INVALID_HEADER, // Header did not match
|
||||||
|
IMG_ERR_READ, // Failed to read from file, maybe it's too short
|
||||||
|
IMG_ERR, // General Error
|
||||||
|
} ImageError;
|
||||||
|
|
||||||
|
static ImageError checkHeader(FILE *file);
|
||||||
|
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file);
|
||||||
|
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file);
|
||||||
|
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series);
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = calloc(1, sizeof(GrayScaleImageSeries));
|
||||||
|
if (series == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *file = fopen(path, "rb");
|
||||||
|
if (file == NULL)
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
series = NULL;
|
||||||
|
return NULL; // fopen failed
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parseImageFile(file, series))
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
series = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series)
|
||||||
|
{
|
||||||
|
if (checkHeader(file))
|
||||||
|
{
|
||||||
|
return IMG_ERR; // header check failed
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned short number, width, height;
|
||||||
|
if (readPictureParams(&number, &width, &height, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR; // read failed
|
||||||
|
}
|
||||||
|
size_t pixels = width * height;
|
||||||
|
|
||||||
|
// now setup the image series
|
||||||
|
series->images = calloc(number, sizeof(GrayScaleImage));
|
||||||
|
if (series->images == NULL)
|
||||||
|
{
|
||||||
|
return IMG_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->labels = malloc(number * sizeof(unsigned char));
|
||||||
|
if (series->labels == NULL)
|
||||||
|
{
|
||||||
|
return IMG_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->count = number;
|
||||||
|
for (size_t imageIdx = 0; imageIdx < number; imageIdx++)
|
||||||
|
{
|
||||||
|
GrayScaleImage *curImage = &series->images[imageIdx];
|
||||||
|
|
||||||
|
curImage->buffer = malloc(sizeof(GrayScalePixelType) * pixels);
|
||||||
|
if (curImage->buffer == NULL)
|
||||||
|
{
|
||||||
|
return IMG_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
curImage->width = width;
|
||||||
|
curImage->height = height;
|
||||||
|
|
||||||
|
if (readImage(pixels, curImage->buffer, &series->labels[imageIdx], file))
|
||||||
|
{
|
||||||
|
return IMG_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return IMG_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ImageError checkHeader(FILE *file)
|
||||||
|
{
|
||||||
|
size_t len = strlen(FILE_HEADER_STRING);
|
||||||
|
char headerBuf[len + 1];
|
||||||
|
size_t charsRead = fread(headerBuf, 1, len, file);
|
||||||
|
if (charsRead < len)
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
headerBuf[len] = '\0';
|
||||||
|
return strcmp(headerBuf, FILE_HEADER_STRING) == 0 ? IMG_SUCCESS : IMG_ERR_INVALID_HEADER;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file)
|
||||||
|
{
|
||||||
|
if (1 != fread(number, sizeof(unsigned short), 1, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1 != fread(width, sizeof(unsigned short), 1, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1 != fread(height, sizeof(unsigned short), 1, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
return IMG_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file)
|
||||||
|
{
|
||||||
|
if (numPixels > fread(pixelBuffer, sizeof(GrayScalePixelType), numPixels, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1 != fread(label, sizeof(unsigned char), 1, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
return IMG_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// frees memory for each image buffer, image, label and finally series
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
|
if (series)
|
||||||
|
{
|
||||||
|
|
||||||
|
int seriesLen = series->count;
|
||||||
|
for (size_t imageIdx = 0; imageIdx < seriesLen; imageIdx++)
|
||||||
|
{
|
||||||
|
free(series->images[imageIdx].buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
30
matrix.c
30
matrix.c
@ -109,7 +109,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
for (size_t n = 0; n < matrix1.cols; n++)
|
for (size_t n = 0; n < matrix1.cols; n++)
|
||||||
{
|
{
|
||||||
// this is unnecessarily complicated because at this point we already know that the matrices are compatible
|
|
||||||
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, n), resMat, m, n);
|
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, n), resMat, m, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,32 +116,37 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return resMat;
|
return resMat;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix A, const Matrix B)
|
||||||
{
|
{
|
||||||
if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL)
|
if (A.cols != B.rows || A.buffer == NULL || B.buffer == NULL)
|
||||||
{
|
{
|
||||||
return createMatrix(0, 0);
|
return createMatrix(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int rows = matrix1.rows, cols = matrix2.cols;
|
int rows = A.rows, cols = B.cols;
|
||||||
Matrix resMat = createMatrix(rows, cols);
|
Matrix C = createMatrix(rows, cols);
|
||||||
|
|
||||||
if (resMat.buffer == NULL)
|
if (C.buffer == NULL)
|
||||||
{
|
{
|
||||||
return createMatrix(0, 0);
|
return createMatrix(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t rowIdx = 0; rowIdx < rows; rowIdx++)
|
// M = Rows, K = Common Dim, N = Cols
|
||||||
|
size_t M = A.rows, K = A.cols, N = B.cols;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < M; i++)
|
||||||
{
|
{
|
||||||
for (size_t colIdx = 0; colIdx < cols; colIdx++)
|
for (size_t k = 0; k < K; k++)
|
||||||
|
|
||||||
{
|
{
|
||||||
int curCellVal = 0;
|
MatrixType valA = A.buffer[i * K + k];
|
||||||
for (size_t k = 0; k < matrix1.cols; k++)
|
for (size_t j = 0; j < N; j++)
|
||||||
{
|
{
|
||||||
curCellVal += getMatrixAt(matrix1, rowIdx, k) * getMatrixAt(matrix2, k, colIdx);
|
// C[i, j] += A[i, k] * B[k, j];
|
||||||
|
// M x N, M x K, K x N
|
||||||
|
C.buffer[i * N + j] += valA * B.buffer[k * N + j];
|
||||||
}
|
}
|
||||||
setMatrixAt(curCellVal, resMat, rowIdx, colIdx);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resMat;
|
return C;
|
||||||
}
|
}
|
||||||
@ -6,9 +6,30 @@
|
|||||||
#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
|
|
||||||
|
static void erzeugeMatrix(FILE *file, const Matrix *m)
|
||||||
|
{
|
||||||
|
fwrite(&m->rows, sizeof(int), 1, file);
|
||||||
|
fwrite(&m->cols, sizeof(int), 1, file);
|
||||||
|
fwrite(m->buffer, sizeof(MatrixType), m->rows * m->cols, file);
|
||||||
|
}
|
||||||
|
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO
|
FILE *file = fopen(path, "wb");
|
||||||
|
if (!file)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const char *header = "__info2_neural_network_file_format__";
|
||||||
|
fwrite(header, sizeof(char), strlen(header), file);
|
||||||
|
fwrite(&nn.numberOfLayers, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
for (int i = 0; i < nn.numberOfLayers; i++)
|
||||||
|
{
|
||||||
|
erzeugeMatrix(file, &nn.layers[i].weights);
|
||||||
|
erzeugeMatrix(file, &nn.layers[i].biases);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user