def gaussian_blur(input, filter_size, filter_sampling_range=3.5, strides=[1, 1, 1, 1], padding='SAME'):
"""
Blur input with a 2D Gaussian filter of size filter_size x filter_size. The filter's values are
sampled from an evenly spaced grid on the 2D standard normal distribution in the range
[-filter_sampling_range, filter_sampling_range] in both dimensions.
:param input: A rank-4 tensor with shape=(samples, x, y, n_channels). The same Gaussian filter
will be applied to all n_channels feature maps of input.
:param filter_size: The size of one edge of the square-shaped Gaussian filter.
:param filter_sampling_range: The range in which to sample from the standard normal distribution in
both dimensions, i.e. a sampling range of 1 corresponds to sampling in a square grid that bounds
the standard deviation circle.
:param strides: Param strides as passed to tf.nn.depthwise_conv2d.
:param padding: Param padding as passed to tf.nn.depthwise_conv2d.
:return: The result of the Gaussian blur as a rank-4 tensor with the same shape as input.
"""
# make 2D distribution
mu = np.repeat(np.float32(0.), 2)
sig = np.repeat(np.float32(1.), 2)
dist = tf.contrib.distributions.MultivariateNormalDiag(mu, sig)
# sample from distribution on a grid
sampling_range = tf.cast(filter_sampling_range, tf.float32)
x_1D = tf.linspace(-sampling_range, sampling_range, filter_size)
x = tf.stack(tf.meshgrid(x_1D, x_1D), 2)
kern = dist.pdf(x)
kern /= tf.reduce_sum(kern)
kern = tf.reshape(kern, kern.shape.as_list() + [1, 1])
kern = tf.tile(kern, [1, 1, input.shape.as_list()[-1], 1])
return tf.nn.depthwise_conv2d(input, kern, strides, padding)
dataprocessing.py 文件源码
python
阅读 36
收藏 0
点赞 0
评论 0
评论列表
文章目录