[Numpy-discussion] Efficient numpy slicing for a "sliding window approach".

Nicolas Pinto pinto at mit.edu
Fri Feb 20 23:46:09 EST 2009


Dear all,

I'm trying to optimize the code below and I was wondering if there is an
efficient method that could reduce the numpy slicing overheard without going
with cython. Is there anyway I could use mgrid to get a matrix with all my
"windows" and then do a large matrix multiply instead?

Any idea?

Thanks in advance.


Best regards,

-- 
Nicolas Pinto
Ph.D. Candidate, Brain & Computer Sciences
Massachusetts Institute of Technology, USA
http://web.mit.edu/pinto


========================================

import numpy as np
from numpy import dot

arrh, arrw, arrd = 480,640,96
arr = np.random.randn(arrh, arrw, arrd).astype("float32")

stride = 16
winh, winw, wind = 128,64,96

limit = 100

clas_w = np.random.randn(8,4,96).astype("float32").ravel()

@profile
def func():
    nh, nw = arrh-winh+1, arrw-winw+1
    rngh = np.arange(nh)
    rngw = np.arange(nw)

    np.random.shuffle(rngh)
    np.random.shuffle(rngw)

    rngh = rngh[:limit]
    rngw = rngw[:limit]

    rngh = np.sort(rngh)
    rngw = np.sort(rngw)
    resps = np.empty((nh,nw), dtype="float32")
    for j in rngh:
        for i in rngw:
            win = arr[j:j+winh:stride, i:i+winw:stride].ravel()
            resp = dot(win, clas_w)
            resps[j,i] = resp

    resps = resps.ravel()

    # ...

func()


========================================

% python kernprof.py -l -v sliding_win.py
Wrote profile results to sliding_win.py.lprof
Timer unit: 1e-06 s

File: sliding_win.py
Function: func at line 14
Total time: 0.224315 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    14                                           @profile
    15                                           def func():
    16         1            7      7.0      0.0      nh, nw = arrh-winh+1,
arrw-winw+1
    17         1           14     14.0      0.0      rngh = np.arange(nh)
    18         1            4      4.0      0.0      rngw = np.arange(nw)
    19
    20         1           89     89.0      0.0      np.random.shuffle(rngh)
    21         1          124    124.0      0.1      np.random.shuffle(rngw)
    22
    23         1            3      3.0      0.0      rngh = rngh[:limit]
    24         1            2      2.0      0.0      rngw = rngw[:limit]
    25
    26         1           39     39.0      0.0      rngh = np.sort(rngh)
    27         1           19     19.0      0.0      rngw = np.sort(rngw)
    28         1           24     24.0      0.0      resps =
np.empty((nh,nw), dtype="float32")
    29       101          127      1.3      0.1      for j in rngh:
    30     10100        12636      1.3      5.6          for i in rngw:
    31     10000       118190     11.8     52.7              win =
arr[j:j+winh:stride, i:i+winw:stride].ravel()
    32     10000        73854      7.4     32.9              resp = dot(win,
clas_w)
    33     10000        19180      1.9      8.6              resps[j,i] =
resp
    34
    35         1            3      3.0      0.0      resps = resps.ravel()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090220/19d6815b/attachment.html>


More information about the NumPy-Discussion mailing list