1

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?

7
  • What are you actually trying to achieve? Commented Jun 10, 2014 at 5:23
  • Actually, this just looks like a big convolution. If that's the case, try one of Scipy's convolution functions, or use an FFT-based convolution approach. Commented Jun 10, 2014 at 5:31
  • The idea is to apply a filter centred at several points across an image, and then take the sum of the values of the result at each point. Come to think about it it is similar to a convolution. Could I just convolve the two and then take the results at each point across? There would be no need to sum either in that case AFAIK? Or have I misunderstood? Commented Jun 10, 2014 at 5:43
  • Basically, yes. A convolution is likely to be much faster, and you can obtain the results at every point in the image (or just at selected points, your choice). Try scipy.signal.fftconvolve. Commented Jun 10, 2014 at 5:46
  • OK That's fabulous- I just tried it. Speedup is on the order of 2-3x - which is a huge improvement, plus a much 'sexier' solution. This completely changes the code, so I guess this question is answered! I can continue to optimise from here. If you want to write the answer below I'll accept it! Thanks! Commented Jun 10, 2014 at 5:54

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.