tipe/src/cnn/cnn.c

250 lines
8.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-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"
#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
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
}
}
}
void forward_propagation(Network* network) {
2022-10-02 20:31:20 +02:00
int activation, input_depth, input_width, output_depth, output_width;
2022-09-19 18:39:49 +02:00
int n = network->size;
float*** input;
float*** output;
2022-10-31 20:08:42 +01:00
float*** output_a;
2022-09-19 18:39:49 +02:00
Kernel* k_i;
for (int i=0; i < n-1; i++) {
2022-10-02 20:31:20 +02:00
// Transférer les informations de 'input' à 'output'
2022-09-19 18:39:49 +02:00
k_i = network->kernel[i];
2022-10-31 20:08:42 +01:00
output_a = network->input_z[i+1];
2022-10-02 20:31:20 +02:00
input = network->input[i];
2022-09-19 18:39:49 +02:00
input_depth = network->depth[i];
2022-10-02 20:31:20 +02:00
input_width = network->width[i];
output = network->input[i+1];
2022-09-19 18:39:49 +02:00
output_depth = network->depth[i+1];
2022-10-02 20:31:20 +02:00
output_width = network->width[i+1];
2022-09-30 15:50:29 +02:00
activation = k_i->activation;
2022-09-19 18:39:49 +02:00
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);
2022-10-31 20:08:42 +01:00
copy_input_to_input_z(output, output_a, output_depth, output_width, output_width);
2022-09-30 15:50:29 +02:00
choose_apply_function_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
2022-10-02 20:31:20 +02:00
if (input_depth==1) { // Vecteur -> Vecteur
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
2022-10-02 20:31:20 +02:00
make_dense_linearised(k_i->nn, input, output[0][0], input_depth, input_width, output_width);
}
2022-10-31 20:08:42 +01:00
copy_input_to_input_z(output, output_a, 1, 1, output_width);
2022-09-30 15:50:29 +02:00
choose_apply_function_vector(activation, output, output_width);
2022-06-30 10:27:42 +02:00
}
2022-10-02 20:31:20 +02:00
else { // Pooling
2022-09-19 18:39:49 +02:00
if (n-2==i) {
2022-10-02 20:31:20 +02:00
printf("Le réseau ne peut pas finir par une 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
2022-09-19 18:39:49 +02:00
make_average_pooling(input, output, activation/100, output_depth, output_width);
2022-06-30 10:27:42 +02:00
}
2022-10-31 20:08:42 +01:00
copy_input_to_input_z(output, output_a, output_depth, output_width, output_width);
2022-05-13 15:28:45 +02:00
}
}
}
2022-06-30 10:27:42 +02:00
2022-10-02 20:31:20 +02:00
void backward_propagation(Network* network, float wanted_number) {
2022-09-28 10:20:08 +02:00
printf_warning("Appel de backward_propagation, incomplet\n");
2022-06-30 10:27:42 +02:00
float* wanted_output = generate_wanted_output(wanted_number);
2022-10-02 20:31:20 +02:00
int n = network->size;
int activation, input_depth, input_width, output_depth, output_width;
float*** input;
float*** output;
Kernel* k_i;
Kernel* k_i_1;
2022-10-31 20:08:42 +01:00
// rms_backward(network->input[n-1][0][0], wanted_output); // Backward sur la dernière colonne
2022-10-26 18:27:46 +02:00
2022-10-02 20:31:20 +02:00
for (int i=n-3; i >= 0; i--) {
// Modifie 'k_i' à partir d'une comparaison d'informations entre 'input' et 'output'
k_i = network->kernel[i];
k_i_1 = network->kernel[i+1];
input = network->input[i];
input_depth = network->depth[i];
input_width = network->width[i];
output = network->input[i+1];
output_depth = network->depth[i+1];
output_width = network->width[i+1];
activation = k_i->activation;
2022-06-30 10:27:42 +02:00
2022-10-26 18:27:46 +02:00
if (k_i->cnn) { // Convolution
} else if (k_i->nn) { // Full connection
if (input_depth==1) { // Vecteur -> Vecteur
} else { // Matrice -> vecteur
}
} else { // Pooling
2022-10-31 20:08:42 +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
}
free(wanted_output);
}
2022-10-31 20:08:42 +01:00
void copy_input_to_input_z(float*** output, float*** output_a, int output_depth, int output_rows, int output_columns) {
for (int i=0; i<output_depth; i++) {
for (int j=0; j<output_rows; j++) {
for (int k=0; k<output_columns; k++) {
output_a[i][j][k] = output[i][j][k];
}
}
}
}
2022-10-26 18:27:46 +02:00
void update_weights(Network* network) {
int n = network->size;
2022-10-31 20:08:42 +01:00
int input_depth, input_width, output_depth, output_width;
Kernel* k_i;
Kernel* k_i_1;
2022-10-26 18:27:46 +02:00
for (int i=0; i<(n-1); i++) {
k_i = network->kernel[i];
k_i_1 = network->kernel[i+1];
input_depth = network->depth[i];
input_width = network->width[i];
2022-10-31 20:08:42 +01:00
output_depth = network->depth[i+1];
2022-10-26 18:27:46 +02:00
output_width = network->width[i+1];
if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i_1->cnn;
int k_size = cnn->k_size;
for (int a=0; a<input_depth; a++) {
2022-10-31 20:08:42 +01:00
for (int b=0; b<output_depth; b++) {
2022-10-26 18:27:46 +02:00
for (int c=0; c<k_size; c++) {
for (int d=0; d<k_size; d++) {
cnn->w[a][b][c][d] += cnn->d_w[a][b][c][d];
}
}
}
}
} else if (k_i->nn) { // Full connection
if (input_depth==1) { // Vecteur -> Vecteur
Kernel_nn* nn = k_i_1->nn;
for (int a=0; a<input_width; a++) {
for (int b=0; b<output_width; b++) {
nn->weights[a][b] += nn->d_weights[a][b];
}
}
} else { // Matrice -> vecteur
Kernel_nn* nn = k_i_1->nn;
int input_size = input_width*input_width*input_depth;
for (int a=0; a<input_size; a++) {
for (int b=0; b<output_width; b++) {
nn->weights[a][b] += nn->d_weights[a][b];
}
}
}
} else { // Pooling
(void)0; // Ne rien faire pour la couche pooling
}
}
}
void update_bias(Network* network) {
int n = network->size;
2022-10-31 20:08:42 +01:00
int output_width, output_depth;
Kernel* k_i;
Kernel* k_i_1;
2022-10-26 18:27:46 +02:00
for (int i=0; i<(n-1); i++) {
k_i = network->kernel[i];
k_i_1 = network->kernel[i+1];
output_width = network->width[i+1];
2022-10-31 20:08:42 +01:00
output_depth = network->depth[i+1];
2022-10-26 18:27:46 +02:00
if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i_1->cnn;
2022-10-31 20:08:42 +01:00
for (int a=0; a<output_depth; a++) {
2022-10-26 18:27:46 +02:00
for (int b=0; b<output_width; b++) {
for (int c=0; c<output_width; c++) {
cnn->bias[a][b][c] += cnn->d_bias[a][b][c];
}
}
}
} else if (k_i->nn) { // Full connection
Kernel_nn* nn = k_i_1->nn;
for (int a=0; a<output_width; a++) {
nn->bias[a] += nn->d_bias[a];
}
} else { // Pooling
(void)0; // Ne rien faire pour la couche pooling
}
}
}
2022-10-07 15:32:54 +02:00
float compute_mean_squared_error(float* output, float* wanted_output, int len) {
if (len==0) {
printf("Erreur MSE: la longueur de la sortie est de 0 -> division par 0 impossible\n");
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]);
}
}
}
return loss;
}
float* generate_wanted_output(float wanted_number) {
float* wanted_output = (float*)malloc(sizeof(float)*10);
for (int i=0; i < 10; 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;
}