python numpy代码的矢量化使得它更慢而不是更快

时间:2016-06-10 00:46:01

标签: python arrays numpy vectorization cross-correlation

我正在尝试执行图像相关以找到一组20帧中的哪一帧(该组存储在3D数组中,x)与给定帧(存储为2D数组,y)最佳匹配。此步骤必须执行1000次。

我尝试对代码进行矢量化以使其运行得更快。但不知何故,矢量化使代码需要两倍的时间。我可能在向量化过程中做错了,这使得它变慢。

这是代码

import numpy as np
import time


def corr2(a,b):


    #Getting shapes and prealocating the auxillairy variables
    k = np.shape(a)

    #Calculating mean values
    AM=np.mean(a)
    BM=np.mean(b)  

    #calculate vectors
    c_vect = (a-AM)*(b-BM)
    d_vect = (a-AM)**2
    e_vect = (b-BM)**2

    #Formula itself
    r_out = np.sum(c_vect)/float(np.sqrt(np.sum(d_vect)*np.sum(e_vect)))

    return r_out


def ZZ_1X_v1_MCC(R,RefImage):

    from img_proccessing import corr2

    Cor = np.zeros(R.shape[2])
    for t in range(R.shape[2]):
        Cor[t]=corr2(RefImage,R[:,:,t]) #Correlation

    #report
    max_correlationvalue_intermediate = np.amax(Cor)
    max_correlatedframe_intermediate = np.argmax(Cor)

    max_correlatedframeandvalue = [max_correlatedframe_intermediate,max_correlationvalue_intermediate];

    return max_correlatedframeandvalue

def ZZ_1X_v1_MCC_vectorized(R,RefImage):


    R_shape = np.asarray(np.shape(R))
    R_flattened = R.swapaxes(0,2).reshape(R_shape[2],R_shape[0]*R_shape[1])
    AA = np.transpose(R_flattened)
    RefImageflattened = RefImage.transpose().ravel()

    #Calculating mean subtracted values
    AAM = AA - np.mean(AA,axis=0)
    BM = RefImageflattened - np.mean(RefImageflattened)

    #calculate vectors
    DD_vect = AAM**2
    E_vect = BM**2
    EE_vect = np.transpose(np.tile(np.transpose(E_vect),(R_shape[2],1)))
    CC_vect = AAM*np.transpose(np.tile(BM,(R_shape[2],1)))

    #Formula itself
    Cor = np.sum(CC_vect,axis=0)/np.sqrt((np.sum(DD_vect,axis=0)*np.sum(EE_vect,axis=0)).astype(float))

    #report
    max_correlationvalue_intermediate = np.amax(Cor)
    max_correlatedframe_intermediate = np.argmax(Cor)

    max_correlatedframeandvalue = [max_correlatedframe_intermediate,max_correlationvalue_intermediate];


    return max_correlatedframeandvalue   


x = np.arange(400000).reshape((20,200,100)).swapaxes(0,2) #3D array with 20 frames
y = np.transpose(np.arange(20000).reshape((200,100))) #2D array with 1 frame



# using for loop
tic = time.time()
for i in range(500):
    [a,b] = ZZ_1X_v1_MCC(x,y)

print(time.time() - tic)


# using vectorization
tic = time.time()
for i in range(500):
    [a,b] = ZZ_1X_v1_MCC_vectorized(x,y)
print(time.time() - tic)

0 个答案:

没有答案