在matlab中使用parfor未定义函数或变量

问题描述 投票:0回答:2

我无法执行以下代码:

parallel_mode = true; %To process multiple images at the same time
parallel_degree = 4; %Number of threads that will be created

if parallel_mode
    if matlabpool('size') == 0
        matlabpool(parallel_degree);
    elseif matlabpool('size') ~= parallel_degree
        matlabpool close;
        matlabpool(parallel_degree);
    end    
end

%Loading dictionary
    try
        load(dictionary_path); 
    catch
        error(['Was impossible to load the dictionary in path: ' dictionary_path ', Please, check the path. ' ...
            'Maybe you should use dictionary_training function to create it.'])
    end

%% Processing test images

test_images = dir([test_im_path, pattern]);
num_test_images = size(test_images,1);

%Pre-allocating memory to speed-up
estimated_count = zeros(1,num_test_images);
true_count = zeros(1,num_test_images);

estimated_upper_count = zeros(1,num_test_images);
estimated_lower_count = zeros(1,num_test_images);
true_upper_count = zeros(1,num_test_images);
true_lower_count = zeros(1,num_test_images);

%Calculating dimensions of the image subregion where we can count
im_test = imread([test_im_path test_images(1).name]);
dis = round(patch_size/2); 
dim_x = dis:size(im_test,2)-dis+1;
dim_y = dis:size(im_test,1)-dis+1;

toGaussian = fspecial('gaussian', hsize, sigma);

parfor a=1:num_test_images

    disp(['Processing image #' num2str(a) ' of ' num2str(num_test_images) '...']);
    im_test = imread([test_im_path test_images(a).name]);
    [~, name, extension] = fileparts(test_images(a).name);
    im_ground_truth = imread([ground_truth_path name 'dots' extension]);

    disp('Extracting features...');
    features = extract_features(im_test, features_type, dic_signal, sparsity, patch_size, mean_rem_flag);
    features = full(features); %ND-sparse arrays are not supported.

    %Re-arranging features
    features = reshape(features', size(dim_y,2), size(dim_x,2), dic_size);

    %Normalizing features
    max_factors_3D = repmat(max_factors_depth, [size(features,1), size(features,2)]);
    max_offset_3D = repmat(max_offset_depth, [size(features,1), size(features,2)]);
    features = (features-max_offset_3D)./max_factors_3D;

    %%Some stuff
    ....
end

当我执行它时,我得到:

未定义的函数或变量“dic_signal”。

当到达 extract_features 函数时。但是,在单线程版本中(使用 for 而不是 parfor)它可以正常工作。 有人可以给我任何提示吗? 谢谢你。

编辑: dic_signal 已在

load(dictionary_path);

中定义并正确加载
matlab parfor
2个回答
0
投票

我怀疑

load
命令不会加载工作人员工作区中的变量,仅在您的 MATLAB 实例工作区中加载变量,这就是为什么它在正常
for
循环中工作,但不适用于
parfor
循环。您可能想尝试一下:

pctRunOnAll load(dictionary_path)

确保正确的数据加载到每个工作人员的工作空间中。


0
投票

当您运行代码时,Matlab 会解析

parfor
中的所有变量名称,如果无法确定变量是否存在,则确定它一定是一个函数,然后在运行时查找该函数。所以这里你的错误说你的“函数或变量”不存在,实际上意味着Matlab无法找到一个名为
dic_signal
的函数。

这在

parfor
文档这里中进行了解释,但在实践中这意味着什么并不是很明显 - 至少对我来说!此行为与
for
循环不同。

解决方案是在代码中显式定义

parfor
中使用的所有变量。所以,例如:

dic_signal = [];
load(dictionary_path)

因此解析器知道

dic_signal
是一个变量。

这种情况发生在使用

load
引入工作区的变量、代码调用的脚本中定义的变量以及
parfor
内未使用的逻辑分支中的代码。

我来这里寻找答案的问题是这样的:

using_a = false;
if using_a
    a = 1;
end
parfor n = 1:5
    if using_a
        b = a;
    end
end

这会在运行时产生错误:

无法识别的函数或变量“a”

可以通过更改为来避免:

using_a = false;
a = []; % Define 'a' even though it isn't used.
if using_a
    a = 1;
end
parfor n = 1:5
    if using_a
        b = a;
    end
end
© www.soinside.com 2019 - 2024. All rights reserved.