注:在利用 tensorflow 做实际项目时,对其中的某些模块使用不明白或实现好奇。因此想到写一篇 trace code 的博客用来记录官方的实现方法。刚开始一个一个模块可能比较凌乱,等到积累到一定程度会整理为一篇系统的 tensorflow 代码解析的博客。
1,SAME or VALID padding
对于 VALID padding 而言,padding 数量为0,因此
output_spatial_shape[i] = ceil((input_spatial_shape[i] - (spatial_filter_shape[i]-1) / strides[i]).
对于SAME padding
而言利用 nn_ops.py中的_with_space_to_batch_base_paddings
来计算两端padding的数量。
def _with_space_to_batch_base_paddings(filter_shape, num_spatial_dims,
rate_or_const_rate):
"""Helper function to compute base_paddings."""
# Spatial dimensions of the filters and the upsampled filters in which we
# introduce (rate - 1) zeros between consecutive filter values.
filter_spatial_shape = filter_shape[:num_spatial_dims]
dilated_filter_spatial_shape = (
filter_spatial_shape + (filter_spatial_shape - 1) *
(rate_or_const_rate - 1))
pad_extra_shape = dilated_filter_spatial_shape - 1
# When full_padding_shape is odd, we pad more at end, following the same
# convention as conv2d.
pad_extra_start = pad_extra_shape // 2
pad_extra_end = pad_extra_shape - pad_extra_start
base_paddings = array_ops.stack(
[[pad_extra_start[i], pad_extra_end[i]] for i in range(num_spatial_dims)])
return base_paddings
因此output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i])