提高 Numpy 性能 [英] Improving Numpy Performance

查看:28
本文介绍了提高 Numpy 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 python 提高卷积的性能,并希望对如何最好地提高性能有一些见解.

I'd like to improve the performance of convolution using python, and was hoping for some insight on how to best go about improving performance.

我目前正在使用 scipy 来执行卷积,使用的代码有点像下面的代码片段:

I am currently using scipy to perform the convolution, using code somewhat like the snippet below:

import numpy
import scipy
import scipy.signal
import timeit

a=numpy.array ( [ range(1000000) ] )
a.reshape(1000,1000)
filt=numpy.array( [ [ 1, 1, 1 ], [1, -8, 1], [1,1,1] ] )

def convolve():
  global a, filt
  scipy.signal.convolve2d ( a, filt, mode="same" )

t=timeit.Timer("convolve()", "from __main__ import convolve")
print "%.2f sec/pass" % (10 * t.timeit(number=10)/100)

我正在处理图像数据,使用灰度(0 到 255 之间的整数值),目前我每个卷积得到大约四分之一秒.我的想法是执行以下操作之一:

I am processing image data, using grayscale (integer values between 0 and 255), and I currently get about a quarter of a second per convolution. My thinking was to do one of the following:

使用corepy,最好进行一些优化用 icc & 重新编译 numpy伊克尔.使用 python-cuda.

Use corepy, preferably with some optimizations Recompile numpy with icc & ikml. Use python-cuda.

我想知道是否有人对这些方法中的任何一种有任何经验(什么样的增益是典型的,以及是否值得花时间),或者是否有人知道使用 Numpy 执行卷积的更好的库.

I was wondering if anyone had any experience with any of these approaches ( what sort of gain would be typical, and if it is worth the time ), or if anyone is aware of a better library to perform convolution with Numpy.

谢谢!

通过使用 Numpy 在 C 中重写 python 循环,速度提高了大约 10 倍.

Speed up of about 10x by re-writing python loop in C over using Numpy.

推荐答案

scipy 中用于进行 2d 卷积的代码有点混乱且未优化.见 http://svn.scipy.org/svn/scipy/如果您想了解 scipy 的底层功能,请访问 trunk/scipy/signal/firfilter.c.

The code in scipy for doing 2d convolutions is a bit messy and unoptimized. See http://svn.scipy.org/svn/scipy/trunk/scipy/signal/firfilter.c if you want a glimpse into the low-level functioning of scipy.

如果您只想使用像您展示的那样一个小的、恒定的内核进行处理,这样的函数可能会起作用:

If all you want is to process with a small, constant kernel like the one you showed, a function like this might work:

def specialconvolve(a):
    # sorry, you must pad the input yourself
    rowconvol = a[1:-1,:] + a[:-2,:] + a[2:,:]
    colconvol = rowconvol[:,1:-1] + rowconvol[:,:-2] + rowconvol[:,2:] - 9*a[1:-1,1:-1]
    return colconvol

这个函数利用了上面建议的 DarenW 内核的可分离性,以及更优化的 numpy 算术例程.根据我的测量,它比 convolve2d 函数快 1000 多倍.

This function takes advantage of the separability of the kernel like DarenW suggested above, as well as taking advantage of the more optimized numpy arithmetic routines. It's over 1000 times faster than the convolve2d function by my measurements.

这篇关于提高 Numpy 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆