上QQ阅读APP看书,第一时间看更新
Input padding
If we did nothing, then the convolution operation will output a result that is spatially smaller than the input to it. To avoid this effect and to ensure that our convolution kernel looks at every image location, we can place zeros on the border of our input image. When we do this, we are said to be padding the image:
The TensorFlow convolution operations provide you with two options for your padding needs: same and valid.
- Valid - TensorFlow does not pad the image. Your convolution kernels will only go to "valid" places in the input.
- Same - If we assume stride is one, then in this case, TensorFlow will pad the input enough so that the output spatial size is the same as the input spatial size.
If you really wish to have more control of your padding, you can use tf.pad() on the input to a layer to pad your input with exactly how many zeros you want.
In general, we can calculate the output size of a convolution operation using the following formulas:
,
(Here, pad is the number of zeros added to each border.)
However in TensorFlow, because of the nature of the valid and same padding options, the formulas become as follows:
# Same padding out_height = ceil(float(in_height) / float(strides[1]))
out_width = ceil(float(in_width) / float(strides[2]))
# Valid padding
out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))