這篇文章主要為大家展示了如何實現(xiàn)Pytorch轉(zhuǎn)keras,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
創(chuàng)新互聯(lián)專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、萊山網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、購物商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為萊山等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。Pytorch憑借動態(tài)圖機制,獲得了廣泛的使用,大有超越tensorflow的趨勢,不過在工程應(yīng)用上,TF仍然占據(jù)優(yōu)勢。有的時候我們會遇到這種情況,需要把模型應(yīng)用到工業(yè)中,運用到實際項目上,TF支持的PB文件和TF的C++接口就成為了有效的工具。今天就給大家講解一下Pytorch轉(zhuǎn)成Keras的方法,進而我們也可以獲得Pb文件,因為Keras是支持tensorflow的,我將會在下一篇博客講解獲得Pb文件,并使用Pb文件的方法。
Pytorch To Keras
首先,我們必須有清楚的認識,網(wǎng)上以及github上一些所謂的pytorch轉(zhuǎn)換Keras或者Keras轉(zhuǎn)換成Pytorch的工具代碼幾乎不能運行或者有使用的局限性(比如僅僅能轉(zhuǎn)換某一些模型),但是我們是可以用這些轉(zhuǎn)換代碼中看出一些端倪來,比如二者的參數(shù)的尺寸(shape)的形式、channel的排序(first or last)是否一樣,掌握到差異性,就能根據(jù)這些差異自己編寫轉(zhuǎn)換代碼,沒錯,自己編寫轉(zhuǎn)換代碼,是最穩(wěn)妥的辦法。整個過程也就分為兩個部分。筆者將會以Nvidia開源的FlowNet為例,將開源的Pytorch代碼轉(zhuǎn)化為Keras模型。
按照Pytorch中模型的結(jié)構(gòu),編寫對應(yīng)的Keras代碼,用keras的函數(shù)式API,構(gòu)建起來會非常方便。
把Pytorch的模型參數(shù),按照層的名稱依次賦值給Keras的模型
以上兩步雖然看上去簡單,但實際我也走了不少彎路。這里一個關(guān)鍵的地方,就是參數(shù)的shape在兩個框架中是否統(tǒng)一,那當(dāng)然是不統(tǒng)一的。下面我以FlowNet為例。
Pytorch中的FlowNet代碼
我們僅僅展示層名稱和層參數(shù),就不把整個結(jié)構(gòu)貼出來了,否則會占很多的空間,形成水文。
先看用Keras搭建的flowNet模型,直接用model.summary()輸出模型信息
__________________________________________________________________________________________________ Layer (type) Output Shape Param # Connected to ================================================================================================== input_1 (InputLayer) (None, 6, 512, 512) 0 __________________________________________________________________________________________________ conv0 (Conv2D) (None, 64, 512, 512) 3520 input_1[0][0] __________________________________________________________________________________________________ leaky_re_lu_1 (LeakyReLU) (None, 64, 512, 512) 0 conv0[0][0] __________________________________________________________________________________________________ zero_padding2d_1 (ZeroPadding2D (None, 64, 514, 514) 0 leaky_re_lu_1[0][0] __________________________________________________________________________________________________ conv1 (Conv2D) (None, 64, 256, 256) 36928 zero_padding2d_1[0][0] __________________________________________________________________________________________________ leaky_re_lu_2 (LeakyReLU) (None, 64, 256, 256) 0 conv1[0][0] __________________________________________________________________________________________________ conv1_1 (Conv2D) (None, 128, 256, 256 73856 leaky_re_lu_2[0][0] __________________________________________________________________________________________________ leaky_re_lu_3 (LeakyReLU) (None, 128, 256, 256 0 conv1_1[0][0] __________________________________________________________________________________________________ zero_padding2d_2 (ZeroPadding2D (None, 128, 258, 258 0 leaky_re_lu_3[0][0] __________________________________________________________________________________________________ conv2 (Conv2D) (None, 128, 128, 128 147584 zero_padding2d_2[0][0] __________________________________________________________________________________________________ leaky_re_lu_4 (LeakyReLU) (None, 128, 128, 128 0 conv2[0][0] __________________________________________________________________________________________________ conv2_1 (Conv2D) (None, 128, 128, 128 147584 leaky_re_lu_4[0][0] __________________________________________________________________________________________________ leaky_re_lu_5 (LeakyReLU) (None, 128, 128, 128 0 conv2_1[0][0] __________________________________________________________________________________________________ zero_padding2d_3 (ZeroPadding2D (None, 128, 130, 130 0 leaky_re_lu_5[0][0] __________________________________________________________________________________________________ conv3 (Conv2D) (None, 256, 64, 64) 295168 zero_padding2d_3[0][0] __________________________________________________________________________________________________ leaky_re_lu_6 (LeakyReLU) (None, 256, 64, 64) 0 conv3[0][0] __________________________________________________________________________________________________ conv3_1 (Conv2D) (None, 256, 64, 64) 590080 leaky_re_lu_6[0][0] __________________________________________________________________________________________________ leaky_re_lu_7 (LeakyReLU) (None, 256, 64, 64) 0 conv3_1[0][0] __________________________________________________________________________________________________ zero_padding2d_4 (ZeroPadding2D (None, 256, 66, 66) 0 leaky_re_lu_7[0][0] __________________________________________________________________________________________________ conv4 (Conv2D) (None, 512, 32, 32) 1180160 zero_padding2d_4[0][0] __________________________________________________________________________________________________ leaky_re_lu_8 (LeakyReLU) (None, 512, 32, 32) 0 conv4[0][0] __________________________________________________________________________________________________ conv4_1 (Conv2D) (None, 512, 32, 32) 2359808 leaky_re_lu_8[0][0] __________________________________________________________________________________________________ leaky_re_lu_9 (LeakyReLU) (None, 512, 32, 32) 0 conv4_1[0][0] __________________________________________________________________________________________________ zero_padding2d_5 (ZeroPadding2D (None, 512, 34, 34) 0 leaky_re_lu_9[0][0] __________________________________________________________________________________________________ conv5 (Conv2D) (None, 512, 16, 16) 2359808 zero_padding2d_5[0][0] __________________________________________________________________________________________________ leaky_re_lu_10 (LeakyReLU) (None, 512, 16, 16) 0 conv5[0][0] __________________________________________________________________________________________________ conv5_1 (Conv2D) (None, 512, 16, 16) 2359808 leaky_re_lu_10[0][0] __________________________________________________________________________________________________ leaky_re_lu_11 (LeakyReLU) (None, 512, 16, 16) 0 conv5_1[0][0] __________________________________________________________________________________________________ zero_padding2d_6 (ZeroPadding2D (None, 512, 18, 18) 0 leaky_re_lu_11[0][0] __________________________________________________________________________________________________ conv6 (Conv2D) (None, 1024, 8, 8) 4719616 zero_padding2d_6[0][0] __________________________________________________________________________________________________ leaky_re_lu_12 (LeakyReLU) (None, 1024, 8, 8) 0 conv6[0][0] __________________________________________________________________________________________________ conv6_1 (Conv2D) (None, 1024, 8, 8) 9438208 leaky_re_lu_12[0][0] __________________________________________________________________________________________________ leaky_re_lu_13 (LeakyReLU) (None, 1024, 8, 8) 0 conv6_1[0][0] __________________________________________________________________________________________________ deconv5 (Conv2DTranspose) (None, 512, 16, 16) 8389120 leaky_re_lu_13[0][0] __________________________________________________________________________________________________ predict_flow6 (Conv2D) (None, 2, 8, 8) 18434 leaky_re_lu_13[0][0] __________________________________________________________________________________________________ leaky_re_lu_14 (LeakyReLU) (None, 512, 16, 16) 0 deconv5[0][0] __________________________________________________________________________________________________ upsampled_flow6_to_5 (Conv2DTra (None, 2, 16, 16) 66 predict_flow6[0][0] __________________________________________________________________________________________________ concatenate_1 (Concatenate) (None, 1026, 16, 16) 0 leaky_re_lu_11[0][0] leaky_re_lu_14[0][0] upsampled_flow6_to_5[0][0] __________________________________________________________________________________________________ inter_conv5 (Conv2D) (None, 512, 16, 16) 4728320 concatenate_1[0][0] __________________________________________________________________________________________________ deconv4 (Conv2DTranspose) (None, 256, 32, 32) 4202752 concatenate_1[0][0] __________________________________________________________________________________________________ predict_flow5 (Conv2D) (None, 2, 16, 16) 9218 inter_conv5[0][0] __________________________________________________________________________________________________ leaky_re_lu_15 (LeakyReLU) (None, 256, 32, 32) 0 deconv4[0][0] __________________________________________________________________________________________________ upsampled_flow5_to4 (Conv2DTran (None, 2, 32, 32) 66 predict_flow5[0][0] __________________________________________________________________________________________________ concatenate_2 (Concatenate) (None, 770, 32, 32) 0 leaky_re_lu_9[0][0] leaky_re_lu_15[0][0] upsampled_flow5_to4[0][0] __________________________________________________________________________________________________ inter_conv4 (Conv2D) (None, 256, 32, 32) 1774336 concatenate_2[0][0] __________________________________________________________________________________________________ deconv3 (Conv2DTranspose) (None, 128, 64, 64) 1577088 concatenate_2[0][0] __________________________________________________________________________________________________ predict_flow4 (Conv2D) (None, 2, 32, 32) 4610 inter_conv4[0][0] __________________________________________________________________________________________________ leaky_re_lu_16 (LeakyReLU) (None, 128, 64, 64) 0 deconv3[0][0] __________________________________________________________________________________________________ upsampled_flow4_to3 (Conv2DTran (None, 2, 64, 64) 66 predict_flow4[0][0] __________________________________________________________________________________________________ concatenate_3 (Concatenate) (None, 386, 64, 64) 0 leaky_re_lu_7[0][0] leaky_re_lu_16[0][0] upsampled_flow4_to3[0][0] __________________________________________________________________________________________________ inter_conv3 (Conv2D) (None, 128, 64, 64) 444800 concatenate_3[0][0] __________________________________________________________________________________________________ deconv2 (Conv2DTranspose) (None, 64, 128, 128) 395328 concatenate_3[0][0] __________________________________________________________________________________________________ predict_flow3 (Conv2D) (None, 2, 64, 64) 2306 inter_conv3[0][0] __________________________________________________________________________________________________ leaky_re_lu_17 (LeakyReLU) (None, 64, 128, 128) 0 deconv2[0][0] __________________________________________________________________________________________________ upsampled_flow3_to2 (Conv2DTran (None, 2, 128, 128) 66 predict_flow3[0][0] __________________________________________________________________________________________________ concatenate_4 (Concatenate) (None, 194, 128, 128 0 leaky_re_lu_5[0][0] leaky_re_lu_17[0][0] upsampled_flow3_to2[0][0] __________________________________________________________________________________________________ inter_conv2 (Conv2D) (None, 64, 128, 128) 111808 concatenate_4[0][0] __________________________________________________________________________________________________ predict_flow2 (Conv2D) (None, 2, 128, 128) 1154 inter_conv2[0][0] __________________________________________________________________________________________________ up_sampling2d_1 (UpSampling2D) (None, 2, 512, 512) 0 predict_flow2[0][0]
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:如何實現(xiàn)Pytorch轉(zhuǎn)keras-創(chuàng)新互聯(lián)
文章起源:http://jinyejixie.com/article32/ccsosc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、手機網(wǎng)站建設(shè)、網(wǎng)站制作、App設(shè)計、網(wǎng)站設(shè)計公司、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容