mirror of
https://github.com/augustin64/projet-tipe
synced 2025-01-24 07:36:24 +01:00
Rename src/mnist_cnn -> src/cnn
This commit is contained in:
parent
3643f4c50e
commit
8b4b7b19f6
@ -15,19 +15,22 @@ Network* create_network(int max_size, int dropout, int initialisation, int input
|
|||||||
network->size = 1;
|
network->size = 1;
|
||||||
network->input = (float****)malloc(sizeof(float***)*max_size);
|
network->input = (float****)malloc(sizeof(float***)*max_size);
|
||||||
network->kernel = (Kernel**)malloc(sizeof(Kernel*)*(max_size-1));
|
network->kernel = (Kernel**)malloc(sizeof(Kernel*)*(max_size-1));
|
||||||
network->dim = (int**)malloc(sizeof(int*)*max_size);
|
network->width = (int*)malloc(sizeof(int*)*max_size);
|
||||||
|
network->depth = (int*)malloc(sizeof(int*)*max_size);
|
||||||
for (int i=0; i < max_size; i++) {
|
for (int i=0; i < max_size; i++) {
|
||||||
network->dim[i] = (int*)malloc(sizeof(int)*2);
|
|
||||||
network->kernel[i] = (Kernel*)malloc(sizeof(Kernel));
|
network->kernel[i] = (Kernel*)malloc(sizeof(Kernel));
|
||||||
}
|
}
|
||||||
network->dim[0][0] = input_dim;
|
network->width[0] = input_dim;
|
||||||
network->dim[0][1] = input_depth;
|
network->depth[0] = input_depth;
|
||||||
|
network->kernel[0]->nn = NULL;
|
||||||
|
network->kernel[0]->cnn = NULL;
|
||||||
create_a_cube_input_layer(network, 0, input_depth, input_dim);
|
create_a_cube_input_layer(network, 0, input_depth, input_dim);
|
||||||
return network;
|
return network;
|
||||||
}
|
}
|
||||||
|
|
||||||
Network* create_network_lenet5(int dropout, int activation, int initialisation) {
|
Network* create_network_lenet5(int dropout, int activation, int initialisation) {
|
||||||
Network* network = create_network(8, dropout, initialisation, 32, 1);
|
Network* network = create_network(8, dropout, initialisation, 32, 1);
|
||||||
|
network->kernel[0]->activation = activation;
|
||||||
add_convolution(network, 6, 5, activation);
|
add_convolution(network, 6, 5, activation);
|
||||||
add_average_pooling(network, 2, activation);
|
add_average_pooling(network, 2, activation);
|
||||||
add_convolution(network, 16, 5, activation);
|
add_convolution(network, 16, 5, activation);
|
||||||
@ -46,8 +49,8 @@ void create_a_cube_input_layer(Network* network, int pos, int depth, int dim) {
|
|||||||
network->input[pos][i][j] = (float*)malloc(sizeof(float)*dim);
|
network->input[pos][i][j] = (float*)malloc(sizeof(float)*dim);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
network->dim[pos][0] = dim;
|
network->width[pos] = dim;
|
||||||
network->dim[pos][1] = depth;
|
network->depth[pos] = depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
void create_a_line_input_layer(Network* network, int pos, int dim) {
|
void create_a_line_input_layer(Network* network, int pos, int dim) {
|
||||||
@ -65,7 +68,7 @@ void add_average_pooling(Network* network, int kernel_size, int activation) {
|
|||||||
network->kernel[n]->cnn = NULL;
|
network->kernel[n]->cnn = NULL;
|
||||||
network->kernel[n]->nn = NULL;
|
network->kernel[n]->nn = NULL;
|
||||||
network->kernel[n]->activation = activation + 100*kernel_size;
|
network->kernel[n]->activation = activation + 100*kernel_size;
|
||||||
create_a_cube_input_layer(network, n, network->dim[n-1][1], network->dim[n-1][0]/2);
|
create_a_cube_input_layer(network, n, network->depth[n-1], network->width[n-1]/2);
|
||||||
network->size++;
|
network->size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +81,7 @@ void add_average_pooling_flatten(Network* network, int kernel_size, int activati
|
|||||||
network->kernel[n]->cnn = NULL;
|
network->kernel[n]->cnn = NULL;
|
||||||
network->kernel[n]->nn = NULL;
|
network->kernel[n]->nn = NULL;
|
||||||
network->kernel[n]->activation = activation + 100*kernel_size;
|
network->kernel[n]->activation = activation + 100*kernel_size;
|
||||||
int dim = (network->dim[n-1][0]*network->dim[n-1][0]*network->dim[n-1][1])/(kernel_size*kernel_size);
|
int dim = (network->width[n-1]*network->width[n-1]*network->depth[n-1])/(kernel_size*kernel_size);
|
||||||
create_a_line_input_layer(network, n, dim);
|
create_a_line_input_layer(network, n, dim);
|
||||||
network->size++;
|
network->size++;
|
||||||
}
|
}
|
||||||
@ -89,7 +92,7 @@ void add_convolution(Network* network, int nb_filter, int kernel_size, int activ
|
|||||||
printf("Impossible de rajouter une couche de convolution, le réseau est déjà plein\n");
|
printf("Impossible de rajouter une couche de convolution, le réseau est déjà plein\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int r = network->dim[n-1][1];
|
int r = network->depth[n-1];
|
||||||
int c = nb_filter;
|
int c = nb_filter;
|
||||||
network->kernel[n]->nn = NULL;
|
network->kernel[n]->nn = NULL;
|
||||||
network->kernel[n]->activation = activation;
|
network->kernel[n]->activation = activation;
|
||||||
@ -123,9 +126,9 @@ void add_convolution(Network* network, int nb_filter, int kernel_size, int activ
|
|||||||
cnn->d_bias[i][j] = (float*)malloc(sizeof(float)*kernel_size);
|
cnn->d_bias[i][j] = (float*)malloc(sizeof(float)*kernel_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
create_a_cube_input_layer(network, n, c, network->dim[n-1][0] - 2*(kernel_size/2));
|
create_a_cube_input_layer(network, n, c, network->width[n-1] - 2*(kernel_size/2));
|
||||||
int n_int = network->dim[n-1][0]*network->dim[n-1][0]*network->dim[n-1][1];
|
int n_int = network->width[n-1]*network->width[n-1]*network->depth[n-1];
|
||||||
int n_out = network->dim[n][0]*network->dim[n][0]*network->dim[n][1];
|
int n_out = network->width[n]*network->width[n]*network->depth[n];
|
||||||
initialisation_3d_matrix(network->initialisation, cnn->bias, c, kernel_size, kernel_size, n_int+n_out);
|
initialisation_3d_matrix(network->initialisation, cnn->bias, c, kernel_size, kernel_size, n_int+n_out);
|
||||||
initialisation_3d_matrix(ZERO, cnn->d_bias, c, kernel_size, kernel_size, n_int+n_out);
|
initialisation_3d_matrix(ZERO, cnn->d_bias, c, kernel_size, kernel_size, n_int+n_out);
|
||||||
initialisation_4d_matrix(network->initialisation, cnn->w, r, c, kernel_size, kernel_size, n_int+n_out);
|
initialisation_4d_matrix(network->initialisation, cnn->w, r, c, kernel_size, kernel_size, n_int+n_out);
|
@ -19,7 +19,7 @@ void free_a_line_input_layer(Network* network, int pos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void free_average_pooling(Network* network, int pos) {
|
void free_average_pooling(Network* network, int pos) {
|
||||||
free_a_cube_input_layer(network, pos, network->dim[pos-1][1], network->dim[pos-1][0]/2);
|
free_a_cube_input_layer(network, pos, network->depth[pos-1], network->width[pos-1]/2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_average_pooling_flatten(Network* network, int pos) {
|
void free_average_pooling_flatten(Network* network, int pos) {
|
||||||
@ -27,57 +27,57 @@ void free_average_pooling_flatten(Network* network, int pos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void free_convolution(Network* network, int pos) {
|
void free_convolution(Network* network, int pos) {
|
||||||
int c = network->kernel[pos].cnn->columns;
|
int c = network->kernel[pos]->cnn->columns;
|
||||||
int k_size = network->kernel[pos].cnn->k_size;
|
int k_size = network->kernel[pos]->cnn->k_size;
|
||||||
int r = network->kernel[pos].cnn->rows;
|
int r = network->kernel[pos]->cnn->rows;
|
||||||
free_a_cube_input_layer(network, pos, c, network->dim[pos-1][0] - 2*(k_size/2));
|
free_a_cube_input_layer(network, pos, c, network->width[pos-1] - 2*(k_size/2));
|
||||||
for (int i=0; i < c; i++) {
|
for (int i=0; i < c; i++) {
|
||||||
for (int j=0; j < k_size; j++) {
|
for (int j=0; j < k_size; j++) {
|
||||||
free(network->kernel[pos].cnn->bias[i][j]);
|
free(network->kernel[pos]->cnn->bias[i][j]);
|
||||||
free(network->kernel[pos].cnn->d_bias[i][j]);
|
free(network->kernel[pos]->cnn->d_bias[i][j]);
|
||||||
}
|
}
|
||||||
free(network->kernel[pos].cnn->bias[i]);
|
free(network->kernel[pos]->cnn->bias[i]);
|
||||||
free(network->kernel[pos].cnn->d_bias[i]);
|
free(network->kernel[pos]->cnn->d_bias[i]);
|
||||||
}
|
}
|
||||||
free(network->kernel[pos].cnn->bias);
|
free(network->kernel[pos]->cnn->bias);
|
||||||
free(network->kernel[pos].cnn->d_bias);
|
free(network->kernel[pos]->cnn->d_bias);
|
||||||
|
|
||||||
for (int i=0; i < r; i++) {
|
for (int i=0; i < r; i++) {
|
||||||
for (int j=0; j < c; j++) {
|
for (int j=0; j < c; j++) {
|
||||||
for (int k=0; k < k_size; k++) {
|
for (int k=0; k < k_size; k++) {
|
||||||
free(network->kernel[pos].cnn->w[i][j][k]);
|
free(network->kernel[pos]->cnn->w[i][j][k]);
|
||||||
free(network->kernel[pos].cnn->d_w[i][j][k]);
|
free(network->kernel[pos]->cnn->d_w[i][j][k]);
|
||||||
}
|
}
|
||||||
free(network->kernel[pos].cnn->w[i][j]);
|
free(network->kernel[pos]->cnn->w[i][j]);
|
||||||
free(network->kernel[pos].cnn->d_w[i][j]);
|
free(network->kernel[pos]->cnn->d_w[i][j]);
|
||||||
}
|
}
|
||||||
free(network->kernel[pos].cnn->w[i]);
|
free(network->kernel[pos]->cnn->w[i]);
|
||||||
free(network->kernel[pos].cnn->d_w[i]);
|
free(network->kernel[pos]->cnn->d_w[i]);
|
||||||
}
|
}
|
||||||
free(network->kernel[pos].cnn->w);
|
free(network->kernel[pos]->cnn->w);
|
||||||
free(network->kernel[pos].cnn->d_w);
|
free(network->kernel[pos]->cnn->d_w);
|
||||||
|
|
||||||
free(network->kernel[pos].cnn);
|
free(network->kernel[pos]->cnn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_dense(Network* network, int pos) {
|
void free_dense(Network* network, int pos) {
|
||||||
free_a_line_input_layer(network, pos);
|
free_a_line_input_layer(network, pos);
|
||||||
int dim = network->kernel[pos].nn->output_units;
|
int dim = network->kernel[pos]->nn->output_units;
|
||||||
for (int i=0; i < dim; i++) {
|
for (int i=0; i < dim; i++) {
|
||||||
free(network->kernel[pos].nn->weights[i]);
|
free(network->kernel[pos]->nn->weights[i]);
|
||||||
free(network->kernel[pos].nn->d_weights[i]);
|
free(network->kernel[pos]->nn->d_weights[i]);
|
||||||
}
|
}
|
||||||
free(network->kernel[pos].nn->weights);
|
free(network->kernel[pos]->nn->weights);
|
||||||
free(network->kernel[pos].nn->d_weights);
|
free(network->kernel[pos]->nn->d_weights);
|
||||||
|
|
||||||
free(network->kernel[pos].nn->bias);
|
free(network->kernel[pos]->nn->bias);
|
||||||
free(network->kernel[pos].nn->d_bias);
|
free(network->kernel[pos]->nn->d_bias);
|
||||||
|
|
||||||
free(network->kernel[pos].nn);
|
free(network->kernel[pos]->nn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_network_creation(Network* network) {
|
void free_network_creation(Network* network) {
|
||||||
free_a_cube_input_layer(network, 0, network->dim[0][1], network->dim[0][0]);
|
free_a_cube_input_layer(network, 0, network->depth[0], network->width[0]);
|
||||||
|
|
||||||
for (int i=0; i < network->max_size; i++) {
|
for (int i=0; i < network->max_size; i++) {
|
||||||
free(network->dim[i]);
|
free(network->dim[i]);
|
@ -86,6 +86,6 @@ void choose_apply_function_input(int activation, float*** input, int depth, int
|
|||||||
apply_function_input(tanh_, input, depth, rows, columns);
|
apply_function_input(tanh_, input, depth, rows, columns);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("Erreur, fonction d'activation inconnue");
|
printf("Erreur, fonction d'activation inconnue: %d\n", activation);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,7 +7,7 @@
|
|||||||
#include "creation.c"
|
#include "creation.c"
|
||||||
#include "make.c"
|
#include "make.c"
|
||||||
|
|
||||||
#include "cnn.h"
|
#include "main.h"
|
||||||
|
|
||||||
// Augmente les dimensions de l'image d'entrée
|
// Augmente les dimensions de l'image d'entrée
|
||||||
#define PADDING_INPUT 2
|
#define PADDING_INPUT 2
|
||||||
@ -35,14 +35,14 @@ void forward_propagation(Network* network) {
|
|||||||
for (int i=0; i < network->size-1; i++) {
|
for (int i=0; i < network->size-1; i++) {
|
||||||
if (network->kernel[i]->nn==NULL && network->kernel[i]->cnn!=NULL) { //CNN
|
if (network->kernel[i]->nn==NULL && network->kernel[i]->cnn!=NULL) { //CNN
|
||||||
output = network->input[i+1];
|
output = network->input[i+1];
|
||||||
output_dim = network->dim[i+1][0];
|
output_dim = network->width[i+1];
|
||||||
output_depth = network->dim[i+1][1];
|
output_depth = network->depth[i+1];
|
||||||
make_convolution(network->input[i], network->kernel[i]->cnn, output, output_dim);
|
make_convolution(network->input[i], network->kernel[i]->cnn, output, output_dim);
|
||||||
choose_apply_function_input(network->kernel[i]->activation, output, output_depth, output_dim, output_dim);
|
choose_apply_function_input(network->kernel[i]->activation, output, output_depth, output_dim, output_dim);
|
||||||
}
|
}
|
||||||
else if (network->kernel[i]->nn!=NULL && network->kernel[i]->cnn==NULL) { //NN
|
else if (network->kernel[i]->nn!=NULL && network->kernel[i]->cnn==NULL) { //NN
|
||||||
make_fully_connected(network->input[i][0][0], network->kernel[i]->nn, network->input[i+1][0][0], network->dim[i][0], network->dim[i+1][0]);
|
make_fully_connected(network->input[i][0][0], network->kernel[i]->nn, network->input[i+1][0][0], network->width[i], network->width[i+1]);
|
||||||
choose_apply_function_input(network->kernel[i]->activation, network->input[i+1], 1, 1, network->dim[i+1][0]);
|
choose_apply_function_input(network->kernel[i]->activation, network->input[i+1], 1, 1, network->width[i+1]);
|
||||||
}
|
}
|
||||||
else { //Pooling
|
else { //Pooling
|
||||||
if (network->size-2==i) {
|
if (network->size-2==i) {
|
||||||
@ -50,12 +50,12 @@ void forward_propagation(Network* network) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (network->kernel[i+1]->nn!=NULL && network->kernel[i+1]->cnn==NULL) {
|
if (network->kernel[i+1]->nn!=NULL && network->kernel[i+1]->cnn==NULL) {
|
||||||
make_average_pooling_flattened(network->input[i], network->input[i+1][0][0], network->kernel[i]->activation/100, network->dim[i][1], network->dim[i][0]);
|
make_average_pooling_flattened(network->input[i], network->input[i+1][0][0], network->kernel[i]->activation/100, network->depth[i], network->width[i]);
|
||||||
choose_apply_function_input(network->kernel[i]->activation%100, network->input[i+1], 1, 1, network->dim[i+1][0]);
|
choose_apply_function_input(network->kernel[i]->activation%100, network->input[i+1], 1, 1, network->width[i+1]);
|
||||||
}
|
}
|
||||||
else if (network->kernel[i+1]->nn==NULL && network->kernel[i+1]->cnn!=NULL) {
|
else if (network->kernel[i+1]->nn==NULL && network->kernel[i+1]->cnn!=NULL) {
|
||||||
make_average_pooling(network->input[i], network->input[i+1], network->kernel[i]->activation/100, network->dim[i+1][1], network->dim[i+1][0]);
|
make_average_pooling(network->input[i], network->input[i+1], network->kernel[i]->activation/100, network->depth[i+1], network->width[i+1]);
|
||||||
choose_apply_function_input(network->kernel[i]->activation%100, network->input[i+1], network->dim[i+1][1], network->dim[i+1][0], network->dim[i+1][0]);
|
choose_apply_function_input(network->kernel[i]->activation%100, network->input[i+1], network->depth[i+1], network->width[i+1], network->width[i+1]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("Le réseau ne peut pas contenir deux pooling layers collées");
|
printf("Le réseau ne peut pas contenir deux pooling layers collées");
|
||||||
@ -68,12 +68,12 @@ void forward_propagation(Network* network) {
|
|||||||
void backward_propagation(Network* network, float wanted_number) {
|
void backward_propagation(Network* network, float wanted_number) {
|
||||||
float* wanted_output = generate_wanted_output(wanted_number);
|
float* wanted_output = generate_wanted_output(wanted_number);
|
||||||
int n = network->size-1;
|
int n = network->size-1;
|
||||||
float loss = compute_cross_entropy_loss(network->input[n][0][0], wanted_output, network->dim[n][0]);
|
float loss = compute_cross_entropy_loss(network->input[n][0][0], wanted_output, network->width[n]);
|
||||||
for (int i=n; i >= 0; i--) {
|
for (int i=n; i >= 0; i--) {
|
||||||
if (i==n) {
|
if (i==n) {
|
||||||
if (network->kernel[i]->activation == SOFTMAX) {
|
if (network->kernel[i]->activation == SOFTMAX) {
|
||||||
int l2 = network->dim[i][0]; // Taille de la dernière couche
|
int l2 = network->width[i]; // Taille de la dernière couche
|
||||||
int l1 = network->dim[i-1][0];
|
int l1 = network->width[i-1];
|
||||||
for (int j=0; j < l2; j++) {
|
for (int j=0; j < l2; j++) {
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
#include "struct.h"
|
#include "struct.h"
|
||||||
|
|
||||||
#ifndef DEF_CNN_H
|
#ifndef DEF_MAIN_H
|
||||||
#define DEF_CNN_H
|
#define DEF_MAIN_H
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
@ -13,7 +13,7 @@ typedef struct Kernel_cnn {
|
|||||||
} Kernel_cnn;
|
} Kernel_cnn;
|
||||||
|
|
||||||
typedef struct Kernel_nn {
|
typedef struct Kernel_nn {
|
||||||
int input_units;
|
int input_units; // Nombre d'éléments en entrée
|
||||||
int output_units;
|
int output_units;
|
||||||
float* bias; // De dimension output_units
|
float* bias; // De dimension output_units
|
||||||
float* d_bias; // De dimension output_units
|
float* d_bias; // De dimension output_units
|
||||||
@ -27,18 +27,16 @@ typedef struct Kernel {
|
|||||||
int activation; // Vaut l'activation sauf pour un pooling où il: vaut kernel_size*100 + activation
|
int activation; // Vaut l'activation sauf pour un pooling où il: vaut kernel_size*100 + activation
|
||||||
} Kernel;
|
} Kernel;
|
||||||
|
|
||||||
typedef struct Layer {
|
|
||||||
|
|
||||||
} Layer;
|
|
||||||
|
|
||||||
typedef struct Network{
|
typedef struct Network{
|
||||||
int dropout; // Contient la probabilité d'abandon entre 0 et 100 (inclus)
|
int dropout; // Contient la probabilité d'abandon entre 0 et 100 (inclus)
|
||||||
int initialisation; // Contient le type d'initialisation
|
int initialisation; // Contient le type d'initialisation
|
||||||
int max_size; // Taille maximale du réseau après initialisation
|
int max_size; // Taille maximale du réseau après initialisation
|
||||||
int size; // Taille actuelle du réseau
|
int size; // Taille actuelle du réseau
|
||||||
int** dim; // Contient les dimensions de l'input (width*depth)
|
int* width; // Contient les dimensions de l'input (width*depth)
|
||||||
|
int* depth; // Contient les dimensions de l'input (width*depth)
|
||||||
Kernel** kernel;
|
Kernel** kernel;
|
||||||
float**** input;
|
float**** input; // Équivalent du z
|
||||||
} Network;
|
} Network;
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user