tensorflow 之tf.nn.depthwise_conv2d and separable_conv2d實現及原理

Depthwise Separable Convolution

1.簡介

Depthwise Separable Convolution 是谷歌公司於2017年的CVPR中在論文」Xception: deep learning with depthwise separable convolutions」中提出。python

2.結構簡介

對輸入圖片進行分通道卷積後作1*1卷積。結構以下圖: 

舉例來講,假設輸入通道數64,輸出通道數64. 
傳統的Conv2D方法的參數數量爲3*3*64*64;而SeparableConv2D的參數數量爲3*3*64+1*1*64*64。ide

3*3*64:對輸入的64個通道分別進行卷積 
1*1*64*64:對concat後的64個通道進行1*1卷積(pointwise Convolution)函數

結論:參數數量減小了32192個。spa

3.適用範圍

假設輸入圖片的空間位置是相較於通道之間關係是高度相關的。.net

depthwise_conv2d來源於深度可分離卷積

tf.nn.depthwise_conv2d(input,filter,strides,padding,rate=None,name=None,data_format=None)

除去name參數用以指定該操做的name,data_format指定數據格式,與方法有關的一共五個參數:code

  • input: 
    指須要作卷積的輸入圖像,要求是一個4維Tensor,具備[batch, height, width, in_channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 圖像通道數]orm

  • filter: 
    至關於CNN中的卷積核,要求是一個4維Tensor,具備[filter_height, filter_width, in_channels, channel_multiplier]這樣的shape,具體含義是[卷積核的高度,卷積核的寬度,輸入通道數,輸出卷積乘子],同理這裏第三維in_channels,就是參數value的第四維blog

  • strides: 
    卷積的滑動步長。圖片

  • padding: 
    string類型的量,只能是」SAME」,」VALID」其中之一,這個值決定了不一樣邊緣填充方式。ip

  • rate: 
    這個參數的詳細解釋見【Tensorflow】tf.nn.atrous_conv2d如何實現空洞卷積?

結果返回一個Tensor,shape爲[batch, out_height, out_width, in_channels * channel_multiplier],注意這裏輸出通道變成了in_channels * channel_multiplier

tf.nn.separable_conv2d

能夠看作,深度卷積tf.nn.depthwise_conv2d的擴展

  • tf.nn.separable_conv2d(input,depthwise_filter,pointwise_filter,strides,padding,rate=None,name=None,data_format=None)

除去name參數用以指定該操做的name,data_format指定數據格式,與方法有關的一共六個參數:

  • input: 
    指須要作卷積的輸入圖像,要求是一個4維Tensor,具備[batch, height, width, in_channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 圖像通道數]

  • depthwise_filter: 
    用來作depthwise_conv2d的卷積核,也就是說這個函數對輸入首先作了一個深度卷積。它的shape規定是[filter_height, filter_width, in_channels, channel_multiplier]

  • pointwise_filter: 
    用來作pointwise卷積的卷積核,什麼是pointwise卷積呢?咱們能夠把它和GoogLeNet最原始版本Inception結構中後面的1*1卷積核作channel降維來作對比,這裏也是用1*1的卷積核,輸入通道是depthwise_conv2d的輸出通道也就是in_channels * channel_multiplier,輸出通道數能夠本身定義。由於前面( 【Tensorflow】tf.nn.depthwise_conv2d如何實現深度卷積? )已經講到過了,depthwise_conv2d是對輸入圖像的每個channel分別作卷積輸出的,那麼這個操做咱們能夠看作是將深度卷積獲得的分離的各個channel的信息作一個融合。它的shape規定是[1, 1, channel_multiplier * in_channels, out_channels]

  • strides: 
    卷積的滑動步長。

  • padding: 
    string類型的量,只能是」SAME」,」VALID」其中之一,這個值決定了不一樣邊緣填充方式。

  • rate: 
    這個參數的詳細解釋見【Tensorflow】tf.nn.atrous_conv2d如何實現空洞卷積?

輸出shape爲[batch, out_height, out_width, out_channels]的Tensor

相關文章
相關標籤/搜索