Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.57 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <float.h>
  6. #include <math.h>
  7.  
  8. #include "sleep.h"
  9. #include "xtime_l.h"
  10. #include "xparameters.h"
  11. #include "xgpio.h"
  12. #include "xstatus.h"
  13. #include "xil_printf.h"
  14.  
  15. /* Definitions */
  16. #define GPIO_DEVICE_ID  XPAR_AXI_GPIO_0_DEVICE_ID   /* GPIO device that LEDs are connected to */
  17. #define LED 0xC3                                    /* Initial LED value - XX0000XX */
  18. #define LED_DELAY 10000000                          /* Software delay length */
  19. #define LED_CHANNEL 1                               /* GPIO port for LEDs */
  20. #define printf xil_printf                           /* smaller, optimised printf */
  21.  
  22. // Include Layer Weights
  23. #include "layer1.h"
  24. #include "layer2.h"
  25. #include "layer3.h"
  26. #include "layer4.h"
  27.  
  28. #define IMGWIDTH 29
  29. #define IMGHEIGHT 29
  30. #define tour 10000
  31.  
  32. #define SIGMOID(x) (1.7159*tanh(0.66666667*x))
  33. #define DSIGMOID(S) (0.66666667/1.7159*(1.7159+(S))*(1.7159-(S)))
  34.  
  35.  
  36. void calculateLayer1(float* input, float* Layer1_Neurons_CPU);
  37. void calculateLayer2(float* Layer1_Neurons_CPU, float* Layer1_Weights_CPU, float* Layer2_Neurons_CPU);
  38. void calculateLayer3(float* Layer2_Neurons_CPU, float* Layer2_Weights_CPU, float* Layer3_Neurons_CPU);
  39. void calculateLayer4(float* Layer3_Neurons_CPU, float* Layer3_Weights_CPU, float* Layer4_Neurons_CPU);
  40. void calculateLayer5(float* Layer4_Neurons_CPU, float* Layer4_Weights_CPU, double* Layer5_Neurons_CPU);
  41.  
  42. XGpio Gpio;
  43.  
  44.  
  45. int LEDOutputExample(int val)
  46. {
  47.  
  48.     volatile int Delay;
  49.     int Status;
  50.     int led = LED; /* Hold current LED value. Initialise to LED definition */
  51.  
  52.         /* GPIO driver initialisation */
  53.         Status = XGpio_Initialize(&Gpio, GPIO_DEVICE_ID);
  54.         if (Status != XST_SUCCESS) {
  55.             return XST_FAILURE;
  56.         }
  57.  
  58.         led = val;
  59.         /*Set the direction for the LEDs to output. */
  60.         XGpio_SetDataDirection(&Gpio, LED_CHANNEL, 0x00);
  61.  
  62.         /* Write output to the LEDs. */
  63.         XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, led);
  64.  
  65.         /* change LEDs value. */
  66.         //led = val+1;
  67.         //led = 0xFF;
  68.  
  69.         /* Wait a small amount of time so that the LED blinking is visible. */
  70.         for (Delay = 0; Delay < LED_DELAY; Delay++);
  71.  
  72.  
  73.         return XST_SUCCESS; /* Should be unreachable */
  74. }
  75.  
  76.  
  77.  
  78. int main(int argc, char** argv){
  79.  
  80.     XTime tCur, tEnd, tUsed;
  81.  
  82.     float
  83.         Layer1_Neurons_CPU[IMGWIDTH*IMGHEIGHT],
  84.         Layer2_Neurons_CPU[6*13*13],
  85.         Layer3_Neurons_CPU[50*5*5],
  86.         Layer4_Neurons_CPU[100];
  87.  
  88.     double
  89.         Layer5_Neurons_CPU[10];
  90.  
  91.     int i;
  92.  
  93.     double scoremax;
  94.  
  95.     float Input[29*29] = {
  96.  
  97.     // caractère "2"
  98.     /*
  99.     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,
  100.     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,
  101.     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,
  102.     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,
  103.     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,
  104.     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,
  105.     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,
  106.     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,
  107.     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,
  108.     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,
  109.     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,
  110.     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,
  111.     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,
  112.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
  113.     1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
  114.     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,
  115.     1,1,1,1,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,
  116.     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,
  117.     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,
  118.     1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,
  119.     1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,
  120.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,
  121.     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,
  122.     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,
  123.     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,
  124.     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,
  125.     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,
  126.     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,
  127.     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
  128.     */
  129.     // caractère "3"
  130.     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,
  131.     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,
  132.     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,
  133.     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,
  134.     1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  135.     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,
  136.     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,
  137.     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,
  138.     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,
  139.     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,
  140.     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,
  141.     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,
  142.     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,
  143.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
  144.     1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
  145.     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,
  146.     1,1,1,1,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,
  147.     1,1,1,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,
  148.     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,
  149.     1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  150.     1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
  151.     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,
  152.     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,
  153.     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,
  154.     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,
  155.     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,
  156.     1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  157.     1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  158.     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
  159.  
  160.     // caractère "8"
  161. /*
  162.     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,
  163.     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,
  164.     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,
  165.     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,
  166.     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,
  167.     1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
  168.     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,
  169.     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,
  170.     1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,
  171.     1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,
  172.     1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,
  173.     1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,
  174.     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,
  175.     1,1,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
  176.     1,1,1,1,1,1,1,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
  177.     1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  178.     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,
  179.     1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  180.     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,
  181.     1,1,1,1,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
  182.     1,1,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
  183.     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,
  184.     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,
  185.     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,
  186.     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,
  187.     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,
  188.     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,
  189.     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,
  190.     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
  191. */
  192.     };
  193.  
  194.     XTime_GetTime((XTime *) &tCur);
  195.     calculateLayer1(Input, Layer1_Neurons_CPU);
  196.     XTime_GetTime((XTime *) &tEnd);
  197.     tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
  198.     printf("tUsed 1 : %lld \n", tUsed);
  199.  
  200.     XTime_GetTime(&tCur);
  201.     calculateLayer2(Layer1_Neurons_CPU, Layer1_Weights_CPU, Layer2_Neurons_CPU);
  202.     XTime_GetTime(&tEnd);
  203.     tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
  204.     printf("tUsed 2 : %lld \n", tUsed);
  205.  
  206.     XTime_GetTime(&tCur);
  207.     calculateLayer3(Layer2_Neurons_CPU, Layer2_Weights_CPU, Layer3_Neurons_CPU);
  208.     XTime_GetTime(&tEnd);
  209.     tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
  210.     printf("tUsed 3 : %lld \n", tUsed);
  211.  
  212.  
  213.     XTime_GetTime(&tCur);
  214.     calculateLayer4(Layer3_Neurons_CPU, Layer3_Weights_CPU, Layer4_Neurons_CPU);
  215.     XTime_GetTime(&tEnd);
  216.     tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
  217.     printf("tUsed 4 : %lld \n", tUsed);
  218.  
  219.     XTime_GetTime(&tCur);
  220.     calculateLayer5(Layer4_Neurons_CPU, Layer4_Weights_CPU, Layer5_Neurons_CPU);
  221.     XTime_GetTime(&tEnd);
  222.     tUsed = ((tEnd-tCur)*100000000)/(COUNTS_PER_SECOND);
  223.     printf("tUsed 5 : %lld \n", tUsed);
  224.  
  225.     scoremax = FLT_MIN;
  226.     int indexmax=-1;
  227.     for(i=0;i<10;i++)
  228.     {
  229.         //printf("%d : %f\n",i,Layer5_Neurons_CPU[i]);
  230.         if(Layer5_Neurons_CPU[i]>scoremax)
  231.         {
  232.             scoremax = Layer5_Neurons_CPU[i];
  233.             indexmax = i;
  234.         }
  235.     }
  236.     printf("Le resultat est : %d \n",indexmax);
  237.  
  238.     int Status;
  239.  
  240.     Status = LEDOutputExample(indexmax);
  241.     if (Status != XST_SUCCESS) {
  242.         xil_printf("GPIO output to the LEDs failed!\r\n");
  243.     }
  244.  
  245.     return 0;
  246.  
  247. }
  248.  
  249. void calculateLayer1(float* input, float* Layer1_Neurons_CPU){
  250.  
  251.     memcpy(Layer1_Neurons_CPU, input, IMGWIDTH*IMGHEIGHT*sizeof(float));
  252.  
  253. }
  254.  
  255. void calculateLayer2(float* Layer1_Neurons_CPU, float* Layer1_Weights_CPU, float* Layer2_Neurons_CPU){
  256.  
  257.     float somme;
  258.     int i,j,k,m,n;
  259.     for(i=0;i<6;i++)
  260.         for(j=0;j<13;j++)
  261.             for(k=0;k<13;k++){
  262.                 somme = Layer1_Weights_CPU[26*i];
  263.                 for(m=0;m<5;m++)
  264.                     for(n=0;n<5;n++)
  265.                         somme += Layer1_Weights_CPU[26*i+5*m+n+1] * Layer1_Neurons_CPU[29*(m+2*j)+n+2*k];
  266.                 Layer2_Neurons_CPU[13*13*i+13*j+k] = (float) SIGMOID(somme);
  267.             }
  268.  
  269. }
  270.  
  271. void calculateLayer3(float* Layer2_Neurons_CPU, float* Layer2_Weights_CPU, float* Layer3_Neurons_CPU){
  272.  
  273.     float somme;
  274.     int i,j,k,m,n;
  275.     for( i=0;i<50;i++)
  276.         for(j=0;j<5;j++)
  277.             for(k=0;k<5;k++){
  278.                 somme = Layer2_Weights_CPU[26*6*i];
  279.  
  280.                 for( m=0;m<5;m++)
  281.                     for( n=0;n<5;n++){
  282.                         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)];
  283.                         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)];
  284.                         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)];
  285.                         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)];
  286.                         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)];
  287.                         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)];
  288.  
  289.                     }
  290.                 Layer3_Neurons_CPU[5*5*i+5*j+k] = (float) SIGMOID(somme);
  291.             }
  292.  
  293. }
  294.  
  295. void calculateLayer4(float* Layer3_Neurons_CPU, float* Layer3_Weights_CPU, float* Layer4_Neurons_CPU){
  296.  
  297.     float somme;
  298.     int i, j, k, m;
  299.     for( i=0;i<100;i++){
  300.         somme = Layer3_Weights_CPU[i*(1+50*25)];
  301.         for( j=0;j<50;j++)
  302.             for( k=0;k<5;k++)
  303.                 for ( m=0;m<5;m++)
  304.                     somme += Layer3_Weights_CPU[i*(1+50*25)+1 + m + k*5 + j*25] * Layer3_Neurons_CPU[m+5*k+25*j];
  305.  
  306.         Layer4_Neurons_CPU[i] = (float) SIGMOID(somme);
  307.     }
  308.  
  309. }
  310.  
  311. void calculateLayer5(float* Layer4_Neurons_CPU, float* Layer4_Weights_CPU, double* Layer5_Neurons_CPU){
  312.  
  313.     float somme;
  314.     int i, j;
  315.     for( i=0;i<10;i++){
  316.         somme = Layer4_Weights_CPU[101*i];
  317.         for( j=0;j<100;j++)
  318.             somme += Layer4_Weights_CPU[1+101*i+j] * Layer4_Neurons_CPU[j];
  319.         Layer5_Neurons_CPU[i] = SIGMOID(somme);
  320.     }
  321.  
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement