【Matlab Debugging】使用 addAttachedFiles(pool, files) 指定要附加的必要文件。

在運行SUN論文源代碼saliencyimage_convolution.m的時候,由於對代碼運行速率與機器性能的充分結合利用,我修改了代碼爲多核並行運行的代碼,裏面含有parfor…end的循環體,但出現瞭如下的問題,發下是Matlab中並行運算之前load的數據不能順利加載入並行運算池中,鑑於網上的資料太少,我加以個人的理解和解決方案在此,以供大家加以借鑑。

Preface

本博客提供了.mat文件在並行運算中的load方案

源代碼分析

function smap=saliencyimage_convolution(img,scale)
% function smap=saliencyimage(img,scale)
%   Calculate saliency map for color image, at certain scale

% each filter is zero summed

% load ICA basis functions
load stats;

d=size(B1,1); % number of filters
D=size(B1,2); % color filter streched length
fsize=D/3; % length of filter at each channel
psize=sqrt(fsize); % square filter

% preprocess image
% if scale~=1
%     img = imresize(img,scale);
% end
img=double(img);
img=img/std(img(:));
[height, width , ~] = size(img);

% process each channel
smap=zeros(height-psize+1,width-psize+1);
parfor f=1:d
    S = conv2(img(:,:,1),reshape(B1(f,1:fsize),psize,psize),'valid');
    S = S+conv2(img(:,:,2),reshape(B1(f,fsize+1:2*fsize),psize,psize),'valid');
    S = S+conv2(img(:,:,3),reshape(B1(f,2*fsize+1:3*fsize),psize,psize),'valid');
    smap=smap+(abs(S)/sigmas(f)).^thetas(f);
end

錯誤提示

Starting parallel pool (parpool) using the 'local' profile ...
connected to 4 workers.
Analyzing and transferring files to the workers ...done.
錯誤使用 saliencyimage_convolution (line 28)
在 'B1' 的工作進程上引發了 UndefinedFunction 錯誤。這可能是因爲在這些工作進程上無法訪問包含 'B1' 的文件。使用 addAttachedFiles(pool,
files) 指定要附加的必要文件。請參閱 'parallel.Pool/addAttachedFiles'的相關文檔以獲取更多詳細信息。

出錯 main_SUN (line 14)
    sm = saliencyimage_convolution(img,0.5);

原因:
    未定義與 'double' 類型的輸入參數相對應的函數 'B1'。

原因分析

使用 addAttachedFiles(pool, files) 指定要附加的必要文件。需要把文件load到並行池中,詳細信息可以查詢matlab官方文檔中對於parallel.Pool的描述,但不奏效。我把原.mat文件中的變量賦值給一個新的變量中,第一,每次在.mat文件中讀取數據,使程序變慢,這樣可以加快程序的運行速率,提高matlab的矩陣運算能力。若大家對此有更好的解決方案,可以私信我。
將原來的load stats代碼直接替換如下

STATS = importdata('stats.mat');
thetas = STATS.thetas; sigmas = STATS.sigmas; B1 = STATS.B1;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章