简体   繁体   中英

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.

This code amounts to 1024 640x480 matrix multiplications and a cast to complex64.

This code takes on the order of 2 seconds to run on my macbook; I'm looking to try and get a speedup of the order of 100x. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM