forked from freudenreichan/info2Praktikum-NeuronalesNetz
Implement prepareNeuralNetworkFile
This commit is contained in:
3 files changed
+112
-57
No files matched your search
+66
-13
@@ -52,8 +52,8 @@ static int checkFileHeader(FILE *file)
|
||||
int fileHeaderLen = strlen(FILE_HEADER_STRING);
|
||||
char buffer[BUFFER_SIZE] = {0};
|
||||
|
||||
if(BUFFER_SIZE-1 < fileHeaderLen)
|
||||
fileHeaderLen = BUFFER_SIZE-1;
|
||||
if(BUFFER_SIZE - 1 < fileHeaderLen)
|
||||
fileHeaderLen = BUFFER_SIZE - 1;
|
||||
|
||||
if(fread(buffer, sizeof(char), fileHeaderLen, file) == fileHeaderLen)
|
||||
isValid = strcmp(buffer, FILE_HEADER_STRING) == 0;
|
||||
@@ -67,17 +67,17 @@ static unsigned int readDimension(FILE *file)
|
||||
|
||||
if(fread(&dimension, sizeof(int), 1, file) != 1)
|
||||
dimension = 0;
|
||||
|
||||
|
||||
return dimension;
|
||||
}
|
||||
|
||||
static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix matrix = createMatrix(rows, cols);
|
||||
|
||||
|
||||
if(matrix.buffer != NULL)
|
||||
{
|
||||
if(fread(matrix.buffer, sizeof(MatrixType), rows*cols, file) != rows*cols)
|
||||
if(fread(matrix.buffer, sizeof(MatrixType), rows * cols, file) != rows * cols)
|
||||
clearMatrix(&matrix);
|
||||
}
|
||||
|
||||
@@ -110,13 +110,13 @@ static void clearLayer(Layer *layer)
|
||||
|
||||
static void assignActivations(NeuralNetwork model)
|
||||
{
|
||||
for(int i = 0; i < (int)model.numberOfLayers-1; i++)
|
||||
for(int i = 0; i < (int)model.numberOfLayers - 1; i++)
|
||||
{
|
||||
model.layers[i].activation = relu;
|
||||
}
|
||||
|
||||
if(model.numberOfLayers > 0)
|
||||
model.layers[model.numberOfLayers-1].activation = softmax;
|
||||
model.layers[model.numberOfLayers - 1].activation = softmax;
|
||||
}
|
||||
|
||||
NeuralNetwork loadModel(const char *path)
|
||||
@@ -128,7 +128,7 @@ NeuralNetwork loadModel(const char *path)
|
||||
{
|
||||
if(checkFileHeader(file))
|
||||
{
|
||||
unsigned int inputDimension = readDimension(file);
|
||||
unsigned int inputDimension = readDimension(file);
|
||||
unsigned int outputDimension = readDimension(file);
|
||||
|
||||
while(inputDimension > 0 && outputDimension > 0)
|
||||
@@ -142,7 +142,7 @@ NeuralNetwork loadModel(const char *path)
|
||||
clearModel(&model);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer));
|
||||
|
||||
if(layerBuffer != NULL)
|
||||
@@ -168,6 +168,59 @@ NeuralNetwork loadModel(const char *path)
|
||||
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)
|
||||
{
|
||||
Matrix matrix = {NULL, 0, 0};
|
||||
@@ -201,7 +254,7 @@ static Matrix forward(const NeuralNetwork model, Matrix inputBatch)
|
||||
{
|
||||
Matrix biasResult;
|
||||
Matrix weightResult;
|
||||
|
||||
|
||||
weightResult = multiply(model.layers[i].weights, result);
|
||||
clearMatrix(&result);
|
||||
biasResult = add(model.layers[i].biases, weightResult);
|
||||
@@ -248,9 +301,9 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[],
|
||||
Matrix outputBatch = forward(model, inputBatch);
|
||||
|
||||
unsigned char *result = argmax(outputBatch);
|
||||
|
||||
|
||||
clearMatrix(&outputBatch);
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -265,4 +318,4 @@ void clearModel(NeuralNetwork *model)
|
||||
model->layers = NULL;
|
||||
model->numberOfLayers = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user