如何制作张量流中值滤波器

时间:2019-03-21 10:59:13

标签: python tensorflow deep-learning

我正在尝试自行构建GAN结构

tensorflow主页上的

关于中值过滤器功能不存在

我希望在生成的图像更清晰之后添加中值滤波功能

我该怎么做?

2 个答案:

答案 0 :(得分:0)

请参阅此拉取请求。

我已经在tensorflow插件中添加了此方法。

https://github.com/tensorflow/addons/pull/111

答案 1 :(得分:0)

我已经使用tf.image.extract_patches实现了中值滤波器,以模拟滑动窗口,并使用tf.math.top_k实现了中值:

def median_filter(data, filt_length=3):
    '''
    Computes a median filtered output of each [n_bins, n_channels] data sample
        and returns output w/ same shape, but median filtered

    NOTE: as TF doesnt have a median filter implemented, this had to be done in very hacky way...
    '''
    edges = filt_length// 2

    # convert to 4D, where data is in 3rd dim (e.g. data[0,0,:,0]
    exp_data = tf.expand_dims(tf.expand_dims(data, 0), -1)
    # get rolling window
    wins = tf.image.extract_patches(images=exp_data, sizes=[1, filt_length, 1, 1],
                       strides=[1, 1, 1, 1], rates=[1, 1, 1, 1], padding='VALID')
    # get median of each window
    wins = tf.math.top_k(wins, k=2)[0][0, :, :, edges]
    # Concat edges
    out = tf.concat((data[:edges, :], wins, data[-edges:, :]), 0)

    return out