mirror of
https://github.com/augustin64/projet-tipe
synced 2025-01-23 23:26:25 +01:00
Add cnn/neuron_io.c
This commit is contained in:
parent
cbd7b5427d
commit
55db9985a5
3
make.sh
3
make.sh
@ -27,6 +27,7 @@ build () {
|
|||||||
echo "Fait."
|
echo "Fait."
|
||||||
return 0
|
return 0
|
||||||
elif [[ $1 == "test" ]]; then
|
elif [[ $1 == "test" ]]; then
|
||||||
|
[ -f "$OUT/test_"* ] && rm "$OUT/test_"*
|
||||||
for i in "test/"*".c"; do
|
for i in "test/"*".c"; do
|
||||||
echo "Compilation de $i"
|
echo "Compilation de $i"
|
||||||
$CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS
|
$CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS
|
||||||
@ -34,6 +35,8 @@ build () {
|
|||||||
done
|
done
|
||||||
if ! command -v nvcc &> /dev/null; then
|
if ! command -v nvcc &> /dev/null; then
|
||||||
echo "Tests CUDA évités"
|
echo "Tests CUDA évités"
|
||||||
|
elif [[ $SKIP_CUDA == 1 ]]; then
|
||||||
|
echo "Tests CUDA évités"
|
||||||
else
|
else
|
||||||
for i in "test/"*".cu"; do
|
for i in "test/"*".cu"; do
|
||||||
echo "Compilation de $i"
|
echo "Compilation de $i"
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "include/creation.h"
|
#include "include/creation.h"
|
||||||
#include "include/function.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) {
|
Network* create_network(int max_size, int dropout, int initialisation, int input_dim, int input_depth) {
|
||||||
if (dropout < 0 || dropout > 100) {
|
if (dropout < 0 || dropout > 100) {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "struct.h"
|
#include "struct.h"
|
||||||
|
#include "initialisation.h"
|
||||||
|
|
||||||
#ifndef DEF_CREATION_H
|
#ifndef DEF_CREATION_H
|
||||||
#define DEF_CREATION_H
|
#define DEF_CREATION_H
|
||||||
|
24
src/cnn/include/neuron_io.h
Normal file
24
src/cnn/include/neuron_io.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#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
|
96
src/cnn/neuron_io.c
Normal file
96
src/cnn/neuron_io.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
}
|
28
test/cnn_neuron_io.c
Normal file
28
test/cnn_neuron_io.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user