【问题标题】:How and why is FFT convolution faster than direct convolution?FFT 卷积如何以及为什么比直接卷积更快?
【发布时间】:2019-12-01 17:34:22
【问题描述】:

我读到卷积在频域计算时速度更快,因为它“只是”一个矩阵乘法(在 2D 中),而在时域中它是很多小矩阵乘法。

所以我编写了这段代码,我们可以看到 FFT 卷积比“普通”卷积更复杂。 很明显,我的假设有问题。

怎么了?

from sympy import exp, log, symbols, init_printing, lambdify
init_printing(use_latex='matplotlib')

import numpy as np
import matplotlib.pyplot as plt

def _complex_mult(n):
  """Complexity of a MatMul of a 2 matrices of size (n, n)"""
  # see https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm
  return n**2.5

def _complex_fft(n):
  """Complexity of fft and ifft"""
  # see https://en.wikipedia.org/wiki/Fast_Fourier_transform
  return n*log(n)

def fft_mult_fft(n, m):
  """Complexity of a convolution in the freq space.
  fft -> mult between M and kernel -> ifft
  """
  return _complex_fft(n) * 2 + _complex_mult(n)

def conv(n, m):
  """Complexity of a convolution in the time space.
  for every n of M, we execute a MatMul of 2 (m, m) matrices
  """
  return n*_complex_mult(m)


n = symbols('n') # size of M = (n, n)
m = symbols('m') # size of kernel = (m, m)

M = np.linspace(1, 1e3+1, 1e1)
kernel_size = np.linspace(2, 7, 7-2+1)**2

fft = fft_mult_fft(n, m)
discrete = conv(n, m)

f1 = lambdify(n, fft, 'numpy')
f2 = lambdify([n, m], discrete, 'numpy')

fig, ax = plt.subplots(1, len(kernel_size), figsize=(30, 10))

f1_computed = f1(M) # independant wrt m, do not compute it at each time

for i, size in enumerate(kernel_size):
  ax[i].plot(M, f1_computed, c='red', label='freq domain (fft)')
  ax[i].plot(M, f2(M, size), c='blue', label='time domain (normal)')
  ax[i].legend(loc='upper left')
  ax[i].set_title("kernel size = {}".format(size))
  ax[i].set_xlabel("Matrix size")
  ax[i].set_ylabel("Complexity")

这是输出:(点击放大)

【问题讨论】:

  • 真的很有趣的问题!我不知道完整的答案,但只是在这里查看 2D FFT 的复杂性:stackoverflow.com/questions/6514861/…,_complex_fft 不应该是 n^2 log(n) 吗?
  • 另外,在_conv 中,我认为你必须将 m x m 矩阵乘以 n^2 次,因为它是二维的。
  • 你说得对,解决了。
  • 你怎么称呼尺寸? n 还是 n² ???
  • 代码中写的n = symbols('n') # size of M = (n, n);内核大小为 m,其中 m = row*col(对于方形过滤器,这就是我尝试使用 2 的幂的原因)

标签: numpy image-processing matrix signals fft


【解决方案1】:

您正在经历两个众所周知的事实:

  • 对于较小的内核大小,空间方法更快,

  • 对于较大的内核大小,频率方法可以更快。

您的内核和图像相对太小,无法观察 FFT 的好处。

【讨论】:

    【解决方案2】:

    正如@user545424 指出的那样,问题在于我正在计算n*complexity(MatMul(kernel)) 而不是n²*complexity(MatMul(kernel)) 来进行“正常”卷积。

    我终于明白了:(其中 n 是输入的大小,m 是内核的大小)


    这是最终代码和新图表。

    from sympy import exp, log, symbols, init_printing, lambdify
    init_printing(use_latex='matplotlib')
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    def _complex_mult(n):
      """Complexity of a MatMul of a 2 matrices of size (n, n)"""
      # see https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm
      return n**2.5
    
    def _complex_fft(n):
      """Complexity of fft and ifft"""
      # see https://stackoverflow.com/questions/6514861/computational-complexity-of-the-fft-in-n-dimensions#comment37078975_6516856
      return 4*(n**2)*log(n)
    
    def fft_mult_fft(n, m):
      """Complexity of a convolution in the freq space.
      fft -> mult between M and kernel -> ifft
      """
      return _complex_fft(n) * 2 + _complex_mult(n)
    
    def conv(n, m):
      """Complexity of a convolution in the time space.
      for every n*n cell of M, we execute a MatMul of 2 (m, m) matrices
      """
      return n*n*_complex_mult(m)
    
    
    n = symbols('n') # size of M = (n, n)
    m = symbols('m') # size of kernel = (m, m)
    
    M = np.linspace(1, 1e3+1, 1e1)
    kernel_size = np.linspace(2, 7, 7-2+1)**2
    
    fft_symb = fft_mult_fft(n, m)
    discrete_symb = conv(n, m)
    
    fft_func = lambdify(n, fft_symb, 'numpy')
    dicrete_func = lambdify([n, m], discrete_symb, 'numpy')
    
    
    fig, ax = plt.subplots(1, len(kernel_size), figsize=(30, 10))
    fig.patch.set_facecolor('grey')
    
    for i, size in enumerate(kernel_size):
      ax[i].plot(M, fft_func(M), c='red', label='freq domain (fft)')
      ax[i].plot(M, dicrete_func(M, size), c='blue', label='time domain (normal)')
      ax[i].legend(loc='upper left')
      ax[i].set_title("kernel size = {}".format(size))
      ax[i].set_xlabel("Matrix size")
      ax[i].set_ylabel("Complexity")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-25
      • 2012-12-10
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 2011-12-30
      • 2011-10-23
      相关资源
      最近更新 更多