Skip to content
DeepForgeby svx
All problems
MediumComputer Visionproj-030

Convolution with Zero Padding

Problem

2D convolution (cross-correlation) with zero padding.

Slide the kernel over the image, multiplying and summing image patches with the kernel weights, and treat out-of-bounds pixels as 0. pad is the number of zero pixels added on every side, so the output shape is (H + 2*pad - kh + 1) x (W + 2*pad - kw + 1).

Hint

Iterate over output positions, then over kernel offsets, offsetting by -pad.

Test cases

case 1
in: [[[1,2],[3,4]],[[1]],1]
expected: [[0,0,0,0],[0,1,2,0],[0,3,4,0],[0,0,0,0]]
case 2
in: [[[1,2,3],[4,5,6],[7,8,9]],[[0,1,0],[1,0,1],[0,1,0]],1]
expected: [[6,9,8],[13,20,17],[12,21,14]]
case 3
in: [[[1,2],[3,4]],[[1,0],[0,1]],0]
expected: [[5]]
case 4
in: [[[2]],[[5]],0]
expected: [[10]]
Starter codePython
def conv2d_padded(image, kernel, pad):
    # Your code here
    pass

4 test cases · run your code against them in the editor