Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
031341d455 | ||
|
|
f1556b18ba | ||
|
|
bc87e13966 |
No files matched your search
@@ -2,7 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "imageInput.h"
|
||||
//test
|
||||
|
||||
#define BUFFER_SIZE 100
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
|
||||
+68
-8
@@ -1,35 +1,95 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
|
||||
Matrix newmatrix = {.cols = 0, .rows = 0, .buffer = NULL};
|
||||
if(rows && cols){
|
||||
newmatrix.cols = cols;
|
||||
newmatrix.rows = rows;
|
||||
newmatrix.buffer = malloc(rows*cols*sizeof(MatrixType));
|
||||
}
|
||||
return newmatrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if(rowIdx < matrix.rows && colIdx < matrix.cols) *(matrix.buffer+rowIdx*matrix.cols+colIdx) = value;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if(rowIdx < matrix.rows && colIdx < matrix.cols) return *(matrix.buffer+rowIdx*matrix.cols+colIdx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix newmatrix = {.cols = 0, .rows = 0, .buffer = NULL};
|
||||
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
||||
newmatrix.cols = matrix1.cols;
|
||||
newmatrix.rows = matrix1.rows;
|
||||
newmatrix.buffer = malloc(matrix1.rows * matrix1.cols * sizeof(MatrixType));
|
||||
//element addition
|
||||
for(int i = 0; i < matrix1.rows; i++){
|
||||
for(int j = 0; j < matrix1.cols; j++){
|
||||
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols+j);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(matrix2.cols == 1 && (matrix2.rows == matrix1.rows)) { //CAREFUL: ONLY VERTICAL VECTORS ARE IMPLEMENTED, TRANSPOSED ARE NOT YET INCLUDED, CAN LEAD TO ISSUES
|
||||
printf("Got broadcast\n");
|
||||
newmatrix.cols = matrix1.cols;
|
||||
newmatrix.rows = matrix1.rows;
|
||||
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
|
||||
for(int i = 0; i < matrix1.rows; i++){
|
||||
for(int j = 0; j < matrix1.cols; j++){
|
||||
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(matrix1.cols == 1 && (matrix2.rows == matrix1.rows)){
|
||||
printf("Got broadcast\n");
|
||||
newmatrix.cols = matrix2.cols;
|
||||
newmatrix.rows = matrix2.rows;
|
||||
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
|
||||
for(int i = 0; i < matrix1.rows; i++){
|
||||
for(int j = 0; j < matrix2.cols; j++){
|
||||
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix2.buffer+i*matrix2.cols+j) + *(matrix1.buffer+i*matrix1.cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
return newmatrix;
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix newmatrix = {.buffer = NULL, .cols = 0, .rows = 0};
|
||||
if(matrix1.cols == matrix2.rows){
|
||||
newmatrix.buffer = malloc(matrix1.rows * matrix2.cols * sizeof(MatrixType));
|
||||
newmatrix.rows = matrix1.rows;
|
||||
newmatrix.cols = matrix2.cols;
|
||||
for(int i = 0; i < newmatrix.rows; i++){
|
||||
for(int j = 0; j < newmatrix.cols; j++){
|
||||
//Set matrix entry to 0
|
||||
*(newmatrix.buffer+i*newmatrix.cols+j) = 0;
|
||||
for(int k = 0; k < matrix1.cols; k++){
|
||||
//Increment matrix entry
|
||||
*(newmatrix.buffer+i*newmatrix.cols+j) += *(matrix1.buffer+i*matrix1.cols+k) * *(matrix2.buffer+k*matrix2.cols+j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return newmatrix;
|
||||
}
|
||||
+5
-2
@@ -5,8 +5,11 @@
|
||||
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct {
|
||||
u_int32_t rows;
|
||||
u_int32_t cols;
|
||||
MatrixType* buffer;
|
||||
} Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -9,59 +9,6 @@
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||
{
|
||||
// TODO
|
||||
|
||||
//Datei zum binaeren schreiben oeffnen
|
||||
FILE *file = fopen(path, "wb");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//Header schreiben, loadModel prüft nach String
|
||||
const char *header = "__info2_neural_network_file_format__";
|
||||
fwrite(header, sizeof(char), strlen(header), file);
|
||||
|
||||
//Prüfen ob Layer existieren
|
||||
if (nn.numberOfLayers > 0)
|
||||
{
|
||||
//Format entspricht anzahl der cols der Matrix des 1ten Layers
|
||||
int inputDimension = nn.layers[0].weights.cols;
|
||||
fwrite(&inputDimension, sizeof(int), 1, file);
|
||||
|
||||
//Layer nacheinander in Datei schreiben
|
||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||
{
|
||||
Layer layer = nn.layers[i];
|
||||
|
||||
//outputDimension entspricht Zeilenzahl seiner Matrix
|
||||
int outputDimension = layer.weights.rows;
|
||||
|
||||
//outputDim. speichern
|
||||
fwrite(&outputDimension, sizeof(int), 1, file);
|
||||
|
||||
//weight werte berechnen
|
||||
int weightCount = layer.weights.rows * layer.weights.cols;
|
||||
|
||||
//Matrix speichern
|
||||
fwrite(layer.weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||
|
||||
//bias berechnen
|
||||
int biasCount = layer.biases.rows * layer.biases.cols;
|
||||
|
||||
//bias speichern
|
||||
fwrite(layer.biases.buffer, sizeof(MatrixType), biasCount, file);
|
||||
}
|
||||
|
||||
//Abbruchkriterium für loadModel
|
||||
int ende= 0;
|
||||
|
||||
fwrite(&ende, sizeof(int), 1, file);
|
||||
}
|
||||
|
||||
//Datei schließen
|
||||
fclose(file);
|
||||
|
||||
}
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
|
||||
Reference in new issue
Block a user