mirror of
https://github.com/augustin64/projet-tipe
synced 2025-01-23 15:16:26 +01:00
Load image from a separate thread
This commit is contained in:
parent
710306a286
commit
3198e6f2d0
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Structure donnée en argument à la fonction 'train_thread'
|
* Structure donnée en argument à la fonction 'train_thread'
|
||||||
*/
|
*/
|
||||||
typedef struct TrainParameters {
|
typedef struct TrainParameters {
|
||||||
Network* network; // Réseau
|
Network* network; // Réseau
|
||||||
@ -25,11 +25,24 @@ typedef struct TrainParameters {
|
|||||||
float loss; // Loss (à renvoyer)
|
float loss; // Loss (à renvoyer)
|
||||||
} TrainParameters;
|
} TrainParameters;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Structure donnée en argument à la fonction 'load_image'
|
||||||
|
*/
|
||||||
|
typedef struct LoadImageParameters {
|
||||||
|
jpegDataset* dataset; // Dataset si de type JPEG
|
||||||
|
int index; // Numéro de l'image à charger
|
||||||
|
} LoadImageParameters;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Partie entière supérieure de a/b
|
* Partie entière supérieure de a/b
|
||||||
*/
|
*/
|
||||||
int div_up(int a, int b);
|
int div_up(int a, int b);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Fonction auxiliaire pour charger (ouvrir et décompresser) les images de manière asynchrone
|
||||||
|
* économise environ 20ms par image pour des images de taille 256*256*3
|
||||||
|
*/
|
||||||
|
void* load_image(void* parameters);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Fonction auxiliaire d'entraînement destinée à être exécutée sur plusieurs threads à la fois
|
* Fonction auxiliaire d'entraînement destinée à être exécutée sur plusieurs threads à la fois
|
||||||
|
@ -28,6 +28,19 @@ int div_up(int a, int b) { // Partie entière supérieure de a/b
|
|||||||
return ((a % b) != 0) ? (a / b + 1) : (a / b);
|
return ((a % b) != 0) ? (a / b + 1) : (a / b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* load_image(void* parameters) {
|
||||||
|
LoadImageParameters* param = (LoadImageParameters*)parameters;
|
||||||
|
|
||||||
|
if (!param->dataset->images[param->index]) {
|
||||||
|
imgRawImage* image = loadJpegImageFile(param->dataset->fileNames[param->index]);
|
||||||
|
param->dataset->images[param->index] = image->lpData;
|
||||||
|
free(image);
|
||||||
|
} else {
|
||||||
|
printf_warning((char*)"Image déjà chargée\n"); // Pas possible techniquement, donc on met un warning
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
void* train_thread(void* parameters) {
|
void* train_thread(void* parameters) {
|
||||||
TrainParameters* param = (TrainParameters*)parameters;
|
TrainParameters* param = (TrainParameters*)parameters;
|
||||||
@ -49,6 +62,16 @@ void* train_thread(void* parameters) {
|
|||||||
float accuracy = 0.;
|
float accuracy = 0.;
|
||||||
float loss = 0.;
|
float loss = 0.;
|
||||||
|
|
||||||
|
pthread_t tid;
|
||||||
|
LoadImageParameters* load_image_param;
|
||||||
|
if (dataset_type != 0) {
|
||||||
|
load_image_param = (LoadImageParameters*)malloc(sizeof(LoadImageParameters));
|
||||||
|
load_image_param->dataset = param->dataset;
|
||||||
|
load_image_param->index = index[start];
|
||||||
|
|
||||||
|
pthread_create(&tid, NULL, load_image, (void*) load_image_param);
|
||||||
|
}
|
||||||
|
|
||||||
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], true);
|
||||||
@ -70,11 +93,17 @@ void* train_thread(void* parameters) {
|
|||||||
accuracy += 1.;
|
accuracy += 1.;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
pthread_join(tid, NULL);
|
||||||
if (!param->dataset->images[index[i]]) {
|
if (!param->dataset->images[index[i]]) {
|
||||||
image = loadJpegImageFile(param->dataset->fileNames[index[i]]);
|
image = loadJpegImageFile(param->dataset->fileNames[index[i]]);
|
||||||
param->dataset->images[index[i]] = image->lpData;
|
param->dataset->images[index[i]] = image->lpData;
|
||||||
free(image);
|
free(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (i != start+nb_images-1) {
|
||||||
|
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_image_in_network_260(param->dataset->images[index[i]], height, width, 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);
|
||||||
@ -89,6 +118,10 @@ void* train_thread(void* parameters) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dataset_type != 0) {
|
||||||
|
free(load_image_param);
|
||||||
|
}
|
||||||
|
|
||||||
param->accuracy = accuracy;
|
param->accuracy = accuracy;
|
||||||
param->loss = loss;
|
param->loss = loss;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
Loading…
Reference in New Issue
Block a user