gpt4 book ai didi

python - as_strided : Linking stepsize (strides of conv2d) with as_strided strides parameter

转载 作者:行者123 更新时间:2023-12-01 02:25:25 24 4
gpt4 key购买 nike

我发现可以从 (X, Y) 步幅为 1,图像要求我们将步幅参数指定为 img.strides * 2img.strides + img.strides我不知道他们如何在知道编号的情况下快速计算出这个值。 conv2d 中的步幅

但是我应该怎么做才能使用 stride< 从相同大小的图像中获取 ((X-x)/stride)+1, ((Y-y)/stride)+1 相同大小的补丁 跨步?

<小时/>

从此 SO answer 稍加修改, channel 和图像数量放在前面

def patchify(img, patch_shape):
a,b,X, Y = img.shape # a images and b channels
x, y = patch_shape
shape = (a, b, X - x + 1, Y - y + 1, x, y)
a_str, b_str, X_str, Y_str = img.strides
strides = (a_str, b_str, X_str, Y_str, X_str, Y_str)
return np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides)

我可以看到它创建了一个大小为 (x,y) 且步长为 1 的滑动窗口(向右移动 1 像素并向下移动 1 像素)。我无法将 as_strided 使用的步幅参数与我们通常用于 conv2d 的步幅关联起来。

如何向上述计算 as_strided 步幅参数的函数添加参数?

def patchify(img, patch_shape, stride):    # stride=stepsize in conv2d eg: 1,2,3,...
a,b,X,Y = img.shape # a images and b channels
x, y = patch_shape
shape = (a,b,((X-x)/stride)+1, ((Y-y)/stride)+1, x, y)
strides = ??? # strides for as_strided
return np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides)

img 是 4d (a, b, X, Y)

  • a=图像数量,
  • b= channel 数,
  • (X,Y) = 宽度和高度

注意:在 conv2d 中跨步 我的意思是步长 不幸的是,这也称为跨步

注释 2:由于 stepsize 通常在两个轴上都相同,因此在我提供的代码中,我只提供了一个参数,但将其用于两个轴尺寸。

Playground :strides here 的内容是什么。我让它运行 stepsize=1 here 。我注意到它可能无法从链接中工作,但在粘贴到新的 playground 中时它可以工作。

这应该让我清楚地了解我需要什么:

[[ 0.5488135   0.71518937  0.60276338  0.54488318]
[ 0.4236548 0.64589411 0.43758721 0.891773 ]
[ 0.96366276 0.38344152 0.79172504 0.52889492]
[ 0.56804456 0.92559664 0.07103606 0.0871293 ]]

# patch_size = 2x2
# stride = 1,1

[[[[ 0.5488135 0.71518937]
[ 0.4236548 0.64589411]]

[[ 0.71518937 0.60276338]
[ 0.64589411 0.43758721]]

[[ 0.60276338 0.54488318]
[ 0.43758721 0.891773 ]]]


[[[ 0.4236548 0.64589411]
[ 0.96366276 0.38344152]]

[[ 0.64589411 0.43758721]
[ 0.38344152 0.79172504]]

[[ 0.43758721 0.891773 ]
[ 0.79172504 0.52889492]]]


[[[ 0.96366276 0.38344152]
[ 0.56804456 0.92559664]]

[[ 0.38344152 0.79172504]
[ 0.92559664 0.07103606]]

[[ 0.79172504 0.52889492]
[ 0.07103606 0.0871293 ]]]]

# stride = 2,2

[[[[[[ 0.5488135 0.71518937]
[ 0.4236548 0.64589411]]

[[ 0.60276338 0.54488318]
[ 0.43758721 0.891773 ]]]


[[[ 0.96366276 0.38344152]
[ 0.56804456 0.92559664]]

[[ 0.79172504 0.52889492]
[ 0.07103606 0.0871293 ]]]]]]

# stride = 2,1

[[[[ 0.5488135 0.71518937]
[ 0.4236548 0.64589411]]

[[ 0.71518937 0.60276338]
[ 0.64589411 0.43758721]]

[[ 0.60276338 0.54488318]
[ 0.43758721 0.891773 ]]]

[[[ 0.96366276 0.38344152]
[ 0.56804456 0.92559664]]

[[ 0.38344152 0.79172504]
[ 0.92559664 0.07103606]]

[[ 0.79172504 0.52889492]
[ 0.07103606 0.0871293 ]]]]

最佳答案

这是一种方法 -

def patchify(img, patch_shape, stepsize_x=1, stepsize_y=1): 
strided = np.lib.stride_tricks.as_strided
x, y = patch_shape
p,q = img.shape[-2:]
sp,sq = img.strides[-2:]

out_shp = img.shape[:-2] + (p-x+1,q-y+1,x,y)
out_stride = img.strides[:-2] + (sp,sq,sp,sq)

imgs = strided(img, shape=out_shp, strides=out_stride)
return imgs[...,::stepsize_x,::stepsize_y,:,:]

示例运行 -

1]输入:

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

In [157]: img = np.random.randint(11,99,(2,4,4))

In [158]: img
Out[158]:
array([[[55, 58, 75, 78],
[78, 20, 94, 32],
[47, 98, 81, 23],
[69, 76, 50, 98]],

[[57, 92, 48, 36],
[88, 83, 20, 31],
[91, 80, 90, 58],
[75, 93, 60, 40]]])

2] 输出 - 案例#1:

In [159]: patchify(img, (2,2), stepsize_x=1, stepsize_y=1)[0]
Out[159]:
array([[[[55, 58],
[78, 20]],

[[58, 75],
[20, 94]],

[[75, 78],
[94, 32]]],


[[[78, 20],
[47, 98]],

[[20, 94],
[98, 81]],

[[94, 32],
[81, 23]]],


[[[47, 98],
[69, 76]],

[[98, 81],
[76, 50]],

[[81, 23],
[50, 98]]]])

3] 输出 - 案例#2:

In [160]: patchify(img, (2,2), stepsize_x=2, stepsize_y=1)[0]
Out[160]:
array([[[[55, 58],
[78, 20]],

[[58, 75],
[20, 94]],

[[75, 78],
[94, 32]]],


[[[47, 98],
[69, 76]],

[[98, 81],
[76, 50]],

[[81, 23],
[50, 98]]]])

4] 输出 - 案例#3:

In [161]: patchify(img, (2,2), stepsize_x=2, stepsize_y=2)[0]
Out[161]:
array([[[[55, 58],
[78, 20]],

[[75, 78],
[94, 32]]],


[[[47, 98],
[69, 76]],

[[81, 23],
[50, 98]]]])

关于python - as_strided : Linking stepsize (strides of conv2d) with as_strided strides parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47469947/

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