Tensorflow tf.nn.depthwise_conv2d如何實(shí)現(xiàn)深度卷積的
實(shí)驗(yàn)環(huán)境:tensorflow版本1.2.0,python2.7
介紹
depthwise_conv2d
來(lái)源于深度可分離卷積:
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)的一共五個(gè)參數(shù):
input:
指需要做卷積的輸入圖像,要求是一個(gè)4維Tensor,具有[batch, height, width, in_channels]
這樣的shape,具體含義是[訓(xùn)練時(shí)一個(gè)batch的圖片數(shù)量, 圖片高度, 圖片寬度, 圖像通道數(shù)]
filter:
相當(dāng)于CNN中的卷積核,要求是一個(gè)4維Tensor,具有[filter_height, filter_width, in_channels, channel_multiplier]
這樣的shape,具體含義是[卷積核的高度,卷積核的寬度,輸入通道數(shù),輸出卷積乘子],同理這里第三維in_channels,就是參數(shù)value的第四維
strides:
卷積的滑動(dòng)步長(zhǎng)。
padding:
string類型的量,只能是”SAME”,”VALID”其中之一,這個(gè)值決定了不同邊緣填充方式。
rate:
這個(gè)參數(shù)的詳細(xì)解釋見(jiàn)【Tensorflow】tf.nn.atrous_conv2d如何實(shí)現(xiàn)空洞卷積?
結(jié)果返回一個(gè)Tensor,shape為[batch, out_height, out_width, in_channels * channel_multiplier]
,注意這里輸出通道變成了in_channels * channel_multiplier
實(shí)驗(yàn)
為了形象的展示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')
好了,用一張圖來(lái)詳細(xì)展示這個(gè)過(guò)程
這是普通的卷積過(guò)程,我們?cè)賮?lái)看深度卷積。
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,我們對(duì)卷積核每一個(gè)out_channel
的兩個(gè)通道分別和輸入的兩個(gè)通道做卷積相加,得到feature map的一個(gè)channel,而depthwise_conv2d
卷積,我們對(duì)每一個(gè)對(duì)應(yīng)的in_channel
,分別卷積生成兩個(gè)out_channel
,所以獲得的feature map的通道數(shù)量可以用in_channel* channel_multiplier
來(lái)表達(dá),這個(gè)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如何實(shí)現(xiàn)深度卷積的的文章就介紹到這了,更多相關(guān)Tensorflow tf.nn.depthwise_conv2d深度卷積內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器的過(guò)程
這篇文章主要介紹了ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04一文帶你深入理解python中pytest-repeat插件的工作原理
這篇文章主要和大家一起深入探討到底pytest_repeat插件的具體功能是如何實(shí)現(xiàn)的呢,相信具體了解了該插件,其他三方插件也可以很快了解它內(nèi)部運(yùn)行機(jī)制,所以本文詳細(xì)講解了python pytest-repeat插件的工作原理,需要的朋友可以參考下2023-09-09Pandas實(shí)現(xiàn)兩個(gè)表的連接功能的方法詳解
這篇文章主要和大家一起說(shuō)說(shuō)pandas的兩個(gè)表的連接技能merge,也就是根據(jù)一個(gè)表的條件去匹配另一個(gè)表的內(nèi)容,感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-09-09Python3實(shí)現(xiàn)騰訊云OCR識(shí)別
這篇文章主要為大家詳細(xì)介紹了Python3實(shí)現(xiàn)騰訊云OCR識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Python 動(dòng)態(tài)變量名定義與調(diào)用方法
這篇文章主要介紹了Python 動(dòng)態(tài)變量名定義與調(diào)用方法,需要的朋友可以參考下2020-02-02Python多進(jìn)程加鎖的實(shí)現(xiàn)
很多時(shí)候,我們需要在多個(gè)進(jìn)程中同時(shí)寫一個(gè)文件,如果不加鎖機(jī)制,就會(huì)導(dǎo)致寫文件錯(cuò)亂,本文主要介紹了Python多進(jìn)程加鎖的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),感興趣的可以了解一下2023-07-07Python的Flask框架開發(fā)驗(yàn)證碼登錄的實(shí)現(xiàn)
在本文我們介紹了如何使用Python的Flask框架開發(fā)一個(gè)簡(jiǎn)單的驗(yàn)證碼登錄功能,將涵蓋生成驗(yàn)證碼、處理用戶輸入、驗(yàn)證驗(yàn)證碼以及實(shí)現(xiàn)安全的用戶認(rèn)證等方面,感興趣的可以了解一下2023-11-11