Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <float.h>
- #include <math.h>
- #include "sleep.h"
- #include "xtime_l.h"
- #include "xparameters.h"
- #include "xgpio.h"
- #include "xstatus.h"
- #include "xil_printf.h"
- /* Definitions */
- #define GPIO_DEVICE_ID XPAR_AXI_GPIO_0_DEVICE_ID /* GPIO device that LEDs are connected to */
- #define LED 0xC3 /* Initial LED value - XX0000XX */
- #define LED_DELAY 10000000 /* Software delay length */
- #define LED_CHANNEL 1 /* GPIO port for LEDs */
- #define printf xil_printf /* smaller, optimised printf */
- // Include Layer Weights
- #include "layer1.h"
- #include "layer2.h"
- #include "layer3.h"
- #include "layer4.h"
- #define IMGWIDTH 29
- #define IMGHEIGHT 29
- #define tour 10000
- #define SIGMOID(x) (1.7159*tanh(0.66666667*x))
- #define DSIGMOID(S) (0.66666667/1.7159*(1.7159+(S))*(1.7159-(S)))
- void calculateLayer1(float* input, float* Layer1_Neurons_CPU);
- void calculateLayer2(float* Layer1_Neurons_CPU, float* Layer1_Weights_CPU, float* Layer2_Neurons_CPU);
- void calculateLayer3(float* Layer2_Neurons_CPU, float* Layer2_Weights_CPU, float* Layer3_Neurons_CPU);
- void calculateLayer4(float* Layer3_Neurons_CPU, float* Layer3_Weights_CPU, float* Layer4_Neurons_CPU);
- void calculateLayer5(float* Layer4_Neurons_CPU, float* Layer4_Weights_CPU, double* Layer5_Neurons_CPU);
- XGpio Gpio;
- int LEDOutputExample(int val)
- {
- volatile int Delay;
- int Status;
- int led = LED; /* Hold current LED value. Initialise to LED definition */
- /* GPIO driver initialisation */
- Status = XGpio_Initialize(&Gpio, GPIO_DEVICE_ID);
- if (Status != XST_SUCCESS) {
- return XST_FAILURE;
- }
- led = val;
- /*Set the direction for the LEDs to output. */
- XGpio_SetDataDirection(&Gpio, LED_CHANNEL, 0x00);
- /* Write output to the LEDs. */
- XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, led);
- /* change LEDs value. */
- //led = val+1;
- //led = 0xFF;
- /* Wait a small amount of time so that the LED blinking is visible. */
- for (Delay = 0; Delay < LED_DELAY; Delay++);
- return XST_SUCCESS; /* Should be unreachable */
- }
- int main(int argc, char** argv){
- XTime tCur, tEnd, tUsed;
- float
- Layer1_Neurons_CPU[IMGWIDTH*IMGHEIGHT],
- Layer2_Neurons_CPU[6*13*13],
- Layer3_Neurons_CPU[50*5*5],
- Layer4_Neurons_CPU[100];
- double
- Layer5_Neurons_CPU[10];
- int i;
- double scoremax;
- float Input[29*29] = {
- // caractère "2"
- /*
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,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1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
- */
- };
- XTime_GetTime((XTime *) &tCur);
- calculateLayer1(Input, Layer1_Neurons_CPU);
- XTime_GetTime((XTime *) &tEnd);
- tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
- printf("tUsed 1 : %lld \n", tUsed);
- XTime_GetTime(&tCur);
- calculateLayer2(Layer1_Neurons_CPU, Layer1_Weights_CPU, Layer2_Neurons_CPU);
- XTime_GetTime(&tEnd);
- tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
- printf("tUsed 2 : %lld \n", tUsed);
- XTime_GetTime(&tCur);
- calculateLayer3(Layer2_Neurons_CPU, Layer2_Weights_CPU, Layer3_Neurons_CPU);
- XTime_GetTime(&tEnd);
- tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
- printf("tUsed 3 : %lld \n", tUsed);
- XTime_GetTime(&tCur);
- calculateLayer4(Layer3_Neurons_CPU, Layer3_Weights_CPU, Layer4_Neurons_CPU);
- XTime_GetTime(&tEnd);
- tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
- printf("tUsed 4 : %lld \n", tUsed);
- XTime_GetTime(&tCur);
- calculateLayer5(Layer4_Neurons_CPU, Layer4_Weights_CPU, Layer5_Neurons_CPU);
- XTime_GetTime(&tEnd);
- tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
- printf("tUsed 5 : %lld \n", tUsed);
- scoremax = FLT_MIN;
- int indexmax=-1;
- for(i=0;i<10;i++)
- {
- //printf("%d : %f\n",i,Layer5_Neurons_CPU[i]);
- if(Layer5_Neurons_CPU[i]>scoremax)
- {
- scoremax = Layer5_Neurons_CPU[i];
- indexmax = i;
- }
- }
- printf("Le resultat est : %d \n",indexmax);
- int Status;
- Status = LEDOutputExample(indexmax);
- if (Status != XST_SUCCESS) {
- xil_printf("GPIO output to the LEDs failed!\r\n");
- }
- return 0;
- }
- void calculateLayer1(float* input, float* Layer1_Neurons_CPU){
- memcpy(Layer1_Neurons_CPU, input, IMGWIDTH*IMGHEIGHT*sizeof(float));
- }
- void calculateLayer2(float* Layer1_Neurons_CPU, float* Layer1_Weights_CPU, float* Layer2_Neurons_CPU){
- float somme;
- int i,j,k,m,n;
- for(i=0;i<6;i++)
- for(j=0;j<13;j++)
- for(k=0;k<13;k++){
- somme = Layer1_Weights_CPU[26*i];
- for(m=0;m<5;m++)
- for(n=0;n<5;n++)
- somme += Layer1_Weights_CPU[26*i+5*m+n+1] * Layer1_Neurons_CPU[29*(m+2*j)+n+2*k];
- Layer2_Neurons_CPU[13*13*i+13*j+k] = (float) SIGMOID(somme);
- }
- }
- void calculateLayer3(float* Layer2_Neurons_CPU, float* Layer2_Weights_CPU, float* Layer3_Neurons_CPU){
- float somme;
- int i,j,k,m,n;
- for( i=0;i<50;i++)
- for(j=0;j<5;j++)
- for(k=0;k<5;k++){
- somme = Layer2_Weights_CPU[26*6*i];
- for( m=0;m<5;m++)
- for( n=0;n<5;n++){
- somme += Layer2_Weights_CPU[26*6*i+1+6*(n+5*m) ] * Layer2_Neurons_CPU[13*13*0+13*(2*j+m)+(2*k+n)];
- somme += Layer2_Weights_CPU[26*6*i+1+6*(n+5*m)+1] * Layer2_Neurons_CPU[13*13*1+13*(2*j+m)+(2*k+n)];
- somme += Layer2_Weights_CPU[26*6*i+1+6*(n+5*m)+2] * Layer2_Neurons_CPU[13*13*2+13*(2*j+m)+(2*k+n)];
- somme += Layer2_Weights_CPU[26*6*i+1+6*(n+5*m)+3] * Layer2_Neurons_CPU[13*13*3+13*(2*j+m)+(2*k+n)];
- somme += Layer2_Weights_CPU[26*6*i+1+6*(n+5*m)+4] * Layer2_Neurons_CPU[13*13*4+13*(2*j+m)+(2*k+n)];
- somme += Layer2_Weights_CPU[26*6*i+1+6*(n+5*m)+5] * Layer2_Neurons_CPU[13*13*5+13*(2*j+m)+(2*k+n)];
- }
- Layer3_Neurons_CPU[5*5*i+5*j+k] = (float) SIGMOID(somme);
- }
- }
- void calculateLayer4(float* Layer3_Neurons_CPU, float* Layer3_Weights_CPU, float* Layer4_Neurons_CPU){
- float somme;
- int i, j, k, m;
- for( i=0;i<100;i++){
- somme = Layer3_Weights_CPU[i*(1+50*25)];
- for( j=0;j<50;j++)
- for( k=0;k<5;k++)
- for ( m=0;m<5;m++)
- somme += Layer3_Weights_CPU[i*(1+50*25)+1 + m + k*5 + j*25] * Layer3_Neurons_CPU[m+5*k+25*j];
- Layer4_Neurons_CPU[i] = (float) SIGMOID(somme);
- }
- }
- void calculateLayer5(float* Layer4_Neurons_CPU, float* Layer4_Weights_CPU, double* Layer5_Neurons_CPU){
- float somme;
- int i, j;
- for( i=0;i<10;i++){
- somme = Layer4_Weights_CPU[101*i];
- for( j=0;j<100;j++)
- somme += Layer4_Weights_CPU[1+101*i+j] * Layer4_Neurons_CPU[j];
- Layer5_Neurons_CPU[i] = SIGMOID(somme);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement