Python中最快的2D卷积或图像过滤器 [英] Fastest 2D convolution or image filter in Python
问题描述
几个用户以numpy或scipy询问了图像卷积的速度或内存消耗[ 1 , 2 , 3 ,
这实际上取决于您要执行的操作...很多时候,您不需要完全通用的(阅读速度较慢)2D卷积...(即,如果滤波器是可分离的,则改用两个1D卷积...这就是为什么各种 无论如何,作为比较点: 这在我的机器上需要6.9秒... 将此与
scipy.ndimage.gaussian
,scipy.ndimage.uniform
比比作为通用nD卷积实现的相同对象要快得多的原因.)>
t = timeit.timeit(stmt='ndimage.convolve(x, y, output=x)', number=1,
setup="""
import numpy as np
from scipy import ndimage
x = np.random.random((2048, 2048)).astype(np.float32)
y = np.random.random((32, 32)).astype(np.float32)
""")
print t
fftconvolve
t = timeit.timeit(stmt="signal.fftconvolve(x, y, mode='same')", number=1,
setup="""
import numpy as np
from scipy import signal
x = np.random.random((2048, 2048)).astype(np.float32)
y = np.random.random((32, 32)).astype(np.float32)
""")
print t
这大约需要10.8秒.但是,在输入大小不同的情况下,使用fft进行卷积可能会更快(尽管目前看来我似乎还没有一个很好的例子……).
Several users have asked about the speed or memory consumption of image convolutions in numpy or scipy [1, 2, 3, 4]. From the responses and my experience using Numpy, I believe this may be a major shortcoming of numpy compared to Matlab or IDL.
None of the answers so far have addressed the overall question, so here it is: "What is the fastest method for computing a 2D convolution in Python?" Common python modules are fair game: numpy, scipy, and PIL (others?). For the sake of a challenging comparison, I'd like to propose the following rules:
- Input matrices are 2048x2048 and 32x32, respectively.
- Single or double precision floating point are both acceptable.
- Time spent converting your input matrix to the appropriate format doesn't count -- just the convolution step.
- Replacing the input matrix with your output is acceptable (does any python library support that?)
- Direct DLL calls to common C libraries are alright -- lapack or scalapack
- PyCUDA is right out. It's not fair to use your custom GPU hardware.
It really depends on what you want to do... A lot of the time, you don't need a fully generic (read: slower) 2D convolution... (i.e. If the filter is separable, you use two 1D convolutions instead... This is why the various scipy.ndimage.gaussian
, scipy.ndimage.uniform
, are much faster than the same thing implemented as a generic n-D convolutions.)
At any rate, as a point of comparison:
t = timeit.timeit(stmt='ndimage.convolve(x, y, output=x)', number=1,
setup="""
import numpy as np
from scipy import ndimage
x = np.random.random((2048, 2048)).astype(np.float32)
y = np.random.random((32, 32)).astype(np.float32)
""")
print t
This takes 6.9 sec on my machine...
Compare this with fftconvolve
t = timeit.timeit(stmt="signal.fftconvolve(x, y, mode='same')", number=1,
setup="""
import numpy as np
from scipy import signal
x = np.random.random((2048, 2048)).astype(np.float32)
y = np.random.random((32, 32)).astype(np.float32)
""")
print t
This takes about 10.8 secs. However, with different input sizes, using fft's to do a convolution can be considerably faster (Though I can't seem to come up with a good example, at the moment...).
这篇关于Python中最快的2D卷积或图像过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!