我正在尝试通过使用starmap的2d数组参数进行多重处理来创建进程池。但是,参数似乎是逐行输入,而不是逐个元素输入。
我想使用每个元素来创建一个3d输出数组,该数组具有与2d输入数组中每个元素相对应的数组
我创建了一个简化的代码来说明我的意思:
import multiprocessing
import numpy as np
MeshNumberY = 5
MeshNumberX = 10
result_list = np.zeros( (MeshNumberX,MeshNumberY,3) )
Xindices = np.tile(np.arange(MeshNumberX),(MeshNumberY,1))
Yindices = np.tile(np.reshape(np.arange(MeshNumberY),(MeshNumberY,1)),(1,MeshNumberX))
def image_pixel_array(x,y):
return np.array([5*x,5*y,255])
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
result_list = np.array(pool.starmap(image_pixel_array, zip(Xindices, Yindices)))
print(result_list)
输入数组Xindices和Yindices是
[[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]]
和
[[0 0 0 0 0 0 0 0 0 0]
[1 1 1 1 1 1 1 1 1 1]
[2 2 2 2 2 2 2 2 2 2]
[3 3 3 3 3 3 3 3 3 3]
[4 4 4 4 4 4 4 4 4 4]]
分别具有对应的输出
[[array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]) 255]
[array([5, 5, 5, 5, 5, 5, 5, 5, 5, 5])
array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]) 255]
[array([10, 10, 10, 10, 10, 10, 10, 10, 10, 10])
array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]) 255]
[array([15, 15, 15, 15, 15, 15, 15, 15, 15, 15])
array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]) 255]
[array([20, 20, 20, 20, 20, 20, 20, 20, 20, 20])
array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]) 255]]
我的目标是获得更类似的输出,
[[[0 0 255] [5 0 255] [10 0 255] [15 0 255] [20 0 255] [25 0 255] [30 0 255] [35 0 255] [40 0 255] [45 0 255]]
[[[0 5 255] [5 5 255] [10 5 255] [15 5 255] [20 5 255] [25 5 255] [30 5 255] [35 5 255] [40 5 255] [45 5 255]]
etc.
如果有人建议优化我的阵列设置方式,那当然也将受到欢迎,因为我对此并不陌生。
这些都是用Python 3.7编写的。
预先感谢您的帮助!
答案 0 :(得分:0)
我尝试过
import multiprocessing
import numpy as np
MeshNumberY = 5
MeshNumberX = 10
result_list = np.zeros( (MeshNumberX,MeshNumberY,3) )
Xindices = np.tile(np.arange(MeshNumberX),(MeshNumberY,1))
Yindices = np.tile(np.reshape(np.arange(MeshNumberY),(MeshNumberY,1)),(1,MeshNumberX))
Zindices = Yindices.copy()
def image_pixel_array(x,y,z):
return np.transpose([5*x,5*y,z*0+255])
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
result_list = np.array(pool.starmap(image_pixel_array, zip(Xindices, Yindices,Zindices)))
print(np.reshape(result_list,(MeshNumberY,MeshNumberX,3),order='F'))