use gpu for image processing

1 次查看(过去 30 天)
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 个评论
MatlabEnthusiast
MatlabEnthusiast 2021-6-13
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 2021-6-14
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-6-13
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 个评论
MatlabEnthusiast
MatlabEnthusiast 2021-6-13
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 2021-6-20
thank you very much. it is solved :).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by