## Monday, October 12, 2020

### Gaussian Filter in Keras (code snippet)

Very often we need to perform basic vision operations on a computational graph like building a Laplacian pyramid or filter a tensor with a specific precalculated filter.

Below i present a code snippet for building a fixed non-trainable gaussian filter in keras.

import kerasimport numpy as npimport scipy.stats as stdef gaussian_filter_block(input_layer,                          kernel_size=3,                          strides=(1, 1),                          dilation_rate=(1, 1),                          padding="same",                          activation=None,                          trainable=False,                          use_bias=False):    """    Build a gaussian filter block    :return:    """    def _gaussian_kernel(kernlen=[21, 21], nsig=[3, 3]):        """        Returns a 2D Gaussian kernel array        """        assert len(nsig) == 2        assert len(kernlen) == 2        kern1d = []        for i in range(2):            interval = (2 * nsig[i] + 1.) / (kernlen[i])            x = np.linspace(-nsig[i] - interval / 2., nsig[i] + interval / 2.,                            kernlen[i] + 1)            kern1d.append(np.diff(st.norm.cdf(x)))        kernel_raw = np.sqrt(np.outer(kern1d[0], kern1d[1]))        # divide by sum so they all add up to 1        kernel = kernel_raw / kernel_raw.sum()        return kernel    # Initialise to set kernel to required value    def kernel_init(shape, dtype):        kernel = np.zeros(shape)        kernel[:, :, 0, 0] = _gaussian_kernel([shape[0], shape[1]])        return kernel    return keras.layers.DepthwiseConv2D(        kernel_size=kernel_size,        strides=strides,        padding=padding,        depth_multiplier=1,        dilation_rate=dilation_rate,        activation=activation,        use_bias=use_bias,        trainable=trainable,        depthwise_initializer=kernel_init,        kernel_initializer=kernel_init)(input_layer)

from my open source project https://github.com/NikolasMarkou/multiscale_variational_autoencoder

#### 1 comment:

1. Hello to everyone out here, I am here to share the unexpected miracle that happened to me … My name is Susan Christian , I live in London, UK. we got married for more than 9 years and have gotten two kids. thing were going well with us and we are always happy. until one day my husband started to behave in a way i could not understand, i was very confused by the way he treat me and the kids. later that month he did not come home again and he called me that he want a divorce, i asked him what have i done wrong to deserve this from him, all he was saying is that he want a divorce that he hate me and do not want to see me again in his life, i was mad and also frustrated do not know what to do, i was sick for more than 2 weeks because of the divorce. i love him so much he was everything to me without him my life is incomplete. i told my sister and she told me to contact a spell caster, i never believe in all this spell casting of a thing. i just want to try if something will come out of it. i contacted Dr Emu for the return of my husband to me, they told me that my husband have been taken by another woman, that she cast a spell on him that is why he hate me and also want us to divorce. then they told me that they have to cast a spell on him that will make him return to me and the kids, they casted the spell and after 24 hours my husband called me and he told me that i should forgive him, he started to apologize on phone and said that he still live me that he did not know what happen to him that he left me. it was the spell that he Dr Emu casted on him that make him come back to me today, me and my family are now happy again today. thank you Dr Emu for what you have done for me i would have been nothing today if not for your great spell. i want you my friends who are passing through all this kind of love problem of getting back their husband, wife , or ex boyfriend and girlfriend to contact Dr Emu ,if you need his help you can contact him through his private mail: emutemple@gmail.com or you can contact him through his website https://emutemple.wordpress.com/ fb page Https://web.facebook.com/Emu-Temple-104891335203341 and you will see that your problem will be solved without any delay.