Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // written by Chan Kim for simple convolution test
- /*
- layer 2 type Convolution num_bottoms 1 num_tops 1
- layer 2 bottom 0 shape "1 64 600 800 (30720000)"
- layer 2 top 0 shape = "1 64 600 800 (30720000)"
- layer 2 kernel_size = 3
- bottom size : 4*64*600*800 = 0x7530000 Bytes
- top size : 4*64*600*800 = 0x7530000 Bytes
- kernel size : 4*3*3*64*64 = 0x24000 Bytes
- in word size, bottom : 0x1d4c000 kernel : 0x9000 Words
- */
- #include <stdio.h>
- #define NUM_IFM 64
- #define NUM_OFM 64
- #define HEIGHT 600
- #define WIDTH 800
- #define K 3 // 3x3 kernel
- float bottom[1][NUM_IFM][HEIGHT][WIDTH];
- float top[1][NUM_IFM][HEIGHT][WIDTH];
- float weights[NUM_OFM][NUM_IFM][K][K];
- float bias[NUM_OFM];
- float conv[1][NUM_IFM][HEIGHT][WIDTH]; // result
- char str[80];
- float kern[K][K];
- float in_square[K][K];
- float sum;
- #define layer 2 // for test
- main()
- {
- FILE *file;
- int ifm_idx;
- int ofm_idx;
- int orix;
- int ocix;
- char c;
- int r;
- int rix, cix;
- char line[80];
- char *ll;
- float v0, v1, v2, v3, v4, v5, v6, v7;
- int kyi, kxi;
- int orixm, orixp;
- int ocixm, ocixp;
- int kx, ky;
- int i;
- // --------------------------------------------------------
- // reading blob data from files into blob memory
- // --------------------------------------------------------
- for(ifm_idx=0;ifm_idx<NUM_IFM;ifm_idx++) {
- sprintf(str, "/home/ckim/Neuro/convhw/ext1/L%02d_Convolution_B0_FN%03d.txt", layer, ifm_idx);
- file = fopen(str, "r");
- c = fgetc(file);
- rix = -1;
- while (c != EOF) {
- if (c == '#') {
- ll = fgets(line, 80, file);
- c = fgetc(file);
- rix++;
- cix = 0;
- }
- else {
- r = ungetc(c, file);
- r = fscanf(file, "%f %f %f %f %f %f %f %f", &v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7);
- bottom[0][ifm_idx][rix][cix++] = v0;
- bottom[0][ifm_idx][rix][cix++] = v1;
- bottom[0][ifm_idx][rix][cix++] = v2;
- bottom[0][ifm_idx][rix][cix++] = v3;
- bottom[0][ifm_idx][rix][cix++] = v4;
- bottom[0][ifm_idx][rix][cix++] = v5;
- bottom[0][ifm_idx][rix][cix++] = v6;
- bottom[0][ifm_idx][rix][cix++] = v7;
- c = fgetc(file);
- }
- }
- printf("file %s read.. \n",str);
- fclose(file);
- }
- //// print input blob ifm 37 for test
- //FILE *fff = fopen("ttt.txt","w");
- //for (rix = 0; rix < HEIGHT; rix++) {
- // fprintf(fff,"### kr = %d ##\n",rix);
- // for (cix = 0; cix < WIDTH; cix++) {
- // fprintf(fff,"%f ",bottom[0][37][rix][cix]);
- // if (cix % 8 == 7) fprintf(fff,"\n");
- // }
- //}
- //fclose(fff);
- // --------------------------------------------------------
- // reading weights from files into blob memory
- // --------------------------------------------------------
- sprintf(str, "/home/ckim/Neuro/convhw/ext1/L%02d_Convolution_Weights.txt", layer);
- file = fopen(str, "r");
- ll = fgets(line, 80, file); // read the layer config line
- for(ofm_idx = 0; ofm_idx < NUM_OFM; ofm_idx++) {
- //for(ifm_idx = 0; ifm_idx < NUM_IFM; ifm_idx++) {
- ll = fgets(line, 80, file); // read the line '## For output map ofm_idx'
- for(ifm_idx = 0; ifm_idx < NUM_IFM; ifm_idx++) {
- //for(ofm_idx = 0; ofm_idx < NUM_OFM; ofm_idx++) {
- ll = fgets(line, 80, file); // read the line '## for input map ifm_idx'
- for(kyi = 0; kyi < K; kyi++) {
- fscanf(file, "%f %f %f", &v0, &v1, &v2); // K=3 always
- weights[ofm_idx][ifm_idx][kyi][0] = v0;
- weights[ofm_idx][ifm_idx][kyi][1] = v1;
- weights[ofm_idx][ifm_idx][kyi][2] = v2;
- ll = fgets(line, 80, file); // read off remaining line
- }
- }
- // read bias values
- }
- for(i = 0, ofm_idx = 0; i < NUM_OFM/8; i++){
- ll = fgets(line, 80, file);
- r = fscanf(file, "%f %f %f %f %f %f %f %f", &v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7);
- bias[ofm_idx++] = v0;
- bias[ofm_idx++] = v1;
- bias[ofm_idx++] = v2;
- bias[ofm_idx++] = v3;
- bias[ofm_idx++] = v4;
- bias[ofm_idx++] = v5;
- bias[ofm_idx++] = v6;
- bias[ofm_idx++] = v7;
- }
- fclose(file);
- //printf("weights ofm 37, ifm 36,\n");
- //for(kyi = 0; kyi < K; kyi++) {
- // for(kxi = 0; kxi < K; kxi++) {
- // printf("%f ",weights[37][36][kyi][kxi]);
- // }
- // printf("\n");
- //}
- //printf("bias of ofm 37 \n");
- // printf("%f\n", bias[37]);
- //getchar();
- #ifdef COMPARE_OUTPUT // do not use yet
- // --------------------------------------------------------
- // reading output data blob for comparison
- // --------------------------------------------------------
- for(ofm_idx=0;ofm_idx<NUM_OFM;ofm_idx++) {
- sprintf(str, "/home/ckim/Neuro/convhw/ext1/L%02d_Convolution_T0_FN%03d.txt", layer, ofm_idx);
- file = fopen(str, "r");
- c = fgetc(file);
- rix = -1;
- while (c != EOF) {
- if (c == '#') {
- ll = fgets(line, 80, file);
- c = fgetc(file);
- rix++;
- cix = 0;
- }
- else {
- r = ungetc(c, file);
- r = fscanf(file, "%f %f %f %f %f %f %f %f", &v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7);
- top[0][ofm_idx][rix][cix++] = v0;
- top[0][ofm_idx][rix][cix++] = v1;
- top[0][ofm_idx][rix][cix++] = v2;
- top[0][ofm_idx][rix][cix++] = v3;
- top[0][ofm_idx][rix][cix++] = v4;
- top[0][ofm_idx][rix][cix++] = v5;
- top[0][ofm_idx][rix][cix++] = v6;
- top[0][ofm_idx][rix][cix++] = v7;
- c = fgetc(file);
- }
- }
- printf("file %s read..\n",str);
- fclose(file);
- }
- #endif
- // --------------------------------------------------------
- // perform convolution
- // --------------------------------------------------------
- printf("starting convolution..\n");
- for(ofm_idx = 0; ofm_idx < NUM_OFM; ofm_idx++) {
- printf("making OFM %d\n", ofm_idx);
- for(orix = 0; orix < HEIGHT; orix++) {
- for(ocix = 0; ocix < WIDTH; ocix++) {
- sum = 0.; // for single point
- // for each input maps
- for(ifm_idx = 0; ifm_idx < NUM_IFM; ifm_idx++) {
- // get kernel
- kern[0][0] = weights[ofm_idx][ifm_idx][0][0];
- kern[0][1] = weights[ofm_idx][ifm_idx][0][1];
- kern[0][2] = weights[ofm_idx][ifm_idx][0][2];
- kern[1][0] = weights[ofm_idx][ifm_idx][1][0];
- kern[1][1] = weights[ofm_idx][ifm_idx][1][1];
- kern[1][2] = weights[ofm_idx][ifm_idx][1][2];
- kern[2][0] = weights[ofm_idx][ifm_idx][2][0];
- kern[2][1] = weights[ofm_idx][ifm_idx][2][1];
- kern[2][2] = weights[ofm_idx][ifm_idx][2][2];
- // nearest-value padding (not used in caffe)
- // get //input (including padding)
- //orixm = (orix == 0) ? orix : orix-1;
- //orixp = (orix == HEIGHT-1) ? orix : orix+1;
- //ocixm = (ocix == 0) ? ocix : ocix-1;
- //ocixp = (ocix == WIDTH-1) ? ocix : ocix+1;
- //in_square[0][0] = bottom[0][ifm_idx][orixm][ocixm];
- //in_square[0][1] = bottom[0][ifm_idx][orixm][ocix];
- //in_square[0][2] = bottom[0][ifm_idx][orixm][ocixp];
- //in_square[1][0] = bottom[0][ifm_idx][orix][ocixm];
- //in_square[1][1] = bottom[0][ifm_idx][orix][ocix];
- //in_square[1][2] = bottom[0][ifm_idx][orix][ocixp];
- //in_square[2][0] = bottom[0][ifm_idx][orixp][ocixm];
- //in_square[2][1] = bottom[0][ifm_idx][orixp][ocix];
- //in_square[2][2] = bottom[0][ifm_idx][orixp][ocixp];
- // zero-value padding (used in caffe)
- in_square[0][0] = (orix == 0 ||ocix == 0) ? 0 : bottom[0][ifm_idx][orix][ocix];
- in_square[0][1] = (orix == 0) ? 0 : bottom[0][ifm_idx][orix][ocix];
- in_square[0][2] = (orix == 0 || ocix == WIDTH-1) ? 0 : bottom[0][ifm_idx][orix][ocix];
- in_square[1][0] = (ocix == 0) ? 0 : bottom[0][ifm_idx][orix][ocix];
- in_square[1][1] = bottom[0][ifm_idx][orix][ocix];
- in_square[1][2] = (ocix == WIDTH-1) ? 0 : bottom[0][ifm_idx][orix][ocix];
- in_square[2][0] = (orix == HEIGHT-1 || ocix == 0) ? 0 : bottom[0][ifm_idx][orix][ocix];
- in_square[2][1] = (orix == HEIGHT-1) ? 0 : bottom[0][ifm_idx][orix][ocix];
- in_square[2][2] = (orix == HEIGHT-1 || ocix == WIDTH-1) ? 0 : bottom[0][ifm_idx][orix][ocix];
- // apply kernel
- for(ky = 0; ky < K; ky++) {
- for(kx = 0; kx < K; kx++) {
- sum += in_square[ky][kx]*kern[ky][kx];
- }
- }
- //// add bias
- //sum += bias[ifm_idx];
- } // ifm_idx
- // add bias
- sum += bias[ofm_idx];
- // store result
- conv[0][ofm_idx][orix][ocix] = sum;
- } // ocix
- } // orix
- printf("OFM %d\n", ofm_idx);
- sprintf(str, "./result/L%02d_Convolution_T0_FN%03d.txt", layer, ofm_idx);
- file = fopen(str, "w");
- printf("writing convolution result to file %s..\n", str);
- for (rix = 0; rix < HEIGHT; rix++) {
- fprintf(file, "### kr = %d ##\n", rix);
- for (cix = 0; cix < WIDTH; cix++) {
- fprintf(file, "%f ",conv[0][ofm_idx][rix][cix]);
- if (cix %8 == 7) fprintf(file, "\n");
- }
- }
- fclose(file);
- }
- printf("Convolution finished\n");
- //// ----------------------------------------
- //// Write convolution result to file
- //// ----------------------------------------
- //printf("writing result to files..\n");
- //for(ofm_idx=0;ofm_idx<NUM_OFM;ofm_idx++) {
- // printf("OFM %d\n", ofm_idx);
- // sprintf(str, "./result/L%02d_Convolution_T0_FN%03d.txt", layer, ofm_idx);
- // file = fopen(str, "w");
- // for (rix = 0; rix < HEIGHT; rix++) {
- // fprintf(file, "### kr = %d ##\n", rix);
- // for (cix = 0; cix < WIDTH; cix++) {
- // fprintf(file, "%f ",conv[0][ofm_idx][rix][cix]);
- // if (cix %8 == 7) fprintf(file, "\n");
- // }
- // }
- // fclose(file);
- // printf("covolution result written to file %s\n", str);
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment