在运行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;