mirror of
https://github.com/augustin64/projet-tipe
synced 2025-01-23 15:16:26 +01:00
Add finetuning option
This commit is contained in:
parent
ec138581c2
commit
5ec7bab488
@ -229,7 +229,7 @@ void forward_propagation(Network* network) {
|
||||
}
|
||||
}
|
||||
|
||||
void backward_propagation(Network* network, int wanted_number) {
|
||||
void backward_propagation(Network* network, int wanted_number, int finetuning) {
|
||||
int n = network->size; // Nombre de couches du réseau
|
||||
|
||||
// Backward sur la dernière couche qui utilise toujours SOFTMAX
|
||||
@ -264,12 +264,18 @@ void backward_propagation(Network* network, int wanted_number) {
|
||||
|
||||
|
||||
if (k_i->cnn) { // Convolution
|
||||
if (finetuning == NN_AND_LINEARISATION || finetuning == NN_ONLY) {
|
||||
return; // On arrête la backpropagation
|
||||
}
|
||||
int kernel_size = k_i->cnn->k_size;
|
||||
backward_convolution(k_i->cnn, input, input_z, output, input_depth, input_width, output_depth, output_width, -activation, is_last_layer, kernel_size, padding, stride);
|
||||
} else if (k_i->nn) { // Full connection
|
||||
if (k_i->linearisation == DOESNT_LINEARISE) { // Vecteur -> Vecteur
|
||||
backward_dense(k_i->nn, input[0][0], input_z[0][0], output[0][0], input_width, output_width, -activation, is_last_layer);
|
||||
} else { // Matrice -> vecteur
|
||||
if (finetuning == NN_ONLY) {
|
||||
return; // On arrête la backpropagation
|
||||
}
|
||||
backward_linearisation(k_i->nn, input, input_z, output[0][0], input_depth, input_width, output_width, -activation);
|
||||
}
|
||||
} else { // Pooling
|
||||
|
@ -5,6 +5,10 @@
|
||||
#ifndef DEF_MAIN_H
|
||||
#define DEF_MAIN_H
|
||||
|
||||
#define EVERYTHING 0
|
||||
#define NN_ONLY 1
|
||||
#define NN_AND_LINEARISATION 2
|
||||
|
||||
/*
|
||||
* Renvoie l'indice de l'élément de valeur maximale dans un tableau de flottants
|
||||
* Utilisé pour trouver le neurone le plus activé de la dernière couche (résultat de la classification)
|
||||
@ -38,7 +42,7 @@ void forward_propagation(Network* network);
|
||||
/*
|
||||
* Propage en arrière le cnn
|
||||
*/
|
||||
void backward_propagation(Network* network, int wanted_number);
|
||||
void backward_propagation(Network* network, int wanted_number, int finetuning);
|
||||
|
||||
/*
|
||||
* Implémente le dropout durant l'apprentissage en suivant le papier de recherche suivant:
|
||||
|
@ -23,6 +23,7 @@ typedef struct TrainParameters {
|
||||
int nb_images; // Nombre d'images à traiter
|
||||
float accuracy; // Accuracy (à renvoyer)
|
||||
float loss; // Loss (à renvoyer)
|
||||
int finetuning; // Distance de parcours de la backpropagation
|
||||
|
||||
bool offset; // Décalage aléatoire de l'image
|
||||
} TrainParameters;
|
||||
@ -54,6 +55,6 @@ void* train_thread(void* parameters);
|
||||
/*
|
||||
* Fonction principale d'entraînement du réseau neuronal convolutif
|
||||
*/
|
||||
void train(int dataset_type, char* images_file, char* labels_file, char* data_dir, int epochs, char* out, char* recover, bool with_offset);
|
||||
void train(int dataset_type, char* images_file, char* labels_file, char* data_dir, int epochs, char* out, char* recover, bool with_offset, int finetuning);
|
||||
|
||||
#endif
|
@ -28,6 +28,7 @@ void help(char* call) {
|
||||
printf("\t (jpg) \t--datadir | -dd [FOLDER]\tDossier contenant les images.\n");
|
||||
printf("\t\t--recover | -r [FILENAME]\tRécupérer depuis un modèle existant.\n");
|
||||
printf("\t\t--epochs | -e [int]\t\tNombre d'époques.\n");
|
||||
printf("\t\t--finetuning | -f [int]\t\tDistance de backpropagation (0: tout; 1: dense et linéarisation; 2:dense).\n");
|
||||
printf("\t\t--out | -o [FILENAME]\tFichier où écrire le réseau de neurones.\n");
|
||||
printf("\trecognize:\n");
|
||||
printf("\t\t--dataset | -d (mnist|jpg)\tFormat de l'image à reconnaître.\n");
|
||||
@ -58,6 +59,7 @@ int main(int argc, char* argv[]) {
|
||||
char* labels_file = NULL;
|
||||
char* data_dir = NULL;
|
||||
int epochs = EPOCHS;
|
||||
int finetuning = 0;
|
||||
int dataset_type = 0;
|
||||
char* out = NULL;
|
||||
char* recover = NULL;
|
||||
@ -84,7 +86,10 @@ int main(int argc, char* argv[]) {
|
||||
epochs = strtol(argv[i+1], NULL, 10);
|
||||
i += 2;
|
||||
}
|
||||
else if ((! strcmp(argv[i], "--out"))||(! strcmp(argv[i], "-o"))) {
|
||||
else if ((! strcmp(argv[i], "--finetuning"))||(! strcmp(argv[i], "-f"))) {
|
||||
finetuning = strtol(argv[i+1], NULL, 10);
|
||||
i += 2;
|
||||
} else if ((! strcmp(argv[i], "--out"))||(! strcmp(argv[i], "-o"))) {
|
||||
out = argv[i+1];
|
||||
i += 2;
|
||||
} else if ((! strcmp(argv[i], "--recover"))||(! strcmp(argv[i], "-r"))) {
|
||||
@ -125,7 +130,12 @@ int main(int argc, char* argv[]) {
|
||||
printf("Pas de fichier de sortie spécifié, défaut: out.bin\n");
|
||||
out = "out.bin";
|
||||
}
|
||||
train(dataset_type, images_file, labels_file, data_dir, epochs, out, recover, offset);
|
||||
|
||||
if (finetuning < 0 || finetuning > 2) {
|
||||
printf_error("Mauvais paramètre pour '--finetuning'\n");
|
||||
return 1;
|
||||
}
|
||||
train(dataset_type, images_file, labels_file, data_dir, epochs, out, recover, offset, finetuning);
|
||||
return 0;
|
||||
}
|
||||
if (! strcmp(argv[1], "test")) {
|
||||
|
@ -57,6 +57,7 @@ void* train_thread(void* parameters) {
|
||||
int dataset_type = param->dataset_type;
|
||||
int start = param->start;
|
||||
int nb_images = param->nb_images;
|
||||
int finetuning = param->finetuning;
|
||||
|
||||
float* wanted_output;
|
||||
float accuracy = 0.;
|
||||
@ -103,7 +104,7 @@ void* train_thread(void* parameters) {
|
||||
loss += compute_mean_squared_error(network->input[network->size-1][0][0], wanted_output, 10);
|
||||
gree(wanted_output, false);
|
||||
|
||||
backward_propagation(network, labels[index[i]]);
|
||||
backward_propagation(network, labels[index[i]], finetuning);
|
||||
|
||||
#ifdef DETAILED_TRAIN_TIMINGS
|
||||
printf("Temps de backward: ");
|
||||
@ -143,7 +144,7 @@ void* train_thread(void* parameters) {
|
||||
#endif
|
||||
|
||||
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]], finetuning);
|
||||
|
||||
#ifdef DETAILED_TRAIN_TIMINGS
|
||||
printf("Temps de backward: ");
|
||||
@ -170,7 +171,7 @@ void* train_thread(void* parameters) {
|
||||
}
|
||||
|
||||
|
||||
void train(int dataset_type, char* images_file, char* labels_file, char* data_dir, int epochs, char* out, char* recover, bool offset) {
|
||||
void train(int dataset_type, char* images_file, char* labels_file, char* data_dir, int epochs, char* out, char* recover, bool offset, int finetuning) {
|
||||
#ifdef USE_CUDA
|
||||
bool compatibility = cuda_setup(true);
|
||||
if (!compatibility) {
|
||||
@ -289,6 +290,7 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
|
||||
param->index = shuffle_index;
|
||||
param->network = copy_network(network);
|
||||
param->offset = offset;
|
||||
param->finetuning = finetuning;
|
||||
}
|
||||
#else
|
||||
// Création des paramètres donnés à l'unique
|
||||
@ -315,6 +317,7 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
|
||||
train_params->nb_images = BATCHES;
|
||||
train_params->index = shuffle_index;
|
||||
train_params->offset = offset;
|
||||
train_params->finetuning = finetuning;
|
||||
#endif
|
||||
|
||||
end_time = omp_get_wtime();
|
||||
@ -485,4 +488,4 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
|
||||
printf("\nTemps total: ");
|
||||
printf_time(elapsed_time);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user