From 329e213e1fc7739600d1fb4b01e53221edd6db9a Mon Sep 17 00:00:00 2001 From: augustin64 Date: Mon, 15 May 2023 11:34:23 +0200 Subject: [PATCH] write image: automatically detect padding --- src/cnn/cnn.c | 29 +++++++++++++++-------------- src/cnn/export.c | 4 ++-- src/cnn/include/cnn.h | 7 +++++-- src/cnn/test_network.c | 4 ++-- src/cnn/train.c | 2 +- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/cnn/cnn.c b/src/cnn/cnn.c index b0de52d..9f2fa1a 100644 --- a/src/cnn/cnn.c +++ b/src/cnn/cnn.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -17,8 +18,6 @@ #include "include/cnn.h" -// Augmente les dimensions de l'image d'entrée -#define PADDING_INPUT 2 int indice_max(float* tab, int n) { int indice = -1; @@ -131,25 +130,27 @@ void write_image_in_network_32(int** image, int height, int width, float** input } } -void write_image_in_network_260(unsigned char* image, int height, int width, float*** input) { - int size_input = 260; - int padding = (size_input - height)/2; +void write_256_image_in_network(unsigned char* image, int img_width, int img_depth, int input_width, float*** input) { + assert(img_width <= input_width); + assert((input_width - img_width)%2 == 0); + + int padding = (input_width - img_width)/2; for (int i=0; i < padding; i++) { - for (int j=0; j < size_input; j++) { - for (int composante=0; composante < 3; composante++) { + for (int j=0; j < input_width; j++) { + for (int composante=0; composante < img_depth; composante++) { input[composante][i][j] = 0.; - input[composante][size_input-1-i][j] = 0.; + input[composante][input_width-1-i][j] = 0.; input[composante][j][i] = 0.; - input[composante][j][size_input-1-i] = 0.; + input[composante][j][input_width-1-i] = 0.; } } } - 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; + for (int i=0; i < img_width; i++) { + for (int j=0; j < img_width; j++) { + for (int composante=0; composante < img_depth; composante++) { + input[composante][i+padding][j+padding] = (float)image[(i*img_width+j)*img_depth + composante] / 255.0f; } } } @@ -219,7 +220,7 @@ void forward_propagation(Network* network) { make_max_pooling(input, output, kernel_size, output_depth, output_width, stride, padding); } else { - printf_error("Impossible de reconnaître le type de couche de pooling: "); + printf_error((char*)"Impossible de reconnaître le type de couche de pooling: "); printf("identifiant: %d, position: %d\n", pooling, i); } } diff --git a/src/cnn/export.c b/src/cnn/export.c index 0befa68..f75549f 100644 --- a/src/cnn/export.c +++ b/src/cnn/export.c @@ -126,7 +126,7 @@ void visual_propagation(char* modele_file, char* mnist_images_file, char* out_ba free(mnist_parameters); if (numero < 0 || numero >= nb_elem) { - printf_error("Numéro d'image spécifié invalide."); + printf_error((char*)"Numéro d'image spécifié invalide."); printf(" Le fichier contient %d images.\n", nb_elem); exit(1); } @@ -145,7 +145,7 @@ void visual_propagation(char* modele_file, char* mnist_images_file, char* out_ba } else { imgRawImage* image = loadJpegImageFile(jpeg_file); - write_image_in_network_260(image->lpData, image->height, image->width, network->input[0]); + write_256_image_in_network(image->lpData, image->width, image->numComponents, network->width[0], network->input[0]); // Free allocated memory from image reading free(image->lpData); diff --git a/src/cnn/include/cnn.h b/src/cnn/include/cnn.h index 150f9b8..2779e21 100644 --- a/src/cnn/include/cnn.h +++ b/src/cnn/include/cnn.h @@ -20,9 +20,12 @@ int will_be_drop(int dropout_prob); void write_image_in_network_32(int** image, int height, int width, float** input, bool random_offset); /* -* Écrit une image linéarisée de 256*256*3 pixels dans un tableau de taille 260*260*3 +* Écrit une image linéarisée de img_width*img_width*img_depth pixels dans un tableau de taille size_input*size_input*3 +* Les conditions suivantes doivent être respectées: +* - l'image est au plus de la même taille que input +* - la différence de taille entre input et l'image doit être un multiple de 2 (pour centrer l'image) */ -void write_image_in_network_260(unsigned char* image, int height, int width, float*** input); +void write_256_image_in_network(unsigned char* image, int img_width, int img_depth, int input_width, float*** input); /* * Propage en avant le cnn. Le dropout est actif que si le réseau est en phase d'apprentissage. diff --git a/src/cnn/test_network.c b/src/cnn/test_network.c index 0c15f4c..2f1605f 100644 --- a/src/cnn/test_network.c +++ b/src/cnn/test_network.c @@ -79,7 +79,7 @@ float* test_network_jpg(Network* network, char* data_dir, bool preview_fails, bo printf("Avancement: %.1f%%\r", 1000*i/(float)dataset->numImages); fflush(stdout); } - write_image_in_network_260(dataset->images[i], dataset->height, dataset->height, network->input[0]); + write_256_image_in_network(dataset->images[i], dataset->height, dataset->numComponents, network->width[0], network->input[0]); forward_propagation(network); maxi = indice_max(network->input[network->size-1][0][0], 50); @@ -196,7 +196,7 @@ void recognize_jpg(Network* network, char* input_file, char* out) { } // Load image in the first layer of the Network - write_image_in_network_260(image->lpData, height, width, network->input[0]); + write_256_image_in_network(image->lpData, width, image->numComponents, network->width[0], network->input[0]); forward_propagation(network); diff --git a/src/cnn/train.c b/src/cnn/train.c index be06527..c292c11 100644 --- a/src/cnn/train.c +++ b/src/cnn/train.c @@ -103,7 +103,7 @@ void* train_thread(void* parameters) { load_image_param->index = index[i+1]; pthread_create(&tid, NULL, load_image, (void*) load_image_param); } - write_image_in_network_260(param->dataset->images[index[i]], height, width, network->input[0]); + write_256_image_in_network(param->dataset->images[index[i]], width, param->dataset->numComponents, network->width[0], network->input[0]); forward_propagation(network); maxi = indice_max(network->input[network->size-1][0][0], param->dataset->numCategories); backward_propagation(network, param->dataset->labels[index[i]]);