在堆叠的 np.array 上滑动窗口(Python)

发布于 2025-01-12 05:55:51 字数 792 浏览 5 评论 0原文

我正在尝试跨堆栈 np 数组创建一个多维窗口,并找到每个窗口的最大值。下面的 res 是一个形状为 (3, 4, 4) 的堆叠数组。我想要一个通过轴的尺寸为 2x2 的窗口。例如,第一个窗口将为 (3,2,2),其值为:

ideal_result = np.array([[13, 45], [1, 2], [11, 22], [1, 2], [1, 2], [1, 7]])

然后 max 窗口将为: np.max(ideal_result) = 45

这将是整个窗口并构造一个 2x2使用 np.array([[45 67],[23 44]])

设置:

a = np.array([[13, 45, 67, 4], [1, 2, 3, 4], [2, 3, 4, 6], [1, 23, 44, 1]])
b = np.array([[11, 22, 33, 57], [1, 2, 3, 4], [2, 3, 94, 6], [1, 23, 44, 1]])
c = np.array([[1, 2, 3, 4], [1, 7, 8, 9], [2, 3, 4, 76], [1, 23, 44, 1]])
res = np.stack((a, b, c))
print(np.shape(res))

尝试的代码:

import numpy as np
v = np.lib.stride_tricks.as_strided(res, shape=(3, 2, 2), strides=(3, 2, 2))

I am trying to create a multi-dimensional window across a stack np array and find the max of each of the windows. Below res is a stacked array with shape (3, 4, 4). I would like to have a window with 2x2 size through the axis. For example the first window will be (3,2,2) with values:

ideal_result = np.array([[13, 45], [1, 2], [11, 22], [1, 2], [1, 2], [1, 7]])

then max the window would be: np.max(ideal_result) = 45

This would be be the entire windows and construct a 2x2 with np.array([[45 67],[23 44]])

Set up:

a = np.array([[13, 45, 67, 4], [1, 2, 3, 4], [2, 3, 4, 6], [1, 23, 44, 1]])
b = np.array([[11, 22, 33, 57], [1, 2, 3, 4], [2, 3, 94, 6], [1, 23, 44, 1]])
c = np.array([[1, 2, 3, 4], [1, 7, 8, 9], [2, 3, 4, 76], [1, 23, 44, 1]])
res = np.stack((a, b, c))
print(np.shape(res))

Code Attempted:

import numpy as np
v = np.lib.stride_tricks.as_strided(res, shape=(3, 2, 2), strides=(3, 2, 2))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末 2025-01-19 05:55:51

通常有助于思考展平数组看起来如何以获得正确的步幅:

res.flatten()

输出:

array([13, 45, 67,  4,  1,  2,  3,  4,  2,  3,  4,  6,  1, 23, 44,  1, 11,
       22, 33, 57,  1,  2,  3,  4,  2,  3, 94,  6,  1, 23, 44,  1,  1,  2,
        3,  4,  1,  7,  8,  9,  2,  3,  4, 76,  1, 23, 44,  1])

数据类型为 int64,它是 8 个字节,因此每个连续元素之间的步幅是 8。我将标记哪个元素应该位于第一个窗口中,步幅为:

        0   1           4   5                                          16
array([13, 45, 67,  4,  1,  2,  3,  4,  2,  3,  4,  6,  1, 23, 44,  1, 11,
        
       17          20  21                                          32  33
       22, 33, 57,  1,  2,  3,  4,  2,  3, 94,  6,  1, 23, 44,  1,  1,  2,

               36  37
        3,  4,  1,  7,  8,  9,  2,  3,  4, 76,  1, 23, 44,  1])

你能看到图案吗?

我们为每个维度设置以下值和步长:

x| values: 13->45, 11->22, 1->2, ... stride: 1
y| values: 13->1, 45->2, 11->2, ... stride: 4
z| values: 13->11, 45->22, 11->1, ... stride: 16
np.lib.stride_tricks.as_strided(res, shape=(3, 2, 2), strides=(8 * 16, 8 * 4, 8 * 1))

输出:

array([[[13, 45],
        [ 1,  2]],

       [[11, 22],
        [ 1,  2]],

       [[ 1,  2],
        [ 1,  7]]])

这只是一个窗口,我们需要 4 个窗口,每个窗口之间的跳转在 x 方向上为 8*2 字节,在 y 方向上为 8*8 字节。

windows = np.lib.stride_tricks.as_strided(res, shape=(2, 2, 3, 2, 2), strides=(8 * 8, 8 * 2, 8 * 16, 8 * 4, 8 * 1))
windows.max(axis=(2, 3, 4))

输出:

array([[45, 67],
       [23, 94]])

假设步幅与内核大小相同(如传统的 2D 最大池化)。

It usually helps to think about how the flattened array looks to get the strides right:

res.flatten()

output:

array([13, 45, 67,  4,  1,  2,  3,  4,  2,  3,  4,  6,  1, 23, 44,  1, 11,
       22, 33, 57,  1,  2,  3,  4,  2,  3, 94,  6,  1, 23, 44,  1,  1,  2,
        3,  4,  1,  7,  8,  9,  2,  3,  4, 76,  1, 23, 44,  1])

the dtype is int64 which is 8 bytes, so the stride between each consecutive element is 8. I'll mark which elements should be in the first window with the stride:

        0   1           4   5                                          16
array([13, 45, 67,  4,  1,  2,  3,  4,  2,  3,  4,  6,  1, 23, 44,  1, 11,
        
       17          20  21                                          32  33
       22, 33, 57,  1,  2,  3,  4,  2,  3, 94,  6,  1, 23, 44,  1,  1,  2,

               36  37
        3,  4,  1,  7,  8,  9,  2,  3,  4, 76,  1, 23, 44,  1])

Can you see the pattern?

We have the following values and strides for each dimension:

x| values: 13->45, 11->22, 1->2, ... stride: 1
y| values: 13->1, 45->2, 11->2, ... stride: 4
z| values: 13->11, 45->22, 11->1, ... stride: 16
np.lib.stride_tricks.as_strided(res, shape=(3, 2, 2), strides=(8 * 16, 8 * 4, 8 * 1))

output:

array([[[13, 45],
        [ 1,  2]],

       [[11, 22],
        [ 1,  2]],

       [[ 1,  2],
        [ 1,  7]]])

This is just one window and we want 4 of them, the jump between each window is 8*2 bytes in x direction and 8*8 bytes in y direction.

windows = np.lib.stride_tricks.as_strided(res, shape=(2, 2, 3, 2, 2), strides=(8 * 8, 8 * 2, 8 * 16, 8 * 4, 8 * 1))
windows.max(axis=(2, 3, 4))

output:

array([[45, 67],
       [23, 94]])

Assuming that the stride is the same as the kernel size (as in conventional 2D max pooling).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文