1

I am looking for a way to extract patches of size m x n from a matrix in Matlab by using GPU. The location of the patch extraction is determined by shifting a m x n window for s pixels horizontally and vertically.

Originally, I solved this problem by just using naive 2 nested for loops. However, it gets slow when I have a large image and small m and n. Because, this task has to be done multiple times, I want to increase the speed. I thought this process could be done in parallel, so I tried using parfor to solve the problem. But, it gets even slower than the normal for loops method.

Right now I am trying to use GPU for helping me in this task. But, currently I have now idea how to implement it. I have checked arrayfun, but it seems that it only support for element-wise calculation.

So, is it possible to use GPU to help solving the problem? It seems like it could be done in parallel.

EDIT: an example to the problem

Let's say I have a 3 x 3 matrix (I will write the matrix in MATLAB format).

`

A = [1 2 3
     4 5 6
     7 8 9];

`

I want to extract patches with size of 2 x 2. And I want the each patches are generated by 1 pixel shift. So, the result should be

`p1 = [1 2; 4 5]; p2 = [2 3; 5 6]; p3 = [4 5; 7 8]; p4 = [5 6; 8 9];`

And here is an example of what I did in the for loops method.

A = magic(3);
patchSize = [2 2];
shift = 1;

finalI = floor((size(A,1) - patchSize(1)) / shift);
finalJ = floor((size(A,2) - patchSize(2)) / shift);
patch = cell(finalI,finalJ);
for i = 0:finalI
    for j = 0:finalJ
        patch{i+1,j+1} = A(1+i*shift : patchSize(1)+i*shift, 1+j*shift : patchSize(2)+j*shift);
    end
end
zynaxsoft
  • 11
  • 4
  • Firstly, do you actually have a suitable GPU and the Parallel Computing Toolbox? – GameOfThrows Dec 16 '16 at 15:12
  • Yes, I have checked with gpuDevice command. I also tested most of the example involve using GPU. – zynaxsoft Dec 16 '16 at 15:22
  • This doesn't really seem like a problem that is well-suited for a GPU. I would recommend specifying your problem a little more and we can help you develop a fast vectorized approach. – Suever Dec 16 '16 at 15:26
  • @Suever I have edited the post to show some examples. – zynaxsoft Dec 16 '16 at 16:03
  • The problem is now solved by using im2col as suggested by @rayryeng. I did not realize that im2col was accelerated by GPU as it is not listed as a GPU supported function in https://www.mathworks.com/help/images/gpu-computing.html. However, thank you for help! – zynaxsoft Dec 16 '16 at 16:40
  • @zynaxsoft I was mistaken. It is not GPU accelerated. However, I did find a MEX GPU accelerated version of `im2col` though. You will need to compile the code with CUDA of course: https://github.com/projectgalateia/im2col_gpu. Good luck! – rayryeng Dec 16 '16 at 16:42
  • Thanks! I will definitely check that. – zynaxsoft Dec 16 '16 at 16:47

0 Answers0