Skip to content

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

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, 2, 3] on [2]: [4] instead of [2, 4, 6]
  • 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
  • testEdgeDetectors: the step response is checked at column 2 instead of 3

The convolution flipped the kernel, as numpy, and a kernel longer than
the input gave as many values as the kernel. It now computes
z_i = sum_j k_j x_{j+i-(l+1)/2} with zero padding, one value for each
input; for an even l the kernel covers one element more to the right of
x_i. The kernel [1, 2, 3] on [2] now gives [4] instead of [2, 4, 6].
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 gradient edge
detector responds at the last column before a step instead of the first
after it.
@dmeoli
dmeoli marked this pull request as ready for review December 2, 2026 05:16
@dmeoli
dmeoli merged commit 1d41413 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