why do we need to flip kernel before using conv2 in CNN?

We know that function conv2 can prefom convolution (between image and kernel ) and flip kernel before apply convolution to image according to defnition of convolution
y = conv2(image, kernel, 'valid')
.However, in convolution neural network(CNN) ,they flip the kernel before the use conv2
kernel = rot90(kernel, 2);
y = conv2(image, kernel, 'valid');
which means the kernel flip twice and this correlation not convolution why

댓글 수: 3

Jonas
Jonas 2022년 7월 4일
편집: Jonas 2022년 7월 4일
not that 90° is not flipped, flipping would be rotation by 180 degress or using flipud(fliplr())
and where exactly did you find this code (file, line number)
function y = Conv(x, W) % % [wrow, wcol, numFilters] = size(W); [xrow, xcol, ~ ] = size(x); yrow = xrow - wrow + 1; ycol = xcol - wcol + 1; y = zeros(yrow, ycol, numFilters); for k = 1:numFilters filter = W(:, :, k); filter = rot90(squeeze(filter), 2); y(:, :, k) = conv2(x, filter, 'valid'); end end
Mohammedee
Mohammedee 2022년 7월 4일
편집: Mohammedee 2022년 7월 4일
Look to this code..kernel rotated 180 Then pass it to conv2 And we know that conv2 will rotate kernel 180 again... This mean kernel rotated twice 180..

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2022년 7월 4일
편집: Matt J 2022년 7월 4일

0 개 추천

The field of neural networks uses the term "convolution" loosely. There are other differences as well. We also know that in traditional DSP theory, convolution operations don't contain a stride parameter, but in the NN world, they do.

댓글 수: 5

It is not clear..
Matt J
Matt J 2022년 7월 10일
편집: Matt J 2022년 7월 10일
Basically, neural networks researchers are not using the terminology "convolution" in the classical way. A true convolution in the original sense of the word should include a flip and should never have stride>1. That's the way convolution was originally defined. Without the flip, it should be called correlation, as you say.
Mohammedee
Mohammedee 2022년 7월 10일
편집: Mohammedee 2022년 7월 10일
http://ufldl.stanford.edu/tutorial/supervised/ExerciseConvolutionAndPooling/
In this link i find other related answer but also not clear they said:
If you use conv2(image, W), MATLAB will first "flip" W, reversing its rows and columns, before convolving W with image, as below:
⎛⎝⎜147258369⎞⎠⎟−→−flip⎛⎝⎜963852741⎞⎠⎟(123456789)→flip(987654321)
If the original layout of W was correct, after flipping, it would be incorrect. For the layout to be correct after flipping, you will have to flip W before passing it into conv2, so that after MATLAB flips W in conv2, the layout will be correct. For conv2, this means reversing the rows and columns, which can be done by rotating W 90 degrees twice with rot90 as shown below:
Mohammedee
Mohammedee 2022년 7월 10일
편집: Mohammedee 2022년 7월 10일
The answer here but i do understand that W will be not corrected after flipping.. Thus u have to flip before use. Conv2
(If the original layout of W was correct, after flipping, it would be incorrect. For the layout to be correct after flipping, you will have to flip W before passing it into conv2, so that after MATLAB flips W in conv2, the layout will be correct)
If you use conv2(image, W), MATLAB will first "flip" W, reversing its rows and columns
Yes, conv2 will flip W internally and that is the correct thing for it to do, because that is the way convolution is defined. This definition ensures that conv2(1,W) = W. Example:
W=[1 2;3 4]
W = 2×2
1 2 3 4
conv2(1,W)
ans = 2×2
1 2 3 4
If you were to flip W manually, prior to giving it to conv2, it would mess this up:
conv2(1,rot90(W,2))
ans = 2×2
4 3 2 1

댓글을 달려면 로그인하십시오.

제품

릴리스

R2021a

질문:

2022년 7월 4일

편집:

2022년 7월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by