我有一个迭代的numpy数组:
import numpy
import math
array = numpy.array([[1, 1, 2, 8, 2, 2],
[5, 5, 4, 1, 3, 2],
[5, 5, 4, 1, 3, 2],
[5, 5, 4, 1, 3, 2],
[9, 5, 8, 8, 2, 2],
[7, 3, 6, 6, 2, 2]])
Pixels = ['U','D','R','L','UL','DL','UR','DR']
for i in range (1,array.shape[0]-1):
for j in range (1,array.shape[1]-1):
list = []
while len(list) < 2:
iToMakeList = i
jToMakeList = j
if iToMakeList > array.shape[0]-1 or iToMakeList < 1 or jToMakeList> array.shape[0]-1 or jToMakeList < 1:
break
PixelCoord = {
'U' : (iToMakeList-1,jToMakeList),
'D' : (iToMakeList+1,jToMakeList),
'R' : (iToMakeList,jToMakeList+1),
'L' : (iToMakeList,jToMakeList-1),
'UL' : (iToMakeList-1,jToMakeList-1),
'DL' : (iToMakeList+1,jToMakeList-1),
'UR' : (iToMakeList-1,jToMakeList+1),
'DR' : (iToMakeList+1,jToMakeList+1)
}
Value = {
'U' : array[iToMakeList-1][jToMakeList],
'D' : array[iToMakeList+1][jToMakeList],
'R' : array[iToMakeList][jToMakeList+1],
'L' : array[iToMakeList][jToMakeList-1],
'UL' : array[iToMakeList-1][jToMakeList-1],
'DL' : array[iToMakeList+1][jToMakeList-1],
'UR' : array[iToMakeList-1][jToMakeList+1],
'DR' : array[iToMakeList+1][jToMakeList+1]
}
candidates = []
for pixel in Pixels:
candidates.append((Value[pixel],pixel))
Lightest = max(candidates)
list.append(PixelCoord[Lightest[1]])
iToMakeList = PixelCoord[Lightest[1]][0]
jToMakeList = PixelCoord[Lightest[1]][1]
我想加快这个过程。非常慢
假设此代码段的输出是我的最终目标,而我唯一想做的就是加速此代码。
这就是我的并行方法。首先,我创建一个查找表,其中每个像素都显示最近邻最大值的坐标。对于我的intel i7双核cpu上的100 * 100矩阵,该代码运行大约2秒钟。到目前为止,代码尚未优化,多重处理内部的数据处理有些奇怪,并且可以肯定变得更容易。请告诉我您想要什么。到目前为止,该代码仅将数据点的坐标添加到列表中,如果您需要这些值,请在适当的点进行更改或仅解析结果lines[]
列表。
import numpy
import multiprocessing as mp
import time
start=time.time()
#Getting the number of CPUs present
num_cpu=mp.cpu_count()
#Creation of random data for testing
data=numpy.random.randint(1,30,size=(200,200))
x,y=data.shape
#Padding is introduced to cope with the border of the dataset.
#Change if you want other behaviour like wrapping, reflection etc.
def pad(data):
'''Can be made faster, by using numpys pad function
if present'''
a=numpy.zeros((x+2,y+2))
a[1:-1,1:-1]=data
return a
data=pad(data)
#Kernel to get only the neighbours, change that if you only want diagonals or other shapes.
kernel=numpy.array([[1,1,1],[1,0,1],[1,1,1]])
result_list=[]
#Setting up functions for Parallel Processing
def log_result(result):
result_list.append(result)
def max_getter(pixel):
'''As this function is going to be used in a parallel processing environment,
the data has to exist globally in order not to have to pickle it in the subprocess'''
temp=data[pixel[0]-1:pixel[0]+2,pixel[1]-1:pixel[1]+2].copy()*kernel
#Getting the submatrix without the central pixel
compare=numpy.max(temp)==temp
coords=numpy.nonzero(compare)
if len(coords[0])!=1:
coords=(coords[0][0],coords[1][0])
#discards every maximum which is not the first. Change if you want.
#Converting back to global coordinates
return (pixel,(pixel[0]+(numpy.asscalar(coords[0])-1),pixel[1]+(numpy.asscalar(coords[1])-1)))
#This assumes, that the maximum is unique in the subset, if this is not the case adjust here
def parallell_max():
pool = mp.Pool()
#You can experiment using more cores if you have hyperthreading and it's not correctly found by cpu_count
for i in range(1,x+1):
for j in range(1,y+1):
pool.apply_async(max_getter, args = ((i,j),),callback=log_result)
pool.close()
pool.join()
#___________START Parallel Processing________
if __name__ == '__main__':
# directions={}
parallell_max()
directions={}
for i in result_list:
directions[i[0]]=i[1]
#Directions is a dictionary-like lookup-table, where every pixel gives the next pixel in the line
lines=[]
#The following code can also be easily parallelized as seen above.
for i in range(1,x+1):
for j in range(1,y+1):
line=[]
first,second=i,j
for k in range(100):
line.append((first,second))
first,second=directions[(first,second)]
lines.append(line)
stop=time.time()
print stop-start
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句