cnn: Add option to disable random offset

This commit is contained in:
augustin64 2023-05-25 13:31:55 +02:00
parent 3183339b7a
commit ec138581c2
3 changed files with 34 additions and 30 deletions

View File

@ -23,6 +23,8 @@ typedef struct TrainParameters {
int nb_images; // Nombre d'images à traiter int nb_images; // Nombre d'images à traiter
float accuracy; // Accuracy (à renvoyer) float accuracy; // Accuracy (à renvoyer)
float loss; // Loss (à renvoyer) float loss; // Loss (à renvoyer)
bool offset; // Décalage aléatoire de l'image
} TrainParameters; } TrainParameters;
/* /*
@ -52,6 +54,6 @@ void* train_thread(void* parameters);
/* /*
* Fonction principale d'entraînement du réseau neuronal convolutif * 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); void train(int dataset_type, char* images_file, char* labels_file, char* data_dir, int epochs, char* out, char* recover, bool with_offset);
#endif #endif

View File

@ -24,6 +24,7 @@ void help(char* call) {
printf("\t\t--dataset | -d (mnist|jpg)\tFormat du set de données.\n"); printf("\t\t--dataset | -d (mnist|jpg)\tFormat du set de données.\n");
printf("\t(mnist)\t--images | -i [FILENAME]\tFichier contenant les images.\n"); printf("\t(mnist)\t--images | -i [FILENAME]\tFichier contenant les images.\n");
printf("\t(mnist)\t--labels | -l [FILENAME]\tFichier contenant les labels.\n"); printf("\t(mnist)\t--labels | -l [FILENAME]\tFichier contenant les labels.\n");
printf("\t(mnist)\t--no-offset \tDésactiver le décalage aléatoire des images.\n");
printf("\t (jpg) \t--datadir | -dd [FOLDER]\tDossier contenant les images.\n"); 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--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--epochs | -e [int]\t\tNombre d'époques.\n");
@ -38,6 +39,7 @@ void help(char* call) {
printf("\t\t--dataset | -d (mnist|jpg)\tFormat du set de données.\n"); printf("\t\t--dataset | -d (mnist|jpg)\tFormat du set de données.\n");
printf("\t(mnist)\t--images | -i [FILENAME]\tFichier contenant les images.\n"); printf("\t(mnist)\t--images | -i [FILENAME]\tFichier contenant les images.\n");
printf("\t(mnist)\t--labels | -l [FILENAME]\tFichier contenant les labels.\n"); printf("\t(mnist)\t--labels | -l [FILENAME]\tFichier contenant les labels.\n");
printf("\t(mnist)\t--no-offset \tDésactiver le décalage aléatoire des images.\n");
printf("\t (jpg) \t--datadir | -dd [FOLDER]\tDossier contenant les images.\n"); printf("\t (jpg) \t--datadir | -dd [FOLDER]\tDossier contenant les images.\n");
printf("\t\t--preview-fails | -p\t\tAfficher les images ayant échoué.\n"); printf("\t\t--preview-fails | -p\t\tAfficher les images ayant échoué.\n");
} }
@ -59,6 +61,7 @@ int main(int argc, char* argv[]) {
int dataset_type = 0; int dataset_type = 0;
char* out = NULL; char* out = NULL;
char* recover = NULL; char* recover = NULL;
bool offset = true;
int i = 2; int i = 2;
while (i < argc) { while (i < argc) {
if ((! strcmp(argv[i], "--dataset"))||(! strcmp(argv[i], "-d"))) { if ((! strcmp(argv[i], "--dataset"))||(! strcmp(argv[i], "-d"))) {
@ -87,6 +90,9 @@ int main(int argc, char* argv[]) {
} else if ((! strcmp(argv[i], "--recover"))||(! strcmp(argv[i], "-r"))) { } else if ((! strcmp(argv[i], "--recover"))||(! strcmp(argv[i], "-r"))) {
recover = argv[i+1]; recover = argv[i+1];
i += 2; i += 2;
} else if (! strcmp(argv[i], "--no-offset")) {
offset = false;
i++;
} else { } else {
printf_warning("Option choisie inconnue: "); printf_warning("Option choisie inconnue: ");
printf("%s\n", argv[i]); printf("%s\n", argv[i]);
@ -119,7 +125,7 @@ int main(int argc, char* argv[]) {
printf("Pas de fichier de sortie spécifié, défaut: out.bin\n"); printf("Pas de fichier de sortie spécifié, défaut: out.bin\n");
out = "out.bin"; out = "out.bin";
} }
train(dataset_type, images_file, labels_file, data_dir, epochs, out, recover); train(dataset_type, images_file, labels_file, data_dir, epochs, out, recover, offset);
return 0; return 0;
} }
if (! strcmp(argv[1], "test")) { if (! strcmp(argv[1], "test")) {
@ -130,33 +136,32 @@ int main(int argc, char* argv[]) {
char* data_dir = NULL; // Dossier d'images (jpg) char* data_dir = NULL; // Dossier d'images (jpg)
int dataset_type; // Type de dataset (0 pour mnist, 1 pour jpg) int dataset_type; // Type de dataset (0 pour mnist, 1 pour jpg)
bool preview_fails = false; bool preview_fails = false;
bool offset = true;
int i = 2; int i = 2;
while (i < argc) { while (i < argc) {
if ((! strcmp(argv[i], "--dataset"))||(! strcmp(argv[i], "-d"))) { if ((! strcmp(argv[i], "--dataset"))||(! strcmp(argv[i], "-d"))) {
dataset = argv[i+1]; dataset = argv[i+1];
i += 2; i += 2;
} } else if ((! strcmp(argv[i], "--modele"))||(! strcmp(argv[i], "-m"))) {
else if ((! strcmp(argv[i], "--modele"))||(! strcmp(argv[i], "-m"))) {
modele = argv[i+1]; modele = argv[i+1];
i += 2; i += 2;
} } else if ((! strcmp(argv[i], "--images"))||(! strcmp(argv[i], "-i"))) {
else if ((! strcmp(argv[i], "--images"))||(! strcmp(argv[i], "-i"))) {
images_file = argv[i+1]; images_file = argv[i+1];
i += 2; i += 2;
} } else if ((! strcmp(argv[i], "--labels"))||(! strcmp(argv[i], "-l"))) {
else if ((! strcmp(argv[i], "--labels"))||(! strcmp(argv[i], "-l"))) {
labels_file = argv[i+1]; labels_file = argv[i+1];
i += 2; i += 2;
} } else if ((! strcmp(argv[i], "--datadir"))||(! strcmp(argv[i], "-dd"))) {
else if ((! strcmp(argv[i], "--datadir"))||(! strcmp(argv[i], "-dd"))) {
data_dir = argv[i+1]; data_dir = argv[i+1];
i += 2; i += 2;
} } else if ((! strcmp(argv[i], "--preview-fails"))||(! strcmp(argv[i], "-p"))) {
else if ((! strcmp(argv[i], "--preview-fails"))||(! strcmp(argv[i], "-p"))) {
preview_fails = true; preview_fails = true;
i++; i++;
} } else if (! strcmp(argv[i], "--no-offset")) {
else { offset = false;
i++;
} else {
printf_warning("Option choisie inconnue: "); printf_warning("Option choisie inconnue: ");
printf("%s\n", argv[i]); printf("%s\n", argv[i]);
i++; i++;
@ -189,7 +194,7 @@ int main(int argc, char* argv[]) {
printf_error("Pas de modèle à utiliser spécifié.\n"); printf_error("Pas de modèle à utiliser spécifié.\n");
return 1; return 1;
} }
(void)test_network(dataset_type, modele, images_file, labels_file, data_dir, preview_fails, true, false); (void)test_network(dataset_type, modele, images_file, labels_file, data_dir, preview_fails, true, offset);
return 0; return 0;
} }
if (! strcmp(argv[1], "recognize")) { if (! strcmp(argv[1], "recognize")) {
@ -203,16 +208,13 @@ int main(int argc, char* argv[]) {
if ((! strcmp(argv[i], "--dataset"))||(! strcmp(argv[i], "-d"))) { if ((! strcmp(argv[i], "--dataset"))||(! strcmp(argv[i], "-d"))) {
dataset = argv[i+1]; dataset = argv[i+1];
i += 2; i += 2;
} } else if ((! strcmp(argv[i], "--modele"))||(! strcmp(argv[i], "-m"))) {
else if ((! strcmp(argv[i], "--modele"))||(! strcmp(argv[i], "-m"))) {
modele = argv[i+1]; modele = argv[i+1];
i += 2; i += 2;
} } else if ((! strcmp(argv[i], "--out"))||(! strcmp(argv[i], "-o"))) {
else if ((! strcmp(argv[i], "--out"))||(! strcmp(argv[i], "-o"))) {
out = argv[i+1]; out = argv[i+1];
i += 2; i += 2;
} } else if ((! strcmp(argv[i], "--input"))||(! strcmp(argv[i], "-i"))) {
else if ((! strcmp(argv[i], "--input"))||(! strcmp(argv[i], "-i"))) {
input_file = argv[i+1]; input_file = argv[i+1];
i += 2; i += 2;
} else { } else {
@ -225,18 +227,20 @@ int main(int argc, char* argv[]) {
dataset_type = 0; dataset_type = 0;
} else if ((dataset!=NULL) && !strcmp(dataset, "jpg")) { } else if ((dataset!=NULL) && !strcmp(dataset, "jpg")) {
dataset_type = 1; dataset_type = 1;
} } else {
else {
printf_error("Pas de type de dataset spécifié.\n"); printf_error("Pas de type de dataset spécifié.\n");
return 1; return 1;
} }
if (!input_file) { if (!input_file) {
printf_error("Pas de fichier d'entrée spécifié, rien à faire.\n"); printf_error("Pas de fichier d'entrée spécifié, rien à faire.\n");
return 1; return 1;
} }
if (!out) { if (!out) {
out = "text"; out = "text";
} }
if (!modele) { if (!modele) {
printf_error("Pas de modèle à utiliser spécifié.\n"); printf_error("Pas de modèle à utiliser spécifié.\n");
return 1; return 1;

View File

@ -77,7 +77,7 @@ void* train_thread(void* parameters) {
for (int i=start; i < start+nb_images; i++) { for (int i=start; i < start+nb_images; i++) {
if (dataset_type == 0) { if (dataset_type == 0) {
write_image_in_network_32(images[index[i]], height, width, network->input[0][0], true); write_image_in_network_32(images[index[i]], height, width, network->input[0][0], param->offset);
#ifdef DETAILED_TRAIN_TIMINGS #ifdef DETAILED_TRAIN_TIMINGS
start_time = omp_get_wtime(); start_time = omp_get_wtime();
@ -170,7 +170,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) { void train(int dataset_type, char* images_file, char* labels_file, char* data_dir, int epochs, char* out, char* recover, bool offset) {
#ifdef USE_CUDA #ifdef USE_CUDA
bool compatibility = cuda_setup(true); bool compatibility = cuda_setup(true);
if (!compatibility) { if (!compatibility) {
@ -288,6 +288,7 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
param->nb_images = BATCHES / nb_threads; param->nb_images = BATCHES / nb_threads;
param->index = shuffle_index; param->index = shuffle_index;
param->network = copy_network(network); param->network = copy_network(network);
param->offset = offset;
} }
#else #else
// Création des paramètres donnés à l'unique // Création des paramètres donnés à l'unique
@ -313,6 +314,7 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
} }
train_params->nb_images = BATCHES; train_params->nb_images = BATCHES;
train_params->index = shuffle_index; train_params->index = shuffle_index;
train_params->offset = offset;
#endif #endif
end_time = omp_get_wtime(); end_time = omp_get_wtime();
@ -432,7 +434,7 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
write_network(out, network); write_network(out, network);
// If you want to test the network between each epoch, uncomment the following lines: // If you want to test the network between each epoch, uncomment the following lines:
/* /*
float* test_results = test_network(0, out, "data/mnist/t10k-images-idx3-ubyte", "data/mnist/t10k-labels-idx1-ubyte", NULL, false, false, true); float* test_results = test_network(0, out, "data/mnist/t10k-images-idx3-ubyte", "data/mnist/t10k-labels-idx1-ubyte", NULL, false, false, offset);
printf("Tests: Accuracy: %0.2lf%%\tLoss: %lf\n", test_results[0], test_results[1]); printf("Tests: Accuracy: %0.2lf%%\tLoss: %lf\n", test_results[0], test_results[1]);
if (test_results[0] < test_accuracy) { if (test_results[0] < test_accuracy) {
network->learning_rate *= 0.1; network->learning_rate *= 0.1;
@ -444,10 +446,6 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
} }
test_accuracy = test_results[0]; test_accuracy = test_results[0];
free(test_results); free(test_results);
test_results = test_network(0, out, "data/mnist/t10k-images-idx3-ubyte", "data/mnist/t10k-labels-idx1-ubyte", NULL, false, false, false);
printf("Tests sans offset: Accuracy: %0.2lf%%\tLoss: %lf\n", test_results[0], test_results[1]);
free(test_results);
*/ */
} }