我编写了一个小的python批处理处理器,它加载二进制数据,执行numpy操作并存储结果。它消耗的内存比它应该消耗的内存多得多。我看了类似的stack-overflow discussions,希望能得到进一步的建议。在
我把光谱数据转换成rgb。光谱数据存储在一个按行交织(BIL)图像文件中。这就是我逐行读取和处理数据的原因。我使用Spectral Python Library读取数据,它返回一个numpy数组。hyp是一个大型光谱文件的描述符:hyp.ncols=1600, hyp.nrows=3430, hyp.nbands=160在
import spectral
import numpy as np
import scipy
class CIE_converter (object):
def __init__(self, cie):
self.cie = cie
def interpolateBand_to_cie_range(self, hyp, hyp_line):
interp = scipy.interpolate.interp1d(hyp.bands.centers,hyp_line, kind='cubic',bounds_error=False, fill_value=0)
return interp(self.cie[:,0])
#@profile
def spectrum2xyz(self, hyp):
out = np.zeros((hyp.ncols,hyp.nrows,3))
spec_line = hyp.read_subregion((0,1), (0,hyp.ncols)).squeeze()
spec_line_int = self.interpolateBand_to_cie_range(hyp, spec_line)
for ii in xrange(hyp.nrows):
spec_line = hyp.read_subregion((ii,ii+1), (0,hyp.ncols)).squeeze()
spec_line_int = self.interpolateBand_to_cie_range(hyp,spec_line)
out[:,ii,:] = np.dot(spec_line_int,self.cie[:,1:4])
return out
所有的大数据都是在循环外初始化的。我天真的解释是内存消耗不应该增加(我是否使用了太多的Matlab?)有人能解释一下增加系数10吗?这不是线性的,因为hyp.nrows公司=3430。 有什么改进内存管理的建议吗?在
^{pr2}$我将range替换为xrange,没有明显改善。我知道三次插值不是最快的,但这与CPU消耗无关。在
谢谢你的评论。它们都帮助我提高了一点内存消耗。 但最终我发现了内存消耗的主要原因是:
SpectralPython图像包含一个Numpy Memmap对象。这与高光谱数据立方体的数据结构具有相同的格式。(如果是BIL格式(nrows、NBAND、NCOL)) 打电话时:
图像不仅作为numpy数组返回值返回,还缓存在hyp.memmap公司. 第二次调用会更快,但在我的情况下,内存只会增加,直到操作系统抱怨为止。由于memmap实际上是一个很好的实现,我将在以后的工作中直接利用它。在
相关问题 更多 >
编程相关推荐