Gaussian filter for images

How can i implement a gaussian filter on a image tensor after the last convolutional layer as a post processing step?

You can create a nn.Conv2d(..., bias=False) layer and set the weights to the gaussian weights with:

conv = nn.Conv2d(..., bias=False)
with torch.no_grad():
    conv.weight = gaussian_weights

Then just apply it on your tensor.

1 Like


I am trying to do as you suggest but i’m kinda stuck and here is a simple example.

import torch
from torch import nn
from scipy.ndimage import gaussian_filter
import numpy as np

def g_difference(image, kernel, sigma1=3, sigma2=5):
    channels_in, channels_out = (image.shape[1], image.shape[1])
    diffs = gaussian_filter(image.cpu().detach().numpy(), sigma=sigma2) - gaussian_filter(image.cpu().detach().numpy() , sigma=sigma1)
    conv2d = nn.Conv2d(channels_in, channels_out, kernel_size=kernel, bias=False)
    with torch.no_grad():
        conv2d.weight = torch.nn.Parameter(torch.FloatTensor(diffs).cuda())
    return conv2d

x = torch.randn([1, 1, 1600, 500])

output = g_difference(x, 1)

print(output(x.cuda()).shape) #  the output is [1,1,1,1]

thanks for your solution. i wonder how to set the gaussian sigma and mean in nn.Conv2d or F.conv2d ?

you need to set the weights either manually or using another function, for example from PIL or from scipy, see the code samples here Is there anyway to do gaussian filtering for an image(2D,3D) in pytorch? - #19 by Jeffrey_Danowitz