How to speed up/ Reduced Time the following code Using Parallel Processing/GPU
Show older comments
I have a code that processes input signals and generates images of size 10,000 x 19,000. The image's width remains constant at 10,000 pixels, while the height depends on the signal length. Unfortunately, the current implementation takes approximately 1.842 seconds to complete. I'm seeking ways to optimize this code and reduce the execution time to milliseconds. Any assistance in modifying the code for this purpose would be greatly appreciated.
tic
[numImages, lenImage] = size( signal);
imbg = false(10000,lenImage); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[10000 lenImage];
% ImageSize
for k= 1:numImages
imData = round( signal(k,:)); % get pattern
[~,Y] = meshgrid(1:lenImage,1:10000); % make a grid
% black and white image
BW = imbg;
BW(Y==imData)=imfg;
valueestimation=imbinarize(imresize(uint8(BW),imSizeOut));
% convert to uint8 (0 255)
valueestimationimage = im2uint8(valueestimation);
% resize (from 1000x1000)
SE=strel('disk',2);
BW=imdilate(BW,SE);
BW=imbinarize(imresize(uint8(BW),imSizeOut));
% convert to uint8 (0 255)
imoriginalestimate = im2uint8(BW);
end
toc
11 Comments
Raymond Norris
on 31 Oct 2023
What do you do with valueestimationimage and imoriginalestimate after the for-loop. At the moment, you overwrite them in each iteration. Do you need to concatenate them during the for-loop?
signal is 1x19000. Presumably, it's actually 10k x 19k and thefore the for-loop runs 10k times, is that right (you're just showing us the 1-iteration version).
Why do you convert from double to uint8? To save on space? time?
Do you have an NVIDIA GPU?
Walter Roberson
on 31 Oct 2023
In my opinion there is no realistic way that you will be able to speed the code up by a factor of 100.
Hammad Younas
on 1 Nov 2023
Hammad Younas
on 1 Nov 2023
Walter Roberson
on 1 Nov 2023
Well go ahead and try it then. Toss in some gpuArray calls and a gather() and see what you get.
Note that the A5000 double precision performance is 1/32 of the single precision performance.
Hammad Younas
on 2 Nov 2023
Walter Roberson
on 2 Nov 2023
I would have to boot an older operating system on my older Mac and use an older version of MATLAB in order to be able to use my decade-old Nvidia GPU. (Nvidia no longer supports Apple). I do not have the budget for a new computer with an A5000.
It would be a lot easier for you to read about gpuArray and gather() and modify your own code.
Hammad Younas
on 2 Nov 2023
Walter Roberson
on 2 Nov 2023
Gsingal = gpuArray(signal);
[numImages, lenImage] = size( Gsignal);
imbg = false(10000,lenImage,'gpuArray' );
...
for k= 1:numImages
imData = round( Gsignal(k,:)); % get pattern
and so on.
Hammad Younas
on 8 Nov 2023
Walter Roberson
on 8 Nov 2023
What is your modified code? What error are you observing?
Answers (1)
Image Analyst
on 31 Oct 2023
Why is this inside the loop:
[~,Y] = meshgrid(1:lenImage,1:10000); % make a grid
lenImage doesn't change so that should be outside the loop. Same thing for the strel() line. No need to calculate it on every iteration. And what about imbinarize() to compute an image-specific threshold. Could you use a constant/fixed threshold instead?
Are you resizing an integer factor? If so you could probably speed it up by just using indexing to downsample the images.
Also, you tagged it with parallel processing but you don't seem to be using it. Try parfor and other stuff to turn it into a parallel process.
Plus, you compute all kinds of things in the loop but you do not save anything. Each iteration just overwrites a variable but once the loop exits they just have the values of the final iteration. You don't display anything, save it to an array that survives the loop, write it to disk, or anything. The loop just does stuff then ends.
Also if BW is a binary image, then there is no point in calling imbinarize on it. If the resize makes it have non-binary values, then you can just use the 'nearest' option in imresize. If it's not of logical class, then you can call logical instead of imbinarize which would be faster since no determination of a threshold would be required.
Categories
Find more on GPU Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!