Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the downsides of convolution by FFT compared to realspace convolution?

Tags:

So I am aware that a convolution by FFT has a lower computational complexity than a convolution in real space. But what are the downsides of an FFT convolution?

Does the kernel size always have to match the image size, or are there functions that take care of this, for example in pythons numpy and scipy packages? And what about anti-aliasing effects?

like image 658
ABDreverhaven Avatar asked Aug 22 '13 14:08

ABDreverhaven


People also ask

Is FFT faster than convolution?

For filter kernels longer than about 64 points, FFT convolution is faster than standard convolution, while producing exactly the same result.

Does PyTorch use FFT for convolution?

This is very easy, because N-dimensional FFTs are already implemented in PyTorch. We simply use the built-in function, and compute the FFT along the last dimension of each Tensor.


1 Answers

FFT convolutions are based on the convolution theorem, which states that given two functions f and g, if Fd() and Fi() denote the direct and inverse Fourier transform, and * and . convolution and multiplication, then:

f*g = Fi(Fd(d).Fd(g)) 

To apply this to a signal f and a kernel g, there are some things you need to take care of:

  • f and g have to be of the same size for the multiplication step to be possible, so you need to zero-pad the kernel (or input, if the kernel is longer than it).
  • When doing a DFT, which is what FFT does, the resulting frequency domain representation of the function is periodic. This means that, by default, your kernel wraps around the edge when doing the convolution. If you want this, then all is great. But if not, you have to add an extra zero-padding the size of the kernel to avoid it.
  • Most (all?) FFT packages only work well (performance-wise) with sizes that do not have any large prime factors. Rounding the signal and kernel size up to the next power of two is a common practice that may result in a (very) significant speed-up.

If your signal and kernel sizes are f_l and g_l, doing a straightforward convolution in time domain requires g_l * (f_l - g_l + 1) multiplications and (g_l - 1) * (f_l - g_l + 1) additions.

For the FFT approach, you have to do 3 FFTs of size at least f_l + g_l, as well as f_l + g_l multiplications.

For large sizes of both f and g, the FFT is clearly superior with its n*log(n) complexity. For small kernels, the direct approach may be faster.

scipy.signal has both convolve and fftconvolve methods for you to play around. And fftconvolve handles all the padding described above transparently for you.

like image 109
Jaime Avatar answered Sep 27 '22 22:09

Jaime