有效地计算平方差的总和

时间:2015-11-14 15:19:44

标签: python performance numpy cython

这是一个循环,用于提取两个图像的RGB值,并计算所有三个通道的平方差的总和。 直接在我的main.py中运行此代码需要0.07秒。如果我在这个.pyx文件中运行它,速度会降低到1秒。我已经阅读了关于cdef函数的内容,但是我没有成功传递数组。任何有关将此函数转换为cdef函数的帮助将不胜感激。我真的需要这个循环尽可能快。

from cpython cimport array
import array
import numpy as np
cimport numpy as np

def fittnes(Orginal, Mutated):

    Fittnes = 0

    for x in range(0, 299):

        for y in range(0, 299):

            DeltaRed   = (Orginal[x][y][0] - Mutated[x][y][0])
            DeltaGreen = (Orginal[x][y][1] - Mutated[x][y][1])
            DeltaBlue  = (Orginal[x][y][2] - Mutated[x][y][2])

            Fittnes += (DeltaRed * DeltaRed + DeltaGreen * DeltaGreen + DeltaBlue * DeltaBlue)

    return Fittnes

我的Main.py函数调用

 NewScore = cythona.fittnes(numpy.array(Orginal), numpy.array(MutatedImage))

1 个答案:

答案 0 :(得分:1)

让我有兴趣知道加速数字,所以我发布这个作为解决方案。因此,正如评论中所述/讨论的那样,如果输入是NumPy数组,您可以使用本机NumPy工具,在这种情况下ndarray.sum(),就像这样 -

out = ((Orginal - Mutated)**2).sum()

你也可以使用效率很高的np.einsum来完成同样的任务,比如 -

sub = Orginal - Mutated
out = np.einsum('ijk,ijk->',sub,sub)

运行时测试

定义函数 -

def org_app(Orginal,Mutated):
    Fittnes = 0
    for x in range(0, Orginal.shape[0]):
        for y in range(0, Orginal.shape[1]):
            DR = (Orginal[x][y][0] - Mutated[x][y][0])
            DG = (Orginal[x][y][1] - Mutated[x][y][1])
            DB  = (Orginal[x][y][2] - Mutated[x][y][2])
            Fittnes += (DR * DR + DG * DG + DB * DB)
    return Fittnes

def einsum_based(Orginal,Mutated):
    sub = Orginal - Mutated
    return np.einsum('ijk,ijk->',sub,sub)

def dot_based(Orginal,Mutated): # @ali_m's suggestion
    sub = Orginal - Mutated
    return np.dot(sub.ravel(), sub.ravel())

def vdot_based(Orginal,Mutated):  # variant of @ali_m's suggestion
    sub = Orginal - Mutated
    return np.vdot(sub, sub)

计时 -

In [14]: M,N = 100,100
    ...: Orginal = np.random.rand(M,N,3)
    ...: Mutated = np.random.rand(M,N,3)
    ...: 

In [15]: %timeit org_app(Orginal,Mutated)
    ...: %timeit ((Orginal - Mutated)**2).sum()
    ...: %timeit einsum_based(Orginal,Mutated)
    ...: %timeit dot_based(Orginal,Mutated)
    ...: %timeit vdot_based(Orginal,Mutated)
    ...: 
10 loops, best of 3: 54.9 ms per loop
10000 loops, best of 3: 112 µs per loop
10000 loops, best of 3: 69.8 µs per loop
10000 loops, best of 3: 86.2 µs per loop
10000 loops, best of 3: 85.3 µs per loop

In [16]: # Inputs
    ...: M,N = 1000,1000
    ...: Orginal = np.random.rand(M,N,3)
    ...: Mutated = np.random.rand(M,N,3)
    ...: 

In [17]: %timeit org_app(Orginal,Mutated)
    ...: %timeit ((Orginal - Mutated)**2).sum()
    ...: %timeit einsum_based(Orginal,Mutated)
    ...: %timeit dot_based(Orginal,Mutated)
    ...: %timeit vdot_based(Orginal,Mutated)
    ...: 
1 loops, best of 3: 5.49 s per loop
10 loops, best of 3: 63 ms per loop
10 loops, best of 3: 23.9 ms per loop
10 loops, best of 3: 24.9 ms per loop
10 loops, best of 3: 24.9 ms per loop