write image: automatically detect padding

This commit is contained in:
augustin64 2023-05-15 11:34:23 +02:00
parent 06abf0bc6b
commit 329e213e1f
5 changed files with 25 additions and 21 deletions

View File

@ -1,5 +1,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <float.h> #include <float.h>
#include <math.h> #include <math.h>
@ -17,8 +18,6 @@
#include "include/cnn.h" #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_max(float* tab, int n) {
int indice = -1; 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) { void write_256_image_in_network(unsigned char* image, int img_width, int img_depth, int input_width, float*** input) {
int size_input = 260; assert(img_width <= input_width);
int padding = (size_input - height)/2; assert((input_width - img_width)%2 == 0);
int padding = (input_width - img_width)/2;
for (int i=0; i < padding; i++) { for (int i=0; i < padding; i++) {
for (int j=0; j < size_input; j++) { for (int j=0; j < input_width; j++) {
for (int composante=0; composante < 3; composante++) { for (int composante=0; composante < img_depth; composante++) {
input[composante][i][j] = 0.; 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][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 i=0; i < img_width; i++) {
for (int j=0; j < height; j++) { for (int j=0; j < img_width; j++) {
for (int composante=0; composante < 3; composante++) { for (int composante=0; composante < img_depth; composante++) {
input[composante][i+2][j+2] = (float)image[(i*height+j)*3 + composante] / 255.0f; 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); make_max_pooling(input, output, kernel_size, output_depth, output_width, stride, padding);
} }
else { 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); printf("identifiant: %d, position: %d\n", pooling, i);
} }
} }

View File

@ -126,7 +126,7 @@ void visual_propagation(char* modele_file, char* mnist_images_file, char* out_ba
free(mnist_parameters); free(mnist_parameters);
if (numero < 0 || numero >= nb_elem) { 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); printf(" Le fichier contient %d images.\n", nb_elem);
exit(1); exit(1);
} }
@ -145,7 +145,7 @@ void visual_propagation(char* modele_file, char* mnist_images_file, char* out_ba
} else { } else {
imgRawImage* image = loadJpegImageFile(jpeg_file); 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 allocated memory from image reading
free(image->lpData); free(image->lpData);

View File

@ -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); 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. * Propage en avant le cnn. Le dropout est actif que si le réseau est en phase d'apprentissage.

View File

@ -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); printf("Avancement: %.1f%%\r", 1000*i/(float)dataset->numImages);
fflush(stdout); 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); forward_propagation(network);
maxi = indice_max(network->input[network->size-1][0][0], 50); 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 // 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); forward_propagation(network);

View File

@ -103,7 +103,7 @@ void* train_thread(void* parameters) {
load_image_param->index = index[i+1]; load_image_param->index = index[i+1];
pthread_create(&tid, NULL, load_image, (void*) load_image_param); 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); forward_propagation(network);
maxi = indice_max(network->input[network->size-1][0][0], param->dataset->numCategories); maxi = indice_max(network->input[network->size-1][0][0], param->dataset->numCategories);
backward_propagation(network, param->dataset->labels[index[i]]); backward_propagation(network, param->dataset->labels[index[i]]);