我们可以扩展
this solution你的
3D情况下,通过利用
np.lib.stride_tricks.as_strided基于
sliding-windowed views高效的补丁提取,像这样-
from skimage.util.shape import view_as_windowsdef get_patches(data, locations, size): # Get 2D sliding windows for each element off data w = view_as_windows(data, (1,1,size,size)) # Use fancy/advanced indexing to select the required ones return w[np.arange(len(locations)), :, locations[:,0], locations[:,1]][:,:,0,0]
我们需要将它们
1,1作为to的窗口参数
view_as_windows,因为它希望窗口具有与输入数据的暗淡数量相同的元素数量。我们沿的最后两个轴滑动
data,因此使前两个保持不变
1s,基本上不沿的前两个轴滑动
data。
样本运行于一个通道,而不只是通道数据-
In [78]: n, c, h, w = 3, 1, 4, 4 # number of channels = 1 ...: data = np.arange(n * c * h * w).reshape(n, c, h, w) ...: ...: size = 2 ...: locations = np.array([ ...: [0, 1], ...: [1, 1], ...: [0, 2] ...: ]) ...: ...: crops = np.stack([d[:, y:y+size, x:x+size] ...: for d, (y,x) in zip(data, locations)])In [79]: print np.allclose(get_patches(data, locations, size), crops)TrueIn [80]: n, c, h, w = 3, 5, 4, 4 # number of channels = 5 ...: data = np.arange(n * c * h * w).reshape(n, c, h, w) ...: ...: size = 2 ...: locations = np.array([ ...: [0, 1], ...: [1, 1], ...: [0, 2] ...: ]) ...: ...: crops = np.stack([d[:, y:y+size, x:x+size] ...: for d, (y,x) in zip(data, locations)])In [81]: print np.allclose(get_patches(data, locations, size), crops)True
标杆管理
其他方法-
# Original solndef stack(data, locations, size): crops = np.stack([d[:, y:y+size, x:x+size] for d, (y,x) in zip(data, locations)]) return crops# scholi's solndef allocate_assign(data, locations, size): n, c, h, w = data.shape crops = np.zeros((n,c,size,size)) for i, (y,x) in enumerate(locations): crops[i,0,:,:] = data[i,0,y:y+size,x:x+size] return crops
从评论来看,OP似乎对具有shape数据
(512,1,60,60)和
sizeas的情况感兴趣
12,24,48。因此,让我们使用一个函数为它们设置数据-
# Setup datadef create_inputs(size): np.random.seed(0) n, c, h, w = 512, 1, 60, 60 data = np.arange(n * c * h * w).reshape(n, c, h, w) locations = np.random.randint(0,3,(n,2)) return data, locations, size
时间-
In [186]: data, locations, size = create_inputs(size=12)In [187]: %timeit stack(data, locations, size) ...: %timeit allocate_assign(data, locations, size) ...: %timeit get_patches(data, locations, size)1000 loops, best of 3: 1.26 ms per loop1000 loops, best of 3: 1.06 ms per loop10000 loops, best of 3: 124 µs per loopIn [188]: data, locations, size = create_inputs(size=24)In [189]: %timeit stack(data, locations, size) ...: %timeit allocate_assign(data, locations, size) ...: %timeit get_patches(data, locations, size)1000 loops, best of 3: 1.66 ms per loop1000 loops, best of 3: 1.55 ms per loop1000 loops, best of 3: 470 µs per loopIn [190]: data, locations, size = create_inputs(size=48)In [191]: %timeit stack(data, locations, size) ...: %timeit allocate_assign(data, locations, size) ...: %timeit get_patches(data, locations, size)100 loops, best of 3: 2.8 ms per loop100 loops, best of 3: 3.33 ms per loop1000 loops, best of 3: 1.45 ms per loop



