使用什么算法将图像阵列映射到多个内核进行处理?我一直在尝试想出一些方法来返回一个(不相交的)范围列表,以便在数组中进行迭代,到目前为止,我有以下内容。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
def divider(arr_dims, coreNum=1):
""" Get a bunch of iterable ranges;
Example input: [[[0, 24], [15, 25]]]
"""
if (coreNum == 1):
return arr_dims
elif (coreNum < 1):
raise ValueError(\
'partitioner expected a positive number of cores, got %d'\
% coreNum
)
elif (coreNum % 2):
raise ValueError(\
'partitioner expected an even number of cores, got %d'\
% coreNum
)
total = []
# Split each coordinate in arr_dims in _half_
for arr_dim in arr_dims:
dY = arr_dim[0][1] - arr_dim[0][0]
dX = arr_dim[1][1] - arr_dim[1][0]
if ((coreNum,)*2 > (dY, dX)):
coreNum = max(dY, dX)
coreNum -= 1 if (coreNum % 2 and coreNum > 1) else 0
new_c1, new_c2, = [], []
if (dY >= dX):
# Subimage height is greater than its width
half = dY // 2
new_c1.append([arr_dim[0][0], arr_dim[0][0] + half])
new_c1.append(arr_dim[1])
new_c2.append([arr_dim[0][0] + half, arr_dim[0][1]])
new_c2.append(arr_dim[1])
else:
# Subimage width is greater than its height
half = dX // 2
new_c1.append(arr_dim[0])
new_c1.append([arr_dim[1][0], half])
new_c2.append(arr_dim[0])
new_c2.append([arr_dim[1][0] + half, arr_dim[1][1]])
total.append(new_c1), total.append(new_c2)
# If the number of cores is 1, we get back the total; Else,
# we split each in total, etc.; it's turtles all the way down
return divider(total, coreNum // 2)
if __name__ == '__main__':
import numpy as np
X = np.random.randn(25 - 1, 36 - 1)
dims = [zip([0, 0], list(X.shape))]
dims = [list(j) for i in dims for j in dims[0] if type(j) != list]
print(divider([dims], 2))
然而,它的局限性非常大,因为它只接受 2 的幂的核心数量,而且我确信存在我忽略的边缘情况。运行它会返回 [[[0, 24], [0, 17]], [[0, 24], [17, 35]]]
,然后使用 pathos
我已将第一组映射到笔记本电脑中的一个核心,将第二组映射到另一个核心。
我想我只是不知道如何以几何方式将图像分割成大小尽可能相似的片段,以便给定机器上的每个核心都具有相同的性能要做的工作量。
最佳答案
我不太确定你想要实现什么,但是如果你想将一个数组(任何维度)分割成多个部分,你可以查看 numpy.array_split 方法 numpy.array_split .
它将数组划分为几乎相等数量的部分,因此即使分区数量无法完全划分数组,它也能正常工作。
关于arrays - 将数组尽可能均匀地划分为子数组以进行核心映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42020554/