diff --git a/make.sh b/make.sh index 66e1f5c..253adcf 100755 --- a/make.sh +++ b/make.sh @@ -27,6 +27,7 @@ build () { echo "Fait." return 0 elif [[ $1 == "test" ]]; then + [ -f "$OUT/test_"* ] && rm "$OUT/test_"* for i in "test/"*".c"; do echo "Compilation de $i" $CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS @@ -34,6 +35,8 @@ build () { done if ! command -v nvcc &> /dev/null; then echo "Tests CUDA évités" + elif [[ $SKIP_CUDA == 1 ]]; then + echo "Tests CUDA évités" else for i in "test/"*".cu"; do echo "Compilation de $i" diff --git a/src/cnn/creation.c b/src/cnn/creation.c index c1017d7..8bae025 100644 --- a/src/cnn/creation.c +++ b/src/cnn/creation.c @@ -2,7 +2,7 @@ #include #include "include/creation.h" #include "include/function.h" -#include "include/initialisation.h" +#include "initialisation.c" Network* create_network(int max_size, int dropout, int initialisation, int input_dim, int input_depth) { if (dropout < 0 || dropout > 100) { diff --git a/src/cnn/include/creation.h b/src/cnn/include/creation.h index 0bad98d..10847d3 100644 --- a/src/cnn/include/creation.h +++ b/src/cnn/include/creation.h @@ -1,4 +1,5 @@ #include "struct.h" +#include "initialisation.h" #ifndef DEF_CREATION_H #define DEF_CREATION_H diff --git a/src/cnn/include/neuron_io.h b/src/cnn/include/neuron_io.h new file mode 100644 index 0000000..e815a3e --- /dev/null +++ b/src/cnn/include/neuron_io.h @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +#include "struct.h" + +#ifndef DEF_NEURON_IO_H +#define DEF_NEURON_IO_H + + + +// Écriture d'un réseau neuronal + +/* +* Écrit un réseau neuronal dans un fichier donné +*/ +void write_network(char* filename, Network* network); + +/* +* Écrit une couche dans le fichier spécifié par le pointeur ptr +*/ +void write_couche(Kernel* kernel, int type_couche, FILE* ptr); +#endif \ No newline at end of file diff --git a/src/cnn/neuron_io.c b/src/cnn/neuron_io.c new file mode 100644 index 0000000..aa734fd --- /dev/null +++ b/src/cnn/neuron_io.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include + +#include "include/neuron_io.h" +#include "include/struct.h" + +#define MAGIC_NUMBER 9023 + +void write_network(char* filename, Network* network) { + FILE *ptr; + int size = network->size; + int type_couche[size]; + + ptr = fopen(filename, "wb"); + + uint32_t buffer[(network->size)*3+4]; + + buffer[0] = MAGIC_NUMBER; + buffer[1] = size; + buffer[2] = network->initialisation; + buffer[3] = network->dropout; + + for (int i=0; i < size; i++) { + buffer[2*i+4] = network->width[i]; + buffer[2*i+5] = network->depth[i]; + } + + for (int i=0; i < size; i++) { + if ((!network->kernel[i]->cnn)&&(!network->kernel[i]->nn)) { + type_couche[i] = 2; + } else if (!network->kernel[i]->cnn) { + type_couche[i] = 1; + } else { + type_couche[i] = 0; + } + buffer[i+2*size+4] = type_couche[i]; + } + + fwrite(buffer, sizeof(buffer), 1, ptr); + + + for (int i=0; i < size; i++) { + write_couche(network->kernel[i], type_couche[i], ptr); + } + + fclose(ptr); +} + + +void write_couche(Kernel* kernel, int type_couche, FILE* ptr) { + uint32_t activation[1]; + int indice; + + activation[0] = kernel->activation; + + fwrite(activation, sizeof(activation), 1, ptr); + if (type_couche == 0) { + Kernel_cnn* cnn = kernel->cnn; + float buffer[2*cnn->k_size*cnn->k_size*cnn->columns*(cnn->rows+1)]; + for (int i=0; i < cnn->columns; i++) { + for (int j=0; j < cnn->k_size; j++) { + for (int k=0; k < cnn->k_size; k++) { + indice = cnn->k_size*(i*cnn->k_size+j)+k; + buffer[indice] = cnn->bias[i][j][k]; + } + } + } + int av_bias = cnn->columns*cnn->k_size*cnn->k_size; + for (int i=0; i < cnn->rows; i++) { + for (int j=0; j < cnn->columns; j++) { + for (int k=0; k < cnn->k_size; k++) { + for (int l=0; l < cnn->k_size; l++) { + indice = ((i*cnn->columns+j)*cnn->k_size+k)*cnn->k_size+l+av_bias; + buffer[indice] = cnn->w[i][j][k][l]; + } + } + } + } + fwrite(buffer, sizeof(buffer), 1, ptr); + } else if (type_couche == 1) { + Kernel_nn* nn = kernel->nn; + float buffer[(1+nn->input_units)*nn->output_units]; + for (int i=0; i < nn->output_units; i++) { + buffer[i] = nn->bias[i]; + } + int av_bias = nn->output_units; + for (int i=0; i < nn->input_units; i++) { + for (int j=0; j < nn->output_units; j++) { + buffer[i*nn->output_units+j+av_bias] = nn->weights[i][j]; + } + } + fwrite(buffer, sizeof(buffer), 1, ptr); + } +} \ No newline at end of file diff --git a/test/cnn_neuron_io.c b/test/cnn_neuron_io.c new file mode 100644 index 0000000..c2e74ae --- /dev/null +++ b/test/cnn_neuron_io.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "../src/cnn/neuron_io.c" +#include "../src/cnn/creation.c" + + + +int main() { + printf("Création du réseau\n"); + Network* network = create_network_lenet5(0, 3, 2); + printf("OK\n"); + + printf("Écriture du réseau\n"); + write_network(".test-cache/cnn_neuron_io.bin", network); + printf("OK\n"); + + /* + printf("Vérification de l'accès en lecture\n"); + Network* network2 = read_network(".test-cache/neuron_io.bin"); + printf("OK\n"); + deletion_of_network(network); + deletion_of_network(network2); + */ + return 0; +} \ No newline at end of file diff --git a/test/neuron_io.c b/test/mnist_neuron_io.c similarity index 100% rename from test/neuron_io.c rename to test/mnist_neuron_io.c