neuron_difKd_2cc_6x4.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <memory.h>
  7. #include "neuron_difKd2cc_6x4.h"
  8. /* -------------------------------------------------------------------------------
  9. Lecture des differrentes Look Up Tables
  10. ------------------------------------------------------------------------------- */
  11. void neuron_lect_LUTs_dif()
  12. {
  13. FILE *fic;
  14. int i,j,poub;
  15. char *ligne=malloc(sizeof(char)*150);
  16. float fpoub;
  17. if( (fic=fopen(LUT_POIDS,"r")) == NULL) {perror(LUT_POIDS); exit(-1);}
  18. fgets(ligne,150,fic);
  19. for(i=0; i<DNC1; i++)
  20. fscanf(fic,"%d %d %f",&poub,&poub,&b1[i]);
  21. for(i=0; i<DNC2; i++)
  22. fscanf(fic,"%d %d %f",&poub,&poub,&b2[i]);
  23. fscanf(fic,"%d %d %f",&poub,&poub,&b3);
  24. for(j=0; j<DNE; j++){
  25. for(i=0; i<DNC1; i++)
  26. fscanf(fic,"%d %d %f",&poub,&poub,&w1[j][i]);
  27. }
  28. for(j=0; j<DNC1; j++){
  29. for(i=0; i<DNC2; i++)
  30. fscanf(fic,"%d %d %f",&poub,&poub,&w2[j][i]);
  31. }
  32. for(i=0; i<DNC2; i++)
  33. fscanf(fic,"%d %d %f",&poub,&poub,&w3[i]);
  34. fclose(fic);
  35. /* if( (fic=fopen(LUT_MOY,"r")) == NULL) {perror(LUT_MOY); exit(-1);}
  36. fscanf(fic,"%f",&fpoub);
  37. for(i=0; i<DNES; i++)
  38. fscanf(fic,"%f",&moy[i]);
  39. fclose(fic);
  40. if( (fic=fopen(LUT_ECART,"r")) == NULL) {perror(LUT_ECART); exit(-1);}
  41. fscanf(fic,"%f",&fpoub);
  42. for(i=0; i<DNES; i++)
  43. fscanf(fic,"%f",&ecart[i]);
  44. fclose(fic);*/
  45. if( (fic=fopen(LUT_MOY,"r")) == NULL) {perror(LUT_MOY); exit(-1);}
  46. fscanf(fic,"%f",&fpoub);
  47. for(i=0; i<DNE; i++)
  48. fscanf(fic,"%f",&moy[i]);
  49. for(i=0; i<3; i++)
  50. fscanf(fic,"%f",&fpoub);
  51. fscanf(fic,"%f",&moy[DNES-1]);
  52. fclose(fic);
  53. if( (fic=fopen(LUT_ECART,"r")) == NULL) {perror(LUT_ECART); exit(-1);}
  54. fscanf(fic,"%f",&fpoub);
  55. for(i=0; i<DNE; i++)
  56. fscanf(fic,"%f",&ecart[i]);
  57. for(i=0; i<3; i++)
  58. fscanf(fic,"%f",&fpoub);
  59. fscanf(fic,"%f",&ecart[DNES-1]);
  60. fclose(fic);
  61. }
  62. /* -------------------------------------------------------------------------------
  63. Calcul du Kd a partir des poids
  64. - Input:
  65. input[DNE] = Rrs (412) 443 490 510 555 670 ASOL
  66. ------------------------------------------------------------------------------- */
  67. float neuron_passe_avant_dif(float input[DNE])
  68. {
  69. float a[DNC1], b[DNC2], y=0.0, x[DNE];
  70. int i,j;
  71. /* Normalisation */
  72. for(i=0; i<DNE; i++){
  73. x[i] = ((2./3.)*(input[i]-moy[i]))/ecart[i];
  74. }
  75. for(i=0;i<DNC1;i++){
  76. a[i] = 0.0;
  77. for(j=0;j<DNE;j++){
  78. a[i] += (x[j]*w1[j][i]);
  79. }
  80. a[i] = 1.715905*(float)tanh((2./3.)*(double)(a[i] + b1[i]));
  81. }
  82. for(i=0;i<DNC2;i++){
  83. b[i] = 0.0;
  84. for(j=0;j<DNC1;j++){
  85. b[i] += (a[j]*w2[j][i]);
  86. }
  87. b[i] = 1.715905*(float)tanh((2./3.)*(double)(b[i] + b2[i]));
  88. }
  89. for(j=0;j<DNC2;j++){
  90. y += (b[j]*w3[j]);
  91. }
  92. /* Denormalisation */
  93. y = 1.5*(y + b3)*ecart[DNES-1] + moy[DNES-1];
  94. y = (float)pow(10.,y);
  95. return(y);
  96. }
  97. /*
  98. int main (int argc, char *argv[])
  99. {
  100. FILE *fic;
  101. float data_in[DNE],result_in,result_out,res_norm, sum_rms=0., sum_rel=0.;
  102. int i,nb,lu;
  103. if( (fic=fopen("../LUTS/base_KdSeaWiFS_IOCCG_NOMAD_BOUM_ss_12_COASTCOLOUR_443_to_670_Kd_lambda_merge_seuil_15_diff_publi_150114_test.dat","r")) == NULL) {perror("input"); exit(-1);}
  104. neuron_lect_LUTs_dif();
  105. nb = 0;
  106. while((lu=fscanf(fic,"%f",&data_in[0])) == 1){
  107. for(i=1; i<DNE; i++)
  108. fscanf(fic,"%f",&data_in[i]);
  109. fscanf(fic,"%f",&result_in);
  110. res_norm = ((2./3.)*(result_in-moy[DNES-1]))/ecart[DNES-1];
  111. result_out = neuron_passe_avant_dif(data_in);
  112. result_in = 1.5*res_norm*ecart[DNES-1] + moy[DNES-1];
  113. result_in = (float)pow(10.,result_in);
  114. sum_rms += (float)pow((double)(result_in-result_out),2.);
  115. sum_rel += (float)sqrt((double)(((result_in-result_out)/result_in) * ((result_in-result_out)/result_in)));
  116. printf("%f %f\n",result_in,result_out);
  117. nb++;
  118. }
  119. printf("rmse = %f, rel err= %f nb = %d\n",(float)sqrt((double)sum_rms/nb),sum_rel/nb,nb);
  120. fclose(fic);
  121. exit(1);
  122. }
  123. */