flyxtop

Convolution test

Sep 19th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.98 KB | None | 0 0
  1. // written by Chan Kim for simple convolution test
  2.  
  3. /*
  4. layer 2 type Convolution num_bottoms 1 num_tops 1
  5. layer 2 bottom 0 shape "1 64 600 800 (30720000)"
  6. layer 2 top 0 shape = "1 64 600 800 (30720000)"
  7. layer 2 kernel_size = 3
  8. bottom size : 4*64*600*800 = 0x7530000 Bytes
  9. top size : 4*64*600*800 = 0x7530000 Bytes
  10. kernel size : 4*3*3*64*64 = 0x24000 Bytes
  11. in word size, bottom : 0x1d4c000 kernel : 0x9000 Words
  12. */
  13.  
  14. #include <stdio.h>
  15. #define NUM_IFM 64
  16. #define NUM_OFM 64
  17. #define HEIGHT 600
  18. #define WIDTH 800
  19. #define K 3 // 3x3 kernel
  20. float bottom[1][NUM_IFM][HEIGHT][WIDTH];
  21. float top[1][NUM_IFM][HEIGHT][WIDTH];
  22. float weights[NUM_OFM][NUM_IFM][K][K];
  23. float bias[NUM_OFM];
  24. float conv[1][NUM_IFM][HEIGHT][WIDTH]; // result
  25. char str[80];
  26. float kern[K][K];
  27. float in_square[K][K];
  28. float sum;
  29.  
  30.  
  31. #define layer 2 // for test
  32.  
  33. main()
  34. {
  35.  
  36. FILE *file;
  37. int ifm_idx;
  38. int ofm_idx;
  39. int orix;
  40. int ocix;
  41. char c;
  42. int r;
  43. int rix, cix;
  44. char line[80];
  45. char *ll;
  46. float v0, v1, v2, v3, v4, v5, v6, v7;
  47. int kyi, kxi;
  48. int orixm, orixp;
  49. int ocixm, ocixp;
  50. int kx, ky;
  51. int i;
  52.  
  53. // --------------------------------------------------------
  54. // reading blob data from files into blob memory
  55. // --------------------------------------------------------
  56. for(ifm_idx=0;ifm_idx<NUM_IFM;ifm_idx++) {
  57. sprintf(str, "/home/ckim/Neuro/convhw/ext1/L%02d_Convolution_B0_FN%03d.txt", layer, ifm_idx);
  58. file = fopen(str, "r");
  59. c = fgetc(file);
  60. rix = -1;
  61. while (c != EOF) {
  62. if (c == '#') {
  63. ll = fgets(line, 80, file);
  64. c = fgetc(file);
  65. rix++;
  66. cix = 0;
  67. }
  68. else {
  69. r = ungetc(c, file);
  70. r = fscanf(file, "%f %f %f %f %f %f %f %f", &v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7);
  71. bottom[0][ifm_idx][rix][cix++] = v0;
  72. bottom[0][ifm_idx][rix][cix++] = v1;
  73. bottom[0][ifm_idx][rix][cix++] = v2;
  74. bottom[0][ifm_idx][rix][cix++] = v3;
  75. bottom[0][ifm_idx][rix][cix++] = v4;
  76. bottom[0][ifm_idx][rix][cix++] = v5;
  77. bottom[0][ifm_idx][rix][cix++] = v6;
  78. bottom[0][ifm_idx][rix][cix++] = v7;
  79. c = fgetc(file);
  80. }
  81. }
  82. printf("file %s read.. \n",str);
  83. fclose(file);
  84. }
  85.  
  86. //// print input blob ifm 37 for test
  87. //FILE *fff = fopen("ttt.txt","w");
  88. //for (rix = 0; rix < HEIGHT; rix++) {
  89. // fprintf(fff,"### kr = %d ##\n",rix);
  90. // for (cix = 0; cix < WIDTH; cix++) {
  91. // fprintf(fff,"%f ",bottom[0][37][rix][cix]);
  92. // if (cix % 8 == 7) fprintf(fff,"\n");
  93. // }
  94. //}
  95. //fclose(fff);
  96.  
  97. // --------------------------------------------------------
  98. // reading weights from files into blob memory
  99. // --------------------------------------------------------
  100. sprintf(str, "/home/ckim/Neuro/convhw/ext1/L%02d_Convolution_Weights.txt", layer);
  101. file = fopen(str, "r");
  102. ll = fgets(line, 80, file); // read the layer config line
  103. for(ofm_idx = 0; ofm_idx < NUM_OFM; ofm_idx++) {
  104. //for(ifm_idx = 0; ifm_idx < NUM_IFM; ifm_idx++) {
  105. ll = fgets(line, 80, file); // read the line '## For output map ofm_idx'
  106. for(ifm_idx = 0; ifm_idx < NUM_IFM; ifm_idx++) {
  107. //for(ofm_idx = 0; ofm_idx < NUM_OFM; ofm_idx++) {
  108. ll = fgets(line, 80, file); // read the line '## for input map ifm_idx'
  109. for(kyi = 0; kyi < K; kyi++) {
  110. fscanf(file, "%f %f %f", &v0, &v1, &v2); // K=3 always
  111. weights[ofm_idx][ifm_idx][kyi][0] = v0;
  112. weights[ofm_idx][ifm_idx][kyi][1] = v1;
  113. weights[ofm_idx][ifm_idx][kyi][2] = v2;
  114. ll = fgets(line, 80, file); // read off remaining line
  115. }
  116. }
  117. // read bias values
  118. }
  119. for(i = 0, ofm_idx = 0; i < NUM_OFM/8; i++){
  120. ll = fgets(line, 80, file);
  121. r = fscanf(file, "%f %f %f %f %f %f %f %f", &v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7);
  122. bias[ofm_idx++] = v0;
  123. bias[ofm_idx++] = v1;
  124. bias[ofm_idx++] = v2;
  125. bias[ofm_idx++] = v3;
  126. bias[ofm_idx++] = v4;
  127. bias[ofm_idx++] = v5;
  128. bias[ofm_idx++] = v6;
  129. bias[ofm_idx++] = v7;
  130. }
  131. fclose(file);
  132.  
  133. //printf("weights ofm 37, ifm 36,\n");
  134. //for(kyi = 0; kyi < K; kyi++) {
  135. // for(kxi = 0; kxi < K; kxi++) {
  136. // printf("%f ",weights[37][36][kyi][kxi]);
  137. // }
  138. // printf("\n");
  139. //}
  140. //printf("bias of ofm 37 \n");
  141. // printf("%f\n", bias[37]);
  142. //getchar();
  143.  
  144. #ifdef COMPARE_OUTPUT // do not use yet
  145. // --------------------------------------------------------
  146. // reading output data blob for comparison
  147. // --------------------------------------------------------
  148. for(ofm_idx=0;ofm_idx<NUM_OFM;ofm_idx++) {
  149. sprintf(str, "/home/ckim/Neuro/convhw/ext1/L%02d_Convolution_T0_FN%03d.txt", layer, ofm_idx);
  150. file = fopen(str, "r");
  151. c = fgetc(file);
  152. rix = -1;
  153. while (c != EOF) {
  154. if (c == '#') {
  155. ll = fgets(line, 80, file);
  156. c = fgetc(file);
  157. rix++;
  158. cix = 0;
  159. }
  160. else {
  161. r = ungetc(c, file);
  162. r = fscanf(file, "%f %f %f %f %f %f %f %f", &v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7);
  163. top[0][ofm_idx][rix][cix++] = v0;
  164. top[0][ofm_idx][rix][cix++] = v1;
  165. top[0][ofm_idx][rix][cix++] = v2;
  166. top[0][ofm_idx][rix][cix++] = v3;
  167. top[0][ofm_idx][rix][cix++] = v4;
  168. top[0][ofm_idx][rix][cix++] = v5;
  169. top[0][ofm_idx][rix][cix++] = v6;
  170. top[0][ofm_idx][rix][cix++] = v7;
  171. c = fgetc(file);
  172. }
  173. }
  174. printf("file %s read..\n",str);
  175. fclose(file);
  176. }
  177. #endif
  178.  
  179. // --------------------------------------------------------
  180. // perform convolution
  181. // --------------------------------------------------------
  182. printf("starting convolution..\n");
  183. for(ofm_idx = 0; ofm_idx < NUM_OFM; ofm_idx++) {
  184. printf("making OFM %d\n", ofm_idx);
  185. for(orix = 0; orix < HEIGHT; orix++) {
  186. for(ocix = 0; ocix < WIDTH; ocix++) {
  187.  
  188. sum = 0.; // for single point
  189.  
  190. // for each input maps
  191. for(ifm_idx = 0; ifm_idx < NUM_IFM; ifm_idx++) {
  192.  
  193. // get kernel
  194. kern[0][0] = weights[ofm_idx][ifm_idx][0][0];
  195. kern[0][1] = weights[ofm_idx][ifm_idx][0][1];
  196. kern[0][2] = weights[ofm_idx][ifm_idx][0][2];
  197. kern[1][0] = weights[ofm_idx][ifm_idx][1][0];
  198. kern[1][1] = weights[ofm_idx][ifm_idx][1][1];
  199. kern[1][2] = weights[ofm_idx][ifm_idx][1][2];
  200. kern[2][0] = weights[ofm_idx][ifm_idx][2][0];
  201. kern[2][1] = weights[ofm_idx][ifm_idx][2][1];
  202. kern[2][2] = weights[ofm_idx][ifm_idx][2][2];
  203.  
  204. // nearest-value padding (not used in caffe)
  205. // get //input (including padding)
  206. //orixm = (orix == 0) ? orix : orix-1;
  207. //orixp = (orix == HEIGHT-1) ? orix : orix+1;
  208. //ocixm = (ocix == 0) ? ocix : ocix-1;
  209. //ocixp = (ocix == WIDTH-1) ? ocix : ocix+1;
  210. //in_square[0][0] = bottom[0][ifm_idx][orixm][ocixm];
  211. //in_square[0][1] = bottom[0][ifm_idx][orixm][ocix];
  212. //in_square[0][2] = bottom[0][ifm_idx][orixm][ocixp];
  213. //in_square[1][0] = bottom[0][ifm_idx][orix][ocixm];
  214. //in_square[1][1] = bottom[0][ifm_idx][orix][ocix];
  215. //in_square[1][2] = bottom[0][ifm_idx][orix][ocixp];
  216. //in_square[2][0] = bottom[0][ifm_idx][orixp][ocixm];
  217. //in_square[2][1] = bottom[0][ifm_idx][orixp][ocix];
  218. //in_square[2][2] = bottom[0][ifm_idx][orixp][ocixp];
  219.  
  220. // zero-value padding (used in caffe)
  221. in_square[0][0] = (orix == 0 ||ocix == 0) ? 0 : bottom[0][ifm_idx][orix][ocix];
  222. in_square[0][1] = (orix == 0) ? 0 : bottom[0][ifm_idx][orix][ocix];
  223. in_square[0][2] = (orix == 0 || ocix == WIDTH-1) ? 0 : bottom[0][ifm_idx][orix][ocix];
  224. in_square[1][0] = (ocix == 0) ? 0 : bottom[0][ifm_idx][orix][ocix];
  225. in_square[1][1] = bottom[0][ifm_idx][orix][ocix];
  226. in_square[1][2] = (ocix == WIDTH-1) ? 0 : bottom[0][ifm_idx][orix][ocix];
  227. in_square[2][0] = (orix == HEIGHT-1 || ocix == 0) ? 0 : bottom[0][ifm_idx][orix][ocix];
  228. in_square[2][1] = (orix == HEIGHT-1) ? 0 : bottom[0][ifm_idx][orix][ocix];
  229. in_square[2][2] = (orix == HEIGHT-1 || ocix == WIDTH-1) ? 0 : bottom[0][ifm_idx][orix][ocix];
  230.  
  231. // apply kernel
  232. for(ky = 0; ky < K; ky++) {
  233. for(kx = 0; kx < K; kx++) {
  234. sum += in_square[ky][kx]*kern[ky][kx];
  235. }
  236. }
  237.  
  238. //// add bias
  239. //sum += bias[ifm_idx];
  240. } // ifm_idx
  241.  
  242. // add bias
  243. sum += bias[ofm_idx];
  244. // store result
  245. conv[0][ofm_idx][orix][ocix] = sum;
  246. } // ocix
  247. } // orix
  248.  
  249. printf("OFM %d\n", ofm_idx);
  250. sprintf(str, "./result/L%02d_Convolution_T0_FN%03d.txt", layer, ofm_idx);
  251. file = fopen(str, "w");
  252. printf("writing convolution result to file %s..\n", str);
  253. for (rix = 0; rix < HEIGHT; rix++) {
  254. fprintf(file, "### kr = %d ##\n", rix);
  255. for (cix = 0; cix < WIDTH; cix++) {
  256. fprintf(file, "%f ",conv[0][ofm_idx][rix][cix]);
  257. if (cix %8 == 7) fprintf(file, "\n");
  258. }
  259. }
  260. fclose(file);
  261. }
  262. printf("Convolution finished\n");
  263.  
  264. //// ----------------------------------------
  265. //// Write convolution result to file
  266. //// ----------------------------------------
  267. //printf("writing result to files..\n");
  268. //for(ofm_idx=0;ofm_idx<NUM_OFM;ofm_idx++) {
  269. // printf("OFM %d\n", ofm_idx);
  270. // sprintf(str, "./result/L%02d_Convolution_T0_FN%03d.txt", layer, ofm_idx);
  271. // file = fopen(str, "w");
  272. // for (rix = 0; rix < HEIGHT; rix++) {
  273. // fprintf(file, "### kr = %d ##\n", rix);
  274. // for (cix = 0; cix < WIDTH; cix++) {
  275. // fprintf(file, "%f ",conv[0][ofm_idx][rix][cix]);
  276. // if (cix %8 == 7) fprintf(file, "\n");
  277. // }
  278. // }
  279. // fclose(file);
  280. // printf("covolution result written to file %s\n", str);
  281. //}
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment