From e4ec06705b9bc1e5fcdd27b47d234fbf90c9dfd7 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 7 Oct 2022 14:26:36 +0200 Subject: [PATCH] Update train.c --- src/cnn/cnn.c | 25 +++++++++++++------------ src/cnn/make.c | 3 --- src/cnn/train.c | 17 +++++++++++------ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/cnn/cnn.c b/src/cnn/cnn.c index 2be6584..1b67846 100644 --- a/src/cnn/cnn.c +++ b/src/cnn/cnn.c @@ -19,14 +19,19 @@ int will_be_drop(int dropout_prob) { } void write_image_in_network_32(int** image, int height, int width, float** input) { - for (int i=0; i < height+2*PADDING_INPUT; i++) { - for (int j=0; j < width+2*PADDING_INPUT; j++) { - if (i < PADDING_INPUT || i >= height+PADDING_INPUT || j < PADDING_INPUT || j >= width+PADDING_INPUT) { - input[i][j] = 0.; - } - else { - input[i][j] = (float)image[i][j] / 255.0f; - } + 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; } } } @@ -49,16 +54,13 @@ void forward_propagation(Network* network) { activation = k_i->activation; if (k_i->cnn!=NULL) { // Convolution - printf("\n(%d)-Convolution of cnn: %dx%dx%d -> %dx%dx%d\n", i, input_depth, input_width, input_width, output_depth, output_width, output_width); make_convolution(k_i->cnn, input, output, output_width); choose_apply_function_matrix(activation, output, output_depth, output_width); } else if (k_i->nn!=NULL) { // Full connection if (input_depth==1) { // Vecteur -> Vecteur - printf("\n(%d)-Densification of nn: %dx%dx%d -> %dx%dx%d\n", i, 1, 1, input_width, 1, 1, output_width); make_dense(k_i->nn, input[0][0], output[0][0], input_width, output_width); } else { // Matrice -> vecteur - printf("\n(%d)-Densification linearised of nn: %dx%dx%d -> %dx%dx%d\n", i, input_depth, input_width, input_width, 1, 1, output_width); make_dense_linearised(k_i->nn, input, output[0][0], input_depth, input_width, output_width); } choose_apply_function_vector(activation, output, output_width); @@ -68,7 +70,6 @@ void forward_propagation(Network* network) { printf("Le réseau ne peut pas finir par une pooling layer\n"); return; } else { // Pooling sur une matrice - printf("\n(%d)-Average pooling: %dx%dx%d -> %dx%dx%d\n", i, input_depth, input_width, input_width, output_depth, output_width, output_width); make_average_pooling(input, output, activation/100, output_depth, output_width); } } diff --git a/src/cnn/make.c b/src/cnn/make.c index 122ae4d..ad5566f 100644 --- a/src/cnn/make.c +++ b/src/cnn/make.c @@ -6,7 +6,6 @@ void make_convolution(Kernel_cnn* kernel, float*** input, float*** output, int output_dim) { float f; int n = kernel->k_size; - printf("max_input %dx%dx%d: %d \n", kernel->rows, n+output_dim -1, output_dim+n -1, n); for (int i=0; i < kernel->columns; i++) { for (int j=0; j < output_dim; j++) { for (int k=0; k < output_dim; k++) { @@ -25,7 +24,6 @@ void make_convolution(Kernel_cnn* kernel, float*** input, float*** output, int o } void make_average_pooling(float*** input, float*** output, int size, int output_depth, int output_dim) { - printf("%d -> %d \n", output_dim*size, output_dim); float average; int n = size*size; for (int i=0; i < output_depth; i++) { @@ -56,7 +54,6 @@ void make_dense(Kernel_nn* kernel, float* input, float* output, int size_input, void make_dense_linearised(Kernel_nn* kernel, float*** input, float* output, int depth_input, int dim_input, int size_output) { int n = depth_input*dim_input*dim_input; - printf("%dx%dx%d (%d) -> %d\n",depth_input, dim_input, dim_input, n, size_output); float f; for (int l=0; linput[0][0]); + forward_propagation(network); //backward_propagation(network, labels[i]); // TODO get_indice_max(network last layer) @@ -118,10 +120,14 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di if (dataset_type == 0) { train_params->images = images; train_params->labels = labels; + train_params->width = 28; + train_params->height = 28; train_params->data_dir = NULL; } else { train_params->data_dir = data_dir; train_params->images = NULL; + train_params->width = 0; + train_params->height = 0; train_params->labels = NULL; } train_params->nb_images = BATCHES; @@ -145,17 +151,16 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di } else { nb_remaining_images -= BATCHES / nb_threads; } - // TODO train_parameters[k]->network = copy_network(network); + train_parameters[k]->network = copy_network(network); train_parameters[k]->start = BATCHES*j + (nb_images_total/BATCHES)*k; pthread_create( &tid[j], NULL, train_thread, (void*) train_parameters[k]); } for (int k=0; k < nb_threads; k++) { - // TODO joindre les threads et afficher la progression // On attend la terminaison de chaque thread un à un pthread_join( tid[j], NULL ); accuracy += train_parameters[k]->accuracy / (float) nb_images_total; // TODO patch_network(network, train_parameters[k]->network, train_parameters[k]->nb_images); - // TODO free_network(train_parameters[k]->network); + free_network(train_parameters[k]->network); } printf("\rThreads [%d]\tÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: %0.1f%%", nb_threads, i, epochs, BATCHES*(j+1), nb_images_total, accuracy*100); #else @@ -172,7 +177,7 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di #endif write_network(out, network); } - // TODO free_network(network) + free_network(network); #ifdef USE_MULTITHREADING free(tid); #else