0

Hello I want to use parallelize my MATLAB code to run High computing server. It is code to make image database for Deep learning. To parallelize the code I found the I have to for parfor loop. But I used that with the first loop or with the second loop it shows me error parfor cannot be run due to the variable imdb and image_counter. Anyone please help me to change the code to work with parfor

for i = 1:length(cur_images)
            X = sprintf('image Numb: %d ',i);
            disp(X)
        cur_image = load(cur_images{i,:});
        cur_image=(cur_image.Image.crop);


        %----------------------------------------------

        cur_image = imresize(cur_image, image_size);


        if(rgb < 1)
            imdb.images.data(:,:,1,image_counter) = cur_image;
        else
            imdb.images.data(:,:,1,image_counter) = cur_image(:,:,1); 
            imdb.images.data(:,:,2,image_counter) = cur_image(:,:,2);
            imdb.images.data(:,:,3,image_counter) = cur_image(:,:,3); 
            imdb.images.data(:,:,4,image_counter) = cur_image(:,:,4); 
            imdb.images.data(:,:,5,image_counter) = cur_image(:,:,5); 
            imdb.images.data(:,:,6,image_counter) = cur_image(:,:,6); 
            imdb.images.data(:,:,7,image_counter) = cur_image(:,:,7); 
            imdb.images.data(:,:,8,image_counter) = cur_image(:,:,8); 
            imdb.images.data(:,:,9,image_counter) = cur_image(:,:,9); 
            imdb.images.data(:,:,10,image_counter) = cur_image(:,:,10);

        end


        imdb.images.set(     1,image_counter) = set;             
        image_counter = image_counter + 1;
    end
1
  • You just need a tutorial on what you can and can not do in parallel Commented May 9, 2018 at 8:49

1 Answer 1

2

The main problem here is that you can't assign to fields of a structure inside parfor in the way that you're trying to do. Also, your outputs need to be indexed by the loop variable to qualify as "sliced" - i.e. don't use image_counter. Putting this together, you need something more like:

% Make a numeric array to store the output.
data_out = zeros([image_size, 10, length(cur_images)]);

parfor i = 1:length(cur_images)
    cur_image = load(cur_images{i, :});
    cur_image=(cur_image.Image.crop);
    cur_image = imresize(cur_image, image_size);

    % Now, assign into 'data_out'. A little care needed
    % here.
    if rgb < 1
        data_tmp = zeros([image_size, 10]);
        data_tmp(:, :, 1) = cur_image;
    else
        data_tmp = cur_image;
    end
    data_out(:, :, :, i) = data_tmp;
end
imdb.images.data = data_out;
Sign up to request clarification or add additional context in comments.

2 Comments

after loading the size of cur_image is 250x250x10. The data should be store in imdb.images.data like (250x250x10x1) for the first image and (250x250x10x2) for the second image. data_out(:, :, :, i) = data_tmp; and imdb.images.data = data_out; will act like same?
There is one more thing is this parfor loop is working another for loop for no of sets. There are 3 sets and for each set this parfor is running. The size of first set is data_out 28x28x61x11028. The size of second and third set is data_out 28x28x61x2048. How can I concatenate it is loop of set?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.