163 lines
4.7 KiB
C
163 lines
4.7 KiB
C
#include <allegro.h>
|
|
#include <time.h>
|
|
#include "percettroni.h"
|
|
|
|
#define IMAGE_WIDTH 32
|
|
#define IMAGE_HEIGHT 32
|
|
#define SCALE_FACTOR 2
|
|
|
|
//Cavalli
|
|
#define CATEGORIA 7
|
|
|
|
BITMAP *buffer;
|
|
BITMAP *image;
|
|
int *previsto;
|
|
ReteNeurale *rete_neurale;
|
|
Dataset *set;
|
|
|
|
|
|
void init_allegro();
|
|
void carica_immagine(int);
|
|
void disegna_interfaccia();
|
|
void evento_click_bottone(int);
|
|
int prevedi(int);
|
|
byte get_out_corretto(byte);
|
|
|
|
void main()
|
|
{
|
|
init_allegro();
|
|
|
|
set = get_dataset("cifar-10-batches/test_batch.bin");
|
|
if (set == NULL) {
|
|
printf("Errore nel caricare il dataset\n");
|
|
return;
|
|
}
|
|
|
|
rete_neurale = caricaReteNeurale("rete_cifar_pesi.bin");
|
|
if (rete_neurale == NULL) {
|
|
printf("Errore nel caricare il modello\n");
|
|
return;
|
|
}
|
|
|
|
int indice_set = rand() % set->size;
|
|
|
|
// Carica la prima immagine
|
|
carica_immagine(indice_set);
|
|
|
|
while(!key[KEY_ESC]) {
|
|
disegna_interfaccia();
|
|
indice_set = rand() % set->size;
|
|
evento_click_bottone(indice_set);
|
|
rest(10);
|
|
}
|
|
|
|
destroy_bitmap(buffer);
|
|
destroy_bitmap(image);
|
|
allegro_exit();
|
|
}
|
|
|
|
void init_allegro() {
|
|
allegro_init();
|
|
install_keyboard();
|
|
install_mouse();
|
|
set_color_depth(32);
|
|
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
|
|
buffer = create_bitmap(800, 600);
|
|
image = create_bitmap(IMAGE_WIDTH, IMAGE_HEIGHT);
|
|
show_mouse(screen);
|
|
}
|
|
|
|
void carica_immagine(int indice_set)
|
|
{
|
|
printf("Immagine indice: %d, categoria: %d, previsione: %d\n", indice_set, set->istanze[indice_set].categoria, prevedi(indice_set));
|
|
|
|
for (int y = 0; y < IMAGE_HEIGHT; y++)
|
|
{
|
|
for (int x = 0; x < IMAGE_WIDTH; x++)
|
|
{
|
|
int r = set->istanze[indice_set].immagine[y * IMAGE_WIDTH + x];
|
|
int g = set->istanze[indice_set].immagine[1024 + y * IMAGE_WIDTH + x];
|
|
int b = set->istanze[indice_set].immagine[2048 + y * IMAGE_WIDTH + x];
|
|
putpixel(image, x, y, makecol(r, g, b));
|
|
}
|
|
}
|
|
}
|
|
|
|
void disegna_interfaccia()
|
|
{
|
|
//printf("\tPrevisione: %d\n", previsione);
|
|
clear_to_color(buffer, makecol(255, 255, 255));
|
|
|
|
// Calcola la posizione per centrare l'immagine ingrandita
|
|
int scaled_width = IMAGE_WIDTH * SCALE_FACTOR;
|
|
int scaled_height = IMAGE_HEIGHT * SCALE_FACTOR;
|
|
int image_x = (800 - scaled_width) / 2;
|
|
int image_y = (600 - scaled_height) / 2 - 20; // Sposta leggermente sopra per fare spazio al pulsante
|
|
|
|
// Disegna l'immagine ingrandita
|
|
stretch_blit(image, buffer, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, image_x, image_y, scaled_width, scaled_height);
|
|
|
|
// Disegna il pulsante "prossima"
|
|
int button_width = 150;
|
|
int button_height = 40;
|
|
int button_x = (800 - button_width) / 2;
|
|
int button_y = 600 - 60; // Posizione in basso
|
|
|
|
rectfill(buffer, button_x, button_y, button_x + button_width, button_y + button_height, makecol(200, 200, 200));
|
|
textout_centre_ex(buffer, font, "prossima", button_x + button_width / 2, button_y + 10, makecol(0, 0, 0), -1);
|
|
|
|
/* if(previsto == 1)
|
|
textout_centre_ex(buffer, font, "cavallo", button_x + button_width / 2, 70, makecol(0, 255, 0), -1);
|
|
else
|
|
textout_centre_ex(buffer, font, "non cavallo", button_x + button_width / 2, 70, makecol(255, 0, 0), -1); */
|
|
|
|
// Copia il buffer sullo schermo
|
|
blit(buffer, screen, 0, 0, 0, 0, 800, 600);
|
|
}
|
|
|
|
void evento_click_bottone(int indice_set)
|
|
{
|
|
if (mouse_b & 1)
|
|
{
|
|
int mx = mouse_x;
|
|
int my = mouse_y;
|
|
|
|
// Coordinate del pulsante
|
|
int button_width = 150;
|
|
int button_height = 40;
|
|
int button_x = (800 - button_width) / 2;
|
|
int button_y = 600 - 60;
|
|
|
|
// Controlla se il clic è avvenuto sul pulsante
|
|
if (mx >= button_x && mx <= button_x + button_width && my >= button_y && my <= button_y + button_height)
|
|
{
|
|
carica_immagine(indice_set);
|
|
rest(200); // Debounce
|
|
}
|
|
}
|
|
}
|
|
|
|
int prevedi(int indice_set)
|
|
{
|
|
double **sigmoidi = (double **)malloc(sizeof(double *) * rete_neurale->size);
|
|
|
|
sigmoidi[0] = (double *)malloc(sizeof(double) * rete_neurale->layers[0].size);
|
|
sigmoidi[0] = funzioni_attivazione_layer_byte(rete_neurale->layers[0], set->istanze[indice_set].immagine);
|
|
|
|
for (int j = 1; j < rete_neurale->size; 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);
|
|
|
|
return previsione(sigmoidi[rete_neurale->size - 1][0]);
|
|
}
|
|
|
|
byte get_out_corretto(byte categoria) {
|
|
if(categoria == CATEGORIA)
|
|
return 1;
|
|
else
|
|
return 0;
|
|
} |