neuron_kd_switch.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_switch.h"
  8. /* -------------------------------------------------------------------------------
  9. Lecture des differrentes Look Up Tables
  10. ------------------------------------------------------------------------------- */
  11. void neuron_lect_LUTswitch()
  12. {
  13. FILE *fic;
  14. int i,j,poub;
  15. char *ligne=malloc(sizeof(char)*150);
  16. float fpoub;
  17. /* ----- LUTs for Rrs490/Rrs555 > 1.5 ------ */
  18. if( (fic=fopen(rsup_LUT_POIDS,"r")) == NULL) {perror(rsup_LUT_POIDS); exit(-1);}
  19. fgets(ligne,150,fic);
  20. for(i=0; i<rsup_NC1; i++)
  21. fscanf(fic,"%d %d %f",&poub,&poub,&rsup_b1[i]);
  22. for(i=0; i<rsup_NC2; i++)
  23. fscanf(fic,"%d %d %f",&poub,&poub,&rsup_b2[i]);
  24. fscanf(fic,"%d %d %f",&poub,&poub,&rsup_b3);
  25. for(j=0; j<rsup_NE; j++){
  26. for(i=0; i<rsup_NC1; i++)
  27. fscanf(fic,"%d %d %f",&poub,&poub,&rsup_w1[j][i]);
  28. }
  29. for(j=0; j<rsup_NC1; j++){
  30. for(i=0; i<rsup_NC2; i++)
  31. fscanf(fic,"%d %d %f",&poub,&poub,&rsup_w2[j][i]);
  32. }
  33. for(i=0; i<rsup_NC2; i++)
  34. fscanf(fic,"%d %d %f",&poub,&poub,&rsup_w3[i]);
  35. fclose(fic);
  36. if( (fic=fopen(rsup_LUT_MOY,"r")) == NULL) {perror(rsup_LUT_MOY); exit(-1);}
  37. for(i=0; i<rsup_NE-1; i++)
  38. fscanf(fic,"%f",&rsup_moy[i]);
  39. fscanf(fic,"%f",&fpoub);
  40. fscanf(fic,"%f",&rsup_moy[rsup_NES-2]);
  41. fscanf(fic,"%f",&fpoub);
  42. fscanf(fic,"%f",&rsup_moy[rsup_NES-1]);
  43. fclose(fic);
  44. if( (fic=fopen(rsup_LUT_ECART,"r")) == NULL) {perror(rsup_LUT_ECART); exit(-1);}
  45. for(i=0; i<rsup_NE-1; i++)
  46. fscanf(fic,"%f",&rsup_ecart[i]);
  47. fscanf(fic,"%f",&fpoub);
  48. fscanf(fic,"%f",&rsup_ecart[rsup_NES-2]);
  49. fscanf(fic,"%f",&fpoub);
  50. fscanf(fic,"%f",&rsup_ecart[rsup_NES-1]);
  51. fclose(fic);
  52. /* ----- LUTs for Rrs490/Rrs555 <= 1.5 ------ */
  53. if( (fic=fopen(rinf_LUT_POIDS,"r")) == NULL) {perror(rinf_LUT_POIDS); exit(-1);}
  54. fgets(ligne,150,fic);
  55. for(i=0; i<rinf_NC1; i++)
  56. fscanf(fic,"%d %d %f",&poub,&poub,&rinf_b1[i]);
  57. for(i=0; i<rinf_NC2; i++)
  58. fscanf(fic,"%d %d %f",&poub,&poub,&rinf_b2[i]);
  59. fscanf(fic,"%d %d %f",&poub,&poub,&rinf_b3);
  60. for(j=0; j<rinf_NE; j++){
  61. for(i=0; i<rinf_NC1; i++)
  62. fscanf(fic,"%d %d %f",&poub,&poub,&rinf_w1[j][i]);
  63. }
  64. for(j=0; j<rinf_NC1; j++){
  65. for(i=0; i<rinf_NC2; i++)
  66. fscanf(fic,"%d %d %f",&poub,&poub,&rinf_w2[j][i]);
  67. }
  68. for(i=0; i<rinf_NC2; i++)
  69. fscanf(fic,"%d %d %f",&poub,&poub,&rinf_w3[i]);
  70. fclose(fic);
  71. if( (fic=fopen(rinf_LUT_MOY,"r")) == NULL) {perror(rinf_LUT_MOY); exit(-1);}
  72. for(i=0; i<rinf_NES-1; i++)
  73. fscanf(fic,"%f",&rinf_moy[i]);
  74. fscanf(fic,"%f",&fpoub);
  75. fscanf(fic,"%f",&rinf_moy[rinf_NES-1]);
  76. fclose(fic);
  77. if( (fic=fopen(rinf_LUT_ECART,"r")) == NULL) {perror(rinf_LUT_ECART); exit(-1);}
  78. for(i=0; i<rinf_NES-1; i++)
  79. fscanf(fic,"%f",&rinf_ecart[i]);
  80. fscanf(fic,"%f",&fpoub);
  81. fscanf(fic,"%f",&rinf_ecart[rinf_NES-1]);
  82. fclose(fic);
  83. }
  84. /* -------------------------------------------------------------------------------
  85. Calcul du Kd a partir des poids
  86. - Input:
  87. input[NE] = Rrs 412 443 490 510 555 Lambda
  88. ------------------------------------------------------------------------------- */
  89. float rsup_neuron_passe_avant(float input[rsup_NE+1])
  90. {
  91. float a[rsup_NC1], b[rsup_NC2], y=0.0, x[rsup_NE];
  92. int i,j;
  93. /* Normalisation */
  94. for(i=0; i<rsup_NE; i++){
  95. x[i] = ((2./3.)*(input[i]-rsup_moy[i]))/rsup_ecart[i];
  96. }
  97. for(i=0;i<rsup_NC1;i++){
  98. a[i] = 0.0;
  99. for(j=0;j<rsup_NE;j++){
  100. a[i] += (x[j]*rsup_w1[j][i]);
  101. }
  102. a[i] = 1.715905*(float)tanh((2./3.)*(double)(a[i] + rsup_b1[i]));
  103. }
  104. for(i=0;i<rsup_NC2;i++){
  105. b[i] = 0.0;
  106. for(j=0;j<rsup_NC1;j++){
  107. b[i] += (a[j]*rsup_w2[j][i]);
  108. }
  109. b[i] = 1.715905*(float)tanh((2./3.)*(double)(b[i] + rsup_b2[i]));
  110. }
  111. for(j=0;j<rsup_NC2;j++){
  112. y += (b[j]*rsup_w3[j]);
  113. }
  114. /* Denormalisation */
  115. y = 1.5*(y + rsup_b3)*rsup_ecart[rsup_NES-1] + rsup_moy[rsup_NES-1];
  116. y = (float)pow(10.,y);
  117. return(y);
  118. }
  119. /* -------------------------------------------------------------------------------
  120. Calcul du Kd a partir des poids
  121. - Input:
  122. input[NE] = Rrs 412 443 490 510 555 670 Lambda
  123. ------------------------------------------------------------------------------- */
  124. float rinf_neuron_passe_avant(float input[rinf_NE])
  125. {
  126. float a[rinf_NC1], b[rinf_NC2], y=0.0, x[rinf_NE];
  127. int i,j;
  128. /* Normalisation */
  129. for(i=0; i<rinf_NE; i++){
  130. x[i] = ((2./3.)*(input[i]-rinf_moy[i]))/rinf_ecart[i];
  131. }
  132. for(i=0;i<rinf_NC1;i++){
  133. a[i] = 0.0;
  134. for(j=0;j<rinf_NE;j++){
  135. a[i] += (x[j]*rinf_w1[j][i]);
  136. }
  137. a[i] = 1.715905*(float)tanh((2./3.)*(double)(a[i] + rinf_b1[i]));
  138. }
  139. for(i=0;i<rinf_NC2;i++){
  140. b[i] = 0.0;
  141. for(j=0;j<rinf_NC1;j++){
  142. b[i] += (a[j]*rinf_w2[j][i]);
  143. }
  144. b[i] = 1.715905*(float)tanh((2./3.)*(double)(b[i] + rinf_b2[i]));
  145. }
  146. for(j=0;j<rinf_NC2;j++){
  147. y += (b[j]*rinf_w3[j]);
  148. }
  149. /* Denormalisation */
  150. y = 1.5*(y + rinf_b3)*rinf_ecart[rinf_NES-1] + rinf_moy[rinf_NES-1];
  151. y = (float)pow(10.,y);
  152. return(y);
  153. }
  154. /*int main (int argc, char *argv[])
  155. {
  156. FILE *fic;
  157. float data_in[rinf_NE],result_in,result_out,res_norm, sumInf_rms=0., sumInf_rel=0., sumSup_rms=0., sumSup_rel=0.;
  158. int i,nb,lu;
  159. 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);}
  160. neuron_lect_LUTs();
  161. nb = 0;
  162. while((lu=fscanf(fic,"%f",&data_in[0])) == 1){
  163. for(i=1; i<rinf_NE; i++)
  164. fscanf(fic,"%f",&data_in[i]);
  165. fscanf(fic,"%f",&result_in);
  166. result_out = rinf_neuron_passe_avant(data_in);
  167. result_in = (float)pow(10.,result_in);
  168. sumInf_rms += (float)pow((double)(result_in-result_out),2.);
  169. sumInf_rel += (float)sqrt((double)(((result_in-result_out)/result_in) * ((result_in-result_out)/result_in)));
  170. printf("%f %f",result_in,result_out);
  171. data_in[5] = data_in[6];
  172. result_out = rsup_neuron_passe_avant(data_in);
  173. sumSup_rms += (float)pow((double)(result_in-result_out),2.);
  174. sumSup_rel += (float)sqrt((double)(((result_in-result_out)/result_in) * ((result_in-result_out)/result_in)));
  175. printf(" %f\n",result_out);
  176. nb++;
  177. }
  178. 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);
  179. fclose(fic);
  180. exit(1);
  181. }*/