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

时间:2014-02-26 10:09:10

标签: matlab parfor

我无法执行以下代码:

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函数时。但是,在单线程版本中(代替parfor)它可以正常工作。 有人可以给我任何暗示吗? 谢谢。

编辑: dic_signal已在load(dictionary_path);

中定义并正确加载

1 个答案:

答案 0 :(得分:0)

我怀疑load命令不会在workers工作区工作空间中加载变量,这只是在MATLAB实例工作空间中,这就是它在普通for循环中工作的原因,而不是{ {1}}循环。您可能想要尝试:

parfor

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