2017-05-07 132 views
0

我試圖在Richardson-Lucy解卷積算法中實現停止標準described in this paper。我的測試圖像有2.2 Mpx。我需要計算:在scipy Python中使用稀疏矩陣進行計算

estimator = numpy.dot(P_e_ortho, im_deconv.flatten()) 

其中

im_deconv = [[]] # Image L channel as a 2D array 
m = 2.2E6 # im_deconv.size 

P_e_ortho = scipy.sparse.identity(m, dtype='int8') - \ 
      1/m * np.ones((m, m), dtype='int8') 

所以基本上,P_e_ortho1 - 1/m對角和其他地方- 1/m

現在這段代碼返回一個內存錯誤(需要4.8×10 12個單元)。我怎樣才能避免在計算中處理整個矩陣?

scipy.sparse.identity(m, dtype='int8') * (1 - 1/m) 

可以很好地設置對角線,但如何更改非對角元素?

發現

回答

0

解決方案:

# Construct a Row-based linked list sparse matrix 
P_e_ortho = scipy.sparse.lil_matrix((m, m)) 

P_e_ortho[:, :] = -1/m # Sets all elements 
P_e_ortho.setdiag(1-1/m) # Sets the diagonal 
P_e_ortho = P_e_ortho.tocsr() # Convert to Compressed Sparse Row matrix 

estimator = P_e_ortho.dot(im_deconv.flatten()) # Compute the dot product 
+0

我很驚訝這不會給你一個內存錯誤。您已將矩陣的所有元素設置爲非零值。總存儲量將大於相應的密集陣列,儘管分割成多個陣列。 '.dot'也會慢很多。 – hpaulj

+0

@hpaulj是的,它不適用於這個問題,我放棄了這種方法。這篇論文展示了這種技術在小圖片上的實現,對大圖片來說並不可行。我改用正規化因素。但是該解決方案仍然適用於構建稀疏矩陣。 –