How to speed up/ Reduced Time the following code Using Parallel Processing/GPU

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

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?
In my opinion there is no realistic way that you will be able to speed the code up by a factor of 100.
@Raymond Norris we have different signals of different length.
1.so the loop is for numImages is the value of how many signals we have. currently we have only signal row so there is only single Signal. so it run one time.
2. To save space.
3. Yes i have NVIDIA GPU A5000
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.
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.
@Walter Roberson No issue with the computer with GPU. Just want to get the idea of the code How it should be written GPU
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.
What is your modified code? What error are you observing?

Sign in to comment.

Answers (1)

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.

2 Comments

@Image Analyst Basically this example is basically on one signal which have the length of 1x19000. But we have different signal which have different length. thats why we used meshgrid(1:lenImage,1:10000); inside the loop. when the length is change the size of image is also changed.
2. Currently i am not using any parallel processing/GPU But i need to modified the code to run it on Parallel Processing or GPU so my time should be in miliseconds.

Sign in to comment.

Products

Release

R2023b

Asked:

on 31 Oct 2023

Commented:

on 8 Nov 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!