finale Änderungen
This commit is contained in:
parent
05fbd80b8c
commit
5f068f1337
@ -118,56 +118,60 @@ static void assignActivations(NeuralNetwork model)
|
|||||||
if(model.numberOfLayers > 0)
|
if(model.numberOfLayers > 0)
|
||||||
model.layers[model.numberOfLayers-1].activation = softmax;
|
model.layers[model.numberOfLayers-1].activation = softmax;
|
||||||
}
|
}
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
NeuralNetwork loadModel(const char *path)
|
NeuralNetwork loadModel(const char *path)
|
||||||
{
|
{
|
||||||
NeuralNetwork model = {NULL, 0};
|
NeuralNetwork nn = {.layers = NULL, .numberOfLayers = 0};
|
||||||
FILE *file = fopen(path, "rb");
|
FILE *file = fopen(path, "rb");
|
||||||
|
if (!file) return nn;
|
||||||
|
|
||||||
if(file != NULL)
|
const char *header = "__info2_neural_network_file_format__";
|
||||||
{
|
size_t headerLen = strlen(header);
|
||||||
if(checkFileHeader(file))
|
char buffer[64] = {0};
|
||||||
{
|
|
||||||
unsigned int inputDimension = readDimension(file);
|
|
||||||
unsigned int outputDimension = readDimension(file);
|
|
||||||
|
|
||||||
while(inputDimension > 0 && outputDimension > 0)
|
if (fread(buffer, sizeof(char), headerLen, file) != headerLen ||
|
||||||
{
|
strncmp(buffer, header, headerLen) != 0) {
|
||||||
Layer layer = readLayer(file, inputDimension, outputDimension);
|
|
||||||
Layer *layerBuffer = NULL;
|
|
||||||
|
|
||||||
if(isEmptyLayer(layer))
|
|
||||||
{
|
|
||||||
clearLayer(&layer);
|
|
||||||
clearModel(&model);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer));
|
|
||||||
|
|
||||||
if(layerBuffer != NULL)
|
|
||||||
model.layers = layerBuffer;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
clearModel(&model);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
model.layers[model.numberOfLayers] = layer;
|
|
||||||
model.numberOfLayers++;
|
|
||||||
|
|
||||||
inputDimension = outputDimension;
|
|
||||||
outputDimension = readDimension(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
return nn;
|
||||||
assignActivations(model);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return model;
|
Layer *layers = NULL;
|
||||||
|
unsigned int inputDim = 0, outputDim = 0;
|
||||||
|
|
||||||
|
while (fread(&inputDim, sizeof(unsigned int), 1, file) == 1 && inputDim != 0) {
|
||||||
|
fread(&outputDim, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
|
layers = realloc(layers, (nn.numberOfLayers + 1) * sizeof(Layer));
|
||||||
|
|
||||||
|
Matrix weights;
|
||||||
|
weights.rows = outputDim;
|
||||||
|
weights.cols = inputDim;
|
||||||
|
weights.buffer = malloc(weights.rows * weights.cols * sizeof(MatrixType));
|
||||||
|
fread(weights.buffer, sizeof(MatrixType), weights.rows * weights.cols, file);
|
||||||
|
|
||||||
|
Matrix biases;
|
||||||
|
biases.rows = outputDim;
|
||||||
|
biases.cols = 1;
|
||||||
|
biases.buffer = malloc(biases.rows * biases.cols * sizeof(MatrixType));
|
||||||
|
fread(biases.buffer, sizeof(MatrixType), biases.rows * biases.cols, file);
|
||||||
|
|
||||||
|
layers[nn.numberOfLayers].weights = weights;
|
||||||
|
layers[nn.numberOfLayers].biases = biases;
|
||||||
|
layers[nn.numberOfLayers].activation = NULL;
|
||||||
|
|
||||||
|
nn.numberOfLayers++; // **WICHTIG**
|
||||||
|
}
|
||||||
|
|
||||||
|
nn.layers = layers;
|
||||||
|
fclose(file);
|
||||||
|
return nn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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};
|
||||||
@ -254,15 +258,16 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[],
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearModel(NeuralNetwork *model)
|
|
||||||
|
void clearModel(NeuralNetwork *nn)
|
||||||
{
|
{
|
||||||
if(model != NULL)
|
if (!nn || !nn->layers) return;
|
||||||
{
|
|
||||||
for(int i = 0; i < model->numberOfLayers; i++)
|
for (unsigned int i = 0; i < nn->numberOfLayers; i++) {
|
||||||
{
|
free(nn->layers[i].weights.buffer);
|
||||||
clearLayer(&model->layers[i]);
|
free(nn->layers[i].biases.buffer);
|
||||||
}
|
|
||||||
model->layers = NULL;
|
|
||||||
model->numberOfLayers = 0;
|
|
||||||
}
|
}
|
||||||
}
|
free(nn->layers);
|
||||||
|
nn->layers = NULL;
|
||||||
|
nn->numberOfLayers = 0;
|
||||||
|
}
|
||||||
|
|||||||
@ -38,6 +38,7 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
|||||||
fwrite(layer.biases.buffer, sizeof(MatrixType), biasCount, file);
|
fwrite(layer.biases.buffer, sizeof(MatrixType), biasCount, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// End-Marker (inputDim = 0)
|
// End-Marker (inputDim = 0)
|
||||||
unsigned int zero = 0;
|
unsigned int zero = 0;
|
||||||
fwrite(&zero, sizeof(unsigned int), 1, file);
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user