【问题标题】:Trick SciPy into convolution for sparse diagonal systems将 SciPy 用于稀疏对角系统的卷积
【发布时间】:2014-05-03 01:13:22
【问题描述】:

我正在尝试将一些代码转换为 Python,但我注意到 SciPy 的稀疏对角线运算在处理对角线系统时遇到了一些问题。

例如,下面的代码可以写成逐像素卷积,在我的 C++ 实现中它非常快。有了重叠,它大约是内存访问时间。我希望 Python 知道这一点,因为系统是对角的。

当我尝试在 Python 中运行它时,我的终端占用了系统资源并几乎杀死了我的系统。例如,Matlab 版本的速度要快得多。

import numpy as np
from scipy import sparse
print(np.version.version)
color=imread('lena.png')
gray=mean(color,2)
[h,w]=gray.shape
img=gray.flatten()
ne=h*w;
L=np.ones(ne);
M=.5*np.ones(ne);
R=np.ones(ne);
Diags=[L,M,R]
mtx = sparse.spdiags(Diags, [-1,0,1], ne, ne);
#blured=np.dot(mtx,img) Dies

我知道我可以将其重写为遍历像素的“for”循环,但是有没有一种方法可以在保持性能的同时保持稀疏数据结构?

【问题讨论】:

    标签: python scipy sparse-matrix


    【解决方案1】:

    使用mtx.dot 代替np.dot

    blured = mtx.dot(img)
    

    或者只是

    blured = mtx * img # where mtx is sparse and img is `dense` or `sparse` 
    

    np.dot 的两个参数处理dense,即使其中一个是sparse。 所以,这将引发MemoryError

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 2011-03-07
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2018-01-11
      • 2013-10-11
      • 2017-10-25
      相关资源
      最近更新 更多