池化層max_pool中兩種paddding操作

max_pool()中padding參數有兩種模式valid和same模式。
Tensorflow的padding和卷積層一樣也有padding操作,兩種不同的操作輸出的結果有區別。

函數原型max_pool(value, ksize, strides, padding, data_format="NHWC", name=None)

這一解釋除了tf.nn.max_pool,還適用於tf.nn.conv2d和tf.layer.*下的相關函數

if padding = "SAME": 
    output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i])
if padding = "VALID": 
    output_spatial_shape[i] = ceil((input_spatial_shape[i] - (window_shape[i] - 1) * dilation_rate[i]) / strides[i])


也就是說:

“VALID” 模式,在剩餘行列數小於池化窗口大小時,將最右邊和最下面的列或行拋棄,只保留有效值;
“SAME” 模式則是在剩餘行列數不足時補充0來滿足池化窗口的大小,保持窗口被池化區域相同;
所以輸出尺寸不是池化窗口的整數倍時,same模式的輸出要比valid的大。

#我們來看看函數的定義
#source code:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/nn_ops.py  line:2116
@tf_export("nn.max_pool")
def max_pool(value, ksize, strides, padding, data_format="NHWC", name=None):
  """Performs the max pooling on the input.
  Args:
    value: A 4-D `Tensor` of the format specified by `data_format`.
    ksize: A list or tuple of 4 ints. The size of the window for each dimension
      of the input tensor.
    strides: A list or tuple of 4 ints. The stride of the sliding window for
      each dimension of the input tensor.
    padding: A string, either `'VALID'` or `'SAME'`. The padding algorithm.
      See the "returns" section of `tf.nn.convolution` for details.
    data_format: A string. 'NHWC', 'NCHW' and 'NCHW_VECT_C' are supported.
    name: Optional name for the operation.
  Returns:
    A `Tensor` of format specified by `data_format`.
    The max pooled output tensor.
  """
  with ops.name_scope(name, "MaxPool", [value]) as name:
    value = ops.convert_to_tensor(value, name="input")
    return gen_nn_ops.max_pool(
        value,
        ksize=ksize,
        strides=strides,
        padding=padding,
        data_format=data_format,
        name=name)

#其中pool()函數 padding的代碼如下所示
#dilation_rate 默認爲1的序列Defaults to [1]*N
#nn_ops.py line876
    If padding = "SAME":
      output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i])
    #'same'方法輸出尺寸直接爲 input_size/strides_size
    
    If padding = "VALID":
      output_spatial_shape[i] =
        ceil((input_spatial_shape[i] - (window_shape[i] - 1) * dilation_rate[i])
             / strides[i]).
	#'valid'方法輸出尺寸爲 [input_size-(pooling_win_size-1)*1]/stride_size


用例子解釋一下:
1.

#"VALID" = without padding:
   inputs:         1  2  3  4  5  6  7  8  9  10 11 (12 13)
                  |________________|                dropped
                                 |_________________|
#"SAME" = with zero padding:
               pad|                                      |pad
   inputs:      0 |1  2  3  4  5  6  7  8  9  10 11 12 13|0  0
               |________________|
                              |_________________|
                                             |________________|


2.

x = tf.constant([[1., 2., 3.],
                 [4., 5., 6.]])

x = tf.reshape(x, [1, 2, 3, 1])  				#先轉爲張量輸入

valid_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')   
same_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

valid_pad.get_shape() == [1, 1, 1, 1]  # valid_pad is [5.]       #3,6那一列被丟掉了
|1 2    ...3|
|4 5    ...6|

å¨è¿éæå¥å¾çæè¿°
 

same_pad.get_shape() == [1, 1, 2, 1]   # same_pad is  [5., 6.]      #加上了第四列,第二個窗口的max是6
|1 2 3 0|
|4 5 6 0|     ->|5,6|

å¨è¿éæå¥å¾çæè¿°

ref:
api:https://www.tensorflow.org/api_docs/python/tf/nn/max_pool
blog:https://blog.csdn.net/accumulate_zhang/article/details/78359856
blog:https://blog.csdn.net/wuzqChom/article/details/74785643
stackflow:https://stackoverflow.com/questions/37674306/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-t
code:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/nn_ops.py
gen_nn_ops.max_pool如何得到:
build:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/BUILD#L273
gen_nn.ops:https://stackoverflow.com/questions/41147734/looking-for-source-code-of-from-gen-nn-ops-in-tensorflow
------------------------------------------ 
原文:https://blog.csdn.net/u014636245/article/details/83618447 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章