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.
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, image.shape) print(channels_out) 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]