【TensorFlow】conv2d函数参数解释以及padding理解
卷積conv2d
CNN在深度學(xué)習(xí)中有著舉足輕重的地位,主要用于特征提取。在TensorFlow中涉及的函數(shù)是tf.nn.conv2d。
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, data_format=“NHWC”, dilations=[1, 1, 1, 1], name=None)
- input 代表做卷積的輸入圖像的Tensor,其shape要求為[batch, in_height, in_width, in_channels],具體含義是[訓(xùn)練時一個batch的圖片數(shù)量, 圖片高度, 圖片寬度, 圖像通道數(shù)],數(shù)據(jù)類型為float32或float64;
- filter 相當于CNN中的卷積核,該Tensor的shape要求為[filter_height, filter_width, in_channels, out_channels],具體含義是[卷積核的高度,卷積核的寬度,圖像通道數(shù),卷積核個數(shù)],要求類型與參數(shù)input相同,filter的通道數(shù)要求與input的in_channels一致,即第三維in_channels,就是參數(shù)input的第四維;
- strides [1,stride_h,stride_w,1]步長,即卷積核每次移動的步長;
- padding string類型的量,只能是"SAME","VALID"其中之一,這個值決定了不同的卷積方式;
輸出結(jié)果是shape為[batch, out_height, out_width, out_channels],batch取決于input,out_channels取決于filter,而out_height與out_width取決于所有參數(shù),參考示意圖
- SAME模式 補
- out_height = ceil ( float ( in_height ) / float ( stride_h) )
- out_width = ceil ( float ( in_width ) / float ( stride_w ) )
- VALID模式 丟
- out_height = ceil(float(in_height - filter_height + 1) / float(stride_h))
- out_width = ceil(float(in_width - filter_width + 1) / float(stride_w))
補的方式如下:
-
補的行數(shù):pad_along_height = max((out_height - 1) * strides[1] + filter_height - in_height, 0)
-
補的列數(shù):pad_along_width = max((out_width - 1) * strides[2] + filter_width - in_width, 0)
-
pad_top = pad_along_height // 2
-
pad_bottom = pad_along_height - pad_top
-
pad_left = pad_along_width // 2
-
pad_right = pad_along_width - pad_left
測試實例
import os os.environ["CUDA_VISIBLE_DEVICES"] = "-1" import tensorflow as tfinput = tf.Variable(tf.random_normal([1,16,64,3])) filter = tf.Variable(tf.random_normal([3,5,3,32])) op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='VALID')with tf.Session() as sess:sess.run(tf.global_variables_initializer())res = (sess.run(op))print (res.shape)# (1, 7, 30, 32)空洞卷積atrous_conv2d
tf.nn.atrous_conv2d(value, filters, rate, padding, name=None)
- input 代表做卷積的輸入圖像的Tensor,其shape要求為[batch, in_height, in_width, in_channels],具體含義是[訓(xùn)練時一個batch的圖片數(shù)量, 圖片高度, 圖片寬度, 圖像通道數(shù)],數(shù)據(jù)類型為float32或float64;
- filter 相當于CNN中的卷積核,該Tensor的shape要求為[filter_height, filter_width, in_channels, out_channels],具體含義是[卷積核的高度,卷積核的寬度,圖像通道數(shù),卷積核個數(shù)],要求類型與參數(shù)input相同,filter的通道數(shù)要求與input的in_channels一致,即第三維in_channels,就是參數(shù)input的第四維;
- rate 要求是一個int型的正數(shù),正常的卷積操作應(yīng)該會有stride(即卷積核的滑動步長),但是空洞卷積是沒有stride參數(shù)的,這一點尤其要注意。取而代之,它使用了新的rate參數(shù),那么rate參數(shù)有什么用呢?它定義為我們在輸入圖像上卷積時的采樣間隔,你可以理解為卷積核當中穿插了(rate-1)數(shù)量的“0”,把原來的卷積核插出了很多“洞洞”,這樣做卷積時就相當于對原圖像的采樣間隔變大了。具體怎么插得,可以看后面更加詳細的描述。此時我們很容易得出rate=1時,就沒有0插入,此時這個函數(shù)就變成了普通卷積;
- padding 填充模式,取值只能為“SAME”或“VALID”;
A positive int32. The stride with which we sample input values across the height and width dimensions. Equivalently, the rate by which we upsample the filter values by inserting zeros across the height and width dimensions. In the literature, the same parameter is sometimes called input stride or dilation.
輸出shape為
-
VALID
[batch, height - 2 * (filter_width - 1), width - 2 * (filter_height - 1), out_channels]. -
SAME
[batch, height, width, out_channels].
深入理解:
- 參考:【Tensorflow】tf.nn.atrous_conv2d如何實現(xiàn)空洞卷積?的示意圖
- 查看函數(shù)說明,其中有優(yōu)化部分
延伸閱讀: - 對于xception非常好的理解
- 【Tensorflow】tf.nn.depthwise_conv2d如何實現(xiàn)深度卷積?
- 【Tensorflow】tf.nn.separable_conv2d如何實現(xiàn)深度可分卷積?
反卷積conv2d_transpose
反卷積操作是卷積的反向
tf.nn.conv2d_transpose(value, filter, output_shape, strides, padding=‘SAME’, data_format=‘NHWC’, name=None)
- value 指需要做反卷積的輸入圖像,它要求是一個Tensor
- filter 卷積核,它要求是一個Tensor,具有[filter_height, filter_width, out_channels, in_channels]這樣的shape,具體含義是[卷積核的高度,卷積核的寬度,卷積核個數(shù),圖像通道數(shù)]
- output_shape 反卷積操作輸出的shape
- strides 反卷積時在圖像每一維的步長,這是一個一維的向量,長度4
- padding string類型的量,只能是"SAME","VALID"其中之一,這個值決定了不同的卷積方式
- data_format string類型的量,'NHWC’和’NCHW’其中之一,這是tensorflow新版本中新加的參數(shù),它說明了value參數(shù)的數(shù)據(jù)格式。'NHWC’指tensorflow標準的數(shù)據(jù)格式[batch, height, width, in_channels],‘NCHW’指Theano的數(shù)據(jù)格式,[batch, in_channels,height, width],當然默認值是’NHWC’
在這里解釋一下output_shape這個參數(shù),反卷積操作是卷積的反向,在卷積計算中輸出維度是上取整,那么這就說明輸入維度可能是多個,所以在反卷積計算時需要用戶給出這個輸入維度。
后續(xù)學(xué)習(xí)資料
- 卷積神經(jīng)網(wǎng)絡(luò)CNN經(jīng)典模型整理Lenet,Alexnet,Googlenet,VGG,Deep Residual Learning
- ResNet解析
- ResNetV2:ResNet深度解析
參考
- tensorflow conv2d的padding解釋以及參數(shù)解釋
- conv2d函數(shù)的padding參數(shù)解釋
- TensorFlow中CNN的兩種padding方式“SAME”和“VALID”
- What is the difference between ‘SAME’ and ‘VALID’ padding in tf.nn.max_pool of tensorflow?
- tf.nn.dynamic_rnn的輸出outputs和state含義
總結(jié)
以上是生活随笔為你收集整理的【TensorFlow】conv2d函数参数解释以及padding理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TextRank、BM25算法提取关键字
- 下一篇: 深信服 linux软件开发面试题整理