我有一个i
频道图像,image
. 我也有f
过滤器,filters
可以应用于频道。我想通过有选择地将过滤器应用于图像的通道来生成o
通道图像。output
我目前用两个列表定义了这个,image_idx
并且filter_idx
,因此处理完成为
for j in xrange(o) :
output[j] = filter[filter_idx[j]](image[image_idx[j]])
因为图像可能非常大,我可能想就地进行此处理。这可能需要按特定顺序处理通道,以避免覆盖您稍后需要的数据。我目前正在检查是否存在这样的订单,并使用以下函数对其进行计算:
def in_place_sequence(indices) :
"""
Figure out a processing sequence for in-place computation.
"""
indices = [j for j in indices]
positions = set(range(len(indices)))
processing_order = []
change = True
while change and len(positions) :
change = False
for j in list(positions) :
val = indices[j]
if (j not in indices) or (indices.count(val) == 1 and val == j) :
indices[j] = None
positions.remove(j)
processing_order.append(j)
change = True
if len(positions) :
return None
return processing_order
例如:
In [21]: in_place_sequence([4, 0, 3, 0, 4])
Out[21]: [1, 2, 3, 0, 4]
避免覆盖的可能处理顺序是:
img[0] -> img[1]
img[3] -> img[2]
img[0] -> img[3]
img[4] -> img[0]
img[4] -> img[4]
这是实现的:
for j in in_place_sequence(image_idx) :
image[j] = filter[filter_idx[j]](image[image_idx[j]])
我开始暗示,当它找不到序列时,是因为image_idx
定义了一个闭环排列。例如:
In [29]: in_place_sequence([2, 0, 3, 1])
返回None
,但它仍然可以在 1 个通道的最小存储量的情况下就地完成:
img[0] -> temp
img[2] -> img[0]
img[3] -> img[2]
img[1] -> img[3]
temp -> img[1]
我在想办法自动实现这一点时遇到了麻烦。我认为你要走的路是保留我当前的算法,如果它无法耗尽positions
,找出闭环并为每个闭环做类似上面的事情。不过,我的印象是,我可能在这里重新发明轮子。所以在开始编码之前,我想我会问:确定处理顺序以最小化中间存储的最佳方法是什么?
编辑在 Sam Mussmann 的鼓励下,我已经开始计算剩余的周期。我的代码现在看起来像这样:
def in_place_sequence(indices) :
"""
Figures out a processing sequence for in-place computation.
Parameters
----------
indices : array-like
The positions that the inputs will take in the output after
processing.
Returns
-------
processing_order : list
The order in which output should be computed to avoid overwriting
data needed for a later computation.
cycles : list of lists
A list of cycles present in `indices`, that will require a one
element intermediate storage to compute in place.
Notes
-----
If not doing the opearation in-place, if `in_` is a sequence of elements
to process with a function `f`, then `indices` would be used as follows to
create the output `out`:
>>> out = []
>>> for idx in indices :
... out.append(f(in_[idx]))
so that `out[j] = f(in_[indices[j]])`.
If the operation is to be done in-place, `in_place_sequence` could be used
as follows:
>>> sequence, cycles = in_place_sequence(indices)
>>> for j, idx in enumerate(sequence) :
... in_[j] = f(in_[idx])
>>> for cycle in cycles :
... temp = in_[cycle[0]]
... for to, from_ in zip(cycle, cycle[1:]) :
... in_[to] = f(in_[from_])
... in_[cycle[-1]] = f(temp)
"""
indices = [j for j in indices]
print indices
positions = set(range(len(indices)))
processing_order = []
change = True
while change and positions :
change = False
for j in list(positions) :
val = indices[j]
if (j not in indices) or (indices.count(val) == 1 and val == j) :
indices[j] = None
positions.remove(j)
processing_order.append(j)
change = True
cycles = []
while positions :
idx = positions.pop()
start = indices.index(idx)
cycle = [start]
while idx != start :
cycle.append(idx)
idx = indices[idx]
positions.remove(idx)
cycles.append(cycle)
return processing_order, cycles