8

Convolutions are essential components of many algorithms in neural networks, image processing, computer vision ... but these are also a bottleneck in terms of computations... In the python ecosystem, there are different existing solutions using numpy, scipy or tensorflow, but which is the fastest?

Just to set the problem, the convolution should operate on two 2-D matrices. We will here always consider the case which is most typical in computer vision:

  • a first matrix $A$ is the input and is typically large ($N \times N$ where $N$ is typically larger than $2^{10}=1024$),
  • a second matrix $B$ is the template and is typically smaller (say $M=128$),
  • the result of the convolution $C = A \ast B$ is padded such that it is of the same size as $A$.

    Often, you need to do that with many images on many kernels, so a method that does it on many has a bonus point.

Thanks for any hint!

PS: the goal is at some point to sum up these results in this blog post which already contains some examples...

Royi
  • 19,608
  • 4
  • 197
  • 238
meduz
  • 816
  • 6
  • 15
  • Do the same. Instead of Multiplication in Frequency do Division. – Royi Sep 26 '17 at 16:19
  • Test with timeit. 2. scipy's should be faster than numpy, we spent a lot of time optimizing it (real FFT method, padding to 5-smooth lengths, using direct convolution when one input is much smaller, etc.) Don't know how it compares to tensorflow.
  • – endolith Sep 26 '17 at 21:52
  • You could also try OpenCV, which has inbuilt algorithms for that (and they are usually quite fast) http://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html – Julian S. Sep 27 '17 at 02:09