gpt4 book ai didi

python - 有效地旋转 3D 阵列中的 block /窗口(矢量化扩散?)

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:45 29 4
gpt4 key购买 nike

我有一个大的 3D np.array,我们假设大小为 (200,200,7)。

我想为第一个轴上的每个 2*2 子数组应用 np.rot90。另一个问题是以随机方式旋转每个子阵列。像这样: enter image description here

颜色仅用于显示不同的 2*2 数组,箭头说明每个数组都根据作为参数生成的随机数旋转 numpy.rot90(m, k=RND(1,2 ,3), 轴=(0, 1)).

这是否可以在一个快速步骤中实现,而无需在每个单独的子数组上循环?

根据 Divakar 的回答,我也尝试做一个扩展,其中只有 x% 的子阵列一步移动,其余的保持不变,希望这表现得像一个 2D 扩散系统。

def vectorized_diffusion(a,H,W,pD):
#pD - chance that a sub-array is rotated in a random direction
rand_shift = np.random.randint(-1,2)
rand_axis = np.random.randint(0,2)
a = np.roll(a, shift = randshift, axis = rand_axis)
# Since the 2*2 subgrid system is fixed, I decided to ocassionally
#disturb the grid by rolling the whole array by one in a given
#direction, as in my work the array is a toroid grid i considered every direction
m,n,r = a.shape
a5D = a.reshape(m//H,H,n//W,W,-1)
cw0 = a5D[:,::-1,:,:,:].transpose(0,2,3,1,4)
ccw0 = a5D[:,:,:,::-1,:].transpose(0,2,3,1,4)
original = a5D[:,:,:,:,:].transpose(0,2,1,3,4)
mask_clockdirection = np.random.choice([False,True],size=(m//H,n//W))
mask_stationary = np.random.choice([True,False],size=(m//H,n//W), p=[1-pD,pD])
w0 = np.where(mask_clockdirection[:,:,None,None,None],cw0,ccw0)
out = np.where(mask_stationary[:,:,None,None,None],original,w0)
out = out.swapaxes(1,2).reshape(a.shape)
out_rerolled = np.roll(out, shift = -1*randshift, axis = rand_axis)
#this way the disturbed grid is rerolled into its original position
return out_rerolled

我知道这可能不是解决这个问题的最优雅的解决方案,但它似乎有效,我对此很满意。

最佳答案

使用翻转和置换轴执行旋转(顺时针和逆时针)的通用方法 -

# Input array
In [176]: k
Out[176]:
array([[26, 48, 71],
[54, 96, 82],
[87, 21, 2]])

# Clockwise
In [178]: k[::-1,:].T
Out[178]:
array([[87, 54, 26],
[21, 96, 48],
[ 2, 82, 71]])

# Anti-clockwise
In [177]: k[:,::-1].T
Out[177]:
array([[71, 82, 2],
[48, 96, 21],
[26, 54, 87]])

扩展到带窗口旋转的2D数组

In [204]: np.random.seed(0)

In [205]: a = np.random.randint(0,100,(6,6))

In [206]: a
Out[206]:
array([[44, 47, 64, 67, 67, 9],
[83, 21, 36, 87, 70, 88],
[88, 12, 58, 65, 39, 87],
[46, 88, 81, 37, 25, 77],
[72, 9, 20, 80, 69, 79],
[47, 64, 82, 99, 88, 49]])

# Clockwise
In [207]: a.reshape(3,2,3,2)[:,::-1,:,:].swapaxes(1,3).reshape(a.shape)
Out[207]:
array([[83, 44, 36, 64, 70, 67],
[21, 47, 87, 67, 88, 9],
[46, 88, 81, 58, 25, 39],
[88, 12, 37, 65, 77, 87],
[47, 72, 82, 20, 88, 69],
[64, 9, 99, 80, 49, 79]])

# Anti-clockwise
In [209]: a.reshape(3,2,3,2)[:,:,:,::-1].swapaxes(1,3).reshape(a.shape)
Out[209]:
array([[47, 21, 67, 87, 9, 88],
[44, 83, 64, 36, 67, 70],
[12, 88, 65, 37, 87, 77],
[88, 46, 58, 81, 39, 25],
[ 9, 64, 80, 99, 79, 49],
[72, 47, 20, 82, 69, 88]])

扩展到 3D 数组,在每个 2D 切片上进行窗口化旋转 -

In [223]: a = np.random.randint(0,100,(6,6,2))

# Clockwise
In [224]: cw = a.reshape(3,2,3,2,2)[:,::-1,:,:,:].swapaxes(1,3).reshape(a.shape)

# Anti-clockwise
In [233]: ccw = a.reshape(3,2,3,2,2)[:,:,:,::-1,:].swapaxes(1,3).reshape(a.shape)

In [225]: a[...,0]
Out[225]:
array([[44, 64, 67, 83, 36, 70],
[88, 58, 39, 46, 81, 25],
[72, 20, 69, 47, 82, 88],
[29, 19, 39, 65, 57, 31],
[23, 75, 28, 0, 36, 5],
[17, 4, 58, 1, 41, 35]])

In [226]: cw[...,0]
Out[226]:
array([[88, 44, 39, 67, 81, 36],
[58, 64, 46, 83, 25, 70],
[29, 72, 39, 69, 57, 82],
[19, 20, 65, 47, 31, 88],
[17, 23, 58, 28, 41, 36],
[ 4, 75, 1, 0, 35, 5]])


In [236]: ccw[...,0]
Out[236]:
array([[64, 58, 83, 46, 70, 25],
[44, 88, 67, 39, 36, 81],
[20, 19, 47, 65, 88, 31],
[72, 29, 69, 39, 82, 57],
[75, 4, 0, 1, 5, 35],
[23, 17, 28, 58, 36, 41]])

解决我们用面具在这两者之间进行选择的情况

我们需要让它适用于我们的案例。我们将使用掩码在顺时针和逆时针版本之间进行选择 -

cw0 = a.reshape(3,2,3,2,2)[:,::-1,:,:,:].swapaxes(1,3)
ccw0 = a.reshape(3,2,3,2,2)[:,:,:,::-1,:].swapaxes(1,3)
mask = np.random.choice([False,True],size=(3,3))
out = np.where(mask[:,:,None,None,None],cw0.swapaxes(1,2),ccw0.swapaxes(1,2))

我们可以优化/使其更紧凑-

cw0 = a.reshape(3,2,3,2,2)[:,::-1,:,:,:].transpose(0,2,3,1,4)
ccw0 = a.reshape(3,2,3,2,2)[:,:,:,::-1,:].transpose(0,2,3,1,4)
out = np.where(mask[:,:,None,None,None],cw0,ccw0)

最后,让它处理一般情况 -

def random_rotate_windows(a,H,W):
m,n,r = a.shape
a5D = a.reshape(m//H,H,n//W,W,-1)
cw0 = a5D[:,::-1,:,:,:].transpose(0,2,3,1,4)
ccw0 = a5D[:,:,:,::-1,:].transpose(0,2,3,1,4)
mask = np.random.choice([False,True],size=(m//H,n//W))
out = np.where(mask[:,:,None,None,None],cw0,ccw0)
return out.swapaxes(1,2).reshape(a.shape)

以示例运行结束 -

In [332]: np.random.seed(0)
...: a = np.random.randint(0,100,(6,6,2))

In [333]: a[...,0]
Out[333]:
array([[44, 64, 67, 83, 36, 70],
[88, 58, 39, 46, 81, 25],
[72, 20, 69, 47, 82, 88],
[29, 19, 39, 65, 57, 31],
[23, 75, 28, 0, 36, 5],
[17, 4, 58, 1, 41, 35]])

In [334]: out = random_rotate_windows(a,2,2)

In [335]: out[...,0]
Out[335]:
array([[64, 58, 83, 46, 81, 36],
[44, 88, 67, 39, 25, 70],
[20, 19, 47, 65, 57, 82],
[72, 29, 69, 39, 31, 88],
[17, 23, 0, 1, 41, 36],
[ 4, 75, 28, 58, 35, 5]])

关于python - 有效地旋转 3D 阵列中的 block /窗口(矢量化扩散?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54910672/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com