Filtering image in pytorch

Does anyone know how to set not learned filter as part of the training in pytorch? Specifically, I would like to have a filter that preserve high freq content. Thanks!

You just need to use the conv2d functional passing your own filter

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

Remember pytorch uses crosscorrelation.

Thanks! And it won’t learn its parameters?
What about back-propagating through it?

It will consider the kernel in order to backprop of course. But, as it is a fixed tensor it won’t learn.
The functional is just a function which applies the convolution. The nn.Module is a class which contains learnable parameters. Those are passed to the optimizer, hence, learned.

Note: Remember that you have to define the filter as a tensor, NOT as a nn.Parameter. Otherwise it will be learned too. :slight_smile:

1 Like

take a look at kornia.filters module

1 Like