将3D numpy数组拆分为3D块

时间:2016-09-10 19:36:11

标签: performance python-2.7 numpy memory-efficient

我想将一个3D numpy数组拆分成一个' pythonic'办法。我正在处理有些大型数组(1000X1200X1600)的图像序列,所以我需要将它们分成几块来进行处理。

我已经编写了这样做的函数,但是我想知道是否有一种本地的numpy方法来实现这一点 - numpy.split似乎没有做我想要的3D数组(但也许我不明白它的功能)

要明确:下面的代码完成了我的任务,但我正在寻求一种更快的方法。

def make_blocks(x,t):
#x should be a yXmXn matrix, and t should even divides m,n
#returns a list of 3D blocks of size yXtXt 
    down =  range(0,x.shape[1],t)
    across = range(0,x.shape[2],t)
    reshaped = []
    for d in down:
        for a in across:
            reshaped.append(x[:,d:d+t,a:a+t])
    return reshaped

def unmake_blocks(x,d,m,n):
#this takes a list of matrix blocks of size dXd that is m*n/d^2 long 
#returns a 2D array of size mXn
    rows = []
    for i in range(0,int(m/d)):
        rows.append(np.hstack(x[i*int(n/d):(i+1)*int(n/d)]))
    return np.vstack(rows)

1 个答案:

答案 0 :(得分:6)

以下是使用np.transposereshapingview_as_blocks进行置换调整的组合的那些循环实现的矢量化版本 -

def make_blocks_vectorized(x,d):
    p,m,n = x.shape
    return x.reshape(-1,m//d,d,n//d,d).transpose(1,3,0,2,4).reshape(-1,p,d,d)

def unmake_blocks_vectorized(x,d,m,n):    
    return np.concatenate(x).reshape(m//d,n//d,d,d).transpose(0,2,1,3).reshape(m,n)

make_blocks -

的示例运行
In [120]: x = np.random.randint(0,9,(2,4,4))

In [121]: make_blocks(x,2)
Out[121]: 
[array([[[4, 7],
         [8, 3]],

        [[0, 5],
         [3, 2]]]), array([[[5, 7],
         [4, 0]],

        [[7, 3],
         [5, 7]]]), ... and so on.

In [122]: make_blocks_vectorized(x,2)
Out[122]: 
array([[[[4, 7],
         [8, 3]],

        [[0, 5],
         [3, 2]]],


       [[[5, 7],
         [4, 0]],

        [[7, 3],
         [5, 7]]],  ... and so on.

unmake_blocks -

的示例运行
In [135]: A = [np.random.randint(0,9,(3,3)) for i in range(6)]

In [136]: d = 3

In [137]: m,n = 6,9

In [138]: unmake_blocks(A,d,m,n)
Out[138]: 
array([[6, 6, 7, 8, 6, 4, 5, 4, 8],
       [8, 8, 3, 2, 7, 6, 8, 5, 1],
       [5, 2, 2, 7, 1, 2, 3, 1, 5],
       [6, 7, 8, 2, 2, 1, 6, 8, 4],
       [8, 3, 0, 4, 4, 8, 8, 6, 3],
       [5, 5, 4, 8, 5, 2, 2, 2, 3]])

In [139]: unmake_blocks_vectorized(A,d,m,n)
Out[139]: 
array([[6, 6, 7, 8, 6, 4, 5, 4, 8],
       [8, 8, 3, 2, 7, 6, 8, 5, 1],
       [5, 2, 2, 7, 1, 2, 3, 1, 5],
       [6, 7, 8, 2, 2, 1, 6, 8, 4],
       [8, 3, 0, 4, 4, 8, 8, 6, 3],
       [5, 5, 4, 8, 5, 2, 2, 2, 3]])

使用{{3}} -

替代make_blocks
from skimage.util.shape import view_as_blocks

def make_blocks_vectorized_v2(x,d):
    return view_as_blocks(x,(x.shape[0],d,d))

运行时测试

1)make_blocks采用原始和view_as_blocks方法 -

In [213]: x = np.random.randint(0,9,(100,160,120)) # scaled down by 10

In [214]: %timeit make_blocks(x,10)
1000 loops, best of 3: 198 µs per loop

In [215]: %timeit view_as_blocks(x,(x.shape[0],10,10))
10000 loops, best of 3: 85.4 µs per loop

2)unmake_blocks采用原始和transpose+reshape方法 -

In [237]: A = [np.random.randint(0,9,(10,10)) for i in range(600)]

In [238]: d = 10

In [239]: m,n = 10*20,10*30

In [240]: %timeit unmake_blocks(A,d,m,n)
100 loops, best of 3: 2.03 ms per loop

In [241]: %timeit unmake_blocks_vectorized(A,d,m,n)
1000 loops, best of 3: 511 µs per loop