conv2 'valid' implementation

I need to implement the conv2 function for a the HDL coder since it's not supported, I believe that I have the main function down, what I'm struggling with is the implementation of conv2 'valid' which is described in the documentation with — Returns only parts of the convolution that are computed without zero-padded edges-
The code for conv2 is the one I'm sharing, any help would be appreciated.
function B = convolve(A, k)
[r, c] = size(A);
[m, n] = size(k);
h = rot90(k, 2);
center = floor((size(h)+1)/2);
left = center(2) - 1;
right = n - center(2);
top = center(1) - 1;
bottom = m - center(1);
Rep = zeros(r + top + bottom, c + left + right);
for x = 1 + top : r + top
for y = 1 + left : c + left
Rep(x,y) = A(x - top, y - left);
end
end
B = zeros(r , c);
for x = 1 : r
for y = 1 : c
for i = 1 : m
for j = 1 : n
q = x - 1;
w = y -1;
B(x, y) = B(x, y) + (Rep(i + q, j + w) * h(i, j));
end
end
end
end

2 Commenti

Juan Pereira
Juan Pereira il 7 Ott 2019
Useful, thank you.
Hello, I am trying to use this code to implement various image filters however I keep getting the an index out of bounds error due to h(i,j). Any ideas?

Accedi per commentare.

 Risposta accettata

Lin Bai
Lin Bai il 9 Gen 2019

0 voti

as mentioned by Bharath, it is more efficient to use Simulink model instead of the MATLAB code to implement the conv2d function. Here I post the link of the Simulink model, which uses the image filer from Vision HDL toolbox to implement conv2 function.
In this model, image filter uses 'same' instead since it is more widely used in deep learning.

7 Commenti

Alla
Alla il 11 Gen 2019
Modificato: Alla il 11 Gen 2019
Thank you Lin, the model is quite helpful.
Convolutional operations yeild feature maps with a dimenstion of ((W−F+2P)/S)+1). The stride S I assume can't be changed and is 1 by default for Image Filter, so when padding P is set to zero the output feature map should be smaller. Applying a filter F of dimension 3 on a feature map W of 28 gives 26.
Not sure if it's possible, but I will see how to modify the control signal to only get that unpadded ouput.
An other quick question, would the Median Filter work for average pooling?
Lin Bai
Lin Bai il 11 Gen 2019
for stride S not equal to 1, current version of filter does not help. But you could check the block named "Pixel Control Bus Creator" to create your onw bus pixel control bus. In this case, you could achieve stride not equal to 1, and even unpadded output. The penalty is timing has been changed.
Median Filter does not work or average pooling, but using the bus creator mentioned above and together with some other logic, average pooling or max pooling is possible.
Alla
Alla il 15 Gen 2019
Thank you Lin, could you give me a hint on how the Bus creator can be be used to implement a pooling function? what I had in mind was to use the Image Filter block but with coeficients that can output an average pooled feature map.
Lin Bai
Lin Bai il 15 Gen 2019
Now I understad your idea about average pooling. Yes, image filter can be used to do average pooling. But you have to pick values of feature map every other row and every other column.
Alla
Alla il 17 Gen 2019
I'm still trying to use the Pixel Control Bus creator to adjust the dimensions of some of my outputs, I'm unsure of how to feed my own control singals or modify the exisiting ones to take only parts of a matrix, for example remove the last column or row.
Lin Bai
Lin Bai il 18 Gen 2019
Here I attached the model using pixel control bus to resize the image, remove the last column and remove the last row separately. please check. alough the funtions mentioned above are achieved, the total time(clock cycles) for one image trnasmit is not changed at all.
You could also check this example "Image Pyramid for FPGA" in Matlab Help. this really achieves the resize function.
Alla
Alla il 21 Gen 2019
Tremendous thanks to you Lin Bai, you have been a great help. there are not may people here who can help with these things specefically so your input is immensly valuable.

Accedi per commentare.

Più risposte (2)

Bharath Venkataraman
Bharath Venkataraman il 2 Nov 2018
Modificato: Bharath Venkataraman il 2 Nov 2018

0 voti

If you are looking for conv2 for image filtering, you can use the ImageFilter block and System object in Vision HDL Toolbox.
The Image Processing Toolbox has an example that shows how to do this .

1 Commento

Alla
Alla il 20 Nov 2018
Modificato: Alla il 20 Nov 2018
Bharath Venkataraman Thank you for your answer, I'm working with matlab code and not simulink, and I plan on using conv2 for operations in a convolutional neural netowrk.

Accedi per commentare.

Bharath Venkataraman
Bharath Venkataraman il 20 Nov 2018

0 voti

You can use the MATLAB workflow in the example for image filtering.

2 Commenti

Alla
Alla il 29 Nov 2018
I have tried using the Imagefilter block and code on a pixel stream of my input while using my own coefficients, I compared the outputs with conv2(in, filter, 'valid') and they're different. The source code for the filt2d function is understandbly inaccessible so I can't modify it to fit my requirements.
conv2 with 'valid' returns a smaller image (without padding), so please make sure you are only comparing the central part of the image.
You may find it closer to matching if you use 'same' instead of 'valid'.

Accedi per commentare.

Categorie

Scopri di più su Code Generation in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by