forked from freudenreichan/info2Praktikum-NeuronalesNetz
Implement prepareNeuralNetworkFile
This commit is contained in:
3 files changed
+63
-8
No files matched your search
@@ -168,6 +168,59 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void writeFileHeader(FILE *file)
|
||||||
|
{
|
||||||
|
fwrite(FILE_HEADER_STRING, sizeof(char), strlen(FILE_HEADER_STRING), file);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void writeDimension(FILE *file, const unsigned int dimension)
|
||||||
|
{
|
||||||
|
fwrite(&dimension, sizeof(unsigned int), 1, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void writeMatrix(FILE *file, const Matrix matrix)
|
||||||
|
{
|
||||||
|
fwrite(matrix.buffer, sizeof(MatrixType), matrix.rows * matrix.cols, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void writeLayer(FILE *file, const Layer layer)
|
||||||
|
{
|
||||||
|
writeMatrix(file, layer.weights);
|
||||||
|
writeMatrix(file, layer.biases);
|
||||||
|
}
|
||||||
|
|
||||||
|
void saveModel(const char *path, const NeuralNetwork nn)
|
||||||
|
{
|
||||||
|
FILE *file = fopen(path, "wb");
|
||||||
|
if(file == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeFileHeader(file);
|
||||||
|
|
||||||
|
if(nn.numberOfLayers > 0)
|
||||||
|
{
|
||||||
|
const unsigned int inputDimension = nn.layers[0].weights.cols;
|
||||||
|
writeDimension(file, inputDimension);
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||||
|
{
|
||||||
|
const Layer layer = nn.layers[i];
|
||||||
|
|
||||||
|
const unsigned int outputDimension = layer.weights.rows;
|
||||||
|
|
||||||
|
writeDimension(file, outputDimension);
|
||||||
|
|
||||||
|
writeLayer(file, layer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writeDimension(file, 0);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
||||||
{
|
{
|
||||||
Matrix matrix = {NULL, 0, 0};
|
Matrix matrix = {NULL, 0, 0};
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ typedef struct
|
|||||||
} NeuralNetwork;
|
} NeuralNetwork;
|
||||||
|
|
||||||
NeuralNetwork loadModel(const char *path);
|
NeuralNetwork loadModel(const char *path);
|
||||||
|
void saveModel(const char *path, const NeuralNetwork nn);
|
||||||
unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[], unsigned int numberOfImages);
|
unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[], unsigned int numberOfImages);
|
||||||
void clearModel(NeuralNetwork *model);
|
void clearModel(NeuralNetwork *model);
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,9 @@
|
|||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
|
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO
|
saveModel(path, nn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
@@ -205,8 +204,8 @@ void test_predictReturnsCorrectLabels(void)
|
|||||||
Matrix biases1 = {.buffer = biasBuffer1, .rows = 2, .cols = 1};
|
Matrix biases1 = {.buffer = biasBuffer1, .rows = 2, .cols = 1};
|
||||||
Matrix biases2 = {.buffer = biasBuffer2, .rows = 3, .cols = 1};
|
Matrix biases2 = {.buffer = biasBuffer2, .rows = 3, .cols = 1};
|
||||||
Matrix biases3 = {.buffer = biasBuffer3, .rows = 5, .cols = 1};
|
Matrix biases3 = {.buffer = biasBuffer3, .rows = 5, .cols = 1};
|
||||||
Layer layers[] = {{.weights=weights1, .biases=biases1, .activation=someActivation}, \
|
Layer layers[] = {{.weights = weights1, .biases = biases1, .activation = someActivation},
|
||||||
{.weights=weights2, .biases=biases2, .activation=someActivation}, \
|
{.weights = weights2, .biases = biases2, .activation = someActivation},
|
||||||
{.weights = weights3, .biases = biases3, .activation = someActivation}};
|
{.weights = weights3, .biases = biases3, .activation = someActivation}};
|
||||||
NeuralNetwork netUnderTest = {.layers = layers, .numberOfLayers = 3};
|
NeuralNetwork netUnderTest = {.layers = layers, .numberOfLayers = 3};
|
||||||
unsigned char *predictedLabels = predict(netUnderTest, inputImages, 2);
|
unsigned char *predictedLabels = predict(netUnderTest, inputImages, 2);
|
||||||
@@ -216,12 +215,14 @@ void test_predictReturnsCorrectLabels(void)
|
|||||||
free(predictedLabels);
|
free(predictedLabels);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUp(void) {
|
void setUp(void)
|
||||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
{
|
||||||
|
// TODO: Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
}
|
}
|
||||||
|
|
||||||
void tearDown(void) {
|
void tearDown(void)
|
||||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
{
|
||||||
|
// TODO: Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
|||||||
Reference in new issue
Block a user