最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tensorflow tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積的

 更新時間:2020年04月20日 11:52:32   作者:xf__mao  
這篇文章主要介紹了Tensorflow tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

實驗環(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)文章

最新評論

灵丘县| 萝北县| 惠东县| 明水县| 洛宁县| 临湘市| 平南县| 乐都县| 曲阳县| 绥滨县| 杂多县| 大英县| 浏阳市| 长兴县| 孝义市| 澄江县| 周口市| 新宾| 凯里市| 高州市| 鸡西市| 达尔| 鹤庆县| 安溪县| 华坪县| 阿拉善右旗| 昌图县| 酒泉市| 宁化县| 夹江县| 德钦县| 安西县| 贡山| 宿州市| 毕节市| 共和县| 卓资县| 平江县| 达州市| 天全县| 宜城市|