int main() { // Store images, 784 rows, 10000 columns double** images = create2DDoubleArray(IMGSIZE, NUMIMGS); // Store layer 1 weights, 200 rows, 784 columns double** wL1 = create2DDoubleArray(LAYER1, IMGSIZE); // Stores layer 2 weights, 200 rows, 200 columns double** wL2 = create2DDoubleArray(LAYER2, LAYER1); // Stores layer 3 weights, 10 rows, 200 columns double** wL3 = create2DDoubleArray(LAYERFINAL, LAYER2); // Stores bias data, two sets of 200 rows, 1 column double** bias = create2DDoubleArray(LAYER1, 2); // Where the computed probabilities are stored double** probabilities = create2DDoubleArray(LAYERFINAL, NUMTEST); // Stores solutions int solutions[NUMIMGS]; if(loadData(images, wL1, wL2, wL3, bias, solutions) == -1) { return -1; } computeProb(images, wL1, wL2, wL3, bias, &probabilities); double accuracy = computeAccuracy(probabilities, solutions); printf("Hit Percentage: %3.2lf%%\n", accuracy * 100); return 0; }
int main() { void* virtual_base; int fd; void* sdram_ptr; if(setup(&fd, &virtual_base) != 0) { return(1); } sdram_ptr = virtual_base + ((unsigned long)(SDRAM_OFST + 0x00) & (unsigned long)(HW_REGS_MASK)); // Store layer 1 weights, 200 rows, 784 columns double** wL1 = create2DDoubleArray(LAYER1, IMGSIZE); // Stores layer 2 weights, 200 rows, 200 columns double** wL2 = create2DDoubleArray(LAYER2, LAYER1); // Stores layer 3 weights, 10 rows, 200 columns double** wL3 = create2DDoubleArray(LAYERFINAL, LAYER2); // Stores bias data, two sets of 200 rows, 1 column double** bias = create2DDoubleArray(LAYER1, 2); // Where the computed probabilities are stored double** probabilities = create2DDoubleArray(LAYERFINAL, NUMTEST); // Stores solutions int solutions[NUMIMGS]; printf("Loading images to SDRAM...\n"); // Loads image data into the SDRAM if(loadImagesSDRAM(sdram_ptr) == -1) { return -1; } /* printf("((double*)sdram_ptr)[177 * NUMIMGS + 0]: %lf\n", ((double*)sdram_ptr)[177 * NUMIMGS + 0]); printf("((double*)sdram_ptr)[177 * NUMIMGS + 6]: %lf\n", ((double*)sdram_ptr)[177 * NUMIMGS + 6]); */ printf("Loading weights, biases and solutions...\n"); if(loadData(wL1, wL2, wL3, bias, solutions) == -1) { return -1; } clock_t begin, end; printf("Computing Probabilities...\n"); // Computes probabilities for each image begin = clock(); computeProb(sdram_ptr, wL1, wL2, wL3, bias, &probabilities); end = clock(); printf("Computing accuracy...\n"); // Stores and computes the accuracy, 0 < accuracy < 1 double accuracy = computeAccuracy(probabilities, solutions); printf("Hit Percentage: %3.2lf%%\n", accuracy * 100); printf("Time Elapsed: %f\n", (double)(end - begin) / CLOCKS_PER_SEC); if(munmap(virtual_base, HW_REGS_SPAN) != 0) { printf("ERROR: munmap() failed...\n"); close(fd); return(1); } close(fd); return 0; }
unsigned long transformImage(unsigned char *imageBuffer, unsigned char* backgroundColor) { frameBuffer frameTarget, frameSource; double xNew, yNew, xOld, yOld, denominator; unsigned long iteration = 0; unsigned long resolution = 0; unsigned long i, j, randomMax, random; unsigned long pos, posX, posY; unsigned long numFuncs; frameTarget.image = imageBuffer; frameSource.image = backgroundColor; numFuncs = functionPara[0].numActiveFuncs; float prob[numFuncs+1]; resolution =functionPara[0].resolution.xLength*functionPara[0].resolution.yWidth; for(i=0; i<resolution;i++) { frameTarget.pixel[i].red = frameSource.pixel[i].red; frameTarget.pixel[i].green = frameSource.pixel[i].green; frameTarget.pixel[i].blue = frameSource.pixel[i].blue; } if(numFuncs == 0) return 0; srand((unsigned)time(NULL)); randomMax = RAND_MAX; iteration = numFuncs*resolution; if (numFuncs<4) iteration = 4*resolution; xNew = yNew = xOld = yOld = denominator = 0.0; computeProb(prob); for(i=0; i<100; i++) { random = rand(); for(j=0; j<numFuncs; j++) { if(!(random < round(randomMax*prob[j]))&&(!(random > round(randomMax*prob[j+1])))) { denominator = functionPara[j].transPara[8] + functionPara[j].transPara[9] * xOld +functionPara[j].transPara[10] * yOld; xNew = (functionPara[j].transPara[0]+functionPara[j].transPara[1] * xOld +(functionPara[j].transPara[2]+functionPara[j].transPara[3] * xOld) * yOld)/denominator; yNew = (functionPara[j].transPara[4]+functionPara[j].transPara[5]*xOld +(functionPara[j].transPara[6]+functionPara[j].transPara[7]*xOld)*yOld)/denominator; break; } } xOld = xNew; yOld = yNew; } for(i=100; i<iteration; i++) { random = rand(); for(j=0; j<numFuncs; j++) { if(!(random < round(randomMax*prob[j]))&&(!(random > round(randomMax*prob[j+1])))) { denominator = functionPara[j].transPara[8] + functionPara[j].transPara[9] * xOld +functionPara[j].transPara[10] * yOld; xNew = (functionPara[j].transPara[0]+functionPara[j].transPara[1] * xOld +(functionPara[j].transPara[2]+functionPara[j].transPara[3] * xOld) * yOld)/denominator; yNew = (functionPara[j].transPara[4]+functionPara[j].transPara[5]*xOld +(functionPara[j].transPara[6]+functionPara[j].transPara[7]*xOld)*yOld)/denominator; break; } } if((!(xNew<0.0))&&(!(xNew>1.0))&&(!(yNew<0.0))&&(!(yNew>1.0))) { posX = round((functionPara[0].resolution.xLength-1)*xNew); posY = round((functionPara[0].resolution.yWidth-1)*(1.0 - yNew)); pos = (functionPara[0].resolution.xLength*posY + posX); frameTarget.pixel[pos].red = functionPara[j].colorMap.red; frameTarget.pixel[pos].green = functionPara[j].colorMap.green; frameTarget.pixel[pos].blue = functionPara[j].colorMap.blue; } if(abs(xNew )>1000) xNew /= 10.0; if(abs(yNew)>1000) yNew /= 10.0; xOld = xNew; yOld = yNew; } return resolution*3; }