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


Depthwise Separable Convolution

1.簡介

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

2.結構簡介

對輸入圖片進行分通道卷積后做1*1卷積。結構如下圖: 

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

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

結論:參數數量減少了32192個。

3.適用范圍

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

depthwise_conv2d來源於深度可分離卷積

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

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

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

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

  • strides: 
    卷積的滑動步長。

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

  • 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM