【问题标题】:Using numpy.take for faster fancy indexing使用 numpy.take 进行更快的花式索引
【发布时间】:2013-01-07 15:02:36
【问题描述】:

编辑我把我面临的更复杂的问题保留在下面,但是我在np.take 的问题可以更好地总结如下。假设您有一个形状为(planes, rows) 的数组img 和另一个形状为(planes, 256) 的数组lut,并且您想使用它们创建一个形状为(planes, rows) 的新数组out,其中out[p,j] = lut[p, img[p, j]] .这可以通过花哨的索引来实现,如下所示:

In [4]: %timeit lut[np.arange(planes).reshape(-1, 1), img]
1000 loops, best of 3: 471 us per loop

但是,如果您不使用花哨的索引,而是使用 take 并在 planes 上使用 python 循环,则可以大大加快速度:

In [6]: %timeit for _ in (lut[j].take(img[j]) for j in xrange(planes)) : pass
10000 loops, best of 3: 59 us per loop

lutimg 可以以某种方式重新排列,以便在没有 python 循环的情况下进行整个操作,而是使用numpy.take(或替代方法)而不是传统的花式索引来保持速度优势?


原始问题 我有一组要在图像上使用的查找表 (LUT)。保存 LUT 的数组的形状为 (planes, 256, n),图像的形状为 (planes, rows, cols)。两者都属于dtype = 'uint8',与LUT 的256 轴相匹配。这个想法是通过LUT的p-th平面中的每个n LUT运行图像的p-th平面。

如果我的lutimg 如下:

planes, rows, cols, n = 3, 4000, 4000, 4
lut = np.random.randint(-2**31, 2**31 - 1,
                        size=(planes * 256 * n // 4,)).view('uint8')
lut = lut.reshape(planes, 256, n)
img = np.random.randint(-2**31, 2**31 - 1,
                    size=(planes * rows * cols // 4,)).view('uint8')
img = img.reshape(planes, rows, cols)

使用像这样的精美索引后,我可以实现自己的目标

out = lut[np.arange(planes).reshape(-1, 1, 1), img]

这给了我一个形状数组 (planes, rows, cols, n) ,其中out[i, :, :, j] 持有imgi-th 平面,穿过j-th LUT 的i-th LUT 平面...

一切都很好,除了这个:

In [2]: %timeit lut[np.arange(planes).reshape(-1, 1, 1), img]
1 loops, best of 3: 5.65 s per loop

这是完全不可接受的,特别是因为我使用np.take 有以下所有看起来不太好看的替代品,而不是运行得更快:

  1. 单个平面上的单个 LUT 运行速度大约快 70 倍:

    In [2]: %timeit np.take(lut[0, :, 0], img[0])
    10 loops, best of 3: 78.5 ms per loop
    
  2. 运行所有所需组合的 python 循环完成速度几乎快 x6:

    In [2]: %timeit for _ in (np.take(lut[j, :, k], img[j]) for j in xrange(planes) for k in xrange(n)) : pass
    1 loops, best of 3: 947 ms per loop
    
  3. 即使在 LUT 和图像中运行所有平面组合,然后丢弃 planes**2 - planes 不需要的平面组合,也比花哨的索引更快:

    In [2]: %timeit np.take(lut, img, axis=1)[np.arange(planes), np.arange(planes)]
    1 loops, best of 3: 3.79 s per loop
    
  4. 我能想到的最快的组合是一个 python 循环在平面上迭代并更快地完成 x13:

    In [2]: %timeit for _ in (np.take(lut[j], img[j], axis=0) for j in xrange(planes)) : pass
    1 loops, best of 3: 434 ms per loop
    

当然,问题是如果没有任何 python 循环就没有办法用np.take 做到这一点?理想情况下,需要的任何重塑或调整大小都应该发生在 LUT 上,而不是图像上,但我愿意接受你们能想到的任何事情......

【问题讨论】:

  • 你的 sn-p 中的 bkpt 是什么 - 无需解释,我只是想提醒你,以防万一是错字 - 我想应该是 lut
  • ...整行不应该是lut = lut.reshape(planes, 256, 4),所以最后一个暗处是4
  • @TheodrosZelleke 感谢您抓到这些!我的lut 实际上是一个断点表,所以在我的代码中它被称为bkpt,而我在为问题翻译它时错过了它。
  • 真的,不知道。看起来一切都很丑陋。一件事,np.take 目前只有在两个输入都是 c 连续的(否则它会复制它们)时才快。您可能可以手动将二维数组转换为一维数组,但如果 img 真的很大,它可能并不重要,如果它值得玩转......
  • 嗨,你应该给出一个完整的例子,如果太长,那么只需在 github 上创建一个 gist。否则人们很难重现您的问题并尝试提供帮助。

标签: python numpy


【解决方案1】:

首先我不得不说我真的很喜欢你的问题。在不重新排列 LUTIMG 的情况下,以下解决方案有效:

%timeit a=np.take(lut, img, axis=1)
# 1 loops, best of 3: 1.93s per loop

但是从结果中你必须查询对角线:a[0,0], a[1,1], a[2,2];得到你想要的。我试图找到一种仅对对角元素进行索引的方法,但仍然没有成功。

以下是重新排列LUTIMG 的一些方法: 如果IMG 中的索引为 0-255,则适用于第 1 个平面,256-511 用于第 2 个平面,512-767 用于第 3 个平面,但这会阻止您使用 'uint8',可能是个大问题...:

lut2 = lut.reshape(-1,4)
%timeit np.take(lut2,img,axis=0)
# 1 loops, best of 3: 716 ms per loop
# or
%timeit np.take(lut2, img.flatten(), axis=0).reshape(3,4000,4000,4)
# 1 loops, best of 3: 709 ms per loop

在我的机器中,您的解决方案仍然是最佳选择,而且非常合适,因为您只需要对角线评估,即 plane1-plane1、plane2-plane2 和 plane3-plane3:

%timeit for _ in (np.take(lut[j], img[j], axis=0) for j in xrange(planes)) : pass
# 1 loops, best of 3: 677 ms per loop

我希望这可以让您对更好的解决方案有所了解。最好使用flatten() 寻找更多选项,以及与np.apply_over_axes()np.apply_along_axis() 类似的方法,这似乎很有希望。

我使用下面的代码来生成数据:

import numpy as np
num = 4000
planes, rows, cols, n = 3, num, num, 4
lut = np.random.randint(-2**31, 2**31-1,size=(planes*256*n//4,)).view('uint8')
lut = lut.reshape(planes, 256, n)
img = np.random.randint(-2**31, 2**31-1,size=(planes*rows*cols//4,)).view('uint8')
img = img.reshape(planes, rows, cols)

【讨论】:

    猜你喜欢
    • 2017-08-01
    • 2013-01-01
    • 2011-11-04
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多