forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d12ecee4b | ||
|
|
adf75b66d2 | ||
|
|
00ac1aa04a | ||
|
|
04768db522 | ||
|
|
01a13090a5 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,3 +3,6 @@ runTests
|
|||||||
*.o
|
*.o
|
||||||
*.exe
|
*.exe
|
||||||
runMatrixTests
|
runMatrixTests
|
||||||
|
runImageInputTests
|
||||||
|
runNeuralNetworkTests
|
||||||
|
.vscode
|
||||||
222
imageInput.c
222
imageInput.c
@ -6,107 +6,165 @@
|
|||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
//Datei öffnen, Header, Anzahl, Höhe und Breite lesen, geöffnete Datei zurückgeben
|
typedef enum
|
||||||
static FILE* openAndReadShort (const char *path, unsigned short *count, unsigned short *width, unsigned short *height) {
|
{
|
||||||
FILE *file = fopen(path, "rb");
|
IMG_SUCCESS = 0,
|
||||||
if (!file) {
|
IMG_ERR_INVALID_HEADER, // Header did not match
|
||||||
return NULL;
|
IMG_ERR_READ, // Failed to read from file, maybe it's too short
|
||||||
}
|
IMG_ERR, // General Error
|
||||||
size_t headerLength = strlen(FILE_HEADER_STRING);
|
} ImageError;
|
||||||
char *header = malloc (headerLength + 1);
|
|
||||||
if(!header) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fread(header, sizeof(char), headerLength, file) != headerLength) {
|
static ImageError checkHeader(FILE *file);
|
||||||
free (header);
|
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file);
|
||||||
return NULL;
|
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file);
|
||||||
}
|
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series);
|
||||||
|
|
||||||
header[headerLength] = '\0';
|
|
||||||
|
|
||||||
if (strcmp (header, FILE_HEADER_STRING) != 0) {
|
|
||||||
free(header);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
free (header);
|
|
||||||
|
|
||||||
fread(count, sizeof(unsigned short), 1, file);
|
|
||||||
fread(width, sizeof(unsigned short), 1, file);
|
|
||||||
fread(height, sizeof(unsigned short), 1, file);
|
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Speicher anlegen und Pixel eines Bildes einlesen
|
|
||||||
static GrayScaleImage* readPixles (FILE *file, unsigned short *width, unsigned short *height) {
|
|
||||||
GrayScaleImage *image = malloc (sizeof(GrayScaleImage));
|
|
||||||
image->width = *width;
|
|
||||||
image->height = *height;
|
|
||||||
image->buffer = malloc ((*width) * (*height) * sizeof(GrayScalePixelType));
|
|
||||||
if (!image->buffer) {
|
|
||||||
free(image);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < (*width) * (*height); i++) {
|
|
||||||
unsigned char pixel;
|
|
||||||
if (fread(&pixel, sizeof(unsigned char), 1, file) != 1) {
|
|
||||||
free(image->buffer);
|
|
||||||
free(image);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
image->buffer[i] = pixel;
|
|
||||||
}
|
|
||||||
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Ausführen von openAndReadShort, Anlegen des Speichers für Bilderserie, readPixles wird für jedes Bild ausgeführt
|
|
||||||
//Nach jedem Bild wird das zugehörige Label gelesen, bei sämtlichen Fehlern wird NULL zurückgegeben und Speicher durch clearSeries bereinigt
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
unsigned short count = 0, width = 0, height = 0;
|
// It's very important to call calloc here because otherwise clearSeries() might try to free random memory
|
||||||
|
GrayScaleImageSeries *series = calloc(1, sizeof(GrayScaleImageSeries));
|
||||||
FILE *file = openAndReadShort(path, &count, &width, &height);
|
if (series == NULL)
|
||||||
if (file == 0) {
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
|
||||||
if (!series) {
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
series->count = count;
|
|
||||||
|
|
||||||
series->images = malloc(count * sizeof(GrayScaleImage));
|
FILE *file = fopen(path, "rb");
|
||||||
series->labels = malloc(count* sizeof(unsigned char));
|
// If fopen() failed
|
||||||
|
if (file == NULL)
|
||||||
for (unsigned int i = 0; i < series->count; i++) {
|
{
|
||||||
GrayScaleImage *image = readPixles(file, &width, &height);
|
|
||||||
series->images[i] = *image;
|
|
||||||
free(image);
|
|
||||||
|
|
||||||
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
|
|
||||||
clearSeries(series);
|
clearSeries(series);
|
||||||
fclose(file);
|
series = NULL;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
|
// Try to parse the whole file. If anything fails memory is freed
|
||||||
|
if (parseImageFile(file, series))
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
series = NULL; // Return NULL after the file is properly closed
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Bereinigt den Speicher
|
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series)
|
||||||
|
{
|
||||||
|
if (checkHeader(file))
|
||||||
|
{
|
||||||
|
return IMG_ERR; // header check failed
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned short imgCNT, width, height;
|
||||||
|
if (readPictureParams(&imgCNT, &width, &height, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR; // read failed
|
||||||
|
}
|
||||||
|
size_t pixels = width * height;
|
||||||
|
|
||||||
|
// The images contain more pointers, definitely use calloc here
|
||||||
|
series->images = calloc(imgCNT, sizeof(GrayScaleImage));
|
||||||
|
if (series->images == NULL)
|
||||||
|
{
|
||||||
|
return IMG_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->labels = malloc(imgCNT * sizeof(unsigned char));
|
||||||
|
if (series->labels == NULL)
|
||||||
|
{
|
||||||
|
return IMG_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->count = imgCNT;
|
||||||
|
// Read every image and it's label
|
||||||
|
for (size_t imageIdx = 0; imageIdx < imgCNT; 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);
|
||||||
|
// Check if the file is to short
|
||||||
|
if (charsRead < len)
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
headerBuf[len] = '\0'; // Terminate string
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
// number of images
|
||||||
|
if (1 != fread(number, sizeof(unsigned short), 1, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// image width
|
||||||
|
if (1 != fread(width, sizeof(unsigned short), 1, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// height
|
||||||
|
if (1 != fread(height, sizeof(unsigned short), 1, file))
|
||||||
|
{
|
||||||
|
return IMG_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
return IMG_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read 1 image and it's label
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < series->count; i++) {
|
if (series)
|
||||||
free(series->images[i].buffer);
|
{
|
||||||
|
|
||||||
|
int seriesLen = series->count;
|
||||||
|
for (size_t imageIdx = 0; imageIdx < seriesLen; imageIdx++)
|
||||||
|
{
|
||||||
|
free(series->images[imageIdx].buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(series->images);
|
free(series->images);
|
||||||
free(series->labels);
|
free(series->labels);
|
||||||
free(series);
|
free(series);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
44
matrix.c
44
matrix.c
@ -67,18 +67,14 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return createMatrix(0, 0);
|
return createMatrix(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// matrices not compatible
|
if (matrix1.cols != matrix2.cols)
|
||||||
|
{
|
||||||
if (matrix1.rows != matrix2.rows)
|
if (matrix1.rows != matrix2.rows)
|
||||||
{
|
{
|
||||||
clearMatrix(&resMat);
|
clearMatrix(&resMat);
|
||||||
return resMat;
|
return resMat;
|
||||||
}
|
}
|
||||||
|
else if (matrix1.cols == 1)
|
||||||
// check if broadcasting is possible
|
|
||||||
if (matrix1.cols != matrix2.cols)
|
|
||||||
{
|
|
||||||
// matrix1 is a vector
|
|
||||||
if (matrix1.cols == 1)
|
|
||||||
{
|
{
|
||||||
// broadcast vector
|
// broadcast vector
|
||||||
for (size_t m = 0; m < matrix2.rows; m++)
|
for (size_t m = 0; m < matrix2.rows; m++)
|
||||||
@ -90,7 +86,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
}
|
}
|
||||||
return resMat;
|
return resMat;
|
||||||
}
|
}
|
||||||
// matrix2 is a vector
|
|
||||||
else if (matrix2.cols == 1)
|
else if (matrix2.cols == 1)
|
||||||
{
|
{
|
||||||
// broadcast vector
|
// broadcast vector
|
||||||
@ -103,7 +98,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
}
|
}
|
||||||
return resMat;
|
return resMat;
|
||||||
}
|
}
|
||||||
// addition not possible
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
clearMatrix(&resMat);
|
clearMatrix(&resMat);
|
||||||
@ -115,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,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
|
||||||
setMatrixAt(curCellVal, resMat, rowIdx, colIdx);
|
C.buffer[i * N + j] += valA * B.buffer[k * N + j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resMat;
|
}
|
||||||
|
return C;
|
||||||
}
|
}
|
||||||
3
matrix.h
3
matrix.h
@ -8,9 +8,10 @@ typedef float MatrixType;
|
|||||||
// Matrixtyp
|
// Matrixtyp
|
||||||
typedef struct Matrix
|
typedef struct Matrix
|
||||||
{
|
{
|
||||||
MatrixType *buffer;
|
|
||||||
size_t rows;
|
size_t rows;
|
||||||
size_t cols;
|
size_t cols;
|
||||||
|
MatrixType *buffer;
|
||||||
|
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
@ -5,44 +5,29 @@
|
|||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
static void writeLayer(FILE *file, const Matrix weights, const Matrix biases, unsigned int inputDim)
|
|
||||||
|
static void erzeugeMatrix(FILE *file, const Matrix *m)
|
||||||
{
|
{
|
||||||
unsigned int outputDim = (unsigned int)weights.rows;
|
fwrite(&m->rows, sizeof(int), 1, file);
|
||||||
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
fwrite(&m->cols, sizeof(int), 1, file);
|
||||||
if (weights.buffer != NULL)
|
fwrite(m->buffer, sizeof(MatrixType), m->rows * m->cols, file);
|
||||||
fwrite(weights.buffer, sizeof(MatrixType), outputDim * inputDim, file);
|
|
||||||
|
|
||||||
if (biases.buffer != NULL)
|
|
||||||
fwrite(biases.buffer, sizeof(MatrixType), outputDim, file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
FILE *file = fopen(path, "wb");
|
FILE *file = fopen(path, "wb");
|
||||||
if (!file) return;
|
if (!file)
|
||||||
|
|
||||||
const char tag[] = "__info2_neural_network_file_format__";
|
|
||||||
fwrite(tag, sizeof(char), strlen(tag), file);
|
|
||||||
|
|
||||||
if (nn.numberOfLayers == 0)
|
|
||||||
{
|
|
||||||
unsigned int zero = 0;
|
|
||||||
fwrite(&zero, sizeof(unsigned int), 1, file);
|
|
||||||
fclose(file);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int inputDim = (unsigned int)nn.layers[0].weights.cols;
|
const char *header = "__info2_neural_network_file_format__";
|
||||||
fwrite(&inputDim, sizeof(unsigned int), 1, file);
|
fwrite(header, sizeof(char), strlen(header), file);
|
||||||
|
fwrite(&nn.numberOfLayers, sizeof(int), 1, file);
|
||||||
|
|
||||||
for (int i = 0; i < nn.numberOfLayers; i++)
|
for (int i = 0; i < nn.numberOfLayers; i++)
|
||||||
{
|
{
|
||||||
writeLayer(file, nn.layers[i].weights, nn.layers[i].biases, inputDim);
|
erzeugeMatrix(file, &nn.layers[i].weights);
|
||||||
inputDim = (unsigned int)nn.layers[i].weights.rows;
|
erzeugeMatrix(file, &nn.layers[i].biases);
|
||||||
}
|
}
|
||||||
unsigned int zero = 0;
|
|
||||||
fwrite(&zero, sizeof(unsigned int), 1, file);
|
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
some_tag
|
|
||||||
Loading…
x
Reference in New Issue
Block a user