Skip to content

Convolution as equation 21.8 (kernel not flipped, same length) - #43

Merged
dmeoli merged 2 commits into
masterfrom
fix/convolution
Dec 2, 2026
Merged

dmeoli merged 2 commits into
masterfrom
fix/convolution

Conversation

@dmeoli

@dmeoli dmeoli commented Dec 2, 2026

Copy link
Copy Markdown
Member

ConvLayer1D.convolve and ImageProcessing.convolve2D now follow equation 21.8: the kernel is not flipped (footnote 4 of chapter 21), zero padding as in figure 21.5, output as long as the input also for a kernel longer than the input, and for an even kernel the extra element on the right (below, in 2-D), as the 'same' padding of Keras and PyTorch

expected values that change:

  • kernel [1, 1, 1] on [1, 2]: [3, 3] instead of the empty vector, and a 3-kernel layer on one input gives one value
  • convolve2D of [[1, 2, 3], [4, 5, 6]] with [[1, -1]]: [[-1, -1, 3], [-1, -1, 6]] instead of [[1, 1, 1], [4, 1, 1]]
  • convolve2D of an impulse: the kernel rotated by 180 degrees
  • EdgeDetectors and EdgesOfAFlatImage: the step response is checked at the last column before the step

The convolution flipped the kernel, as numpy, and a kernel longer than
the input gave the empty vector, as the valid convolution of Figure
21.4. It now computes z_i = sum_j k_j x_{j+i-(l+1)/2} with the zero
padding of Figure 21.5, one value for each input; for an even l the
kernel covers one element more to the right of x_i. The kernel [1, 1, 1]
on [1, 2] now gives [3, 3] instead of the empty vector.
convolve2D flipped the kernel, as scipy. It now follows the conventions
of ConvLayer1D.convolve: the kernel [1, -1] on [[1, 2, 3], [4, 5, 6]]
gives [[-1, -1, 3], [-1, -1, 6]] instead of [[1, 1, 1], [4, 1, 1]], an
impulse gives the kernel rotated by 180 degrees, and the edge detectors
respond at the last column before a step, so the tests look there.
@dmeoli
dmeoli marked this pull request as ready for review December 2, 2026 05:16
@dmeoli
dmeoli merged commit f95f568 into master Dec 2, 2026
1 check passed
@dmeoli
dmeoli deleted the fix/convolution branch December 2, 2026 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant