how to parallelize this time consuming loops

90 views Asked by At

am Traying to find the distance matrix between image blocks each item of a matrix represent the distance between image block and other blocks my code is doing ok but it consumes a lot of time I need to parallelize the loops enter image description here

def calchellinger(glcm):
    tmp = np.zeros((glcm.shape[0],glcm.shape[1]))
    for i in range(glcm.shape[0]):
        for j in range(glcm.shape[1]):
            sum = 0 
            for x in range(glcm.shape[0]):                
                for y in range(glcm.shape[1]):
                    sum  = sum + hellinger(np.array(glcm[i,j]).flatten(), np.array(glcm[x,y]).flatten())                    
                tmp[i,j] = sum
    return tmp
def hellinger(p,q):
   return sum([(np.sqrt(t[0])-np.sqrt(t[1]))*(np.sqrt(t[0])-np.sqrt(t[1]))\
                for t in zip(p,q)])/np.sqrt(2.)
1

There are 1 answers

2
Wilson.L On BEST ANSWER

You can try Embarrassingly parallel for loops in joblib (https://joblib.readthedocs.io/en/latest/parallel.html). It is a easy way to brakes loops and run in parallel