Answers.Body: <p>I assume that you would like to use convolution with padding with the same size of output matrix as this picture. You messed up calculations with convolution with "full padding". If we imagine these two matrices as windows that slide on each other, you can see that the filter is symmetrically inverted. I used a little bit different filter to show you in a better way how it works (I changed the last row to [-3, 0 3]).</p>
  
  <p>Assuming these matrices:</p>
  
  <p><a href="https://i.sstatic.net/ENEWg.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ENEWg.png" alt="Enter image description here"></a></p>
  
  <p>You should add to your picture matrix two rows and columns of zeros:</p>
  
  <p><a href="https://i.sstatic.net/A36DE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/A36DE.png" alt="Enter image description here"></a></p>
  
  <p>Then you can start matrix multiplication, but notice that the filter is symmetrically inverted. The result shown as matrix <span class="math-container">$4 \times 4$</span> is the convolution with "full padding". The <span class="math-container">$2 \times 2$</span> matrix in the middle is a result of convolution with the "same padding", that you requested.</p>
  
  <p><a href="https://i.sstatic.net/qfaRg.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/qfaRg.png" alt="Enter image description here"></a></p>
  
  <p>Next step:</p>
  
  <p><a href="https://i.sstatic.net/s4D0h.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/s4D0h.png" alt="Enter image description here"></a></p>
  
  <p>Some iterations later:</p>
  
  <p><a href="https://i.sstatic.net/ARh69.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ARh69.png" alt="Enter image description here"></a></p>
  
  <p>And later:</p>
  
  <p><a href="https://i.sstatic.net/VPWHV.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/VPWHV.png" alt="Enter image description here"></a></p>
  
  <p>And finally:</p>
  
  <p><a href="https://i.sstatic.net/VOYpc.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/VOYpc.png" alt="Enter image description here"></a></p>
  
  <p>For convolution with the same size, the result will be the small <span class="math-container">$2 \times 2$</span> matrix in the middle.</p>
  
  <p><a href="https://i.sstatic.net/JdwHl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JdwHl.png" alt="Enter image description here"></a></p>
  
  <p>After using the ReLU function, the result will be exactly the same.</p>
  
  <p>So with using of your filter, the result would look like [[0,90], [0,90]].</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-05T13:04:59.697Z
Answers.Text: Totally appreciate your detailed explanation. But it would really help if you can educate me further on the below 2 questions.
  
  1. Why did you choose the different filter? I mean is it completely random or there is a process to arrive to the correct filter.
  
  2. Since the pixel value (0 -> 30 -> 0) changes in in same proportion in the input image so in the convolved image if i use your filter then why are we getting different values (90, 150)? I guess the ixels in the convolved image gives the rate of change of pixel value in the original picture right? Shouldn't theybe same?
Answers.UserDisplayName: Abhranil Mukhopadhyay
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-06T14:16:09.440Z
Answers.Text: Ad 1. Your filter is OK. I decided to change values in last row to make it more visible, that matrix is "flipped" in both axis. That can be confusing. Ad 2. That's the metter of values in my filter and those "3s" in last row. Pure math. With using of your filter, the result is (90,90).
Answers.UserDisplayName: ketzul
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-05T12:35:59.827Z
Answers.LastActivityDate: 2019-06-05T21:42:11.947Z
Answers.LastEditDate: 2019-06-05T21:42:11.947Z
Answers.UserDisplayName: ketzul
Body: <p>Let's say I have a <span class="math-container">$2 \times 2$</span> pixel of grayscale picture, where there is one edge such that the left pixel contains a value, 30, and the right pixels contain a value 0 (in red below). And for edge detection I have zero-padded the input image and then used the Sobel vertical filter to find out the vertical edges and apply <a href="https://en.wikipedia.org/wiki/Rectifier_(neural_networks)" rel="nofollow noreferrer">ReLU</a> to the output. The output is a <span class="math-container">$2 \times 2$</span> matrix with all pixel values <span class="math-container">$0$</span>. So that should mean there is no edge in the picture whereas in actual case it has one. Where am I going wrong?</p>
  
  <p><a href="https://i.sstatic.net/464WN.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/464WN.png" alt="Enter image description here"></a></p>
  
Tags: <convolutional-neural-networks>
Title: Understanding the application of Sobel kernel followed by ReLU to a zero-padded image
UserDisplayName: Abhranil Mukhopadhyay
ThreadText: # Understanding the application of Sobel kernel followed by ReLU to a zero-padded image
  
  (Asked by: username_0 on 2019-01-05)
  
  Let's say I have a $2 \times 2$ pixel of grayscale picture, where there is one edge such that the left pixel contains a value, 30, and the right pixels contain a value 0 (in red below). And for edge detection I have zero-padded the input image and then used the Sobel vertical filter to find out the vertical edges and apply [ReLU](https://en.wikipedia.org/wiki/Rectifier_(neural_networks)) to the output. The output is a $2 \times 2$ matrix with all pixel values $0$. So that should mean there is no edge in the picture whereas in actual case it has one. Where am I going wrong?
  
  [![Enter image description here](https://i.sstatic.net/464WN.png)](https://i.sstatic.net/464WN.png)
  
  **Tags:** convolutional-neural-networks
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-05. Score: 1 | Accepted answer
  
  I assume that you would like to use convolution with padding with the same size of output matrix as this picture. You messed up calculations with convolution with "full padding". If we imagine these two matrices as windows that slide on each other, you can see that the filter is symmetrically inverted. I used a little bit different filter to show you in a better way how it works (I changed the last row to [-3, 0 3]).
  
  Assuming these matrices:
  
  [![Enter image description here](https://i.sstatic.net/ENEWg.png)](https://i.sstatic.net/ENEWg.png)
  
  You should add to your picture matrix two rows and columns of zeros:
  
  [![Enter image description here](https://i.sstatic.net/A36DE.png)](https://i.sstatic.net/A36DE.png)
  
  Then you can start matrix multiplication, but notice that the filter is symmetrically inverted. The result shown as matrix $4 \times 4$ is the convolution with "full padding". The $2 \times 2$ matrix in the middle is a result of convolution with the "same padding", that you requested.
  
  [![Enter image description here](https://i.sstatic.net/qfaRg.png)](https://i.sstatic.net/qfaRg.png)
  
  Next step:
  
  [![Enter image description here](https://i.sstatic.net/s4D0h.png)](https://i.sstatic.net/s4D0h.png)
  
  Some iterations later:
  
  [![Enter image description here](https://i.sstatic.net/ARh69.png)](https://i.sstatic.net/ARh69.png)
  
  And later:
  
  [![Enter image description here](https://i.sstatic.net/VPWHV.png)](https://i.sstatic.net/VPWHV.png)
  
  And finally:
  
  [![Enter image description here](https://i.sstatic.net/VOYpc.png)](https://i.sstatic.net/VOYpc.png)
  
  For convolution with the same size, the result will be the small $2 \times 2$ matrix in the middle.
  
  [![Enter image description here](https://i.sstatic.net/JdwHl.png)](https://i.sstatic.net/JdwHl.png)
  
  After using the ReLU function, the result will be exactly the same.
  
  So with using of your filter, the result would look like [[0,90], [0,90]].
  
  > **Comments:**
  > **username_0**: Totally appreciate your detailed explanation. But it would really help if you can educate me further on the below 2 questions.  1. Why did you choose the different filter? I mean is it completely random or there is a process to arrive to the correct filter.  2. Since the pixel value (0 -> 30 -> 0) changes in in same proportion in the input image so in the convolved image if i use your filter then why are we getting different values (90, 150)? I guess the ixels in the convolved image gives the rate of change of pixel value in the original picture right? Shouldn't theybe same?  
  > **username_1**: Ad 1. Your filter is OK. I decided to change values in last row to make it more visible, that matrix is "flipped" in both axis. That can be confusing. Ad 2. That's the metter of values in my filter and those "3s" in last row. Pure math. With using of your filter, the result is (90,90).  
  
  ---
  
  