use gpu for image processing

2 views (last 30 days)
hello, I have created about 1500 images on disk. I process them, store the results as mat files on disk, say image1_result1.mat, then read these results from another function which also creates a mat file after its processing say image1_result2.mat.
so i do these iteratively for all the 1500 images. But i noticed that my pc, despite having gpu, is using the cpu. In fact GPU used by matlab is 0% while cpu oscillates between 24% and about 94%.
Now am wondering if there's a way to execute these operations using the gpu in matlab. if so how best can I modify the code.
Structure of the main script that is looping is like this:
for i=1:total_images % do this for all images
[p, n, e] = fileparts(image_name);
image = image_name; % 'Image1.jpg';
param_one = p1;
param_two = p2;
% generate params for algorithm 1 from the image
generateParams(image, param_one); % saves results in image1_result.mat
% create corresponding database for the image
create_data(image, param_two); % reads param_two and others from image1_result.mat
% get the re-created image using algorithm
create_final(image_name, param_one); % stores results in image1_final.mat
end
for i=1:total_images
load('imagei_final'); % loads final results of image i from disk for calculations
use these results to calculate statistics.
end
How best can i convert this to a gpu executable algorithm to have faster performance, if possible? thanks.
  3 Comments
MatlabEnthusiast
MatlabEnthusiast on 13 Jun 2021
Hello, I have. The problem is that am not sure which part I should modify and how? Will suffice for me to say make any of the arrays in any function (at least an array per function) all of the functions as a gpuArray? Will the rest of the code then be executed on gpu affterwards?
Joss Knight
Joss Knight on 14 Jun 2021
Sorry, it's not possible to know without seeing your code. Every function that has gpuArray input will have gpuArray outputs, so typically you only need to convert your initial input data; unless you are loading or generating new data inside your function. And then if you are using certain functions in Deep Learning and some other toolboxes you may find that no changes are needed to leverage the GPU. So we can't say for sure.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jun 2021
generateParams(image, param_one); % saves results in image1_result.mat
% create corresponding database for the image
create_data(image, param_two); % reads param_two and others from image1_result.mat
I recommend that you avoid the trip to the file system if you want to optimize your code.
image1_result = generateParams(image_name, param_one);
% create corresponding database for the image
image1_data = create_data(image_name, param_two, image1_result);
create_final(image_name, param_one, image1_data)
(and do not use image as a variable name, as image() is a fundamental graphics function.)
image1_result and image1_data could use gpuArray as needed.
Since image1_result could be a struct, you could store the image_name as part of it and so not need to pass it to the other two functions. Also since it could be a struct, you could use the same struct for image1_result and image1_data
  2 Comments
MatlabEnthusiast
MatlabEnthusiast on 13 Jun 2021
I recommend that you avoid the trip to the file system if you want to optimize your code.
Normally i was not doing this before, I was passing this information as arguments to functions but since i need to store it, the array or struct i was using grew so large, i started to get out of memory errors. This is the only work around i could think of. actually, Is just writing the information to file system and not having to read from file system fine? I could just store whatever i needand still pass the arguments. Thanks i am going to implement that.
image1_result and image1_data could use gpuArray as needed.
thanks am going to try this.
MatlabEnthusiast
MatlabEnthusiast on 20 Jun 2021
thank you very much. it is solved :).

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!