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)文章
ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器的過程
這篇文章主要介紹了ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04一文帶你深入理解python中pytest-repeat插件的工作原理
這篇文章主要和大家一起深入探討到底pytest_repeat插件的具體功能是如何實現(xiàn)的呢,相信具體了解了該插件,其他三方插件也可以很快了解它內(nèi)部運行機(jī)制,所以本文詳細(xì)講解了python pytest-repeat插件的工作原理,需要的朋友可以參考下2023-09-09Python 動態(tài)變量名定義與調(diào)用方法
這篇文章主要介紹了Python 動態(tài)變量名定義與調(diào)用方法,需要的朋友可以參考下2020-02-02Python的Flask框架開發(fā)驗證碼登錄的實現(xiàn)
在本文我們介紹了如何使用Python的Flask框架開發(fā)一個簡單的驗證碼登錄功能,將涵蓋生成驗證碼、處理用戶輸入、驗證驗證碼以及實現(xiàn)安全的用戶認(rèn)證等方面,感興趣的可以了解一下2023-11-11