as_strided:将步长(conv2d 的步幅)与 as_strided strides 参数链接起来
as_strided: Linking stepsize (strides of conv2d) with as_strided strides parameter
我发现为了从 (X,Y)
生成 (X - x + 1, Y - y + 1)
大小 (x,y)
的补丁,步幅为 1,图像要求我们将步幅参数设置为 img.strides * 2
或 img.strides + img.strides
. 我不知道他们是如何在知道否的情况下快速计算出来的。 conv2d
的进步
但是我应该怎么做才能从具有 stride
步幅的相同大小的图像中获得 ((X-x)/stride)+1, ((Y-y)/stride)+1
个相同大小的补丁?
来自这个 SO 稍作修改,将频道和图像数量放在前面
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)
我可以看到它创建了一个滑动 window,大小为 (x,y),步幅为 1(向右移动 1 个像素,向下移动 1 个像素)。我无法将 as_strided
使用的步幅参数与我们通常用于 conv2d 的步幅相关联。
如何向上述计算 as_strided
strides 参数的函数添加参数?
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
=no.of 图片,
b
=no.of 个频道,
(X,Y)
= 宽度和高度
注意:stride in conv2d
我的意思是stepsize
不幸的是,这也被称为步幅。
注2:由于stepsize
通常在两个轴上是相同的,在我提供的代码中,我只提供了一个参数,但是使用了它对于两个维度。
游乐场:
strides
here. I have it running for stepsize=1
here. I noticed that it might not work from the link but it works when pasted in new 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]]]])
我发现为了从 (X,Y)
生成 (X - x + 1, Y - y + 1)
大小 (x,y)
的补丁,步幅为 1,图像要求我们将步幅参数设置为 img.strides * 2
或 img.strides + img.strides
. 我不知道他们是如何在知道否的情况下快速计算出来的。 conv2d
但是我应该怎么做才能从具有 stride
步幅的相同大小的图像中获得 ((X-x)/stride)+1, ((Y-y)/stride)+1
个相同大小的补丁?
来自这个 SO
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)
我可以看到它创建了一个滑动 window,大小为 (x,y),步幅为 1(向右移动 1 个像素,向下移动 1 个像素)。我无法将 as_strided
使用的步幅参数与我们通常用于 conv2d 的步幅相关联。
如何向上述计算 as_strided
strides 参数的函数添加参数?
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
=no.of 图片,b
=no.of 个频道,(X,Y)
= 宽度和高度
注意:stride in conv2d
我的意思是stepsize
不幸的是,这也被称为步幅。
注2:由于stepsize
通常在两个轴上是相同的,在我提供的代码中,我只提供了一个参数,但是使用了它对于两个维度。
游乐场:
strides
here. I have it running for stepsize=1
here. I noticed that it might not work from the link but it works when pasted in new 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]]]])