Tensorflow tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積的
實驗環(huán)境:tensorflow版本1.2.0,python2.7
介紹
depthwise_conv2d來源于深度可分離卷積:
Xception: Deep Learning with Depthwise Separable Convolutions
tf.nn.depthwise_conv2d(input,filter,strides,padding,rate=None,name=None,data_format=None)
除去name參數(shù)用以指定該操作的name,data_format指定數(shù)據(jù)格式,與方法有關(guān)的一共五個參數(shù):
input:
指需要做卷積的輸入圖像,要求是一個4維Tensor,具有[batch, height, width, in_channels]這樣的shape,具體含義是[訓(xùn)練時一個batch的圖片數(shù)量, 圖片高度, 圖片寬度, 圖像通道數(shù)]
filter:
相當(dāng)于CNN中的卷積核,要求是一個4維Tensor,具有[filter_height, filter_width, in_channels, channel_multiplier]這樣的shape,具體含義是[卷積核的高度,卷積核的寬度,輸入通道數(shù),輸出卷積乘子],同理這里第三維in_channels,就是參數(shù)value的第四維
strides:
卷積的滑動步長。
padding:
string類型的量,只能是”SAME”,”VALID”其中之一,這個值決定了不同邊緣填充方式。
rate:
這個參數(shù)的詳細(xì)解釋見【Tensorflow】tf.nn.atrous_conv2d如何實現(xiàn)空洞卷積?
結(jié)果返回一個Tensor,shape為[batch, out_height, out_width, in_channels * channel_multiplier],注意這里輸出通道變成了in_channels * channel_multiplier
實驗
為了形象的展示depthwise_conv2d,我們必須要建立自定義的輸入圖像和卷積核
img1 = tf.constant(value=[[[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]]]],dtype=tf.float32) img2 = tf.constant(value=[[[[1],[1],[1],[1]],[[1],[1],[1],[1]],[[1],[1],[1],[1]],[[1],[1],[1],[1]]]],dtype=tf.float32) img = tf.concat(values=[img1,img2],axis=3)
filter1 = tf.constant(value=0, shape=[3,3,1,1],dtype=tf.float32) filter2 = tf.constant(value=1, shape=[3,3,1,1],dtype=tf.float32) filter3 = tf.constant(value=2, shape=[3,3,1,1],dtype=tf.float32) filter4 = tf.constant(value=3, shape=[3,3,1,1],dtype=tf.float32) filter_out1 = tf.concat(values=[filter1,filter2],axis=2) filter_out2 = tf.concat(values=[filter3,filter4],axis=2) filter = tf.concat(values=[filter_out1,filter_out2],axis=3)
建立好了img和filter,就可以做卷積了
out_img = tf.nn.conv2d(input=img, filter=filter, strides=[1,1,1,1], padding='VALID')
好了,用一張圖來詳細(xì)展示這個過程

這是普通的卷積過程,我們再來看深度卷積。
out_img = tf.nn.depthwise_conv2d(input=img, filter=filter, strides=[1,1,1,1], rate=[1,1], padding='VALID')

現(xiàn)在我們可以形象的解釋一下depthwise_conv2d卷積了??雌胀ǖ木矸e,我們對卷積核每一個out_channel的兩個通道分別和輸入的兩個通道做卷積相加,得到feature map的一個channel,而depthwise_conv2d卷積,我們對每一個對應(yīng)的in_channel,分別卷積生成兩個out_channel,所以獲得的feature map的通道數(shù)量可以用in_channel* channel_multiplier來表達(dá),這個channel_multiplier,就可以理解為卷積核的第四維。
代碼清單
import tensorflow as tf img1 = tf.constant(value=[[[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]]]],dtype=tf.float32) img2 = tf.constant(value=[[[[1],[1],[1],[1]],[[1],[1],[1],[1]],[[1],[1],[1],[1]],[[1],[1],[1],[1]]]],dtype=tf.float32) img = tf.concat(values=[img1,img2],axis=3) filter1 = tf.constant(value=0, shape=[3,3,1,1],dtype=tf.float32) filter2 = tf.constant(value=1, shape=[3,3,1,1],dtype=tf.float32) filter3 = tf.constant(value=2, shape=[3,3,1,1],dtype=tf.float32) filter4 = tf.constant(value=3, shape=[3,3,1,1],dtype=tf.float32) filter_out1 = tf.concat(values=[filter1,filter2],axis=2) filter_out2 = tf.concat(values=[filter3,filter4],axis=2) filter = tf.concat(values=[filter_out1,filter_out2],axis=3) out_img = tf.nn.depthwise_conv2d(input=img, filter=filter, strides=[1,1,1,1], rate=[1,1], padding='VALID')
輸出:
rate=1, VALID mode result:
[[[[ 0. 36. 9. 27.]
[ 0. 54. 9. 27.]][[ 0. 36. 9. 27.]
[ 0. 54. 9. 27.]]]]
到此這篇關(guān)于Tensorflow tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積的的文章就介紹到這了,更多相關(guān)Tensorflow tf.nn.depthwise_conv2d深度卷積內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python筆記(1) 關(guān)于我們應(yīng)不應(yīng)該繼續(xù)學(xué)習(xí)python
關(guān)于Python,如果你要學(xué)習(xí),建議大家查看一下網(wǎng)站:因為本人也是剛剛決定收集點零碎時間來學(xué)習(xí)下它,推薦可能并不是最好的2012-10-10
PyTorch 導(dǎo)數(shù)應(yīng)用的使用教程
這篇文章主要介紹了PyTorch 導(dǎo)數(shù)應(yīng)用的使用教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
python使用property完成數(shù)據(jù)隱藏封裝與校驗
這篇文章主要為大家詳細(xì)介紹了python使用property完成數(shù)據(jù)隱藏封裝與校驗,實現(xiàn)安全修改,文中的示例代碼講解詳細(xì),希望對大家有所幫助2024-11-11
深入探討Python中的內(nèi)置類屬性`__repr__`
在Python中,__repr__是一個特殊的內(nèi)置類屬性,用于定義類的字符串表示形式,本文將深入探討__repr__的作用、用法以及一些實際應(yīng)用場景,希望對大家有所幫助2023-12-12
Python小工具之消耗系統(tǒng)指定大小內(nèi)存的方法
今天小編就為大家分享一篇Python小工具之消耗系統(tǒng)指定大小內(nèi)存的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python實現(xiàn)帶下標(biāo)索引的遍歷操作示例
這篇文章主要介紹了Python實現(xiàn)帶下標(biāo)索引的遍歷操作,結(jié)合具體實例形式分析了2種帶索引的遍歷操作實現(xiàn)方法及相關(guān)操作注意事項,需要的朋友可以參考下2019-05-05
django echarts餅圖數(shù)據(jù)動態(tài)加載的實例
今天小編就為大家分享一篇django echarts餅圖數(shù)據(jù)動態(tài)加載的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

