简体   繁体   English

从3D Matrix中提取补丁

[英]Extract patches from 3D Matrix

I have a 3D matrix A of dimensions hxwxc . 我有一个尺寸为hxwxc的3D矩阵A I want to extract patches of dimensions ph x pw from each "channel" c . 我想从每个“通道” c提取尺寸为ph x pw贴片。 ph divides h and pw divides w . ph除以hpw除以w In this example, 在这个例子中,

h x w x c = 4 x 4 x 3
ph x pw = 2 x 2

例

I know how to do this in tensorflow using gather_nd but I was hoping for something more efficient in terms of setting it up, because the dimensions will be big and I'd rather not have the indices array of gather_nd in memory. 我知道如何使用gather_nd在tensorflow中执行此操作,但我希望在设置它时更有效率,因为维度很大,我宁愿在内存中没有gather_nd的indices数组。 Is there possibly an intelligent reshape? 可能有一个聪明的重塑? Either numpy or tensorflow solution would be very nice! numpy或tensorflow解决方案都非常好!

You could use some reshaping and swapping of axes - 你可以使用一些重塑和交换轴 -

A.reshape(h//ph,ph,w//pw,pw,-1).swapaxes(1,2)

Sample run - 样品运行 -

In [46]: # Sample inputs
    ...: h,w,c = 10,12,3
    ...: ph, pw = 2,2
    ...: A = np.random.randint(0,9,(h,w,c))
    ...: 

In [47]: A.reshape(h//ph,ph,w//pw,pw,-1).swapaxes(1,2).shape
Out[47]: (5, 6, 2, 2, 3)

Each element (as block) along first two axes represent the patches. 沿前两个轴的每个元素(作为块)代表贴片。 Thus. 从而。 for the sample provided, we would have 5 x 6 = 30 patches. 对于提供的样本,我们将有5 x 6 = 30补丁。

If you want those patches along one merged first axis, use one more reshape - 如果您希望这些补丁沿着一个合并的第一个轴,请再使用一个reshape -

In [85]: out = A.reshape(h//ph,ph,w//pw,pw,-1).swapaxes(1,2).reshape(-1,ph,pw,c)

In [86]: out.shape
Out[86]: (30, 2, 2, 3)

Let's verify by manually inspecting values themselves - 让我们通过手动检查值本身来验证 -

In [81]: A[:ph,:pw] # First patch
Out[81]: 
array([[[6, 5, 2],
        [4, 0, 1]],

       [[0, 0, 4],
        [2, 3, 0]]])

In [82]: A[:ph,pw:2*pw] # Second patch
Out[82]: 
array([[[8, 3, 3],
        [0, 0, 2]],

       [[8, 5, 4],
        [3, 4, 6]]])

In [83]: out[0]
Out[83]: 
array([[[6, 5, 2],
        [4, 0, 1]],

       [[0, 0, 4],
        [2, 3, 0]]])

In [84]: out[1]
Out[84]: 
array([[[8, 3, 3],
        [0, 0, 2]],

       [[8, 5, 4],
        [3, 4, 6]]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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