tipe/src/cnn/cnn.c

254 lines
9.2 KiB
C
Raw Normal View History

2022-06-30 10:27:42 +02:00
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
2022-10-02 20:31:20 +02:00
#include <float.h> // Is it used ?
2022-09-28 10:20:08 +02:00
2022-11-03 18:45:38 +01:00
#include "include/backpropagation.h"
2022-09-26 18:00:31 +02:00
#include "include/initialisation.h"
2022-10-24 12:54:51 +02:00
#include "include/function.h"
#include "include/creation.h"
2022-11-03 16:28:03 +01:00
#include "include/update.h"
2022-10-24 12:54:51 +02:00
#include "include/make.h"
2022-10-24 12:54:51 +02:00
#include "../include/colors.h"
2022-09-30 15:54:21 +02:00
#include "include/cnn.h"
2022-06-30 10:27:42 +02:00
2022-07-05 08:13:25 +02:00
// Augmente les dimensions de l'image d'entrée
#define PADDING_INPUT 2
2022-06-30 10:27:42 +02:00
2022-11-25 15:17:47 +01:00
int indice_max(float* tab, int n) {
int indice = -1;
float maxi = FLT_MIN;
2023-01-17 15:34:29 +01:00
2022-11-25 15:17:47 +01:00
for (int i=0; i < n; i++) {
if (tab[i] > maxi) {
maxi = tab[i];
indice = i;
}
}
return indice;
}
2022-06-30 10:27:42 +02:00
int will_be_drop(int dropout_prob) {
return (rand() % 100) < dropout_prob;
2022-06-30 10:27:42 +02:00
}
void write_image_in_network_32(int** image, int height, int width, float** input) {
2022-10-07 14:26:36 +02:00
int padding = (32 - height)/2;
for (int i=0; i < padding; i++) {
for (int j=0; j < 32; j++) {
input[i][j] = 0.;
input[31-i][j] = 0.;
input[j][i] = 0.;
input[j][31-i] = 0.;
}
}
for (int i=0; i < width; i++) {
for (int j=0; j < height; j++) {
input[i+2][j+2] = (float)image[i][j] / 255.0f;
2022-06-30 10:27:42 +02:00
}
}
}
2022-11-19 16:09:07 +01:00
void write_image_in_network_260(unsigned char* image, int height, int width, float*** input) {
2023-02-19 13:43:09 +01:00
int size_input = 260;
int padding = (size_input - height)/2;
2022-11-19 16:09:07 +01:00
for (int i=0; i < padding; i++) {
2023-02-19 13:43:09 +01:00
for (int j=0; j < size_input; j++) {
2022-11-19 16:09:07 +01:00
for (int composante=0; composante < 3; composante++) {
input[composante][i][j] = 0.;
2023-02-19 13:43:09 +01:00
input[composante][size_input-1-i][j] = 0.;
2022-11-19 16:09:07 +01:00
input[composante][j][i] = 0.;
2023-02-19 13:43:09 +01:00
input[composante][j][size_input-1-i] = 0.;
2022-11-19 16:09:07 +01:00
}
}
}
for (int i=0; i < width; i++) {
for (int j=0; j < height; j++) {
for (int composante=0; composante < 3; composante++) {
input[composante][i+2][j+2] = (float)image[(i*height+j)*3 + composante] / 255.0f;
}
}
}
}
2022-06-30 10:27:42 +02:00
void forward_propagation(Network* network) {
2023-03-03 21:58:05 +01:00
int n = network->size; // Nombre de couches du réseau, il contient n-1 kernels
2022-09-19 18:39:49 +02:00
for (int i=0; i < n-1; i++) {
2023-03-03 21:58:05 +01:00
/*
* On procède kernel par kernel:
* On considère à chaque fois une couche d'entrée, une couche de sortie et le kernel qui contient les informations
* pour passer d'une couche à l'autre
*/
Kernel* k_i = network->kernel[i];
float*** input = network->input[i]; // Couche d'entrée
int input_depth = network->depth[i]; // Dimensions de la couche d'entrée
int input_width = network->width[i];
float*** output_z = network->input_z[i+1]; // Couche de sortie avant que la fonction d'activation ne lui soit appliquée
float*** output = network->input[i+1]; // Couche de sortie
int output_depth = network->depth[i+1]; // Dimensions de la couche de sortie
int output_width = network->width[i+1];
int activation = k_i->activation;
int pooling = k_i->pooling;
2022-09-19 18:39:49 +02:00
2022-11-04 12:02:00 +01:00
if (k_i->nn) {
drop_neurones(input, 1, 1, input_width, network->dropout);
} else {
drop_neurones(input, input_depth, input_width, input_width, network->dropout);
}
2023-03-03 21:58:05 +01:00
/*
* Pour chaque couche excepté le pooling, on propage les valeurs de la couche précédente,
* On copie les valeurs de output dans output_z, puis on applique la fonction d'activation à output_z
*/
2022-10-07 15:32:54 +02:00
if (k_i->cnn) { // Convolution
2022-10-02 20:31:20 +02:00
make_convolution(k_i->cnn, input, output, output_width);
2023-03-03 21:58:05 +01:00
copy_input_to_input_z(output, output_z, output_depth, output_width, output_width);
apply_function_to_matrix(activation, output, output_depth, output_width);
2022-06-30 10:27:42 +02:00
}
2022-10-07 15:32:54 +02:00
else if (k_i->nn) { // Full connection
2023-01-17 12:49:35 +01:00
if (k_i->linearisation == 0) { // Vecteur -> Vecteur
2022-10-02 20:31:20 +02:00
make_dense(k_i->nn, input[0][0], output[0][0], input_width, output_width);
2022-10-26 18:27:46 +02:00
} else { // Matrice -> Vecteur
2023-02-28 11:47:57 +01:00
make_dense_linearized(k_i->nn, input, output[0][0], input_depth, input_width, output_width);
2022-10-02 20:31:20 +02:00
}
2023-03-03 21:58:05 +01:00
copy_input_to_input_z(output, output_z, 1, 1, output_width);
apply_function_to_vector(activation, output, output_width);
2022-06-30 10:27:42 +02:00
}
2022-10-02 20:31:20 +02:00
else { // Pooling
2023-03-03 21:58:05 +01:00
if (i == n-2) {
printf_error("Le réseau ne peut pas finir par un pooling layer\n");
2022-06-30 10:27:42 +02:00
return;
2022-10-02 20:31:20 +02:00
} else { // Pooling sur une matrice
2023-03-03 21:58:05 +01:00
if (pooling == 1) {
make_average_pooling(input, output, input_width/output_width, output_depth, output_width);
2023-03-03 21:58:05 +01:00
} else if (pooling == 2) {
make_max_pooling(input, output, input_width/output_width, output_depth, output_width);
2023-01-30 09:39:45 +01:00
} else {
2023-03-03 21:58:05 +01:00
printf_error("Impossible de reconnaître le type de couche de pooling: ");
printf("identifiant: %d, position: %d\n", pooling, i);
2023-01-30 09:39:45 +01:00
}
2022-06-30 10:27:42 +02:00
}
2023-03-03 21:58:05 +01:00
copy_input_to_input_z(output, output_z, output_depth, output_width, output_width);
2022-05-13 15:28:45 +02:00
}
}
}
2022-06-30 10:27:42 +02:00
void backward_propagation(Network* network, int wanted_number) {
2023-03-03 21:58:05 +01:00
int n = network->size; // Nombre de couches du réseau
// Backward sur la dernière couche qui utilise toujours SOFTMAX
float* wanted_output = generate_wanted_output(wanted_number, network->width[network->size -1]); // Sortie désirée, permet d'initialiser une erreur
softmax_backward_cross_entropy(network->input[n-1][0][0], wanted_output, network->width[n-1]);
2023-03-03 21:58:05 +01:00
free(wanted_output);
2022-10-26 18:27:46 +02:00
2023-03-03 21:58:05 +01:00
/*
* On propage à chaque étape:
* - les dérivées de l'erreur par rapport aux poids et biais, que l'on ajoute à ceux existants dans kernel->_->d_bias/d_weights
* - les dérivées de l'erreur par rapport à chaque case de input, qui servent uniquement à la propagation des informations.
* Ainsi, on écrase les valeurs contenues dans input, mais on utilise celles restantes dans input_z qui indiquent les valeurs avant
* la composition par la fonction d'activation pour pouvoir continuer à remonter.
*/
2022-11-03 17:50:11 +01:00
for (int i=n-2; i >= 0; i--) {
2022-10-02 20:31:20 +02:00
// Modifie 'k_i' à partir d'une comparaison d'informations entre 'input' et 'output'
2023-03-03 21:58:05 +01:00
Kernel* k_i = network->kernel[i];
float*** input = network->input[i];
float*** input_z = network->input_z[i];
int input_depth = network->depth[i];
int input_width = network->width[i];
float*** output = network->input[i+1];
int output_depth = network->depth[i+1];
int output_width = network->width[i+1];
int activation = i==0?SIGMOID:network->kernel[i-1]->activation;
2022-06-30 10:27:42 +02:00
2023-01-17 15:34:29 +01:00
2022-10-26 18:27:46 +02:00
if (k_i->cnn) { // Convolution
2023-03-03 21:58:05 +01:00
ptr d_f = get_activation_function(-activation);
2022-11-03 17:50:11 +01:00
backward_convolution(k_i->cnn, input, input_z, output, input_depth, input_width, output_depth, output_width, d_f, i==0);
2022-10-26 18:27:46 +02:00
} else if (k_i->nn) { // Full connection
2023-03-03 21:58:05 +01:00
ptr d_f = get_activation_function(-activation);
2023-01-17 12:49:35 +01:00
if (k_i->linearisation == 0) { // Vecteur -> Vecteur
2023-02-17 14:56:05 +01:00
backward_dense(k_i->nn, input[0][0], input_z[0][0], output[0][0], input_width, output_width, d_f, i==0);
2022-10-26 18:27:46 +02:00
} else { // Matrice -> vecteur
2022-11-03 17:50:11 +01:00
backward_linearisation(k_i->nn, input, input_z, output[0][0], input_depth, input_width, output_width, d_f);
2022-10-26 18:27:46 +02:00
}
} else { // Pooling
2022-11-03 17:50:11 +01:00
backward_2d_pooling(input, output, input_width, output_width, input_depth); // Depth pour input et output a la même valeur
2022-10-26 18:27:46 +02:00
}
2022-06-30 10:27:42 +02:00
}
}
2022-11-04 12:02:00 +01:00
void drop_neurones(float*** input, int depth, int dim1, int dim2, int dropout) {
2023-03-03 21:58:05 +01:00
for (int i=0; i < depth; i++) {
for (int j=0; j < dim1; j++) {
for (int k=0; k < dim2; k++) {
2022-11-04 12:02:00 +01:00
if (will_be_drop(dropout))
input[i][j][k] = 0;
}
}
}
}
2023-03-03 21:58:05 +01:00
void copy_input_to_input_z(float*** output, float*** output_z, int output_depth, int output_rows, int output_columns) {
2022-10-31 20:08:42 +01:00
for (int i=0; i<output_depth; i++) {
for (int j=0; j<output_rows; j++) {
for (int k=0; k<output_columns; k++) {
2023-03-03 21:58:05 +01:00
output_z[i][j][k] = output[i][j][k];
2022-10-31 20:08:42 +01:00
}
}
}
}
2022-10-07 15:32:54 +02:00
float compute_mean_squared_error(float* output, float* wanted_output, int len) {
2023-03-03 21:58:05 +01:00
/*
* $E = \frac{ \sum_{i=0}^n (output_i - desired output_i)^2 }{n}$
*/
if (len == 0) {
printf_error("MSE: division par 0\n");
2022-10-07 15:32:54 +02:00
return 0.;
}
float loss=0.;
for (int i=0; i < len ; i++) {
loss += (output[i]-wanted_output[i])*(output[i]-wanted_output[i]);
}
return loss/len;
}
2022-06-30 10:27:42 +02:00
float compute_cross_entropy_loss(float* output, float* wanted_output, int len) {
float loss=0.;
for (int i=0; i < len ; i++) {
2022-06-30 10:27:42 +02:00
if (wanted_output[i]==1) {
if (output[i]==0.) {
loss -= log(FLT_EPSILON);
}
else {
loss -= log(output[i]);
}
}
}
2023-02-24 11:01:59 +01:00
return loss/len;
2022-06-30 10:27:42 +02:00
}
2023-01-17 15:34:29 +01:00
float* generate_wanted_output(int wanted_number, int size_output) {
float* wanted_output = (float*)malloc(sizeof(float)*size_output);
for (int i=0; i < size_output; i++) {
2022-06-30 10:27:42 +02:00
if (i==wanted_number) {
wanted_output[i]=1;
}
else {
wanted_output[i]=0;
}
}
return wanted_output;
}