FC2カウンター FPGAの部屋 TensorFlow + Kerasを使ってみた9(畳み込み層の重みをC のヘッダに変換)
FC2ブログ

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

FPGAの部屋

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

TensorFlow + Kerasを使ってみた9(畳み込み層の重みをC のヘッダに変換)

TensorFlow + Kerasを使ってみた8(全結合層の統計情報)”の続き。

前回は、全結合層の最大値、最小値、絶対値の最大値、最小値、標準偏差などの統計情報を取得した。今回は、”「ゼロから作るDeep Learning」の畳み込みニューラルネットワークのハードウェア化3”で行った重みやバイアスを C ヘッダにするPython コードをKeras でうまく行くかどうか?を確かめてみよう。

まずは、畳み込み層の重みをC のヘッダに変換してみよう。
結論から言うと、一部キャストを追加したら畳み込み層の重みをC のヘッダに変換するPython コードが動作した。
畳み込み層の重みをC のヘッダに変換するPython コード fwrite_conv_weight を示す。

# 畳み込み層の重みをCヘッダファイルに書き出す
# 2018/05/31 by marsee

def fwrite_conv_weight(weight, wfile_name, float_wt_name, fixed_wt_name, MAGNIFICATION):
    import datetime
    import numpy as np
    
    f = open(wfile_name, 'w')
    todaytime = datetime.datetime.today()
    f.write('// '+wfile_name+'\n')
    strdtime = todaytime.strftime("%Y/%m/%d %H:%M:%S")
    f.write('// {0} by marsee\n'.format(strdtime))
    f.write("\n")
    
    f.write('const float '+float_wt_name+'['+str(weight.shape[0])+']['+str(weight.shape[1])+']['+str(weight.shape[2])+']['+str(weight.shape[3])+'] = \n{\n')
    for i in range(weight.shape[0]):
        f.write("\t{\n")
        for j in range(weight.shape[1]):
            f.write("\t\t{\n")
            for k in range(weight.shape[2]):
                f.write("\t\t\t{")
                for m in range(weight.shape[3]):
                    f.write(str(weight[i][j][k][m]))
                    if (m==weight.shape[3]-1):
                        f.write("}")
                    else:
                        f.write(",")
                
                if (k==weight.shape[2]-1):
                    f.write("\n\t\t}\n")
                else:
                    f.write(",\n")

            if (j==weight.shape[1]-1):
                f.write("\t}\n")
            else:
                f.write(",\n")
        
        
        if (i==weight.shape[0]-1):
            f.write("};\n")
        else:
            f.write("\t,\n")

    f.write("\n")
    f.write('const ap_fixed<'+str(int(np.log2(MAGNIFICATION))+1)+', 1, AP_TRN_ZERO, AP_SAT> '+fixed_wt_name+'['+str(weight.shape[0])+']['+str(weight.shape[1])+']['+str(weight.shape[2])+']['+str(weight.shape[3])+'] = \n{\n')
    for i in range(weight.shape[0]):
        f.write("\t{\n")
        for j in range(weight.shape[1]):
            f.write("\t\t{\n")
            for k in range(weight.shape[2]):
                f.write("\t\t\t{")
                for m in range(weight.shape[3]):
                    w_int = int(weight[i][j][k][m]*MAGNIFICATION+0.5)
                    if (w_int > MAGNIFICATION-1):
                        w_int = MAGNIFICATION-1
                    elif (w_int < -MAGNIFICATION):
                        w_int = -MAGNIFICATION
                    f.write(str(float(w_int)/float(MAGNIFICATION)))
                    if (m==weight.shape[3]-1):
                        f.write("}")
                    else:
                        f.write(",")
                
                if (k==weight.shape[2]-1):
                    f.write("\n\t\t}\n")
                else:
                     f.write(",\n")

            if (j==weight.shape[1]-1):
                f.write("\t}\n")
            else:
                f.write(",\n")
        
        
        if (i==weight.shape[0]-1):
            f.write("};\n")
        else:
            f.write("\t,\n")
 
    f.close()


下に示すPython コードを実行すると畳み込み層の重みが conv1_weight10.h という C のヘッダファイルに出力される。
(2018/06/05:修正 畳み込み層の重みの配列は(カーネルサイズh,カーネルサイズw, 入力チャネル, 出力チャネル)ということなので、Pythonコードを修正した。@NORA__0013 さんありがとうございました。)

MAGNIFICATION_CONV = 2 ** (9-1)
fwrite_conv_weight(conv_layer_weight.transpose(3,2,0,1), 'conv1_weight10.h', 'conv1_fweight', 'conv1_weight', MAGNIFICATION_CONV)


conv1_weight10.h を貼っておく。

// conv1_weight10.h
// 2018/06/03 16:10:12 by marsee

const float conv1_fweight[10][1][5][5] = 
{
    {
        {
            {0.20261094,-0.07934965,-0.22467291,-0.43532223,-0.23157348},
            {-0.3398506,-0.33518496,-0.39792794,-0.08618319,0.16632372},
            {-0.5767307,-0.4275438,-0.072111405,0.3187846,0.2654636},
            {0.11835691,0.25123549,0.38731813,0.27912328,0.15583257},
            {0.13021287,0.38388866,0.24981308,0.022721838,-0.04710154}
        }
    }
    ,
    {
        {
            {-0.48611653,-0.6372757,-0.40548003,-0.33244497,-0.13435255},
            {0.15439186,-0.34480083,-0.56099683,-0.3915109,-0.41839477},
            {0.4068115,0.3836496,0.10779987,-0.1230321,-0.4722871},
            {0.3514016,0.3169199,0.32510042,0.2981098,-0.10132303},
            {0.16548221,0.2640638,0.3619229,0.35238296,0.113044925}
        }
    }
    ,
    {
        {
            {-0.13130069,0.18190795,0.15537558,-0.1676253,-0.14785497},
            {0.06223634,0.35684425,-0.112429045,-0.44136783,-0.10125857},
            {0.10510171,0.25642243,-0.2288756,-0.29937005,0.12721944},
            {0.021834752,0.008635783,0.040261764,-0.017128099,0.055860933},
            {-0.16628554,0.12985978,0.08550146,0.2620432,0.105794474}
        }
    }
    ,
    {
        {
            {0.15156339,0.4940948,0.22510909,0.19534814,0.24242142},
            {-0.20048767,0.3314954,0.45060343,0.116408214,0.054000396},
            {-0.5791418,-0.1274401,0.15244117,0.2987521,-0.008650972},
            {-0.65549827,-0.3982863,-0.23712645,-0.04862794,-0.030009096},
            {-0.25779864,-0.3313806,-0.025542857,0.04132852,0.12885101}
        }
    }
    ,
    {
        {
            {0.36471996,0.11910628,0.0868587,-0.26476386,-0.40310845},
            {0.35694337,0.07778469,0.1454417,-0.27914035,-0.43084973},
            {0.27650365,0.19447733,0.022587685,-0.4387378,-0.27778476},
            {0.35590482,0.060368076,-0.2499182,-0.33735904,-0.2462857},
            {0.13169082,-0.12147922,-0.19614659,-0.033236343,0.04993651}
        }
    }
    ,
    {
        {
            {-0.08484216,0.17010233,0.397805,0.18052064,-0.20084426},
            {0.19511358,0.2240115,0.23805015,-0.28208354,-0.30468363},
            {0.58113253,0.23622094,-0.1035163,-0.29351595,-0.2777929},
            {-0.12703945,-0.31102535,-0.45656392,-0.36484626,0.08292956},
            {-0.516542,-0.59745365,-0.34286296,0.064657405,-0.016369406}
        }
    }
    ,
    {
        {
            {-0.05747546,-0.025008501,0.2489682,0.0009843357,-0.31173185},
            {0.10129268,0.140934,0.18465307,-0.29655868,-0.3589846},
            {0.0927546,0.12933072,0.23520534,-0.13283624,-0.2216169},
            {0.01556351,0.19052765,0.26735055,-0.11904856,0.05286852},
            {-0.16821466,0.20077062,0.24849436,-0.027033936,-0.0066970563}
        }
    }
    ,
    {
        {
            {-0.46006405,-0.07625411,0.072767265,0.21414295,0.014650909},
            {-0.41662437,-0.01859824,0.20107509,0.14830865,0.08253051},
            {-0.26404095,-0.023522798,0.15815544,0.24796312,-0.08803863},
            {-0.27005908,0.030365303,0.3283318,0.015161242,0.014568055},
            {0.0034153308,0.10755768,0.23039222,-0.050392643,-0.17668988}
        }
    }
    ,
    {
        {
            {0.09082198,-0.033604637,0.113404974,0.20493641,0.14139216},
            {0.38919494,0.21474971,0.20264329,0.2751836,0.20002662},
            {0.33294797,0.37199846,0.37084493,0.10829608,0.17661056},
            {-0.5168951,-0.29824486,-0.32331055,-0.20219678,-0.22110288},
            {-0.62100536,-0.6191712,-0.5669018,-0.39315876,-0.28045934}
        }
    }
    ,
    {
        {
            {0.12587526,0.171594,0.19060391,-0.17589498,-0.2094244},
            {-0.013645746,0.22744659,0.12572204,-0.028848726,-0.37416157},
            {-0.2322505,-0.05975187,0.3344037,0.20712087,-0.084726445},
            {-0.14462651,-0.18951881,0.26089588,0.19588387,0.12522626},
            {-0.03129309,-0.2751198,-0.120508276,0.0149853965,0.06411268}
        }
    }
};

const ap_fixed<9, 1, AP_TRN, AP_WRAP> conv1_weight[10][1][5][5] = 
{
    {
        {
            {0.203125,-0.07421875,-0.22265625,-0.4296875,-0.2265625},
            {-0.3359375,-0.33203125,-0.39453125,-0.08203125,0.16796875},
            {-0.57421875,-0.421875,-0.06640625,0.3203125,0.265625},
            {0.1171875,0.25,0.38671875,0.27734375,0.15625},
            {0.12890625,0.3828125,0.25,0.0234375,-0.04296875}
        }
    }
    ,
    {
        {
            {-0.48046875,-0.6328125,-0.40234375,-0.328125,-0.12890625},
            {0.15625,-0.33984375,-0.55859375,-0.38671875,-0.4140625},
            {0.40625,0.3828125,0.109375,-0.1171875,-0.46875},
            {0.3515625,0.31640625,0.32421875,0.296875,-0.09765625},
            {0.1640625,0.265625,0.36328125,0.3515625,0.11328125}
        }
    }
    ,
    {
        {
            {-0.12890625,0.18359375,0.15625,-0.1640625,-0.14453125},
            {0.0625,0.35546875,-0.109375,-0.4375,-0.09765625},
            {0.10546875,0.2578125,-0.2265625,-0.296875,0.12890625},
            {0.0234375,0.0078125,0.0390625,-0.01171875,0.0546875},
            {-0.1640625,0.12890625,0.0859375,0.26171875,0.10546875}
        }
    }
    ,
    {
        {
            {0.15234375,0.4921875,0.2265625,0.1953125,0.2421875},
            {-0.1953125,0.33203125,0.44921875,0.1171875,0.0546875},
            {-0.57421875,-0.125,0.15234375,0.296875,-0.00390625},
            {-0.65234375,-0.39453125,-0.234375,-0.04296875,-0.02734375},
            {-0.25390625,-0.328125,-0.0234375,0.04296875,0.12890625}
        }
    }
    ,
    {
        {
            {0.36328125,0.1171875,0.0859375,-0.26171875,-0.3984375},
            {0.35546875,0.078125,0.14453125,-0.2734375,-0.42578125},
            {0.27734375,0.1953125,0.0234375,-0.43359375,-0.2734375},
            {0.35546875,0.05859375,-0.24609375,-0.33203125,-0.2421875},
            {0.1328125,-0.1171875,-0.19140625,-0.03125,0.05078125}
        }
    }
    ,
    {
        {
            {-0.08203125,0.171875,0.3984375,0.1796875,-0.1953125},
            {0.1953125,0.22265625,0.23828125,-0.27734375,-0.30078125},
            {0.58203125,0.234375,-0.1015625,-0.2890625,-0.2734375},
            {-0.125,-0.30859375,-0.453125,-0.359375,0.08203125},
            {-0.51171875,-0.59375,-0.33984375,0.06640625,-0.01171875}
        }
    }
    ,
    {
        {
            {-0.0546875,-0.01953125,0.25,0.0,-0.30859375},
            {0.1015625,0.140625,0.18359375,-0.29296875,-0.35546875},
            {0.09375,0.12890625,0.234375,-0.12890625,-0.21875},
            {0.015625,0.19140625,0.265625,-0.11328125,0.0546875},
            {-0.1640625,0.19921875,0.25,-0.0234375,-0.00390625}
        }
    }
    ,
    {
        {
            {-0.45703125,-0.07421875,0.07421875,0.21484375,0.015625},
            {-0.4140625,-0.015625,0.19921875,0.1484375,0.08203125},
            {-0.26171875,-0.01953125,0.15625,0.24609375,-0.0859375},
            {-0.265625,0.03125,0.328125,0.015625,0.015625},
            {0.00390625,0.109375,0.23046875,-0.046875,-0.171875}
        }
    }
    ,
    {
        {
            {0.08984375,-0.03125,0.11328125,0.203125,0.140625},
            {0.390625,0.21484375,0.203125,0.2734375,0.19921875},
            {0.33203125,0.37109375,0.37109375,0.109375,0.17578125},
            {-0.51171875,-0.29296875,-0.3203125,-0.19921875,-0.21875},
            {-0.6171875,-0.6171875,-0.5625,-0.390625,-0.27734375}
        }
    }
    ,
    {
        {
            {0.125,0.171875,0.19140625,-0.171875,-0.20703125},
            {-0.0078125,0.2265625,0.125,-0.0234375,-0.37109375},
            {-0.2265625,-0.0546875,0.3359375,0.20703125,-0.08203125},
            {-0.140625,-0.1875,0.26171875,0.1953125,0.125},
            {-0.02734375,-0.26953125,-0.1171875,0.015625,0.0625}
        }
    }
};

  1. 2018年05月31日 05:01 |
  2. TensorFlow, Keras
  3. | トラックバック:0
  4. | コメント:0

コメント

コメントの投稿


管理者にだけ表示を許可する

トラックバック URL
http://marsee101.blog.fc2.com/tb.php/4184-68b32794
この記事にトラックバックする(FC2ブログユーザー)