neuron_kd_switch2.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_switch2.h"
  8. /* -------------------------------------------------------------------------------
  9. Lecture des differrentes Look Up Tables
  10. ------------------------------------------------------------------------------- */
  11. void neuron_lect_LUTswitch2()
  12. {
  13. FILE *fic;
  14. int i,j,poub;
  15. char *ligne=malloc(sizeof(char)*150);
  16. float fpoub;
  17. /* ----- LUTs for Rrs490/Rrs555 >= .85 ------ */
  18. if( (fic=fopen(rsup2_LUT_POIDS,"r")) == NULL) {perror(rsup2_LUT_POIDS); exit(-1);}
  19. fgets(ligne,150,fic);
  20. for(i=0; i<rsup2_NC1; i++)
  21. fscanf(fic,"%d %d %f",&poub,&poub,&rsup2_b1[i]);
  22. fscanf(fic,"%d %d %f",&poub,&poub,&rsup2_b2);
  23. for(j=0; j<rsup2_NE; j++){
  24. for(i=0; i<rsup2_NC1; i++)
  25. fscanf(fic,"%d %d %f",&poub,&poub,&rsup2_w1[j][i]);
  26. }
  27. for(j=0; j<rsup2_NC1; j++)
  28. fscanf(fic,"%d %d %f",&poub,&poub,&rsup2_w2[j]);
  29. fclose(fic);
  30. if( (fic=fopen(rsup2_LUT_MOY,"r")) == NULL) {perror(rsup2_LUT_MOY); exit(-1);}
  31. fscanf(fic,"%f",&fpoub);
  32. for(i=0; i<rsup2_NE-1; i++)
  33. fscanf(fic,"%f",&rsup2_moy[i]);
  34. fscanf(fic,"%f",&fpoub);
  35. fscanf(fic,"%f",&rsup2_moy[rsup2_NE-1]);
  36. fscanf(fic,"%f",&fpoub);
  37. fscanf(fic,"%f",&rsup2_moy[rsup2_NES-1]);
  38. fclose(fic);
  39. if( (fic=fopen(rsup2_LUT_ECART,"r")) == NULL) {perror(rsup2_LUT_ECART); exit(-1);}
  40. fscanf(fic,"%f",&fpoub);
  41. for(i=0; i<rsup2_NE-1; i++)
  42. fscanf(fic,"%f",&rsup2_ecart[i]);
  43. fscanf(fic,"%f",&fpoub);
  44. fscanf(fic,"%f",&rsup2_ecart[rsup2_NE-1]);
  45. fscanf(fic,"%f",&fpoub);
  46. fscanf(fic,"%f",&rsup2_ecart[rsup2_NES-1]);
  47. fclose(fic);
  48. /* ----- LUTs for Rrs490/Rrs555 < .85 ------ */
  49. if( (fic=fopen(rinf2_LUT_POIDS,"r")) == NULL) {perror(rinf2_LUT_POIDS); exit(-1);}
  50. fgets(ligne,150,fic);
  51. for(i=0; i<rinf2_NC1; i++)
  52. fscanf(fic,"%d %d %f",&poub,&poub,&rinf2_b1[i]);
  53. fscanf(fic,"%d %d %f",&poub,&poub,&rinf2_b2);
  54. for(j=0; j<rinf2_NE; j++){
  55. for(i=0; i<rinf2_NC1; i++)
  56. fscanf(fic,"%d %d %f",&poub,&poub,&rinf2_w1[j][i]);
  57. }
  58. for(j=0; j<rinf2_NC1; j++)
  59. fscanf(fic,"%d %d %f",&poub,&poub,&rinf2_w2[j]);
  60. fclose(fic);
  61. if( (fic=fopen(rinf2_LUT_MOY,"r")) == NULL) {perror(rinf2_LUT_MOY); exit(-1);}
  62. fscanf(fic,"%f",&fpoub);
  63. for(i=0; i<rinf2_NE; i++)
  64. fscanf(fic,"%f",&rinf2_moy[i]);
  65. fscanf(fic,"%f",&fpoub);
  66. fscanf(fic,"%f",&rinf2_moy[rinf2_NES-1]);
  67. fclose(fic);
  68. if( (fic=fopen(rinf2_LUT_ECART,"r")) == NULL) {perror(rinf2_LUT_ECART); exit(-1);}
  69. fscanf(fic,"%f",&fpoub);
  70. for(i=0; i<rinf2_NE; i++)
  71. fscanf(fic,"%f",&rinf2_ecart[i]);
  72. fscanf(fic,"%f",&fpoub);
  73. fscanf(fic,"%f",&rinf2_ecart[rinf2_NES-1]);
  74. fclose(fic);
  75. }
  76. /* -------------------------------------------------------------------------------
  77. Calcul du Kd a partir des poids
  78. - Input:
  79. input[NE] = Rrs 443 490 510 555 Lambda
  80. ------------------------------------------------------------------------------- */
  81. float rsup2_neuron_passe_avant(float input[rsup2_NE])
  82. {
  83. float a[rsup2_NC1], y=0.0, x[rsup2_NE];
  84. int i,j;
  85. /* Normalisation */
  86. for(i=0; i<rsup2_NE; i++){
  87. x[i] = ((2./3.)*(input[i]-rsup2_moy[i]))/rsup2_ecart[i];
  88. }
  89. for(i=0;i<rsup2_NC1;i++){
  90. a[i] = 0.0;
  91. for(j=0;j<rsup2_NE;j++){
  92. a[i] += (x[j]*rsup2_w1[j][i]);
  93. }
  94. a[i] = 1.715905*(float)tanh((2./3.)*(double)(a[i] + rsup2_b1[i]));
  95. }
  96. for(j=0;j<rsup2_NC1;j++){
  97. y += (a[j]*rsup2_w2[j]);
  98. }
  99. /* Denormalisation */
  100. y = 1.5*(y + rsup2_b2)*rsup2_ecart[rsup2_NES-1] + rsup2_moy[rsup2_NES-1];
  101. y = (float)pow(10.,(double)y);
  102. return(y);
  103. }
  104. /* -------------------------------------------------------------------------------
  105. Calcul du Kd a partir des poids
  106. - Input:
  107. input[NE] = Rrs 443 490 510 555 670 Lambda
  108. ------------------------------------------------------------------------------- */
  109. float rinf2_neuron_passe_avant(float input[rinf2_NE])
  110. {
  111. float a[rinf2_NC1], y=0.0, x[rinf2_NE];
  112. int i,j;
  113. /* Normalisation */
  114. for(i=0; i<rinf2_NE; i++){
  115. x[i] = ((2./3.)*(input[i]-rinf2_moy[i]))/rinf2_ecart[i];
  116. }
  117. for(i=0;i<rinf2_NC1;i++){
  118. a[i] = 0.0;
  119. for(j=0;j<rinf2_NE;j++){
  120. a[i] += (x[j]*rinf2_w1[j][i]);
  121. }
  122. a[i] = 1.715905*(float)tanh((2./3.)*(double)(a[i] + rinf2_b1[i]));
  123. }
  124. for(j=0;j<rinf2_NC1;j++){
  125. y += (a[j]*rinf2_w2[j]);
  126. }
  127. /* Denormalisation */
  128. y = 1.5*(y + rinf2_b2)*rinf2_ecart[rinf2_NES-1] + rinf2_moy[rinf2_NES-1];
  129. y = (float)pow(10.,(double)y);
  130. return(y);
  131. }
  132. /*int main (int argc, char *argv[])
  133. {
  134. FILE *fic;
  135. float data_in[rinf2_NE],result_in,result_out,res_norm, sumInf_rms=0., sumInf_rel=0., sumSup_rms=0., sumSup_rel=0.;
  136. int i,nb,lu;
  137. if( (fic=fopen("../LUTS/base_KdSeaWiFS_IOCCG_NOMAD_BOUM_ss_12_COASTCOLOUR_412_to_670_log_Kd_lambda_merge_seuil_15_publi_091213_test.dat","r")) == NULL) {perror("input"); exit(-1);}
  138. neuron_lect_LUTs();
  139. nb = 0;
  140. while((lu=fscanf(fic,"%f",&data_in[0])) == 1){
  141. for(i=1; i<rinf2_NE; i++)
  142. fscanf(fic,"%f",&data_in[i]);
  143. fscanf(fic,"%f",&result_in);
  144. result_out = rinf2_neuron_passe_avant(data_in);
  145. result_in = (float)pow(10.,result_in);
  146. sumInf_rms += (float)pow((double)(result_in-result_out),2.);
  147. sumInf_rel += (float)sqrt((double)(((result_in-result_out)/result_in) * ((result_in-result_out)/result_in)));
  148. printf("%f %f",result_in,result_out);
  149. data_in[5] = data_in[6];
  150. result_out = rsup2_neuron_passe_avant(data_in);
  151. sumSup_rms += (float)pow((double)(result_in-result_out),2.);
  152. sumSup_rel += (float)sqrt((double)(((result_in-result_out)/result_in) * ((result_in-result_out)/result_in)));
  153. printf(" %f\n",result_out);
  154. nb++;
  155. }
  156. printf("rmseInf = %f, rel errInf= %f, rmseSup = %f, rel errSup= %f nb = %d\n",(float)sqrt((double)sumInf_rms/nb),sumInf_rel/nb, (float)sqrt((double)sumSup_rms/nb),sumSup_rel/nb, nb);
  157. fclose(fic);
  158. exit(1);
  159. }*/