FC2カウンター FPGAの部屋 2018年06月24日
FC2ブログ

FPGAやCPLDの話題やFPGA用のツールの話題などです。 マニアックです。 日記も書きます。

FPGAの部屋

FPGAの部屋の有用と思われるコンテンツのまとめサイトを作りました。Xilinx ISEの初心者の方には、FPGAリテラシーおよびチュートリアルのページをお勧めいたします。

DMA付きテンプレートを使用したMNISTのCNN2(C シミュレーション1)

DMA付きテンプレートを使用したMNISTのCNN1(C ソースコード)”の続き。

前回は、DMA 付きテンプレートを使用したMNIST のCNN のC ソースコードを貼った。今回は、その C ソースコードを利用してC シミュレーションをしてみよう。

まずは、数に示したように一番下の 7, 8, 9 をCNN に入力してみよう。
mnist_conv_nn3_ko_dma_2_180624.jpg

DMA付きテンプレートを使用したMNISTのCNN1(C ソースコード)”に書いたように、1回毎に 5 ピクセルずつ 28 x 28 ピクセルに切り取る画像の x 軸を右にずらしていく。もう一度書いておくと、around_count と center_count は、28 x 28 ピクセルのMNIST の手書き文字の周り 3 ピクセル分、(3+3)x28 + (3+3) x 26 = 300 ピクセルと真ん中の 300 ピクセルの 0x80 より大きなピクセルを数えて、その比を求めようと思う。これは、手書き文字がどこにあるのか?をサーチするのに使用したい。とりあえず、どのような結果になるかを検証してみよう。

mnist_conv_nn3_ko_dma プロジェクトのC シミュレーションを行った。
mnist_conv_nn3_ko_dma_3_180624.png

結果を示す。

INFO: [SIM 2] *************** CSIM start ***************
INFO: [SIM 4] CSIM will launch CLANG as the compiler.
   Compiling ../../../mnist_square_dma.cpp in debug mode
   Generating csim.exe

X_POS = 502, Y_POS = 185
                            
                            
          ************      
          ************      
          **       ***      
         ***        **      
        ***        ***      
        **         ***      
        **        ***       
         *        ***       
                  **        
                  **        
                  **        
                  **        
                 ***        
                 ***        
                ***         
                **          
                **          
                **          
                **          
               ***          
              ****          
              ***           
              ***           
                            
                            
                            
around_count = 12, center_count = 42, center/around = 3.500000
result_out = 3
max_id_hw = 3
max_id_sw = 3
result_ap_fixed[0] = -6.687500, result_float[0] = -6.598463
result_ap_fixed[1] = -1.187500, result_float[1] = -0.928407
result_ap_fixed[2] = -2.437500, result_float[2] = -1.751157
result_ap_fixed[3] = -0.031250, result_float[3] = 0.870602
result_ap_fixed[4] = -2.406250, result_float[4] = -2.517686
result_ap_fixed[5] = -6.562500, result_float[5] = -6.144890
result_ap_fixed[6] = -9.875000, result_float[6] = -9.862177
result_ap_fixed[7] = -2.781250, result_float[7] = -2.362119
result_ap_fixed[8] = -3.250000, result_float[8] = -3.122242
result_ap_fixed[9] = -2.343750, result_float[9] = -2.650363

X_POS = 507, Y_POS = 185
                            
                            
     ************           
     ************           
     **       ***           
    ***        **           
   ***        ***           
   **         ***           
   **        ***            
    *        ***            
             **             
             **             
             **             
             **             
            ***             
            ***             
           ***              
           **               
           **               
           **               
           **               
          ***               
         ****               
         ***                
         ***                
                            
                            
                            
around_count = 12, center_count = 40, center/around = 3.333333
result_out = 1
max_id_hw = 1
max_id_sw = 1
result_ap_fixed[0] = -4.093750, result_float[0] = -3.944167
result_ap_fixed[1] = 0.843750, result_float[1] = 1.401140
result_ap_fixed[2] = 0.562500, result_float[2] = 1.179666
result_ap_fixed[3] = 0.125000, result_float[3] = 0.740295
result_ap_fixed[4] = -7.281250, result_float[4] = -8.019794
result_ap_fixed[5] = -8.343750, result_float[5] = -8.311672
result_ap_fixed[6] = -4.906250, result_float[6] = -4.912206
result_ap_fixed[7] = -5.781250, result_float[7] = -5.561728
result_ap_fixed[8] = -0.593750, result_float[8] = -0.508428
result_ap_fixed[9] = -10.031250, result_float[9] = -10.652685

X_POS = 512, Y_POS = 185
                            
                            
************                
************                
**       ***                
**        **                
*        ***                
         ***                
        ***                 
        ***                 
        **                  
        **                  
        **                  
        **                  
       ***                  
       ***                  
      ***                   
      **                    
      **                    
      **                    
      **                    
     ***                    
    ****                    
    ***                     
    ***                     
                            
                            
                            
around_count = 20, center_count = 37, center/around = 1.850000
result_out = 6
max_id_hw = 6
max_id_sw = 6
result_ap_fixed[0] = 1.375000, result_float[0] = 1.538496
result_ap_fixed[1] = -3.125000, result_float[1] = -3.003051
result_ap_fixed[2] = -3.593750, result_float[2] = -3.063903
result_ap_fixed[3] = -7.656250, result_float[3] = -7.790153
result_ap_fixed[4] = -3.968750, result_float[4] = -3.929864
result_ap_fixed[5] = -4.937500, result_float[5] = -5.745523
result_ap_fixed[6] = 1.812500, result_float[6] = 2.254609
result_ap_fixed[7] = -3.093750, result_float[7] = -2.851094
result_ap_fixed[8] = -0.343750, result_float[8] = -0.149853
result_ap_fixed[9] = -2.781250, result_float[9] = -3.073567

X_POS = 517, Y_POS = 185
                            
                            
*******                     
*******                     
    ***                     
     **                     
    ***                     
    ***                     
   ***                      
   ***                      
   **                       
   **                       
   **                       
   **                       
  ***                       
  ***                       
 ***                        
 **                         
 **                         
 **                         
 **                         
***                         
***                         
**                          
**                          
                            
                            
                            
around_count = 32, center_count = 13, center/around = 0.406250
result_out = 6
max_id_hw = 6
max_id_sw = 6
result_ap_fixed[0] = -0.593750, result_float[0] = -0.710496
result_ap_fixed[1] = -1.406250, result_float[1] = -1.237631
result_ap_fixed[2] = -2.312500, result_float[2] = -2.128094
result_ap_fixed[3] = -1.312500, result_float[3] = -0.931199
result_ap_fixed[4] = -0.812500, result_float[4] = -0.698710
result_ap_fixed[5] = -1.937500, result_float[5] = -2.167314
result_ap_fixed[6] = 0.062500, result_float[6] = 0.688496
result_ap_fixed[7] = -1.968750, result_float[7] = -1.845435
result_ap_fixed[8] = -0.750000, result_float[8] = -0.674898
result_ap_fixed[9] = -0.843750, result_float[9] = -1.345316

X_POS = 522, Y_POS = 185
                            
                            
**                          
**                          
**                          
**                          
**                          
**                          
*                           
*                           
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 14, center_count = 0, center/around = 0.000000
result_out = 3
max_id_hw = 3
max_id_sw = 3
result_ap_fixed[0] = -1.125000, result_float[0] = -1.619331
result_ap_fixed[1] = -1.875000, result_float[1] = -2.359333
result_ap_fixed[2] = -0.406250, result_float[2] = 0.069491
result_ap_fixed[3] = 0.375000, result_float[3] = 1.420542
result_ap_fixed[4] = -1.687500, result_float[4] = -2.624235
result_ap_fixed[5] = -0.718750, result_float[5] = -0.753983
result_ap_fixed[6] = -2.250000, result_float[6] = -3.265238
result_ap_fixed[7] = -1.687500, result_float[7] = -1.984813
result_ap_fixed[8] = 0.343750, result_float[8] = 0.805721
result_ap_fixed[9] = -0.250000, result_float[9] = -0.241826

X_POS = 527, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.906250, result_float[0] = -1.296470
result_ap_fixed[1] = -1.468750, result_float[1] = -1.965718
result_ap_fixed[2] = -0.156250, result_float[2] = 0.285129
result_ap_fixed[3] = 0.437500, result_float[3] = 1.279455
result_ap_fixed[4] = -1.687500, result_float[4] = -2.641459
result_ap_fixed[5] = -0.281250, result_float[5] = -0.265089
result_ap_fixed[6] = -1.812500, result_float[6] = -2.805036
result_ap_fixed[7] = -1.812500, result_float[7] = -2.567419
result_ap_fixed[8] = 0.593750, result_float[8] = 0.916473
result_ap_fixed[9] = -0.468750, result_float[9] = -0.466774

X_POS = 532, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.937500, result_float[0] = -1.329478
result_ap_fixed[1] = -1.437500, result_float[1] = -1.938128
result_ap_fixed[2] = -0.156250, result_float[2] = 0.330411
result_ap_fixed[3] = 0.375000, result_float[3] = 1.262094
result_ap_fixed[4] = -1.593750, result_float[4] = -2.625525
result_ap_fixed[5] = -0.218750, result_float[5] = -0.105831
result_ap_fixed[6] = -1.781250, result_float[6] = -2.788874
result_ap_fixed[7] = -1.625000, result_float[7] = -2.566367
result_ap_fixed[8] = 0.531250, result_float[8] = 0.966137
result_ap_fixed[9] = -0.437500, result_float[9] = -0.442092

X_POS = 537, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.906250, result_float[0] = -1.390456
result_ap_fixed[1] = -1.281250, result_float[1] = -1.837113
result_ap_fixed[2] = -0.218750, result_float[2] = 0.437072
result_ap_fixed[3] = 0.312500, result_float[3] = 1.242732
result_ap_fixed[4] = -1.500000, result_float[4] = -2.527295
result_ap_fixed[5] = -0.312500, result_float[5] = -0.236968
result_ap_fixed[6] = -1.687500, result_float[6] = -2.775196
result_ap_fixed[7] = -1.593750, result_float[7] = -2.510123
result_ap_fixed[8] = 0.500000, result_float[8] = 0.975650
result_ap_fixed[9] = -0.406250, result_float[9] = -0.408239

X_POS = 542, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                          **
                          **
                        ****
                        *** 
                        **  
                        **  
                        **  
                        *** 
                          **
                          **
                           *
                            
                            
                            
                            
                            
around_count = 19, center_count = 0, center/around = 0.000000
result_out = 2
max_id_hw = 2
max_id_sw = 2
result_ap_fixed[0] = -0.531250, result_float[0] = -0.738625
result_ap_fixed[1] = -0.781250, result_float[1] = -0.758915
result_ap_fixed[2] = 0.437500, result_float[2] = 2.021734
result_ap_fixed[3] = -0.312500, result_float[3] = 0.229120
result_ap_fixed[4] = -2.500000, result_float[4] = -3.328397
result_ap_fixed[5] = -0.468750, result_float[5] = -1.405289
result_ap_fixed[6] = -1.062500, result_float[6] = -1.902044
result_ap_fixed[7] = -2.093750, result_float[7] = -2.454546
result_ap_fixed[8] = -0.781250, result_float[8] = -1.369310
result_ap_fixed[9] = -2.125000, result_float[9] = -3.382636

X_POS = 547, Y_POS = 185
                            
                          **
                        ****
                       **** 
                       **   
                       **   
                       **   
                       **   
                        ****
                         ***
                        ****
                       *****
                     ****   
                     ****   
                   ****     
                   ***      
                   **       
                   **       
                   **       
                   ***      
                     **     
                     ***    
                      ******
                       *****
                            
                            
                            
                            
around_count = 26, center_count = 29, center/around = 1.115385
result_out = 4
max_id_hw = 4
max_id_sw = 4
result_ap_fixed[0] = -1.593750, result_float[0] = -2.247005
result_ap_fixed[1] = -3.812500, result_float[1] = -3.951203
result_ap_fixed[2] = -2.000000, result_float[2] = -1.570028
result_ap_fixed[3] = -5.031250, result_float[3] = -4.849737
result_ap_fixed[4] = 3.718750, result_float[4] = 4.487070
result_ap_fixed[5] = -0.406250, result_float[5] = 0.060456
result_ap_fixed[6] = -1.718750, result_float[6] = -1.914177
result_ap_fixed[7] = -3.406250, result_float[7] = -3.879769
result_ap_fixed[8] = -0.750000, result_float[8] = -0.644624
result_ap_fixed[9] = -0.625000, result_float[9] = -0.510215

X_POS = 552, Y_POS = 185
                            
                     ****** 
                   *********
                  ****    **
                  **      **
                  **      **
                  **    ****
                  **   **** 
                   ******   
                    ****    
                   *****    
                  ******    
                ****   ***  
                ****    **  
              ****      *** 
              ***        ***
              **          **
              **          **
              **           *
              ***         **
                **        **
                ***       **
                 ***********
                  ********  
                            
                            
                            
                            
around_count = 48, center_count = 53, center/around = 1.104167
result_out = 2
max_id_hw = 2
max_id_sw = 2
result_ap_fixed[0] = -16.593750, result_float[0] = -17.208761
result_ap_fixed[1] = 1.531250, result_float[1] = 2.165034
result_ap_fixed[2] = 5.437500, result_float[2] = 6.572360
result_ap_fixed[3] = 0.031250, result_float[3] = 0.297143
result_ap_fixed[4] = -1.937500, result_float[4] = -1.676362
result_ap_fixed[5] = -12.156250, result_float[5] = -12.265809
result_ap_fixed[6] = -5.937500, result_float[6] = -5.896308
result_ap_fixed[7] = -10.000000, result_float[7] = -9.892697
result_ap_fixed[8] = 2.125000, result_float[8] = 2.661707
result_ap_fixed[9] = -5.031250, result_float[9] = -5.242616

X_POS = 557, Y_POS = 185
                            
                ******      
              *********     
             ****    ***    
             **      **     
             **      **     
             **    ****     
             **   ****      
              ******        
               ****         
              *****         
             ******         
           ****   ***       
           ****    **       
         ****      ***      
         ***        ***     
         **          **     
         **          **     
         **           **    
         ***         ***    
           **        **     
           ***       **     
            ***********     
             ********       
                            
                            
                            
                            
around_count = 15, center_count = 80, center/around = 5.333333
result_out = 8
max_id_hw = 8
max_id_sw = 8
result_ap_fixed[0] = -1.687500, result_float[0] = -1.081046
result_ap_fixed[1] = -4.875000, result_float[1] = -4.630392
result_ap_fixed[2] = -3.250000, result_float[2] = -2.767578
result_ap_fixed[3] = -4.218750, result_float[3] = -3.703644
result_ap_fixed[4] = -5.031250, result_float[4] = -4.921146
result_ap_fixed[5] = -1.687500, result_float[5] = -1.303151
result_ap_fixed[6] = 0.937500, result_float[6] = 1.242822
result_ap_fixed[7] = -9.625000, result_float[7] = -9.519215
result_ap_fixed[8] = 4.593750, result_float[8] = 5.101025
result_ap_fixed[9] = -10.000000, result_float[9] = -9.979488

X_POS = 562, Y_POS = 185
                            
           ******           
         *********          
        ****    ***         
        **      **          
        **      **          
        **    ****          
        **   ****           
         ******             
          ****              
         *****              
        ******              
      ****   ***            
      ****    **            
    ****      ***           
    ***        ***          
    **          **          
    **          **          
    **           **         
    ***         ***         
      **        **          
      ***       **          
       ***********          
        ********            
                            
                            
                            
                            
around_count = 15, center_count = 80, center/around = 5.333333
result_out = 5
max_id_hw = 5
max_id_sw = 5
result_ap_fixed[0] = -5.781250, result_float[0] = -5.452863
result_ap_fixed[1] = -5.843750, result_float[1] = -5.953544
result_ap_fixed[2] = -8.468750, result_float[2] = -8.320899
result_ap_fixed[3] = 1.250000, result_float[3] = 1.922730
result_ap_fixed[4] = -9.593750, result_float[4] = -9.907320
result_ap_fixed[5] = 2.531250, result_float[5] = 3.078276
result_ap_fixed[6] = -5.437500, result_float[6] = -5.247157
result_ap_fixed[7] = -14.062500, result_float[7] = -14.017766
result_ap_fixed[8] = -0.843750, result_float[8] = -0.499992
result_ap_fixed[9] = -8.000000, result_float[9] = -7.772581

X_POS = 567, Y_POS = 185
                            
      ******                
    *********               
   ****    ***              
   **      **               
   **      **               
   **    ****               
   **   ****                
    ******                  
     ****                   
    *****                   
   ******                   
 ****   ***                 
 ****    **                 
***      ***                
**        ***               
*          **               
*          **               
*           **              
**         ***              
 **        **               
 ***       **               
  ***********               
   ********                 
                            
                            
                            
                            
around_count = 34, center_count = 51, center/around = 1.500000
result_out = 3
max_id_hw = 3
max_id_sw = 3
result_ap_fixed[0] = -5.812500, result_float[0] = -5.792901
result_ap_fixed[1] = -4.656250, result_float[1] = -4.453804
result_ap_fixed[2] = -7.750000, result_float[2] = -7.388370
result_ap_fixed[3] = -0.406250, result_float[3] = 0.467601
result_ap_fixed[4] = -7.093750, result_float[4] = -7.120575
result_ap_fixed[5] = -1.031250, result_float[5] = -0.734315
result_ap_fixed[6] = -5.156250, result_float[6] = -5.283511
result_ap_fixed[7] = -8.562500, result_float[7] = -8.129398
result_ap_fixed[8] = -3.375000, result_float[8] = -2.987090
result_ap_fixed[9] = -1.250000, result_float[9] = -0.776801

X_POS = 572, Y_POS = 185
                            
 ******                     
********                    
**    ***                   
      **                    
      **                    
    ****                    
   ****                     
*****                       
****                        
****                        
****                        
   ***                      
    **                      
    ***                     
     ***                    
      **                    
      **                    
       **                   
      ***                   
      **                    
      **                    
********                    
******                      
                            
                            
                            
                            
around_count = 34, center_count = 27, center/around = 0.794118
result_out = 2
max_id_hw = 2
max_id_sw = 2
result_ap_fixed[0] = -2.468750, result_float[0] = -2.650382
result_ap_fixed[1] = 0.250000, result_float[1] = 0.538665
result_ap_fixed[2] = 0.375000, result_float[2] = 1.258284
result_ap_fixed[3] = -0.375000, result_float[3] = 0.272008
result_ap_fixed[4] = -2.406250, result_float[4] = -2.322924
result_ap_fixed[5] = -5.187500, result_float[5] = -5.700843
result_ap_fixed[6] = -3.531250, result_float[6] = -3.706063
result_ap_fixed[7] = -0.500000, result_float[7] = 0.025969
result_ap_fixed[8] = -2.031250, result_float[8] = -1.912965
result_ap_fixed[9] = -3.031250, result_float[9] = -3.784628

X_POS = 577, Y_POS = 185
                            
**                          
***                         
 ***                        
 **                         
 **                         
***                         
**                          
                            
                            
                            
                            
*                           
*                           
**                          
***                         
 **                         
 **                         
  **                        
 ***                        
 **                         
 **                         
***                         
*                           
                            
                            
                            
                            
around_count = 38, center_count = 0, center/around = 0.000000
result_out = 2
max_id_hw = 2
max_id_sw = 3
result_ap_fixed[0] = -1.718750, result_float[0] = -2.488659
result_ap_fixed[1] = -1.875000, result_float[1] = -2.093781
result_ap_fixed[2] = 0.812500, result_float[2] = 1.519924
result_ap_fixed[3] = 0.562500, result_float[3] = 1.925102
result_ap_fixed[4] = -2.687500, result_float[4] = -3.053965
result_ap_fixed[5] = -1.375000, result_float[5] = -1.572820
result_ap_fixed[6] = -3.250000, result_float[6] = -3.985408
result_ap_fixed[7] = -1.687500, result_float[7] = -1.889108
result_ap_fixed[8] = 0.312500, result_float[8] = 0.334581
result_ap_fixed[9] = -0.875000, result_float[9] = -1.184953

X_POS = 582, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.781250, result_float[0] = -1.287772
result_ap_fixed[1] = -1.281250, result_float[1] = -1.798043
result_ap_fixed[2] = -0.156250, result_float[2] = 0.356295
result_ap_fixed[3] = 0.312500, result_float[3] = 1.086336
result_ap_fixed[4] = -1.343750, result_float[4] = -2.392095
result_ap_fixed[5] = -0.281250, result_float[5] = -0.224637
result_ap_fixed[6] = -1.500000, result_float[6] = -2.600045
result_ap_fixed[7] = -1.468750, result_float[7] = -2.357529
result_ap_fixed[8] = 0.468750, result_float[8] = 0.972850
result_ap_fixed[9] = -0.468750, result_float[9] = -0.335838

X_POS = 587, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.781250, result_float[0] = -1.220394
result_ap_fixed[1] = -1.281250, result_float[1] = -1.839818
result_ap_fixed[2] = -0.187500, result_float[2] = 0.401741
result_ap_fixed[3] = 0.250000, result_float[3] = 1.110537
result_ap_fixed[4] = -1.375000, result_float[4] = -2.384243
result_ap_fixed[5] = -0.281250, result_float[5] = -0.168204
result_ap_fixed[6] = -1.531250, result_float[6] = -2.608488
result_ap_fixed[7] = -1.500000, result_float[7] = -2.379575
result_ap_fixed[8] = 0.468750, result_float[8] = 0.984628
result_ap_fixed[9] = -0.375000, result_float[9] = -0.316269

X_POS = 592, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.812500, result_float[0] = -1.300202
result_ap_fixed[1] = -1.312500, result_float[1] = -1.806584
result_ap_fixed[2] = -0.218750, result_float[2] = 0.371900
result_ap_fixed[3] = 0.312500, result_float[3] = 1.214317
result_ap_fixed[4] = -1.375000, result_float[4] = -2.446056
result_ap_fixed[5] = -0.218750, result_float[5] = -0.112995
result_ap_fixed[6] = -1.625000, result_float[6] = -2.746831
result_ap_fixed[7] = -1.562500, result_float[7] = -2.425787
result_ap_fixed[8] = 0.468750, result_float[8] = 1.018132
result_ap_fixed[9] = -0.437500, result_float[9] = -0.380261

X_POS = 597, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                           *
                           *
                           *
                           *
                           *
                           *
                           *
                           *
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 8, center_count = 0, center/around = 0.000000
result_out = 8
max_id_hw = 8
max_id_sw = 8
result_ap_fixed[0] = -0.875000, result_float[0] = -1.595969
result_ap_fixed[1] = -1.031250, result_float[1] = -1.375270
result_ap_fixed[2] = 0.031250, result_float[2] = 0.657372
result_ap_fixed[3] = 0.156250, result_float[3] = 0.948161
result_ap_fixed[4] = -1.750000, result_float[4] = -2.468961
result_ap_fixed[5] = -0.031250, result_float[5] = 0.506728
result_ap_fixed[6] = -1.812500, result_float[6] = -3.221714
result_ap_fixed[7] = -1.687500, result_float[7] = -2.368360
result_ap_fixed[8] = 0.437500, result_float[8] = 1.243422
result_ap_fixed[9] = -0.531250, result_float[9] = -0.634587

X_POS = 602, Y_POS = 185
                            
                            
                            
                            
                        ****
                        ****
                       ***  
                      ***   
                      **    
                      **    
                      **    
                      **    
                      **    
                      **    
                      ******
                       *****
                            
                            
                            
                            
                            
                            
                           *
                          **
                          **
                          **
                          **
                          **
around_count = 24, center_count = 17, center/around = 0.708333
result_out = 4
max_id_hw = 4
max_id_sw = 4
result_ap_fixed[0] = -0.937500, result_float[0] = -1.223027
result_ap_fixed[1] = -2.156250, result_float[1] = -2.179262
result_ap_fixed[2] = -2.187500, result_float[2] = -2.192808
result_ap_fixed[3] = -2.125000, result_float[3] = -2.247617
result_ap_fixed[4] = -0.093750, result_float[4] = -0.292071
result_ap_fixed[5] = -2.125000, result_float[5] = -2.427494
result_ap_fixed[6] = -1.375000, result_float[6] = -2.113518
result_ap_fixed[7] = -1.375000, result_float[7] = -1.207759
result_ap_fixed[8] = -1.500000, result_float[8] = -1.335060
result_ap_fixed[9] = -4.406250, result_float[9] = -5.568094

X_POS = 607, Y_POS = 185
                            
                            
                            
                            
                   *******  
                   ******** 
                  ***     **
                 ***      **
                 **       **
                 **      ***
                 **      ** 
                 **      ** 
                 **    **** 
                 **    **** 
                 ********** 
                  ********* 
                        **  
                        *   
                       **   
                       **   
                       **   
                       **   
                      ***   
                     ***    
                     **     
                     **     
                     **     
                     **     
around_count = 31, center_count = 34, center/around = 1.096774
result_out = 4
max_id_hw = 4
max_id_sw = 4
result_ap_fixed[0] = -4.031250, result_float[0] = -4.133974
result_ap_fixed[1] = -2.625000, result_float[1] = -2.239853
result_ap_fixed[2] = -4.968750, result_float[2] = -4.674777
result_ap_fixed[3] = -1.968750, result_float[3] = -1.642591
result_ap_fixed[4] = 4.281250, result_float[4] = 5.110765
result_ap_fixed[5] = -2.531250, result_float[5] = -2.270406
result_ap_fixed[6] = -3.500000, result_float[6] = -3.630763
result_ap_fixed[7] = -3.843750, result_float[7] = -3.728224
result_ap_fixed[8] = -0.656250, result_float[8] = -0.452520
result_ap_fixed[9] = -2.781250, result_float[9] = -2.926447

X_POS = 612, Y_POS = 185
                            
                            
                            
                            
              *******       
              ********      
             ***     **     
            ***      ***    
            **       ***    
            **      ***     
            **      **      
            **      **      
            **    ****      
            **    ****      
            **********      
             *********      
                   **       
                   *        
                  **        
                  **        
                  **        
                  **        
                 ***        
                ***         
                **          
                **          
                **          
                **          
around_count = 6, center_count = 66, center/around = 11.000000
result_out = 8
max_id_hw = 8
max_id_sw = 8
result_ap_fixed[0] = -12.968750, result_float[0] = -13.174346
result_ap_fixed[1] = -6.468750, result_float[1] = -6.281274
result_ap_fixed[2] = -6.843750, result_float[2] = -6.845981
result_ap_fixed[3] = 2.218750, result_float[3] = 2.776402
result_ap_fixed[4] = -2.718750, result_float[4] = -2.315293
result_ap_fixed[5] = -6.250000, result_float[5] = -6.071845
result_ap_fixed[6] = -10.843750, result_float[6] = -11.015667
result_ap_fixed[7] = -7.000000, result_float[7] = -6.841413
result_ap_fixed[8] = 3.750000, result_float[8] = 4.229207
result_ap_fixed[9] = -0.437500, result_float[9] = -0.011891

X_POS = 617, Y_POS = 185
                            
                            
                            
                            
         *******            
         ********           
        ***     **          
       ***      ***         
       **       ***         
       **      ***          
       **      **           
       **      **           
       **    ****           
       **    ****           
       **********           
        *********           
              **            
              *             
             **             
             **             
             **             
             **             
            ***             
           ***              
           **               
           **               
           **               
           **               
around_count = 6, center_count = 66, center/around = 11.000000
result_out = 8
max_id_hw = 8
max_id_sw = 8
result_ap_fixed[0] = -9.687500, result_float[0] = -9.947154
result_ap_fixed[1] = -9.187500, result_float[1] = -9.279012
result_ap_fixed[2] = -7.531250, result_float[2] = -7.597316
result_ap_fixed[3] = 1.875000, result_float[3] = 2.737737
result_ap_fixed[4] = -4.656250, result_float[4] = -4.532206
result_ap_fixed[5] = -3.656250, result_float[5] = -3.266784
result_ap_fixed[6] = -12.343750, result_float[6] = -12.734062
result_ap_fixed[7] = -3.125000, result_float[7] = -2.826399
result_ap_fixed[8] = 4.875000, result_float[8] = 5.315073
result_ap_fixed[9] = 2.281250, result_float[9] = 2.924442

X_POS = 622, Y_POS = 185
                            
                            
                            
                            
    *******                 
    ********                
   ***     **               
  ***      ***              
  **       ***              
  **      ***               
  **      **                
  **      **                
  **    ****                
  **    ****                
  **********                
   *********                
         **                 
         *                  
        **                  
        **                  
        **                  
        **                  
       ***                  
      ***                   
      **                    
      **                    
      **                    
      **                    
around_count = 14, center_count = 49, center/around = 3.500000
result_out = 7
max_id_hw = 7
max_id_sw = 7
result_ap_fixed[0] = -2.625000, result_float[0] = -2.513616
result_ap_fixed[1] = -1.531250, result_float[1] = -1.173316
result_ap_fixed[2] = -0.718750, result_float[2] = 0.351554
result_ap_fixed[3] = -1.343750, result_float[3] = -1.153538
result_ap_fixed[4] = 0.281250, result_float[4] = 0.777785
result_ap_fixed[5] = -10.000000, result_float[5] = -10.576931
result_ap_fixed[6] = -6.250000, result_float[6] = -6.237439
result_ap_fixed[7] = 0.687500, result_float[7] = 1.322895
result_ap_fixed[8] = -5.281250, result_float[8] = -4.995121
result_ap_fixed[9] = -5.562500, result_float[9] = -5.764553

X_POS = 627, Y_POS = 185
                            
                            
                            
                            
******                      
*******                     
*     **                    
      ***                   
      ***                   
     ***                    
     **                     
     **                     
   ****                     
   ****                     
*******                     
*******                     
    **                      
    *                       
   **                       
   **                       
   **                       
   **                       
  ***                       
 ***                        
 **                         
 **                         
 **                         
 **                         
around_count = 24, center_count = 32, center/around = 1.333333
result_out = 0
max_id_hw = 0
max_id_sw = 0
result_ap_fixed[0] = 0.687500, result_float[0] = 0.791694
result_ap_fixed[1] = -2.156250, result_float[1] = -2.344940
result_ap_fixed[2] = -4.187500, result_float[2] = -4.291689
result_ap_fixed[3] = -4.375000, result_float[3] = -4.177928
result_ap_fixed[4] = -0.062500, result_float[4] = -0.151814
result_ap_fixed[5] = -2.593750, result_float[5] = -3.096778
result_ap_fixed[6] = 0.031250, result_float[6] = 0.079751
result_ap_fixed[7] = -2.468750, result_float[7] = -2.334525
result_ap_fixed[8] = -1.500000, result_float[8] = -1.337001
result_ap_fixed[9] = -0.312500, result_float[9] = -0.457717

X_POS = 632, Y_POS = 185
                            
                            
                            
                            
*                           
**                          
 **                         
 ***                        
 ***                        
***                         
**                          
**                          
**                          
**                          
**                          
**                          
*                           
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 25, center_count = 0, center/around = 0.000000
result_out = 3
max_id_hw = 3
max_id_sw = 3
result_ap_fixed[0] = -0.562500, result_float[0] = -1.318965
result_ap_fixed[1] = -2.125000, result_float[1] = -2.567228
result_ap_fixed[2] = -0.843750, result_float[2] = -0.476693
result_ap_fixed[3] = 0.156250, result_float[3] = 1.306376
result_ap_fixed[4] = -1.281250, result_float[4] = -1.726599
result_ap_fixed[5] = -0.906250, result_float[5] = -1.148492
result_ap_fixed[6] = -2.125000, result_float[6] = -2.973163
result_ap_fixed[7] = -0.718750, result_float[7] = -0.478881
result_ap_fixed[8] = -0.093750, result_float[8] = 0.278913
result_ap_fixed[9] = -0.187500, result_float[9] = -0.585260

X_POS = 637, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.750000, result_float[0] = -1.350178
result_ap_fixed[1] = -1.125000, result_float[1] = -1.817338
result_ap_fixed[2] = -0.125000, result_float[2] = 0.408225
result_ap_fixed[3] = 0.312500, result_float[3] = 1.204863
result_ap_fixed[4] = -1.312500, result_float[4] = -2.470115
result_ap_fixed[5] = -0.093750, result_float[5] = -0.141706
result_ap_fixed[6] = -1.343750, result_float[6] = -2.660645
result_ap_fixed[7] = -1.250000, result_float[7] = -2.317636
result_ap_fixed[8] = 0.468750, result_float[8] = 0.968406
result_ap_fixed[9] = -0.437500, result_float[9] = -0.434542

X_POS = 642, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.687500, result_float[0] = -1.279108
result_ap_fixed[1] = -1.093750, result_float[1] = -1.801685
result_ap_fixed[2] = -0.156250, result_float[2] = 0.382397
result_ap_fixed[3] = 0.312500, result_float[3] = 1.128036
result_ap_fixed[4] = -1.250000, result_float[4] = -2.419693
result_ap_fixed[5] = -0.062500, result_float[5] = -0.134422
result_ap_fixed[6] = -1.312500, result_float[6] = -2.609115
result_ap_fixed[7] = -1.343750, result_float[7] = -2.328017
result_ap_fixed[8] = 0.437500, result_float[8] = 0.966102
result_ap_fixed[9] = -0.406250, result_float[9] = -0.377428

X_POS = 647, Y_POS = 185
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
around_count = 0, center_count = 0, 
result_out = 8
max_id_hw = 8
max_id_sw = 3
result_ap_fixed[0] = -0.718750, result_float[0] = -1.336163
result_ap_fixed[1] = -1.093750, result_float[1] = -1.808135
result_ap_fixed[2] = -0.250000, result_float[2] = 0.418296
result_ap_fixed[3] = 0.343750, result_float[3] = 1.161598
result_ap_fixed[4] = -1.343750, result_float[4] = -2.456500
result_ap_fixed[5] = -0.125000, result_float[5] = -0.176320
result_ap_fixed[6] = -1.312500, result_float[6] = -2.647960
result_ap_fixed[7] = -1.281250, result_float[7] = -2.320159
result_ap_fixed[8] = 0.437500, result_float[8] = 0.986491
result_ap_fixed[9] = -0.437500, result_float[9] = -0.386770
INFO: [SIM 1] CSim done with 0 errors.
INFO: [SIM 3] *************** CSIM finish ***************


center/around の値で手書き数字のセンタリングをしても良いのかな?とおもう。もう少しデータを収集したい。
なお、7 は認識できなかった。8, 9 は認識できた。
  1. 2018年06月24日 07:59 |
  2. DNN
  3. | トラックバック:0
  4. | コメント:0

DMA付きテンプレートを使用したMNISTのCNN1(C ソースコード)

MNISTのCNN 用のDMA IP を”TensorFlow + Kerasを使ってみた20(特徴マップ3個のMNIST用CNNをVivado HLSで実装)”のテンプレートを使用した特徴マップ3個のMNIST のCNN に付けた。これで、PYNQ に搭載した”手書き数字認識用畳み込みニューラルネットワーク回路の製作3(畳み込みNN)”の入出力フォーマットと同じになったので、”Kerasを使用したMNIST CNNで手書き文字認識1(以前のVivado プロジェクトをVivado 2017.4に変換)”のmnist_conv_nn10_sDMA IP と入れ替えれば、DMA 付きテンプレートを使用したMNIST のCNN を試してみることができる。

DMA 付きテンプレートを使用したMNIST のCNN として作成したmnist_conv_nn3_ko_dma プロジェクトを示す。
mnist_conv_nn3_ko_dma_1_180624.png

ここで、”MNISTのCNN 用DMA IP 1(C シミュレーション)”のmnist_square_dma.cpp を変更したので、貼っておく。

// mnist_square_dma.cpp
// 2018/06/19 by marsee
// 2018/06/23 : 表示を追加
//

#include <ap_fixed.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>

int conv_rgb2y(int rgb);

int mnist_square_dma(int in[22400], int addr_offset, hls::stream<ap_axiu<8,1,1,1> >&outs){
//#pragma HLS INTERFACE axis register both port=outs
//#pragma HLS INTERFACE m_axi depth=22400 port=in offset=slave
//#pragma HLS INTERFACE s_axilite port=return

    ap_axiu<8,1,1,1> pixel;

#ifndef __SYNTHESIS__
    int around_count = 0;
    int center_count = 0;
#endif

    row_count: for(int i=0; i<28; i++){
        column_count: for(int j=0; j<28; j++){
#pragma HLS PIPELINE II=1
            pixel.data = 255 - conv_rgb2y(in[addr_offset+i*800+j]);
            // 白地に黒数字から黒地に白数字に変換
#ifndef __SYNTHESIS__
            if(i<3 || i>24 || j<3 || j>24){
                if(pixel.data >= 0x80)
                    around_count++;
            }else if((i>6 && i<22)&&(j>3 && j<24)){
                if(pixel.data >= 0x80)
                    center_count++;
            }

            if(pixel.data >= 0x80)
                printf("*");
            else
                printf(" ");
#endif
            if(i==0 && j==0)
                pixel.user = 1;
            else
                pixel.user = 0;
            if(j == 27)
                pixel.last = 1;
            else
                pixel.last = 0;

            outs << pixel;
        }
#ifndef __SYNTHESIS__
        printf("\n");
#endif
    }
#ifndef __SYNTHESIS__
    printf("around_count = %d, center_count = %d, ", around_count, center_count);
    if(around_count != 0)
        printf("center/around = %f\n", (float)center_count/(float)around_count);
    else
        printf("\n");
#endif

    return(0);
}

// RGBからYへの変換
// RGBのフォーマットは、{8'd0, R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y =  0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int conv_rgb2y(int rgb){
    int r, g, b, y_f;
    int y;

    b = rgb & 0xff;
    g = (rgb>>8) & 0xff;
    r = (rgb>>16) & 0xff;

    y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
    y = y_f >> 8; // 256で割る

    if (y >= 256)
        y = 255;

    return(y);
}


なお、around_count と center_count は、28 x 28 ピクセルのMNIST の手書き文字の周り 3 ピクセル分、(3+3)x28 + (3+3) x 26 = 300 ピクセルと真ん中の 300 ピクセルの 0x80 より大きなピクセルを数えて、その比を求めようと思う。これは、手書き文字がどこにあるのか?をサーチするのに使用したい。とりあえず、どのような結果になるかを検証してみよう。

次に、mnist_conv_nn3_hlss_dma.cpp を貼っておく。

// mnist_conv_nn3_hlss_dma.cpp
// 2018/06/23 by marsee
//

#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include <hls_video.h>

#include "layer_general.h"
#include "mnist_conv_nn3_hlss.h"

int mnist_square_dma(int in[22400], int addr_offset, hls::stream<ap_axiu<8,1,1,1> >&outs);

int input_layer(hls::stream<ap_axiu<8,1,INPUT_CHANNELS,1> >&ins,
    hls::stream<ap_fixed_axis<INPUT_BIT_LENGTH,INPUT_INTEGER_LEN,INPUT_CHANNELS,1> >&outs);

int conv_layer1(hls::stream<ap_fixed_axis<INPUT_BIT_LENGTH,INPUT_INTEGER_LEN,INPUT_CHANNELS,1> >& ins,
    hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> >& outs);

int relu_conv1(hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> >& ins,
    hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> >& outs);

int max_pooling(hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> >& ins,
    hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> >& outs);

int affine_layer1(hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> >& ins,
    hls::stream<ap_fixed_axis<AFFINE_BIT_LENGTH,AFFINE_INTEGER_LEN,1,1> >& outs);

int relu_affine1(hls::stream<ap_fixed_axis<AFFINE_BIT_LENGTH,AFFINE_INTEGER_LEN,1,1> >& ins,
    hls::stream<ap_fixed_axis<AFFINE_BIT_LENGTH,AFFINE_INTEGER_LEN,1,1> >& outs);

int affine_layer2(hls::stream<ap_fixed_axis<AFFINE_BIT_LENGTH,AFFINE_INTEGER_LEN,1,1> >& ins,
    hls::stream<ap_fixed_axis<OUTPUT_BIT_LENGTH,OUTPUT_INTEGER_LEN,1,1> >& outs);

int output_layer(hls::stream<ap_fixed_axis<OUTPUT_BIT_LENGTH,OUTPUT_INTEGER_LEN,1,1> >& ins, output_type& output,
    out_affine_type dot2[NUMBER_OF_OUTPUT_LAYER]);

int all_layers(int in[22400], int addr_offset, output_type& output, out_affine_type dot2[NUMBER_OF_OUTPUT_LAYER]){
#pragma HLS INTERFACE s_axilite port=addr_offset
#pragma HLS INTERFACE s_axilite port=output
#pragma HLS INTERFACE s_axilite port=dot2
//#pragma HLS ARRAY_PARTITION variable=dot2 complete dim=1
#pragma HLS DATAFLOW
#pragma HLS INTERFACE s_axilite port=return
#pragma HLS INTERFACE m_axi depth=22400 port=in offset=slave

    hls::stream<ap_axiu<8,1,1,1> > dma_outs;
// #pragma HLS STREAM variable=dma_outs depth=784 dim=1
    hls::stream<ap_fixed_axis<INPUT_BIT_LENGTH,INPUT_INTEGER_LEN,INPUT_CHANNELS,1> > outs_input_layer;
// #pragma HLS STREAM variable=outs_input_layer depth=560 dim=1
    hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> > outs_conv_layer;
// #pragma HLS STREAM variable=outs_conv_layer depth=312 dim=1
    hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> > outs_relu_conv1;
// #pragma HLS STREAM variable=outs_relu depth=312 dim=1
    hls::stream<ap_fixed_axis<CONV_BIT_LENGTH,CONV_INTEGER_LEN,CONV_CHANNELS,1> > outs_max_pooling;
// #pragma HLS STREAM variable=outs_max_pooling depth=78 dim=1
    hls::stream<ap_fixed_axis<AFFINE_BIT_LENGTH,AFFINE_INTEGER_LEN,1,1> > outs_affine_layer1;
// #pragma HLS STREAM variable=outs_affine_layer1 depth=100 dim=1
    hls::stream<ap_fixed_axis<AFFINE_BIT_LENGTH,AFFINE_INTEGER_LEN,1,1> > outs_relu_affine1;
// #pragma HLS STREAM variable=outs_relu_affine1 depth=100 dim=1
    hls::stream<ap_fixed_axis<OUTPUT_BIT_LENGTH,OUTPUT_INTEGER_LEN,1,1> > outs_affine_layer2;
// #pragma HLS STREAM variable=outs_affine_layer2 depth=3 dim=1

    mnist_square_dma(in, addr_offset, dma_outs);
    input_layer(dma_outs, outs_input_layer);
    conv_layer1(outs_input_layer, outs_conv_layer);
    relu_conv1(outs_conv_layer, outs_relu_conv1);
    max_pooling(outs_relu_conv1, outs_max_pooling);
    affine_layer1(outs_max_pooling, outs_affine_layer1);
    relu_affine1(outs_affine_layer1, outs_relu_affine1);
    affine_layer2(outs_relu_affine1, outs_affine_layer2);
    output_layer(outs_affine_layer2, output, dot2);

    return(0);
}


テストベンチのmnist_conv_nn_hlss_dma_tb.cpp を貼っておく。

// mnist_conv_nn_hlss_dma_tb.cpp
// 2017/06/14 by marsee
// 畳み込み層のカーネル数 10
// 2017/06/29 : ストライドDMAのためのテストベンチ
// 2018/06/25 : mnist_conv_nn_hlss_dma_tb.cppに変更
//

#include <stdio.h>
#include <ap_fixed.h>

#include "conv1_weight.h"
#include "conv1_bias.h"
#include "af1_weight.h"
#include "af1_bias.h"
#include "af2_weight.h"
#include "af2_bias.h"

#include "bmp_header.h"
#include "mnist_conv_nn3_hlss.h"

int all_layers(int in[22400], int addr_offset, output_type& output, out_affine_type dot2[NUMBER_OF_OUTPUT_LAYER]);
int mnist_conv_nn_float(int in[22400], int addr_offset, float out[10]);
int max_ap_fixed(ap_fixed<12, 7, AP_TRN, AP_WRAP> out[10]);
int max_float(float out[10]);
float conv_rgb2y_soft(int rgb);

#define READ_BMP_FILE_NAME    "bmp_file0.bmp"

// 8
//#define X_POS    560
//#define Y_POS    183
#define X_POS    502
#define Y_POS    185
// 7
//#define X_POS    504
//#define Y_POS    184
// 5
//#define X_POS    390
//#define Y_POS    138
// 0
//#define X_POS    390
//#define Y_POS    70
#define WIDTH    28
#define HEIGHT    28

int main(){
    ap_fixed<12, 7, AP_TRN, AP_WRAP> result_ap_fixed[10];
    float result_float[10];
    int max_id_hw, max_id_sw, max_id_ref;
    int *in;
    int *inf;
    output_type result_out;

    BITMAPFILEHEADER bmpfhr; // BMPファイルのファイルヘッダ(for Read)
    BITMAPINFOHEADER bmpihr; // BMPファイルのINFOヘッダ(for Read)
    FILE *fbmpr;
    int *rd_bmp;
    int blue, green, red;

    if ((fbmpr = fopen(READ_BMP_FILE_NAME, "rb")) == NULL){ // test.bmp をオープン
        fprintf(stderr, "Can't open ");
        fprintf(stderr, READ_BMP_FILE_NAME);
        fprintf(stderr, " by binary read mode\n");
        exit(1);
    }
    // bmpヘッダの読み出し
    fread(&bmpfhr.bfType, sizeof(uint16_t), 1, fbmpr);
    fread(&bmpfhr.bfSize, sizeof(uint32_t), 1, fbmpr);
    fread(&bmpfhr.bfReserved1, sizeof(uint16_t), 1, fbmpr);
    fread(&bmpfhr.bfReserved2, sizeof(uint16_t), 1, fbmpr);
    fread(&bmpfhr.bfOffBits, sizeof(uint32_t), 1, fbmpr);
    fread(&bmpihr, sizeof(BITMAPINFOHEADER), 1, fbmpr);

    // ピクセルを入れるメモリをアロケートする
    if ((rd_bmp =(int *)malloc(sizeof(int) * (bmpihr.biWidth * bmpihr.biHeight))) == NULL){
        fprintf(stderr, "Can't allocate rd_bmp memory\n");
        exit(1);
    }

    if ((in =(int *)malloc(sizeof(int) * (800 * 28))) == NULL){
        fprintf(stderr, "Can't allocate (ap_ufixed<8, 0, AP_TRN_ZERO, AP_SAT>)in memory\n");
        exit(1);
    }

    if ((inf =(int *)malloc(sizeof(int) * (800 * 28))) == NULL){
        fprintf(stderr, "Can't allocate (float)inf memory\n");
        exit(1);
    }

    // rd_bmp にBMPのピクセルを代入。その際に、行を逆転する必要がある
    for (int y=0; y<bmpihr.biHeight; y++){
        for (int x=0; x<bmpihr.biWidth; x++){
            blue = fgetc(fbmpr);
            green = fgetc(fbmpr);
            red = fgetc(fbmpr);
            rd_bmp[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] = (blue & 0xff) | ((green & 0xff)<<8) | ((red & 0xff)<<16);
        }
    }
    fclose(fbmpr);

    int x_pos_p = X_POS;
    for(int i=0; i<30; i++){
        // rd_bmp を in と inf に入力
        for (int y=Y_POS; y<Y_POS+HEIGHT; y++){
            for (int x=0; x<bmpihr.biWidth; x++){
                in[(y-Y_POS)*bmpihr.biWidth+x] = rd_bmp[y*bmpihr.biWidth+x];
                inf[(y-Y_POS)*bmpihr.biWidth+x] = rd_bmp[y*bmpihr.biWidth+x];
            }
        }

        printf("\nX_POS = %d, Y_POS = %d\n", x_pos_p, Y_POS);

        all_layers(in, x_pos_p, result_out, result_ap_fixed);
        mnist_conv_nn_float(inf, x_pos_p, result_float);

        printf("result_out = %d\n", (int)result_out);

        max_id_hw = max_ap_fixed(result_ap_fixed);
        max_id_sw = max_float(result_float);

        printf("max_id_hw = %d\n", max_id_hw);
        printf("max_id_sw = %d\n", max_id_sw);

        for(int i=0; i<10; i++){
            printf("result_ap_fixed[%d] = %f", i, (float)result_ap_fixed[i]);
            printf(", result_float[%d] = %f\n", i, result_float[i]);
        }

        x_pos_p += 5;
    }

    return(0);
}

int mnist_conv_nn_float(int in[22400], int addr_offset, float out[10]){
    float buf[28][28];
    float conv_out[CONV_CHANNELS][24][24];
    float pool_out[CONV_CHANNELS][12][12];
    float dot1[100];
    float dot2[10];

    // 手書き数字の値を表示
    /*for (int i=0; i<28; i++){
        for (int j=0; j<800; j++){
            if (j>=addr_offset && j<addr_offset+28)
                printf("%2x, ", (int)(conv_rgb2y_soft(in[i*800+j])*256.0));
        }
        printf("\n");
    }*/

    buf_copy1: for(int i=0; i<28; i++){
        buf_copy2: for(int j=0; j<800; j++){
            if (j>=addr_offset && j<addr_offset+28)
                buf[i][j-addr_offset] = (float)0.99609375 - (float)conv_rgb2y_soft(in[i*800+j]);
        }
    }

    // Convolutional Neural Network 5x5 kernel, Stride = 1, Padding = 0
    // + ReLU
    CONV1: for(int i=0; i<CONV_CHANNELS; i++){    // カーネルの個数
        CONV2: for(int j=0; j<24; j++){
            CONV3: for(int k=0; k<24; k++){
                conv_out[i][j][k] = 0;
                CONV4: for(int m=0; m<5; m++){
                    CONV5: for(int n=0; n<5; n++){
                        conv_out[i][j][k] += buf[j+m][k+n] * conv1_fweight[i][0][m][n];
                    }
                }
                conv_out[i][j][k] += conv1_fbias[i];

                if(conv_out[i][j][k]<0)    // ReLU
                    conv_out[i][j][k] = 0;
            }
        }
    }

    // Pooling Kernel = 2 x 2, Stride = 2
    POOL1: for(int i=0; i<CONV_CHANNELS; i++){
        POOL2: for(int j=0; j<24; j += 2){
            POOL3: for(int k=0; k<24; k += 2){
                POOL4: for(int m=0; m<2; m++){
                    POOL5: for(int n=0; n<2; n++){
                        if(m==0 && n==0){
                            pool_out[i][j/2][k/2] = conv_out[i][j][k];
                        } else if(pool_out[i][j/2][k/2] < conv_out[i][j+m][k+n]){
                            pool_out[i][j/2][k/2] = conv_out[i][j+m][k+n];
                        }
                    }
                }
            }
        }
    }

    af1_dot1: for(int col=0; col<100; col++){
        dot1[col] = 0;
        af1_dot2: for(int i=0; i<CONV_CHANNELS; i++){
            af1_dot3: for(int j=0; j<12; j++){
                af1_dot4: for(int k=0; k<12; k++){
                    dot1[col] += pool_out[i][j][k]*af1_fweight[i*12*12+j*12+k][col];
                }
            }
        }
        dot1[col] += af1_fbias[col];

        if(dot1[col] < 0)    // ReLU
            dot1[col] = 0;
    }

    af2_dot1: for(int col=0; col<10; col++){
        dot2[col] = 0;
        af2_dot2: for(int row=0; row<100; row++){
            dot2[col] += dot1[row]*af2_fweight[row][col];
        }
        dot2[col] += af2_fbias[col];

        out[col] = dot2[col];
    }

    return(0);
}

int max_ap_fixed(ap_fixed<12, 7, AP_TRN, AP_WRAP> out[10]){
    int max_id;
    ap_fixed<12, 7, AP_TRN, AP_WRAP> max;

    for(int i=0; i<10; i++){
        if(i == 0){
            max = out[0];
            max_id = 0;
        }else if(out[i]>max){
            max = out[i];
            max_id = i;
        }
    }
    return(max_id);
}

int max_float(float out[10]){
    int max_id;
    float max;

    for(int i=0; i<10; i++){
        if(i == 0){
            max = out[0];
            max_id = 0;
        }else if(out[i]>max){
            max = out[i];
            max_id = i;
        }
    }
    return(max_id);
}


// RGBからYへの変換
// RGBのフォーマットは、{8'd0, R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y =  0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
// 2017/06/30 : retval を float にした
float conv_rgb2y_soft(int rgb){
    int r, g, b, y_f;
    int y;
    float y_float;

    b = rgb & 0xff;
    g = (rgb>>8) & 0xff;
    r = (rgb>>16) & 0xff;

    y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
    y = y_f >> 8; // 256で割る

    if (y >= 256)
        y = 255;

    y_float = (float)y/256.0;

    return(y_float);
}


X_POS、Y_POS の値を初期値として、X_POS の値を +5 ピクセルしながら X 軸方向にCNN をかけていく。
  1. 2018年06月24日 05:42 |
  2. DNN
  3. | トラックバック:0
  4. | コメント:0