generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a4ff89df7 | ||
|
|
7d30feef54 | ||
|
|
4e2dd7b4d4 | ||
|
|
22b79b5cd3 | ||
|
|
1f1a7a23b1 | ||
|
|
0c85fb19bc | ||
|
|
e0ded7b738 | ||
|
|
880514b55f | ||
|
|
419296af9e | ||
|
|
4d1908ed27 | ||
|
|
ce371e4228 | ||
|
|
98aa5f354a | ||
|
|
15d74d972d | ||
|
|
5ce0982e17 | ||
|
|
cfac9ae60e | ||
|
|
e7373fe73a | ||
|
|
9bd18d3681 | ||
|
|
04d9b35c36 | ||
|
|
57bf46bfc5 | ||
|
|
2783663ab9 | ||
|
|
a2f2be5592 | ||
|
|
82c335ca89 | ||
|
|
f8456a7d5e | ||
|
|
05a29b50c6 | ||
|
|
8ef0d7688f | ||
|
|
32cf681534 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,7 @@
|
||||
mnist
|
||||
runTests
|
||||
runMatrixTests
|
||||
runImageInputTests
|
||||
runNeuralNetworkTests
|
||||
*.o
|
||||
*.exe
|
||||
109
imageInput.c
109
imageInput.c
@ -1,4 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "imageInput.h"
|
||||
@ -6,17 +7,115 @@
|
||||
#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
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
/// @brief Gets the next char value from specified and opened file in the moment that is only ONE short int.
|
||||
/// @param openedFile stream FILE, from which to read
|
||||
static int getCharValueFromFile(FILE* openedFile) {
|
||||
int addToFile = 0;
|
||||
|
||||
if (openedFile == NULL) return 0;
|
||||
|
||||
addToFile = fgetc(openedFile);
|
||||
addToFile += fgetc(openedFile) * 256;
|
||||
return addToFile;
|
||||
}
|
||||
|
||||
GrayScaleImageSeries* readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
// uint16_t
|
||||
FILE* openFile;
|
||||
|
||||
openFile = fopen(path, "rb");
|
||||
|
||||
// file Could not be opened/does not exist
|
||||
if (openFile != NULL) {
|
||||
|
||||
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING)+1 * sizeof(char));
|
||||
int numberOfImages = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
|
||||
char tmpFileChar = fgetc(openFile);
|
||||
|
||||
if (tmpFileChar == EOF) {
|
||||
fclose(openFile);
|
||||
return NULL;
|
||||
}
|
||||
actualFileTag[i] = tmpFileChar;
|
||||
}
|
||||
actualFileTag[strlen(FILE_HEADER_STRING)] = '\0';
|
||||
|
||||
// checks if the files are equal
|
||||
int fileTagEqual = strcmp(actualFileTag, FILE_HEADER_STRING);
|
||||
|
||||
//we only need the fileTag to verify its an image for our series.
|
||||
free(actualFileTag);
|
||||
actualFileTag = NULL;
|
||||
|
||||
|
||||
//only if equal, the method continues
|
||||
if (fileTagEqual == 0) {
|
||||
numberOfImages = getCharValueFromFile(openFile);
|
||||
|
||||
// no Images in series -> No image-series
|
||||
if (numberOfImages == 0) {
|
||||
fclose(openFile);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
width = getCharValueFromFile(openFile);
|
||||
height = getCharValueFromFile(openFile);
|
||||
|
||||
// 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
|
||||
series = malloc(sizeof(GrayScaleImageSeries));
|
||||
|
||||
series->count = 0;
|
||||
series->images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
||||
series->labels = malloc(numberOfImages * sizeof(unsigned char));
|
||||
|
||||
for (int i = 0; i < numberOfImages; i++) {
|
||||
series->images[i].height = height;
|
||||
series->images[i].width = width;
|
||||
series->images[i].buffer = malloc(width * height);
|
||||
|
||||
for (int j = 0; j < (width * height); j++) {
|
||||
// allocating the actual matrix image for image
|
||||
series->images[i].buffer[j] = fgetc(openFile);
|
||||
}
|
||||
//rest of the values that only affect the image itself
|
||||
series->labels[i] = fgetc(openFile);
|
||||
series->count++;
|
||||
}
|
||||
}
|
||||
|
||||
// the fclose happens here, since every other path closes the file beforehand
|
||||
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);
|
||||
series->images[i].buffer = NULL;
|
||||
}
|
||||
|
||||
free(series->images);
|
||||
series->images = NULL;
|
||||
free(series->labels);
|
||||
series->labels = NULL;
|
||||
free(series);
|
||||
series = NULL;
|
||||
}
|
||||
165
matrix.c
165
matrix.c
@ -2,46 +2,6 @@
|
||||
#include <string.h>
|
||||
#include "matrix.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;
|
||||
@ -54,7 +14,7 @@ Matrix createMatrix(size_t rows, size_t cols)
|
||||
return m;
|
||||
}
|
||||
|
||||
// Single allocation for entire matrix
|
||||
//Allocate Matrix buffer
|
||||
m.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||
|
||||
if(!m.buffer){
|
||||
@ -62,7 +22,7 @@ Matrix createMatrix(size_t rows, size_t cols)
|
||||
return m;
|
||||
}
|
||||
|
||||
// Initialize (optional)
|
||||
//Initialize matrix with default value
|
||||
for(unsigned int i = 0; i < rows * cols; i++){
|
||||
m.buffer[i] = UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
@ -70,77 +30,120 @@ Matrix createMatrix(size_t rows, size_t cols)
|
||||
return m;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
|
||||
void clearMatrix(Matrix* matrix)
|
||||
{
|
||||
for (int i = 0; i < matrix->rows; i++) {
|
||||
for (int j = 0; j < matrix->cols;j++) {
|
||||
matrix->buffer[i-1 + matrix->rows*(j-1)] = UNDEFINED_MATRIX_VALUE;
|
||||
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
|
||||
// matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE;
|
||||
matrix->buffer[j + matrix->cols * i] = UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
}
|
||||
free(matrix->buffer);
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
|
||||
matrix->buffer = NULL;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)] = value;
|
||||
//checks, if the given incies are allowed
|
||||
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
|
||||
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
|
||||
// matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value;
|
||||
matrix.buffer[colIdx + matrix.cols * rowIdx] = value;
|
||||
}
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
MatrixType returnVal;
|
||||
returnVal = matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)];
|
||||
return returnVal;
|
||||
//checks, if the given indices are allowed
|
||||
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
|
||||
MatrixType returnVal;
|
||||
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
|
||||
// returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)];
|
||||
returnVal = matrix.buffer[colIdx + rowIdx * matrix.cols];
|
||||
return returnVal;
|
||||
}
|
||||
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);
|
||||
|
||||
unsigned int doBroadcast = 0;
|
||||
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 = 1;
|
||||
}
|
||||
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
|
||||
{
|
||||
larger = matrix2;
|
||||
smaller = matrix1;
|
||||
doBroadcast = 1;
|
||||
}
|
||||
else{
|
||||
Matrix m = {NULL, 0, 0};
|
||||
return m;
|
||||
}
|
||||
|
||||
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
|
||||
if(doBroadcast){
|
||||
//Broadcasting
|
||||
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{
|
||||
//Classic execution
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
if(matrix1.cols != matrix2.rows){
|
||||
Matrix m = {NULL, 0, 0};
|
||||
return m;
|
||||
}
|
||||
|
||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||
if(!outputMatrix.buffer){
|
||||
Matrix m = {NULL, 0, 0};
|
||||
return m;
|
||||
}
|
||||
|
||||
//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 < matrix1.cols; 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];
|
||||
outputMatrix.buffer[i * outputMatrix.cols + j] += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[j + matrix2.cols * k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return outputMatrix;
|
||||
}
|
||||
2
matrix.h
2
matrix.h
@ -7,9 +7,9 @@ typedef float MatrixType;
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct Matrix {
|
||||
MatrixType *buffer;
|
||||
size_t rows; //X-Element
|
||||
size_t cols; //Y-Element
|
||||
MatrixType *buffer;
|
||||
} Matrix;
|
||||
|
||||
|
||||
|
||||
1
matrixTests.sh
Normal file
1
matrixTests.sh
Normal file
@ -0,0 +1 @@
|
||||
make clean && make matrixTests && ./runMatrixTests
|
||||
@ -164,7 +164,6 @@ NeuralNetwork loadModel(const char *path)
|
||||
|
||||
assignActivations(model);
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
1
neuralNetwork.sh
Normal file
1
neuralNetwork.sh
Normal file
@ -0,0 +1 @@
|
||||
make clean && make && make neuralNetworkTests
|
||||
@ -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 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 each layer into file
|
||||
for(int i = 0; i < nn.numberOfLayers; i++){
|
||||
|
||||
//Write output dimension
|
||||
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
|
||||
|
||||
//Write weight 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 data
|
||||
int biasSize = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasSize, file);
|
||||
}
|
||||
|
||||
//EOF Terminator
|
||||
int zero = 0;
|
||||
fwrite(&zero, sizeof(int), 1, file);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
{
|
||||
const char *path = "some__nn_test_file.info2";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user