124 lines
4.1 KiB
C
124 lines
4.1 KiB
C
#include "visualizzatore.h"
|
|
|
|
#define NUM_LAYERS 4
|
|
|
|
#define PERCETTRONI_LAYER_0 256
|
|
#define INPUT_LAYER_0 3072
|
|
#define PERCETTRONI_LAYER_1 128
|
|
#define INPUT_LAYER_1 256
|
|
#define PERCETTRONI_LAYER_2 64
|
|
#define INPUT_LAYER_2 128
|
|
#define PERCETTRONI_LAYER_3 1
|
|
#define INPUT_LAYER_3 64
|
|
//#define PERCETTRONI_LAYER_4 1
|
|
//#define INPUT_LAYER_4 10
|
|
|
|
#define MAX_EPOCHE 100
|
|
|
|
//Scelgo quale categoria voglio identificare. La 7 sono i cavalli. La rete mi dirà per ogni immagine se è un cavallo o no
|
|
#define CATEGORIA 7
|
|
byte get_out_corretto(byte);
|
|
void stampa_layer_indirizzo(Layer*);
|
|
|
|
void main() {
|
|
srand(time(NULL));
|
|
|
|
/* init_allegro();
|
|
|
|
// Carica la prima immagine
|
|
load_current_image(set);
|
|
|
|
while (!key[KEY_ESC]) {
|
|
draw_interface();
|
|
handle_input(set);
|
|
rest(10);
|
|
}
|
|
|
|
destroy_bitmap(buffer);
|
|
destroy_bitmap(image);
|
|
allegro_exit(); */
|
|
|
|
Dataset *set_appoggio = get_dataset("cifar-10-batches/data_batch_1.bin");
|
|
if(set_appoggio == NULL)
|
|
return;
|
|
Dataset set = *set_appoggio;
|
|
free(set_appoggio);
|
|
|
|
ReteNeurale rete_neurale = inizializza_rete_neurale(NUM_LAYERS);
|
|
//inizializzo layer 0
|
|
rete_neurale.layers[0] = inizializza_layer(PERCETTRONI_LAYER_0, INPUT_LAYER_0);
|
|
//inizializzo layer 1
|
|
rete_neurale.layers[1] = inizializza_layer(PERCETTRONI_LAYER_1, INPUT_LAYER_1);
|
|
//inizializzo layer 2
|
|
rete_neurale.layers[2] = inizializza_layer(PERCETTRONI_LAYER_2, INPUT_LAYER_2);
|
|
//inizializzo layer 3
|
|
rete_neurale.layers[3] = inizializza_layer(PERCETTRONI_LAYER_3, INPUT_LAYER_3);
|
|
//inizializzo layer ULTIMO
|
|
//rete_neurale.layers[4] = inizializza_layer(PERCETTRONI_LAYER_4, INPUT_LAYER_4);
|
|
|
|
printf("Numero immagini: %d\n", 1000);
|
|
|
|
//ADDESTRAMENTO
|
|
for(int i = 0; i < MAX_EPOCHE; i++) {
|
|
printf("Epoca %d\n", i);
|
|
|
|
int corrette = 0;
|
|
|
|
for(int indice_set = 0; indice_set < 1000; indice_set++) {
|
|
|
|
//printf("\timmagine: %d\n", indice_set);
|
|
|
|
double **sigmoidi = (double **)malloc(sizeof(double*) * NUM_LAYERS);
|
|
|
|
sigmoidi[0] = (double*)malloc(sizeof(double) * PERCETTRONI_LAYER_0);
|
|
sigmoidi[0] = funzioni_attivazione_layer_byte(rete_neurale.layers[0], set.istanze[indice_set].immagine);
|
|
|
|
for(int j = 1; j < NUM_LAYERS; j++) {
|
|
sigmoidi[j] = (double*)malloc(sizeof(double) * rete_neurale.layers[j].size);
|
|
sigmoidi[j] = funzioni_attivazione_layer_double(rete_neurale.layers[j], sigmoidi[j-1]);
|
|
}
|
|
|
|
byte output_corretto = get_out_corretto(set.istanze[indice_set].categoria);
|
|
|
|
//Se prevede male
|
|
if(previsione(sigmoidi[NUM_LAYERS-1][0]) != output_corretto) {
|
|
|
|
double **gradienti = (double**)malloc(sizeof(double*) * NUM_LAYERS);
|
|
|
|
for(int indice_layer = 0; indice_layer < NUM_LAYERS; indice_layer++) {
|
|
gradienti[indice_layer] = (double*)malloc(sizeof(double) * rete_neurale.layers[indice_layer].size);
|
|
}
|
|
|
|
gradienti[NUM_LAYERS-1][0] = output_corretto - sigmoidi[NUM_LAYERS-1][0];
|
|
|
|
correggi_layer_interni(&rete_neurale, gradienti, sigmoidi);
|
|
correggi_layer_input(&rete_neurale.layers[0], gradienti, sigmoidi, set.istanze[indice_set].immagine, NUM_LAYERS);
|
|
}
|
|
else
|
|
{
|
|
//printf("immagine: %d categoria: %d, risposta_esatta: %d, previsione: %d\n", indice_set, set.istanze[indice_set].categoria, output_corretto, previsione(*sigmoide_out));
|
|
corrette++;
|
|
}
|
|
}
|
|
printf("\tRisposte corrette: %d\n", corrette);
|
|
}
|
|
}
|
|
|
|
//Questa funzione ritorna 1 se la categoria è quella che voglio individuare, altrimenti 0
|
|
byte get_out_corretto(byte categoria) {
|
|
if(categoria == CATEGORIA)
|
|
return 1;
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
void stampa_layer_indirizzo(Layer *layer) {
|
|
for(int i = 0; i < layer->size; i++) {
|
|
printf("Percettrone %d ->", i);
|
|
for(int j = 0; j < layer->percettroni->size; j++) {
|
|
printf("\t peso %d, valore: %f",j, layer->percettroni[i].pesi[j]);
|
|
layer->percettroni[i].pesi[j] += 1;
|
|
}
|
|
printf("\n");
|
|
}
|
|
} |