【发布时间】:2022-01-09 21:50:03
【问题描述】:
我正在做一些光流分析。目标是遍历长电影中的每一帧,计算密集的光流,并将得到的角度和幅度附加到不断增长的 numpy 数组中。我发现完成每个连续循环所需的时间越来越长,我不知道为什么。这是一个概括问题的简单示例循环:
import numpy as np
arraySize = (1, 256, 256) # correct array size
emptyArray = np.zeros(arraySize) # empty array to fill with angles from every image pair
timeElapsed = [] # empty list to fill with time values
for i in range(100): # iterates through the frames in the image stack
start = time.time() # start the time
newArray = np.zeros(arraySize) # makes an example new array
emptyArray = np.concatenate((emptyArray, newArray)) # concats new and growing arrays
end = time.time() # stop the time
timeElapsed.append(end-start) # append the total time for the loop to the growing list
如果我随后绘制每个循环所用的时间,我会在每次通过循环时得到线性增加。在这个例子中,它仍然可以容忍,但对于我的实际数据集,它不是。
我猜测较大的数组需要更多时间来处理,但我不知道该怎么做才能避免这种情况。有没有更好、更快或更 Pythonic 的方法来做到这一点?
------------- 编辑 -------------
根据 mathfux 的建议:我将循环修改如下:
arraySize = (1, 256, 256) # correct array size
emptyArray = np.concatenate([np.zeros(arraySize) for i in range(100)]) # empty array to fill with angles from every image pair
timeElapsed = [] # empty list to fill with time values
for i in range(100): # iterates through the frames in the image stack
start = time.time() # start the time
newArray = np.zeros(arraySize) # makes an example new array
emptyArray[i] = newArray[0] # overwrites empty array with newarray values at the relevant position
end = time.time() # stop the time
timeElapsed.append(end-start) # append the total time for the loop to the growing list
现在迭代之间的时间/循环非常一致:
谢谢!
【问题讨论】:
-
如果您的数据只包含零,
np.zeros((100, 256, 256), dtype=int)应该足够了。最有效的方法是将数据展平,然后对其进行重塑。 -
指定 dtype 会提高内存效率吗?如果我理解正确,我不能指定“int”,因为我将用浮点数替换这些值。
-
IMO 最好使用此更新修改 mathfux 的答案,而不是将其添加到问题中。
-
我不知道我可以用这个更新修改 mathfux 的答案,当我点击“编辑”时,它说建议的编辑队列已满。
标签: python numpy concatenation numpy-ndarray