- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我发现可以从 (X, Y)
步幅为 1,图像要求我们将步幅参数指定为 img.strides * 2
或 img.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/
我发现可以从 (X, Y) 步幅为 1,图像要求我们将步幅参数指定为 img.strides * 2 或 img.strides + img.strides。 我不知道他们如何在知道编号的情况下快速计
我有一个 4 张量 x。 6 张量 y 计算如下: x = np.random.randn(64, 28, 28, 1) strided_shape = 64, 26, 26, 3, 3, 1 y =
请问numpy.lib.stride_tricks.as_strided的结果取决于 NumPy 数组的数据类型? 这个问题源于.strides的定义,也就是 Tuple of bytes to st
我有一个方形数组x,形状为(N, N),我想检索形状为(n, n)的方形子数组),它们以 x 的主对角线为中心。例如,使用 N = 3 & n = 2,并操作 x = np.arange(9).res
在处理滚动窗口时,我用列表推导式的方式编写函数 [np.std(x[i:i+framesize]) for i in range(0, len(x)-framesize, hopsize)])] 最近
我想知道是否有可能在不使用 for 循环的情况下使用 as_strided 和一些就地编辑内存的操作来编写迭代算法。 例如,如果我想编写一个算法,将数组中的数字替换为其邻居的总和。我想到了这个令人厌恶
我正在寻找一种将 numpy 数组分割成重叠 block 的有效方法。我知道 numpy.lib.stride_tricks.as_strided 可能是要走的路,但我似乎无法理解它在适用于任意形状数
今天早上花了一段时间寻找一个概括性的问题,以指出有关 as_strided 和/或 how to make generalized window functions 的问题的重复项。关于如何(安全地)
我正在尝试使用 numpy.strided_tricks reshape 一个 numpy 数组。这是我正在关注的指南:https://stackoverflow.com/a/2487551/4909
考虑数组a import numpy as np import pandas as pd np.random.seed([3,1415]) a = np.random.randint(100, siz
我将 numpy 从旧版本更新到了 1.12.0 版本,不幸的是我没有检查哪个版本。我有一个在更新之前运行得非常好的脚本。它使用以下代码行: from numpy.lib.stride_tricks
我是一名优秀的程序员,十分优秀!