我只是想制作一个高斯滤波器(例如'scipy.ndimage.gaussian_filter')来处理TensorFlow中的4-D张量,所以4-D张量的形状为:[16,96,96, 3](批处理大小为16,图像块大小为96,通道数为3)。我该如何实现?
谢谢!
答案 0 :(得分:1)
您只需要创建一个高斯2D内核并使用2D卷积:
import tensorflow as tf
# Make Gaussian kernel following SciPy logic
def make_gaussian_2d_kernel(sigma, truncate=4.0, dtype=tf.float32):
radius = tf.to_int32(sigma * truncate)
x = tf.cast(tf.range(-radius, radius + 1), dtype=dtype)
k = tf.exp(-0.5 * tf.square(x / sigma))
k = k / tf.reduce_sum(k)
return tf.expand_dims(k, 1) * k
# Input data
image = tf.placeholder(tf.float32, [16, 96, 96, 3])
# Convolution kernel
kernel = make_gaussian_2d_kernel(5)
# Apply kernel to each channel (see https://stackoverflow.com/q/55687616/1782792)
kernel = tf.tile(kernel[:, :, tf.newaxis, tf.newaxis], [1, 1, 3, 1])
image_filtered = tf.nn.separable_conv2d(
image, kernel, tf.eye(3, batch_shape=[1, 1]),
strides=[1, 1, 1, 1], padding='SAME')