在Python中实现MATLAB的im2col“滑动”

在Python中实现MATLAB的im2col“滑动”,第1张

在Python中实现MATLAB的im2col“滑动

方法1

我们可以

broadcasting
在这里使用一些来一次性获得所有这些滑动窗口的所有索引,从而通过索引实现a
vectorized solution
。这是受启发的
Efficient Implementation of im2col andcol2im

这是实现-

def im2col_sliding_broadcasting(A, BSZ, stepsize=1):    # Parameters    M,N = A.shape    col_extent = N - BSZ[1] + 1    row_extent = M - BSZ[0] + 1    # Get Starting block indices    start_idx = np.arange(BSZ[0])[:,None]*N + np.arange(BSZ[1])    # Get offsetted indices across the height and width of input array    offset_idx = np.arange(row_extent)[:,None]*N + np.arange(col_extent)    # Get all actual indices & index into input array for final output    return np.take (A,start_idx.ravel()[:,None] + offset_idx.ravel()[::stepsize])

方法#2

利用新获得的知识

NumPy array strides
,我们可以创建此类滑动窗口,我们将有另一个有效的解决方案-

def im2col_sliding_strided(A, BSZ, stepsize=1):    # Parameters    m,n = A.shape    s0, s1 = A.strides        nrows = m-BSZ[0]+1    ncols = n-BSZ[1]+1    shp = BSZ[0],BSZ[1],nrows,ncols    strd = s0,s1,s0,s1    out_view = np.lib.stride_tricks.as_strided(A, shape=shp, strides=strd)    return out_view.reshape(BSZ[0]*BSZ[1],-1)[:,::stepsize]

方法#3

先前方法中列出的跨步方法已合并到

scikit-image
模块中,以减少混乱,例如-

from skimage.util import view_as_windows as viewWdef im2col_sliding_strided_v2(A, BSZ, stepsize=1):    return viewW(A, (BSZ[0],BSZ[1])).reshape(-1,BSZ[0]*BSZ[1]).T[:,::stepsize]

样品运行-

In [106]: a      # Input arrayOut[106]: array([[ 0,  1,  2,  3,  4],       [ 5,  6,  7,  8,  9],       [10, 11, 12, 13, 14],       [15, 16, 17, 18, 19]])In [107]: im2col_sliding_broadcasting(a, (2,3))Out[107]: array([[ 0,  1,  2,  5,  6,  7, 10, 11, 12],       [ 1,  2,  3,  6,  7,  8, 11, 12, 13],       [ 2,  3,  4,  7,  8,  9, 12, 13, 14],       [ 5,  6,  7, 10, 11, 12, 15, 16, 17],       [ 6,  7,  8, 11, 12, 13, 16, 17, 18],       [ 7,  8,  9, 12, 13, 14, 17, 18, 19]])In [108]: im2col_sliding_broadcasting(a, (2,3), stepsize=2)Out[108]: array([[ 0,  2,  6, 10, 12],       [ 1,  3,  7, 11, 13],       [ 2,  4,  8, 12, 14],       [ 5,  7, 11, 15, 17],       [ 6,  8, 12, 16, 18],       [ 7,  9, 13, 17, 19]])

运行时测试
In [183]: a = np.random.randint(0,255,(1024,1024))In [184]: %timeit im2col_sliding(img, (8,8), skip=1)     ...: %timeit im2col_sliding_broadcasting(img, (8,8), stepsize=1)     ...: %timeit im2col_sliding_strided(img, (8,8), stepsize=1)     ...: %timeit im2col_sliding_strided_v2(img, (8,8), stepsize=1)     ...: 1 loops, best of 3: 1.29 s per loop1 loops, best of 3: 226 ms per loop10 loops, best of 3: 84.5 ms per loop10 loops, best of 3: 111 ms per loopIn [185]: %timeit im2col_sliding(img, (8,8), skip=4)     ...: %timeit im2col_sliding_broadcasting(img, (8,8), stepsize=4)     ...: %timeit im2col_sliding_strided(img, (8,8), stepsize=4)     ...: %timeit im2col_sliding_strided_v2(img, (8,8), stepsize=4)     ...: 1 loops, best of 3: 1.31 s per loop10 loops, best of 3: 104 ms per loop10 loops, best of 3: 84.4 ms per loop10 loops, best of 3: 109 ms per loop

16x
加速过程中,采用了原始循环版本上的跨步方法!



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5632160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存