2D convolution optimization

5 views (last 30 days)
Dennis Jacob
Dennis Jacob on 1 Mar 2018
Edited: Dennis Jacob on 1 Mar 2018
----- german version --------
Hallo Matlab Community,
im Rahmen meiner Bachelorarbeit benötige ich die 2D Faltung. Bisher verwende ich in der Auswertung die build in conv2(A,B,'full') Funktion von Matlab. Dabei haben A und B die gleiche Größe und sind "klein". Da bei meiner Auswertung das Maximum der Faltung immer im Bereich des Mittelpunktes liegt, ist es nicht nötig die komplette Faltung zu berechnen. Ich habe also die Faltung selber implementiert um eine Beschleunigung zu erreichen (Quellcode siehe English Version).
Die Resultate sind völlig korrekt, aber die Berechnung dauert länger als mit der eingebauten Funktion, obwohl deutlich weniger Schleifendurchläufe ausgeführt werden. Also würde ich erwarten, dass mein Algorithmus schneller sein sollte. Lässt sich mein Code optimieren? Leider ist der Quellcode von conv2 nicht open source und ich habe auch keine Details zur Implementierung gefunden. Ich denke lineare Indizierung ist langsamer. Mit conv2(A,B,'same') erhalte ich vergleichbare Resultate, aber trotzdem frage ich mich warum mein Code langsamer ist. Scheinbar ist die Variante mit 'same' aber auch etwas langsamer.
Ein Beispiel zur Ausführung der Faltung findet sich ebenfalls in der englischen Verison.
Mit freundlichen Grüßen aus Bremen Dennis Jacob, Systems Engineering Student Ich benutze R2017b auf einem Lenove Laptop
----- english version --------
Hey Matlab Community,
as part of my bachelor thesis im working with convolution in 2D. The calculation uses the build in conv2(A,B,'full') function so far, where A and B are the same size and "small". Because in my case the peak is always in the center or its direct vicinity, its not necessary to calculate the whole convolution. I have implemented the convolution by myself to speedup my evaluation.
% code
%input matrix A, kernel k
% A and k have same size
%area which should be calculated; from center point
function B = conv2_own(A, k, area)
[r,c] = size(A);
[m,n] = size(k);
h = rot90(k, 2);
%zero padding; faster than padarray
Rep = zeros(r + m*2-2, c + n*2-2);
for x = m : m+r-1
for y = n : n+r-1
Rep(x,y) = A(x-m+1, y-n+1);
end
end
%convolution
B = zeros(r+m-1,n+c-1);
for x = r-area : r+area
for y = c-area : c+area
tmp = Rep(y:y+r-1, x:x+c-1) .* h;
B(y,x) = sum(sum(tmp));
end
end
This algorithm gives me nice results, but is slower than the build in function although there are significant less calculations to perform. Im wondering if there is any optimization to my code? I would think, that my code must be faster. Sadly thought the matlab code of conv2 is not open source an i havent found any implementation details. I have though about linear indexing of the nested for loops, but think its not faster. I know that i can use conv2(A,B,'same') to get similar results, but my questions/confusion remains. conv2(A,B,'same') seems to be slower als well
To execute the code you could use this example:
% code
A = randi(100,31);
K = randi(100,31);
C = conv2_own(A,K,10);
figure();
imagesc(C);
D = conv2(A,K);
figure();
imagesc(D);
Greetings from Bremen, Germany Dennis Jacob, student Systems Engineering I am using R2017 on a Lenovo Laptop

Answers (0)

Categories

Find more on MATLAB 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!