[英]Speeding up Python Numpy code
I have the following code: 我有以下代码:
big_k = gabor((height * 2, width *2), (height, width))
for r_slice in range(0,radialSlices):
r_pixels = r_slice * radialWidth
for a_slice in range(0,angularSlices):
a_pixels = a_slice * angularWidth
k_win = big_k[height - r_pixels:2*height - r_pixels,width - a_pixels:2 * width - a_pixels]
result = np.sum(img * k_win)
img
is a uint8
array of 640x480, and big_k
is complex64
1280x960. img
是一个640x480的uint8
数组,而big_k
是complex64
1280x960。
This code amounts to 1024 640x480 matrix multiplications and a cast to complex64. 此代码总计1024 640x480矩阵乘法,并强制转换为complex64。
This code takes on the order of 2 seconds to run on my macbook; 此代码大约需要2秒才能在我的Macbook上运行; I'm looking to try and get a speedup of the order of 100x.
我希望尝试获得100倍的加速比。 What can I do?
我能做什么?
What you're doing looks kind of a like a convolution, so I'd recommend trying to implement it using a convolution operation. 您正在执行的操作看起来像是卷积,因此建议您尝试使用卷积操作来实现它。 Convolutions can be computed very efficiently with an FFT-based approach, and are implemented in SciPy as
scipy.signal.fftconvolve
. 可以使用基于FFT的方法非常有效地计算卷积,并在SciPy
scipy.signal.fftconvolve
其实现为scipy.signal.fftconvolve
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.