mirror of
https://github.com/augustin64/projet-tipe
synced 2025-01-23 15:16:26 +01:00
Compare commits
5 Commits
10a9714db9
...
ec138581c2
Author | SHA1 | Date | |
---|---|---|---|
ec138581c2 | |||
3183339b7a | |||
a2cd6ef551 | |||
dd9613f159 | |||
46aa64da01 |
@ -94,9 +94,16 @@ void write_image(float** data, int width, int height, char* base_filename, int l
|
||||
image->height = height;
|
||||
image->lpData = (unsigned char*)malloc(sizeof(unsigned char)*width*height*3);
|
||||
|
||||
float maxi = 1e-7;
|
||||
for (int i=0; i < height; i++) {
|
||||
for (int j=0; j < width; j++) {
|
||||
float color = fmax(fmin(data[i][j], 1.), 0.)*255;
|
||||
maxi = fmax(maxi ,data[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0; i < height; i++) {
|
||||
for (int j=0; j < width; j++) {
|
||||
float color = fmax(data[i][j]/maxi, 0.)*255;
|
||||
|
||||
image->lpData[(i*width+j)*3] = color;
|
||||
image->lpData[(i*width+j)*3 + 1] = color;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "struct.h"
|
||||
|
||||
#ifndef DEF_MAIN_H
|
||||
|
@ -23,6 +23,8 @@ typedef struct TrainParameters {
|
||||
int nb_images; // Nombre d'images à traiter
|
||||
float accuracy; // Accuracy (à renvoyer)
|
||||
float loss; // Loss (à renvoyer)
|
||||
|
||||
bool offset; // Décalage aléatoire de l'image
|
||||
} TrainParameters;
|
||||
|
||||
/*
|
||||
@ -52,6 +54,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);
|
||||
void train(int dataset_type, char* images_file, char* labels_file, char* data_dir, int epochs, char* out, char* recover, bool with_offset);
|
||||
|
||||
#endif
|
@ -24,6 +24,7 @@ void help(char* call) {
|
||||
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--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\t--recover | -r [FILENAME]\tRécupérer depuis un modèle existant.\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(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--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\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;
|
||||
char* out = NULL;
|
||||
char* recover = NULL;
|
||||
bool offset = true;
|
||||
int i = 2;
|
||||
while (i < argc) {
|
||||
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"))) {
|
||||
recover = argv[i+1];
|
||||
i += 2;
|
||||
} else if (! strcmp(argv[i], "--no-offset")) {
|
||||
offset = false;
|
||||
i++;
|
||||
} else {
|
||||
printf_warning("Option choisie inconnue: ");
|
||||
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");
|
||||
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;
|
||||
}
|
||||
if (! strcmp(argv[1], "test")) {
|
||||
@ -130,33 +136,32 @@ int main(int argc, char* argv[]) {
|
||||
char* data_dir = NULL; // Dossier d'images (jpg)
|
||||
int dataset_type; // Type de dataset (0 pour mnist, 1 pour jpg)
|
||||
bool preview_fails = false;
|
||||
bool offset = true;
|
||||
|
||||
int i = 2;
|
||||
while (i < argc) {
|
||||
if ((! strcmp(argv[i], "--dataset"))||(! strcmp(argv[i], "-d"))) {
|
||||
dataset = argv[i+1];
|
||||
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];
|
||||
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];
|
||||
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];
|
||||
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];
|
||||
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;
|
||||
i++;
|
||||
}
|
||||
else {
|
||||
} else if (! strcmp(argv[i], "--no-offset")) {
|
||||
offset = false;
|
||||
i++;
|
||||
} else {
|
||||
printf_warning("Option choisie inconnue: ");
|
||||
printf("%s\n", argv[i]);
|
||||
i++;
|
||||
@ -189,7 +194,7 @@ int main(int argc, char* argv[]) {
|
||||
printf_error("Pas de modèle à utiliser spécifié.\n");
|
||||
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;
|
||||
}
|
||||
if (! strcmp(argv[1], "recognize")) {
|
||||
@ -203,16 +208,13 @@ int main(int argc, char* argv[]) {
|
||||
if ((! strcmp(argv[i], "--dataset"))||(! strcmp(argv[i], "-d"))) {
|
||||
dataset = argv[i+1];
|
||||
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];
|
||||
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];
|
||||
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];
|
||||
i += 2;
|
||||
} else {
|
||||
@ -225,18 +227,20 @@ int main(int argc, char* argv[]) {
|
||||
dataset_type = 0;
|
||||
} else if ((dataset!=NULL) && !strcmp(dataset, "jpg")) {
|
||||
dataset_type = 1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
printf_error("Pas de type de dataset spécifié.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!input_file) {
|
||||
printf_error("Pas de fichier d'entrée spécifié, rien à faire.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
out = "text";
|
||||
}
|
||||
|
||||
if (!modele) {
|
||||
printf_error("Pas de modèle à utiliser spécifié.\n");
|
||||
return 1;
|
||||
|
@ -9,24 +9,24 @@
|
||||
#define BLOCKSIZE_y 16
|
||||
|
||||
#ifdef __CUDACC__
|
||||
__global__ void matrix_mul_kernel(float** Md, float** Nd, float** Pd, int n, int p, int q) {
|
||||
// Chaque thread calcule toutes les multiplications utilisant l'élément Nd[tx][ty]
|
||||
int tx = (blockIdx.x*blockDim.x) + threadIdx.x; // Indice de colonne
|
||||
int ty = (blockIdx.y*blockDim.y) + threadIdx.y; // Indice de ligne
|
||||
__global__ void matrix_mul_kernel(float** M, float** N, float** P, int n, int p, int q) {
|
||||
// Ce fil calcule toutes les multiplications utilisant l'élément N[idx][idy]
|
||||
int idx = (blockIdx.x*blockDim.x) + threadIdx.x; // Indice de colonne
|
||||
int idy = (blockIdx.y*blockDim.y) + threadIdx.y; // Indice de ligne
|
||||
|
||||
if (tx >= p || ty >= q) {
|
||||
return;
|
||||
if (idx >= p || idy >= q) {
|
||||
return; // On vérifie que l'on est bien à un emplacement valide
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
atomicAdd(&(Pd[i][ty]), Md[i][tx]*Nd[tx][ty]);
|
||||
// P[i][ty] += P[i][tx] * N[tx][ty]
|
||||
atomicAdd(&(P[i][idy]), M[i][idx]*N[idx][idy]);
|
||||
// P[i][idy] += M[i][idx] * N[idx][idy]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void matrix_multiplication_device(float** m1, float** m2, float** result, int n, int p, int q) {
|
||||
// Traitement
|
||||
// On découpe la tâche en un certain nombre de blocs,
|
||||
// la taille d'un bloc étant limitée par CUDA à 1024
|
||||
dim3 gridSize(i_div_up(p, BLOCKSIZE_x), i_div_up(q, BLOCKSIZE_y));
|
||||
dim3 blockSize(BLOCKSIZE_x, BLOCKSIZE_y);
|
||||
|
||||
|
@ -173,7 +173,7 @@ Network* read_network(char* filename) {
|
||||
printf_error((char*)"Incorrect magic number !\n");
|
||||
if (INITIAL_MAGIC_NUMBER < magic && magic >= INITIAL_MAGIC_NUMBER) {
|
||||
printf("\tThis backup is no longer supported\n");
|
||||
printf("\tnPlease update it manually or re-train the network.\n");
|
||||
printf("\tPlease update it manually or re-train the network.\n");
|
||||
printf("\t(You can update it with a script or manually with a Hex Editor)\n");
|
||||
}
|
||||
exit(1);
|
||||
|
@ -77,7 +77,7 @@ void* train_thread(void* parameters) {
|
||||
|
||||
for (int i=start; i < start+nb_images; i++) {
|
||||
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
|
||||
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
|
||||
bool compatibility = cuda_setup(true);
|
||||
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->index = shuffle_index;
|
||||
param->network = copy_network(network);
|
||||
param->offset = offset;
|
||||
}
|
||||
#else
|
||||
// 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->index = shuffle_index;
|
||||
train_params->offset = offset;
|
||||
#endif
|
||||
|
||||
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);
|
||||
// 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]);
|
||||
if (test_results[0] < test_accuracy) {
|
||||
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];
|
||||
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);
|
||||
*/
|
||||
}
|
||||
|
||||
|
@ -14,13 +14,16 @@
|
||||
// L'initialisation passe de 1h02 à 2.4s sur mon matériel
|
||||
#define MEMORY_TAIL_OPT
|
||||
|
||||
// We define our memory with a linked list of memory blocks
|
||||
// Liste chaînée de blocs de mémoire
|
||||
typedef struct Memory {
|
||||
void* start; // Start of the allocated memory
|
||||
void* cursor; // Current cursor
|
||||
void* start; // Début du bloc de mémoire alloué
|
||||
void* cursor; // Curseur actuel
|
||||
|
||||
size_t size; // Taille de la mémoire allouée
|
||||
int nb_alloc; // Nombre d'allocations dans le bloc
|
||||
unsigned int id; // Nombre aléatoire permettant d'identifier le bloc plus facilement lors du débogage
|
||||
int nb_alloc; // Nombre d'allocations actives dans le bloc
|
||||
|
||||
unsigned int id; // Nombre aléatoire permettant d'identifier le bloc
|
||||
// plus facilement lors du débogage
|
||||
struct Memory* next; // Élément suivant
|
||||
} Memory;
|
||||
|
||||
|
@ -28,7 +28,7 @@ void help(char* call);
|
||||
* network: réseau neuronal
|
||||
* height, width: dimensions de l'image
|
||||
*/
|
||||
void write_image_in_network(int** image, Network* network, int height, int width);
|
||||
void write_image_in_network(int** image, Network* network, int height, int width, bool random_offset);
|
||||
|
||||
/*
|
||||
* Sous fonction de 'train' assignée à un thread
|
||||
@ -49,7 +49,7 @@ void* train_thread(void* parameters);
|
||||
* nb_images_to_process: nombre d'images sur lesquelles entraîner le réseau (-1 si non utilisé)
|
||||
* start: index auquel démarrer si nb_images_to_process est utilisé (0 si non utilisé)
|
||||
*/
|
||||
void train(int epochs, char* recovery, char* image_file, char* label_file, char* out, char* delta, int nb_images_to_process, int start);
|
||||
void train(int epochs, char* recovery, char* image_file, char* label_file, char* out, char* delta, int nb_images_to_process, int start, bool random_offset);
|
||||
|
||||
/*
|
||||
* Échange deux éléments d'un tableau
|
||||
@ -66,7 +66,7 @@ void knuth_shuffle(int* tab, int n);
|
||||
* modele: nom du fichier contenant le réseau neuronal
|
||||
* entree: nom du fichier contenant les images à reconnaître
|
||||
*/
|
||||
float** recognize(char* modele, char* entree);
|
||||
float** recognize(char* modele, char* entree, bool random_offset);
|
||||
|
||||
/*
|
||||
* Renvoie les prédictions d'images sur stdout
|
||||
@ -74,7 +74,7 @@ float** recognize(char* modele, char* entree);
|
||||
* entree: fichier contenant les images
|
||||
* sortie: vaut 'text' ou 'json', spécifie le format auquel afficher les prédictions
|
||||
*/
|
||||
void print_recognize(char* modele, char* entree, char* sortie);
|
||||
void print_recognize(char* modele, char* entree, char* sortie, bool random_offset);
|
||||
|
||||
/*
|
||||
* Teste un réseau neuronal avec un fichier d'images ainsi que leurs propriétés
|
||||
@ -83,7 +83,7 @@ void print_recognize(char* modele, char* entree, char* sortie);
|
||||
* fichier_labels: nom du fichier contenant les labels
|
||||
* preview_fails: faut-il afficher les images qui ne sont pas correctement reconnues ?
|
||||
*/
|
||||
void test(char* modele, char* fichier_images, char* fichier_labels, bool preview_fails);
|
||||
void test(char* modele, char* fichier_images, char* fichier_labels, bool preview_fails, bool random_offset);
|
||||
|
||||
int main(int argc, char* argv[]);
|
||||
|
||||
|
@ -22,8 +22,8 @@
|
||||
#define PRINT_BIAIS false
|
||||
|
||||
// Mettre à 1 pour désactiver
|
||||
#define DROPOUT 0.7
|
||||
#define ENTRY_DROPOUT 0.85
|
||||
#define DROPOUT 1
|
||||
#define ENTRY_DROPOUT 1
|
||||
|
||||
|
||||
bool drop(float prob);
|
||||
|
104
src/dense/main.c
104
src/dense/main.c
@ -29,6 +29,7 @@ typedef struct TrainParameters {
|
||||
int height;
|
||||
int width;
|
||||
float accuracy;
|
||||
bool offset;
|
||||
} TrainParameters;
|
||||
|
||||
|
||||
@ -71,6 +72,7 @@ void help(char* call) {
|
||||
printf("\t\t--delta | -d [FILENAME]\tFichier où écrire le réseau différentiel.\n");
|
||||
printf("\t\t--nb-images | -N [int]\tNombres d'images à traiter.\n");
|
||||
printf("\t\t--start | -s [int]\tPremière image à traiter.\n");
|
||||
printf("\t\t--offset \tActiver le décalage aléatoire.\n");
|
||||
printf("\trecognize:\n");
|
||||
printf("\t\t--modele | -m [FILENAME]\tFichier contenant le réseau de neurones.\n");
|
||||
printf("\t\t--in | -i [FILENAME]\tFichier contenant les images à reconnaître.\n");
|
||||
@ -80,16 +82,67 @@ void help(char* call) {
|
||||
printf("\t\t--labels | -l [FILENAME]\tFichier contenant les labels.\n");
|
||||
printf("\t\t--modele | -m [FILENAME]\tFichier contenant le réseau de neurones.\n");
|
||||
printf("\t\t--preview-fails | -p\tAfficher les images ayant échoué.\n");
|
||||
printf("\t\t--offset \tActiver le décalage aléatoire.\n");
|
||||
}
|
||||
|
||||
|
||||
void write_image_in_network(int** image, Network* network, int height, int width) {
|
||||
for (int i=0; i < height; i++) {
|
||||
for (int j=0; j < width; j++) {
|
||||
if (!drop(ENTRY_DROPOUT)) {
|
||||
network->layers[0]->neurons[i*height+j]->z = (float)image[i][j] / 255.0f;
|
||||
void write_image_in_network(int** image, Network* network, int height, int width, bool random_offset) {
|
||||
int i_offset = 0;
|
||||
int j_offset = 0;
|
||||
int min_col = 0;
|
||||
int min_ligne = 0;
|
||||
|
||||
if (random_offset) {
|
||||
int sum_colonne[width];
|
||||
int sum_ligne[height];
|
||||
|
||||
for (int i=0; i < width; i++) {
|
||||
sum_colonne[i] = 0;
|
||||
}
|
||||
for (int j=0; j < height; j++) {
|
||||
sum_ligne[j] = 0;
|
||||
}
|
||||
|
||||
for (int i=0; i < width; i++) {
|
||||
for (int j=0; j < height; j++) {
|
||||
sum_ligne[i] += image[i][j];
|
||||
sum_colonne[j] += image[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
min_ligne = -1;
|
||||
while (sum_ligne[min_ligne+1] == 0 && min_ligne < width+1) {
|
||||
min_ligne++;
|
||||
}
|
||||
|
||||
int max_ligne = width;
|
||||
while (sum_ligne[max_ligne-1] == 0 && max_ligne > 0) {
|
||||
max_ligne--;
|
||||
}
|
||||
|
||||
min_col = -1;
|
||||
while (sum_colonne[min_col+1] == 0 && min_col < height+1) {
|
||||
min_col++;
|
||||
}
|
||||
|
||||
int max_col = height;
|
||||
while (sum_colonne[max_col-1] == 0 && max_col > 0) {
|
||||
max_col--;
|
||||
}
|
||||
|
||||
i_offset = 27-max_ligne+min_ligne == 0 ? 0 : rand()%(27-max_ligne+min_ligne);
|
||||
j_offset = 27 - max_col + min_col == 0 ? 0 : rand()%(27-max_col+min_col);
|
||||
}
|
||||
|
||||
for (int i=0; i < width; i++) {
|
||||
for (int j=0; j < height; j++) {
|
||||
int adjusted_i = i + min_ligne - i_offset;
|
||||
int adjusted_j = j + min_col - j_offset;
|
||||
// Make sure not to be out of the image
|
||||
if (!drop(ENTRY_DROPOUT) && adjusted_i < height && adjusted_j < width && adjusted_i >= 0 && adjusted_j >= 0) {
|
||||
network->layers[0]->neurons[i*height+j]->z = (float)image[adjusted_i][adjusted_j] / 255.0f;
|
||||
} else {
|
||||
network->layers[0]->neurons[i*height+j]->z = 0;
|
||||
network->layers[0]->neurons[i*height+j]->z = 0.;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -114,7 +167,7 @@ void* train_thread(void* parameters) {
|
||||
int* desired_output;
|
||||
|
||||
for (int i=start; i < start+nb_images; i++) {
|
||||
write_image_in_network(images[shuffle[i]], network, height, width);
|
||||
write_image_in_network(images[shuffle[i]], network, height, width, param->offset);
|
||||
desired_output = desired_output_creation(network, labels[shuffle[i]]);
|
||||
forward_propagation(network, true);
|
||||
backward_propagation(network, desired_output);
|
||||
@ -134,7 +187,7 @@ void* train_thread(void* parameters) {
|
||||
}
|
||||
|
||||
|
||||
void train(int epochs, char* recovery, char* image_file, char* label_file, char* out, char* delta, int nb_images_to_process, int start) {
|
||||
void train(int epochs, char* recovery, char* image_file, char* label_file, char* out, char* delta, int nb_images_to_process, int start, bool offset) {
|
||||
// Entraînement du réseau sur le set de données MNIST
|
||||
Network* network;
|
||||
Network* delta_network;
|
||||
@ -207,6 +260,7 @@ void train(int epochs, char* recovery, char* image_file, char* label_file, char*
|
||||
train_parameters[j]->width = width;
|
||||
train_parameters[j]->nb_images = BATCHES / nb_threads;
|
||||
train_parameters[j]->shuffle_indices = shuffle_indices;
|
||||
train_parameters[j]->offset = offset;
|
||||
}
|
||||
|
||||
for (int i=0; i < epochs; i++) {
|
||||
@ -245,7 +299,7 @@ void train(int epochs, char* recovery, char* image_file, char* label_file, char*
|
||||
if (delta != NULL)
|
||||
write_delta_network(delta, delta_network);
|
||||
|
||||
test(out, "data/mnist/t10k-images-idx3-ubyte", "data/mnist/t10k-labels-idx1-ubyte", false);
|
||||
test(out, "data/mnist/t10k-images-idx3-ubyte", "data/mnist/t10k-labels-idx1-ubyte", false, offset);
|
||||
}
|
||||
write_network(out, network);
|
||||
if (delta != NULL) {
|
||||
@ -283,7 +337,7 @@ void knuth_shuffle(int* tab, int n) {
|
||||
}
|
||||
}
|
||||
|
||||
float** recognize(char* modele, char* entree) {
|
||||
float** recognize(char* modele, char* entree, bool offset) {
|
||||
Network* network = read_network(modele);
|
||||
Layer* last_layer = network->layers[network->nb_layers-1];
|
||||
|
||||
@ -299,7 +353,7 @@ float** recognize(char* modele, char* entree) {
|
||||
for (int i=0; i < nb_images; i++) {
|
||||
results[i] = (float*)malloc(sizeof(float)*last_layer->nb_neurons);
|
||||
|
||||
write_image_in_network(images[i], network, height, width);
|
||||
write_image_in_network(images[i], network, height, width, offset);
|
||||
forward_propagation(network, false);
|
||||
|
||||
for (int j=0; j < last_layer->nb_neurons; j++) {
|
||||
@ -310,7 +364,7 @@ float** recognize(char* modele, char* entree) {
|
||||
return results;
|
||||
}
|
||||
|
||||
void print_recognize(char* modele, char* entree, char* sortie) {
|
||||
void print_recognize(char* modele, char* entree, char* sortie, bool offset) {
|
||||
Network* network = read_network(modele);
|
||||
int nb_last_layer = network->layers[network->nb_layers-1]->nb_neurons;
|
||||
|
||||
@ -319,7 +373,7 @@ void print_recognize(char* modele, char* entree, char* sortie) {
|
||||
int* parameters = read_mnist_images_parameters(entree);
|
||||
int nb_images = parameters[0];
|
||||
|
||||
float** results = recognize(modele, entree);
|
||||
float** results = recognize(modele, entree, offset);
|
||||
|
||||
if (! strcmp(sortie, "json")) {
|
||||
printf("{\n");
|
||||
@ -356,7 +410,7 @@ void print_recognize(char* modele, char* entree, char* sortie) {
|
||||
}
|
||||
}
|
||||
|
||||
void test(char* modele, char* fichier_images, char* fichier_labels, bool preview_fails) {
|
||||
void test(char* modele, char* fichier_images, char* fichier_labels, bool preview_fails, bool offset) {
|
||||
Network* network = read_network(modele);
|
||||
int nb_last_layer = network->layers[network->nb_layers-1]->nb_neurons;
|
||||
|
||||
@ -368,7 +422,7 @@ void test(char* modele, char* fichier_images, char* fichier_labels, bool preview
|
||||
int height = parameters[2];
|
||||
int*** images = read_mnist_images(fichier_images);
|
||||
|
||||
float** results = recognize(modele, fichier_images);
|
||||
float** results = recognize(modele, fichier_images, offset);
|
||||
unsigned int* labels = read_mnist_labels(fichier_labels);
|
||||
float accuracy = 0.;
|
||||
|
||||
@ -402,6 +456,8 @@ int main(int argc, char* argv[]) {
|
||||
char* recovery = NULL;
|
||||
char* out = NULL;
|
||||
char* delta = NULL;
|
||||
bool offset = false;
|
||||
|
||||
int i = 2;
|
||||
while (i < argc) {
|
||||
// Utiliser un switch serait sans doute plus élégant
|
||||
@ -429,6 +485,9 @@ int main(int argc, char* argv[]) {
|
||||
} else if ((! strcmp(argv[i], "--start"))||(! strcmp(argv[i], "-s"))) {
|
||||
start = strtol(argv[i+1], NULL, 10);
|
||||
i += 2;
|
||||
} else if (! strcmp(argv[i], "--offset")) {
|
||||
offset = true;
|
||||
i++;
|
||||
} else {
|
||||
printf("%s : Argument non reconnu\n", argv[i]);
|
||||
i++;
|
||||
@ -446,8 +505,8 @@ int main(int argc, char* argv[]) {
|
||||
printf("Pas de fichier de sortie spécifié, default: out.bin\n");
|
||||
out = "out.bin";
|
||||
}
|
||||
// Entraînement en sourçant neural_network.c
|
||||
train(epochs, recovery, images, labels, out, delta, nb_images, start);
|
||||
// Entraînement (dans neural_network.c)
|
||||
train(epochs, recovery, images, labels, out, delta, nb_images, start, offset);
|
||||
return 0;
|
||||
}
|
||||
if (! strcmp(argv[1], "recognize")) {
|
||||
@ -481,7 +540,7 @@ int main(int argc, char* argv[]) {
|
||||
if (! out) {
|
||||
out = "text";
|
||||
}
|
||||
print_recognize(modele, in, out);
|
||||
print_recognize(modele, in, out, false);
|
||||
// Reconnaissance puis affichage des données sous le format spécifié
|
||||
return 0;
|
||||
}
|
||||
@ -490,6 +549,7 @@ int main(int argc, char* argv[]) {
|
||||
char* images = NULL;
|
||||
char* labels = NULL;
|
||||
bool preview_fails = false;
|
||||
bool offset = false;
|
||||
int i = 2;
|
||||
while (i < argc) {
|
||||
if ((! strcmp(argv[i], "--images"))||(! strcmp(argv[i], "-i"))) {
|
||||
@ -504,9 +564,15 @@ int main(int argc, char* argv[]) {
|
||||
} else if ((! strcmp(argv[i], "--preview-fails"))||(! strcmp(argv[i], "-p"))) {
|
||||
preview_fails = true;
|
||||
i++;
|
||||
} else if (! strcmp(argv[i], "--offset")) {
|
||||
offset = true;
|
||||
i++;
|
||||
} else {
|
||||
printf("%s : Argument non reconnu\n", argv[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
test(modele, images, labels, preview_fails);
|
||||
test(modele, images, labels, preview_fails, offset);
|
||||
return 0;
|
||||
}
|
||||
printf("Option choisie non reconnue: %s\n", argv[1]);
|
||||
|
Loading…
Reference in New Issue
Block a user