diff --git a/questions/201_zero_padding/description.md b/questions/201_zero_padding/description.md new file mode 100644 index 00000000..7cd7b332 --- /dev/null +++ b/questions/201_zero_padding/description.md @@ -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. \ No newline at end of file diff --git a/questions/201_zero_padding/example.json b/questions/201_zero_padding/example.json new file mode 100644 index 00000000..d25089c8 --- /dev/null +++ b/questions/201_zero_padding/example.json @@ -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." +} diff --git a/questions/201_zero_padding/learn.md b/questions/201_zero_padding/learn.md new file mode 100644 index 00000000..4912b0a5 --- /dev/null +++ b/questions/201_zero_padding/learn.md @@ -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. + + \ No newline at end of file diff --git a/questions/201_zero_padding/meta.json b/questions/201_zero_padding/meta.json new file mode 100644 index 00000000..e48340a5 --- /dev/null +++ b/questions/201_zero_padding/meta.json @@ -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" + } + ] +} diff --git a/questions/201_zero_padding/solution.py b/questions/201_zero_padding/solution.py new file mode 100644 index 00000000..def32626 --- /dev/null +++ b/questions/201_zero_padding/solution.py @@ -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 \ No newline at end of file diff --git a/questions/201_zero_padding/starter_code.py b/questions/201_zero_padding/starter_code.py new file mode 100644 index 00000000..12f80ce4 --- /dev/null +++ b/questions/201_zero_padding/starter_code.py @@ -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 \ No newline at end of file diff --git a/questions/201_zero_padding/tests.json b/questions/201_zero_padding/tests.json new file mode 100644 index 00000000..e1829438 --- /dev/null +++ b/questions/201_zero_padding/tests.json @@ -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]]" + } +]