Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions questions/201_zero_padding/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In image processing, padding is important because it helps preserve spatial dimensions during convolution operations, prevents information loss at image borders, and enables consistent output sizes across layers in deep learning models. Write a function `pad_matrix` that will return a zero-padded version of the input matrix. The original matrix will be called `a` and the amount of padding will be controlled by another integer parameter called `padding`. Note that the input matrix may not necessarily be square.
5 changes: 5 additions & 0 deletions questions/201_zero_padding/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"input": "a = [[1, 2], [3, 4]], padding = 1",
"output": "[[0, 0, 0, 0], [0, 1, 2, 0], [0, 3, 4, 0], [0, 0, 0, 0]]",
"reasoning": "Since the padding here is 1, a length one 'buffer' is included around the original input matrix. This is sometimes referred to as 'same' padding."
}
27 changes: 27 additions & 0 deletions questions/201_zero_padding/learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Understanding Padding

In computer vision and image processing, padding refers to the process of adding pixels (or matrix values) around the border of in input to preserve its dimension and prevent information loss during processing by, for example, and convolutional neural network (CNN). There are different strategies for padding, such as the position where padding is added (ie. left of image vs. around the image), the amount of padding added to each different axis, and even the specific value that is used as the padding value. In this simple example, the padding is done around the entire image, and each axis gets the same amount of padding.

Consider the following input matrix:

$$
\begin{bmatrix}
a_{11} & a_{12} \\
a_{21} & a_{22}
\end{bmatrix}
$$

If we consider a padding of 1, the padded matrix would have an additional "1 pixel" of values (zeros in this case) around it. This would look like:

$$
\begin{bmatrix}
0 & 0 & 0 & 0 \\
0 & a_{11} & a_{12} & 0 \\
0 & a_{21} & a_{22} & 0 \\
0 & 0 & 0 & 0
\end{bmatrix}
$$

With a padding of 2, there would be "2 pixels" around the picture, and so on. If the input matrix happened to be a rectangle the same logic will apply. Though in practice there may be more ways to customize this process, this simple idea of padding can help maintain certain dimensions of data throughout image processing and help reduce information loss at the edges of an image.


15 changes: 15 additions & 0 deletions questions/201_zero_padding/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "201",
"title": "Zero Padding",
"difficulty": "easy",
"category": "Computer Vision",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{
"profile_link": "https://github.com/vnethrapalli",
"name": "vnet"
}
]
}
12 changes: 12 additions & 0 deletions questions/201_zero_padding/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def pad_matrix(a: list[list[int|float]], padding: int) -> list[list[int|float]]:
# return a new matrix with padding of p zeros on both sides of each axis

rows = len(a)
cols = len(a[0])

padded_matrix = [[0]*(cols + 2*padding) for _ in range(rows + 2*padding)]
for i in range(rows):
for j in range(cols):
padded_matrix[i + padding][j + padding] = a[i][j]

return padded_matrix
3 changes: 3 additions & 0 deletions questions/201_zero_padding/starter_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def pad_matrix(a: list[list[int|float]], padding: int) -> list[list[int|float]]:
# return a new matrix with padding of p zeros on both sides of each axis
pass
18 changes: 18 additions & 0 deletions questions/201_zero_padding/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"test": "print(pad_matrix([[1, 2], [3, 4]], 1))",
"expected_output": "[[0, 0, 0, 0], [0, 1, 2, 0], [0, 3, 4, 0], [0, 0, 0, 0]]"
},
{
"test": "print(pad_matrix([[1]], 2))",
"expected_output": "[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]"
},
{
"test": "print(pad_matrix([[1, 2]], 1))",
"expected_output": "[[0, 0, 0, 0], [0, 1, 2, 0], [0, 0, 0, 0]]"
},
{
"test": "print(pad_matrix([[1], [2]], 1))",
"expected_output": "[[0, 0, 0], [0, 1, 0], [0, 2, 0], [0, 0, 0]]"
}
]