최태규
최태규

Reputation: 11

How to make tensorflow median filter?

I'm trying to build GAN structure on my own

at tensorflow homepage doesn't exist about median filter function

and i hope to add median filter function after generated image more cleaner

how can i do that??

Upvotes: 1

Views: 2392

Answers (3)

J Agustin Barrachina
J Agustin Barrachina

Reputation: 4100

I think what you are looking for is tfa.image.median_filter2d.

Here you have the documentation.

Upvotes: 0

DankMasterDan
DankMasterDan

Reputation: 2133

I have implemented a median filter using tf.image.extract_patches to simulate a sliding window and tf.math.top_k to get the median:

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

Upvotes: 1

Mainak Dutta
Mainak Dutta

Reputation: 304

Please refer to this pull request.

I have added this method in tensorflow addons.

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

Upvotes: 0

Related Questions