/* ** ** ** PROGRAMME :som_lvq.c ** ORIGINAL AUTHOR: Alessandra Chiuderi (J.R.C. Italy) ** MODIFIED BY: Brandt Tso ** ** This programme uses the Kohonen Self Organizing Map ** (Euclidean distance) and the LVQ1 algorithm ** to achieve image classification (unsupervised & supervised) ** The program may generate 4 different results: ** 1) Clusters after Kohonen's training ** 2) Clusters after LVQ training ** 3) Classification (clusters are labelled) ** 4) Confusion matrix ** */ #include #include #include #include #include #include #include #define MAX 200000000 #define BIG 300000000 #define TRUE 1 #define FALSE 0 FILE *fp_r ; /* Image file pointer */ FILE *fp_w_k ; /* Kohonen's map clustered image file pointer*/ FILE *fp_w_g ; /*LVQ clustered image file pointer*/ FILE *fp_w_cl; /* Classified image file pointer*/ FILE *fp_c; /* pixel counts file pointer */ /*cluster mean and cluster unit number pointer*/ FILE *fp_cls_mean; FILE *fp_cls_num; /*output cluster sequrntial file for each band*/ FILE *f1, *f2, *f3, *f4, *f5, *f6, *f7; /* Training data file pointers */ FILE *fg ; FILE *fpt, *fpcx; unsigned char *buf_i ; /* Array containing 1 image line */ unsigned char *y ; /* array containing 1 output clustered image line */ unsigned char *g ; /* array containing 1 output classified image line */ double *x ; /* input vector for the SOM */ double **w ; /* weights matrix (SOM)*/ double **v; /* weights matrix (LVQ) */ double **cls_mean; /*clsters' mean*/ double *bb; int *label, correct =0, incorrect=0; int r,num, dimnk ,l_righe, s_time; long ltime; char store[10],s0[2],s1[2],s2[2],s3[2],s4[2],s5[2],s6[2],s7[2]; char testname[50]; char cname[50]; char koutname[50]; char goutname[50]; char clname[80]; char nomefile[80] ; char gname[70]; /* training set file name*/ char savename[50]; /* file containing network's weights */ char fname[50] ; /* File containing the the following network's fundamental parameters: */ int lines; /* number of image lines */ int pixels; /* number of image pixel */ int nbande; /* number of input nodes*/ int ncols; /* number of units per row in the SOM */ int nrows; /* number of rows in the SOM */ int nuscite; /* number of classes */ int *cl_kcount; /* Number of pixel per cluster (SOM) */ int *cl_gcount; /* Numero of pixel per cluster (LVQ) */ int *cl_cl_count; /* Number of pixel per class */ int nkiter=0 ; /* Number of iter durineg previous training sessions */ int ngiter =0; int np_kiter; int np_giter; double alfa; /* Learning parameters */ double gamma1 ; int nb_r=0; int num_train_cell = 0; /*number of training blocks in training set (for LVQ)*/ /*Header I_param, *O_param;*/ /* Procedures employed */ int BEST_UNIT_CHOICE () ; void WEIGHTS_COMPUTE () ; void K_WEIGHTS_INITIALIZE () ; void MEMORY_ALLOCATION () ; void FREE_ALL(); void TRAIN_NET1 () ; void SAVE_TRAINING () ; void READ_TRAINING () ; void READ_IMAGE(); void PARAM_COMPUTE () ; void START_AGAIN(); int PIXEL_COUNT (); double GETRANDOM(); int ROOF(); void SHOW_MAP(); void LABEL_UNITS(); double DISTANCE(); void LVQ(); void LVQ_WEIGHTS(); void CONFUSION_MATRIX(); main() { int p, q,ntot ; int i,j, count1; int choice,goon ; int banda, dif ; /***********************/ int BestUnit ; double dist, BestDist ; BestUnit = 0 ; BestDist = BIG; /***************************/ printf (" - COUNTERPROPAGATION NEURAL NETWORK -\n") ; printf (" *************************************\n\n\n") ; printf ("\n") ; printf (" 1) Start a new training session (Kohonen layer)\n") ; printf (" 2) Improve previous training (Kohonen layer)\n"); printf (" 3) Run the supervised LVQ1 algorithm for the first time\n") ; printf (" 4) Improve previous LVQ1 training\n"); printf (" 5) Test the network (Kohonen layer)\n") ; printf (" 6) Test the network (Supervised layer)\n") ; printf ("Enter your choice : ") ; scanf ("%d",&choice) ; ltime=time(NULL); s_time = (unsigned) ltime/100; switch (choice) { case 1 : TRAIN_NET1 (1) ; fclose(fp_r); break ; case 2: printf("- Enter filename of previous training session: "); scanf("%s",fname); READ_TRAINING (1); TRAIN_NET1(2); fclose(fp_r); break; case 3: printf("- Enter filename containing Kohonen's layer weights: "); scanf("%s", fname); READ_TRAINING(2); TRAIN_NET1(3); break; case 4: printf("- Enter filename of previous training session: "); scanf("%s", fname); READ_TRAINING(3); TRAIN_NET1(4); break; case 5: printf("- Enter filename of trained network : "); scanf("%s",fname); READ_TRAINING(1); goon=0; goto l2; break; case 6: printf("- Enter filename of trained network : "); scanf("%s",fname); READ_TRAINING(3); break; default: printf("Bad choice !\n"); exit(1); } /* end switch */ printf ("\n\n START OF TEST\n") ; printf ("\n\n") ; printf (" 0) Kohonen map\n") ; printf (" 1) Both layers(Kohonen and LVQ no labelling)\n") ; printf (" 2) Supervised layer only\n") ; printf (" 3) Test set (confusion matrix generation)\n") ; printf (" 4) No testing\n"); printf ("Enter your choice : ") ; scanf ("%d",&goon) ; l2: if (goon!=4) { if ((goon!=3)) { READ_IMAGE(); /*O_param = &I_param; O_param->nbands=1; O_param->nclass=dimnk;*/ for(i=0;inclass=nuscite; VaxErdasWriteHeader(fp_w_cl,O_param);*/ } /* end if goon==2 */ dif = nbande -1; for (i=0;i=nrows)?(nrows-1-(u/ncols)):nb_r; startj=((u%ncols)=ncols)?(ncols-1-(u%ncols)): nb_r; for(i=starti;i<=stopi;i++) { prod = u+i*ncols ; for (j=startj; j<=stopj; j++) { for (k=0;k 255) t1 = t1%255; w[j][i] =(double) t1; /* w[j][i] = ran3(&idum)*255.0; noise = GETRANDOM(0,255) ; w[j][i] = noise; */ } } /* printf("Initial values \n"); for(i=0;i: "); /*period of times for saving feature map weight*/ scanf("%d", &n_save); /* SOM training */ ngb =1; dif = nbande-1 ; cont=np_kiter; nc_count=0; for (iter=0; itermax_no_change) max_no_change=nc_count; printf("Max n. of iterations without changes: %d\n",max_no_change); goto l1 ; break ; case 2 : break ; } } /* Chiude (if flag==1 || flag==2) */ nkiter=nkiter+np_kiter; printf ("- Enter supervised training file name : "); scanf ("%s",gname) ; /* printf("- Enter number of training block in training set :"); scanf ("%d",&num_train_cell); printf("- Enter number of band"); scanf ("%d",&nbande);*/ if ((fg=fopen(gname,"r")) == NULL) { printf ("\nERROR IN SUPERVISED TRAINING FILE\n") ; exit (1) ; } printf ("- Enter number of iterations : ") ; scanf ("%d",&ngiter) ; /* Supervised LVQ1 training */ printf("\n\nLearning Vector Quantization....\n"); cont=np_giter; if(flag==3) { for(i=0;ihigh) { high=majority[i][j]; best=j; } } if(high) label[i]=best; else label[i]=-1; } for(i=0;i