From c82b9f39cd108dd3132ba342d97d819bdf77620e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Nov 2021 00:37:32 +0530 Subject: [PATCH 01/13] adding pooling algorithms --- computer_vision/pooling.py | 90 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 computer_vision/pooling.py diff --git a/computer_vision/pooling.py b/computer_vision/pooling.py new file mode 100644 index 000000000000..94c50ec1f18c --- /dev/null +++ b/computer_vision/pooling.py @@ -0,0 +1,90 @@ +# Source : https://computersciencewiki.org/index.php/Max-pooling_/_Pooling + +# Importing the libraries +import numpy as np +from PIL import Image + +# Maxpooling Function +def maxpooling(arr, size, stride): + """ + Args: + arr: numpy array + size: size of pooling matrix + stride: the number of pixels shifts over the input matrix + Returns: + numpy array + """ + i = 0 + j = 0 + maxpool_shape = (arr.shape[0]-size)//stride + 1 + updated_arr = np.zeros((maxpool_shape, maxpool_shape)) + + while i < arr.shape[0]: + if i+size > arr.shape[0]: + break + while j < arr.shape[1]: + if j+size > arr.shape[1]: + break + updated_arr[i][j] = np.max(arr[i:i+size, j:j+size]) + j += stride + i += stride + j = 0 + + return updated_arr + +# Averagepooling Function +def avgpooling(arr, size, stride): + """ + Args: + arr: numpy array + size: size of pooling matrix + stride: the number of pixels shifts over the input matrix + Returns: + numpy array + """ + i = 0 + j = 0 + # compute the shape of the output matrix + maxpool_shape = (arr.shape[0]-size)//stride + 1 + updated_arr = np.zeros((maxpool_shape, maxpool_shape)) + + while i < arr.shape[0]: + # if the end of the matrix is reached, break + if i+size > arr.shape[0]: + break + while j < arr.shape[1]: + # if the end of the matrix is reached, break + if j+size > arr.shape[1]: + break + # compute the average of the pooling matrix + updated_arr[i][j] = int(np.average(arr[i:i+size, j:j+size])) + j += stride + + i += stride + j = 0 + + return updated_arr + + +# Main Function +if __name__ == "__main__": + # Loading the image + img = Image.open("path_to_image") + + + # Converting the image to numpy array and maxpooling, displaying the result + # Ensure that the image is a square matrix + + # Parameters + size = 3 + stride = 2 + Image.fromarray(maxpooling(np.array(img), size, stride)).show() + + # Converting the image to numpy array and averagepooling, displaying the result + # Ensure that the image is a square matrix + + # Parameters + size = 3 + stride = 2 + Image.fromarray(avgpooling(np.array(img), size, stride)).show() + From 15fa325f4997d2e00a621915d4861367e664fd01 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Nov 2021 00:48:39 +0530 Subject: [PATCH 02/13] pooling.py: Adding pooling algorithms to computer vision pull_number= --- computer_vision/pooling.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/computer_vision/pooling.py b/computer_vision/pooling.py index 94c50ec1f18c..6326cf49f816 100644 --- a/computer_vision/pooling.py +++ b/computer_vision/pooling.py @@ -7,6 +7,8 @@ # Maxpooling Function def maxpooling(arr, size, stride): """ + This function is used to perform maxpooling on the input array of 2D matrix(image) + Args: arr: numpy array size: size of pooling matrix @@ -14,6 +16,8 @@ def maxpooling(arr, size, stride): Returns: numpy array """ + if arr.shape[0] != arr.shape[1]: + raise ValueError("The input array is not a square matrix") i = 0 j = 0 maxpool_shape = (arr.shape[0]-size)//stride + 1 @@ -35,6 +39,8 @@ def maxpooling(arr, size, stride): # Averagepooling Function def avgpooling(arr, size, stride): """ + This function is used to perform avgpooling on the input array of 2D matrix(image) + Args: arr: numpy array size: size of pooling matrix @@ -42,6 +48,8 @@ def avgpooling(arr, size, stride): Returns: numpy array """ + if arr.shape[0] != arr.shape[1]: + raise ValueError("The input array is not a square matrix") i = 0 j = 0 # compute the shape of the output matrix From 4c2168ce3871738f1c4a8dfc75bb4c3a03fd82d1 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Nov 2021 01:17:25 +0530 Subject: [PATCH 03/13] pooling.py: Adding pooling algorithms to computer vision --- .../{pooling.py => pooling_functions.py} | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) rename computer_vision/{pooling.py => pooling_functions.py} (59%) diff --git a/computer_vision/pooling.py b/computer_vision/pooling_functions.py similarity index 59% rename from computer_vision/pooling.py rename to computer_vision/pooling_functions.py index 6326cf49f816..d6c92e750fdf 100644 --- a/computer_vision/pooling.py +++ b/computer_vision/pooling_functions.py @@ -1,9 +1,9 @@ -# Source : https://computersciencewiki.org/index.php/Max-pooling_/_Pooling - +# Source : https://computersciencewiki.org/index.php/Max-pooling_/_Pooling # Importing the libraries import numpy as np from PIL import Image + # Maxpooling Function def maxpooling(arr, size, stride): """ @@ -14,79 +14,91 @@ def maxpooling(arr, size, stride): size: size of pooling matrix stride: the number of pixels shifts over the input matrix Returns: - numpy array + numpy array of maxpooled matrix """ if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix") i = 0 j = 0 - maxpool_shape = (arr.shape[0]-size)//stride + 1 + # compute the shape of the output matrix + maxpool_shape = (arr.shape[0] - size) // stride + 1 + # initialize the output matrix with zeros of shape maxpool_shape updated_arr = np.zeros((maxpool_shape, maxpool_shape)) while i < arr.shape[0]: - if i+size > arr.shape[0]: + if i + size > arr.shape[0]: + # if the end of the matrix is reached, break break while j < arr.shape[1]: - if j+size > arr.shape[1]: + # if the end of the matrix is reached, break + if j + size > arr.shape[1]: break - updated_arr[i][j] = np.max(arr[i:i+size, j:j+size]) + # compute the maximum of the pooling matrix + updated_arr[i][j] = np.max(arr[i:i + size, j:j + size]) + # shift the pooling matrix by stride of column pixels j += stride + + # shift the pooling matrix by stride of row pixels i += stride + # reset the column index to 0 j = 0 - + return updated_arr + # Averagepooling Function def avgpooling(arr, size, stride): """ This function is used to perform avgpooling on the input array of 2D matrix(image) - Args: arr: numpy array size: size of pooling matrix stride: the number of pixels shifts over the input matrix Returns: - numpy array + numpy array of avgpooled matrix """ if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix") i = 0 j = 0 # compute the shape of the output matrix - maxpool_shape = (arr.shape[0]-size)//stride + 1 - updated_arr = np.zeros((maxpool_shape, maxpool_shape)) + avgpool_shape = (arr.shape[0] - size) // stride + 1 + # initialize the output matrix with zeros of shape avgpool_shape + updated_arr = np.zeros((avgpool_shape, avgpool_shape)) while i < arr.shape[0]: # if the end of the matrix is reached, break - if i+size > arr.shape[0]: + if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break - if j+size > arr.shape[1]: + if j + size > arr.shape[1]: break # compute the average of the pooling matrix - updated_arr[i][j] = int(np.average(arr[i:i+size, j:j+size])) + updated_arr[i][j] = int(np.average(arr[i:i + size, j:j + size])) + # shift the pooling matrix by stride of column pixels j += stride + # shift the pooling matrix by stride of row pixels i += stride + # reset the column index to 0 j = 0 - + return updated_arr # Main Function if __name__ == "__main__": # Loading the image - img = Image.open("path_to_image") - + image = Image.open("path_to_image") # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix - + # Parameters size = 3 stride = 2 - Image.fromarray(maxpooling(np.array(img), size, stride)).show() + Image.fromarray(maxpooling(np.array(image), size, stride)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix @@ -94,5 +106,4 @@ def avgpooling(arr, size, stride): # Parameters size = 3 stride = 2 - Image.fromarray(avgpooling(np.array(img), size, stride)).show() - + Image.fromarray(avgpooling(np.array(image), size, stride)).show() From 64494cafb6215299ac023fd9729d7db94ec93b54 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Nov 2021 01:30:22 +0530 Subject: [PATCH 04/13] pooling_functions.py: Adding pooling algorithms to computer vision --- computer_vision/pooling_functions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index d6c92e750fdf..3e039752acba 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -5,7 +5,7 @@ # Maxpooling Function -def maxpooling(arr, size, stride): +def maxpooling(arr:list, size:int, stride:int) -> list: """ This function is used to perform maxpooling on the input array of 2D matrix(image) @@ -47,7 +47,7 @@ def maxpooling(arr, size, stride): # Averagepooling Function -def avgpooling(arr, size, stride): +def avgpooling(arr:list, size:int, stride:int) -> list: """ This function is used to perform avgpooling on the input array of 2D matrix(image) Args: From 868e42abde0e8995cbc322020d4ebb7cde08dc02 Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Mon, 15 Nov 2021 20:00:21 +0530 Subject: [PATCH 05/13] pooling.py: Adding Pooling Algorithms --- computer_vision/pooling_functions.py | 44 +++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index 3e039752acba..1663a36b33af 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -2,24 +2,37 @@ # Importing the libraries import numpy as np from PIL import Image - +from doctest import testmod # Maxpooling Function def maxpooling(arr:list, size:int, stride:int) -> list: """ This function is used to perform maxpooling on the input array of 2D matrix(image) - Args: arr: numpy array size: size of pooling matrix stride: the number of pixels shifts over the input matrix Returns: numpy array of maxpooled matrix + + Sample Input Output: + >>> maxpooling([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 2, 2) + array([[ 6., 8.], + [14., 16.]]) + + >>> maxpooling([[136,228,0,166,51],[148,42,89,146,230],[3,37,215,145,69],[83,43,20,22,80],[172,171,233,165,48]], 3, 1) + array([[228., 228., 230.], + [215., 215., 230.], + [233., 233., 233.]]) """ + arr = np.array(arr) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix") i = 0 j = 0 + max_mat_i = 0 + max_mat_j = 0 + # compute the shape of the output matrix maxpool_shape = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape @@ -34,14 +47,18 @@ def maxpooling(arr:list, size:int, stride:int) -> list: if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix - updated_arr[i][j] = np.max(arr[i:i + size, j:j + size]) + updated_arr[max_mat_i][max_mat_j] = np.max(arr[i:i + size, j:j + size]) # shift the pooling matrix by stride of column pixels j += stride + max_mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride + max_mat_i += 1 + # reset the column index to 0 j = 0 + max_mat_j = 0 return updated_arr @@ -50,6 +67,15 @@ def maxpooling(arr:list, size:int, stride:int) -> list: def avgpooling(arr:list, size:int, stride:int) -> list: """ This function is used to perform avgpooling on the input array of 2D matrix(image) + >>> avgpooling([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 2, 2) + array([[ 3., 5.], + [11., 13.]]) + + >>> avgpooling([[136,228,0,166,51],[148,42,89,146,230],[3,37,215,145,69],[83,43,20,22,80],[172,171,233,165,48]], 3, 1) + array([[ 99., 118., 123.], + [ 75., 84., 112.], + [108., 116., 110.]]) + Args: arr: numpy array size: size of pooling matrix @@ -57,10 +83,14 @@ def avgpooling(arr:list, size:int, stride:int) -> list: Returns: numpy array of avgpooled matrix """ + arr = np.array(arr) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix") i = 0 j = 0 + avg_mat_i = 0 + avg_mat_j = 0 + # compute the shape of the output matrix avgpool_shape = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape @@ -75,20 +105,26 @@ def avgpooling(arr:list, size:int, stride:int) -> list: if j + size > arr.shape[1]: break # compute the average of the pooling matrix - updated_arr[i][j] = int(np.average(arr[i:i + size, j:j + size])) + updated_arr[avg_mat_i][avg_mat_j] = int(np.average(arr[i:i + size, j:j + size])) # shift the pooling matrix by stride of column pixels j += stride + avg_mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride + avg_mat_i += 1 # reset the column index to 0 j = 0 + avg_mat_j = 0 return updated_arr # Main Function if __name__ == "__main__": + # test the functions + testmod(name ='avgpooling', verbose = True) + # Loading the image image = Image.open("path_to_image") From a7b76aee29e2c80b3f71973fb27c1fb8eaa582f9 Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Tue, 16 Nov 2021 20:41:32 +0530 Subject: [PATCH 06/13] pooling_functions.py Add and Update --- computer_vision/pooling_functions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index 1663a36b33af..7dc2e8e9d648 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -5,7 +5,7 @@ from doctest import testmod # Maxpooling Function -def maxpooling(arr:list, size:int, stride:int) -> list: +def maxpooling(arr:np.ndarray, size:int, stride:int) -> np.ndarray: """ This function is used to perform maxpooling on the input array of 2D matrix(image) Args: @@ -64,7 +64,7 @@ def maxpooling(arr:list, size:int, stride:int) -> list: # Averagepooling Function -def avgpooling(arr:list, size:int, stride:int) -> list: +def avgpooling(arr:np.ndarray, size:int, stride:int) -> np.ndarray: """ This function is used to perform avgpooling on the input array of 2D matrix(image) >>> avgpooling([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 2, 2) From 57e5eb5bff561a610e7a7779923a7f4ddd3997a9 Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Tue, 16 Nov 2021 21:18:24 +0530 Subject: [PATCH 07/13] Update pooling_functions.py --- computer_vision/pooling_functions.py | 45 +++++++++++++--------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index 7dc2e8e9d648..98145136afc6 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -5,7 +5,9 @@ from doctest import testmod # Maxpooling Function -def maxpooling(arr:np.ndarray, size:int, stride:int) -> np.ndarray: + + +def maxpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: """ This function is used to perform maxpooling on the input array of 2D matrix(image) Args: @@ -14,16 +16,13 @@ def maxpooling(arr:np.ndarray, size:int, stride:int) -> np.ndarray: stride: the number of pixels shifts over the input matrix Returns: numpy array of maxpooled matrix - Sample Input Output: >>> maxpooling([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 2, 2) array([[ 6., 8.], [14., 16.]]) - - >>> maxpooling([[136,228,0,166,51],[148,42,89,146,230],[3,37,215,145,69],[83,43,20,22,80],[172,171,233,165,48]], 3, 1) - array([[228., 228., 230.], - [215., 215., 230.], - [233., 233., 233.]]) + >>> maxpooling([[147, 180, 122],[241, 76, 32],[126, 13, 157]], 2, 1) + array([[241., 180.], + [241., 157.]]) """ arr = np.array(arr) if arr.shape[0] != arr.shape[1]: @@ -64,32 +63,30 @@ def maxpooling(arr:np.ndarray, size:int, stride:int) -> np.ndarray: # Averagepooling Function -def avgpooling(arr:np.ndarray, size:int, stride:int) -> np.ndarray: +def avgpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: """ This function is used to perform avgpooling on the input array of 2D matrix(image) - >>> avgpooling([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 2, 2) - array([[ 3., 5.], - [11., 13.]]) - - >>> avgpooling([[136,228,0,166,51],[148,42,89,146,230],[3,37,215,145,69],[83,43,20,22,80],[172,171,233,165,48]], 3, 1) - array([[ 99., 118., 123.], - [ 75., 84., 112.], - [108., 116., 110.]]) - Args: arr: numpy array size: size of pooling matrix stride: the number of pixels shifts over the input matrix Returns: numpy array of avgpooled matrix + Sample Input Output: + >>> avgpooling([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 2, 2) + array([[ 3., 5.], + [11., 13.]]) + >>> avgpooling([[147, 180, 122],[241, 76, 32],[126, 13, 157]], 2, 1) + array([[161., 102.], + [114., 69.]]) """ arr = np.array(arr) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix") i = 0 j = 0 - avg_mat_i = 0 - avg_mat_j = 0 + mat_i = 0 + mat_j = 0 # compute the shape of the output matrix avgpool_shape = (arr.shape[0] - size) // stride + 1 @@ -105,17 +102,17 @@ def avgpooling(arr:np.ndarray, size:int, stride:int) -> np.ndarray: if j + size > arr.shape[1]: break # compute the average of the pooling matrix - updated_arr[avg_mat_i][avg_mat_j] = int(np.average(arr[i:i + size, j:j + size])) + updated_arr[mat_i][mat_j] = int(np.average(arr[i:i + size, j:j + size])) # shift the pooling matrix by stride of column pixels j += stride - avg_mat_j += 1 + mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride - avg_mat_i += 1 + mat_i += 1 # reset the column index to 0 j = 0 - avg_mat_j = 0 + mat_j = 0 return updated_arr @@ -123,7 +120,7 @@ def avgpooling(arr:np.ndarray, size:int, stride:int) -> np.ndarray: # Main Function if __name__ == "__main__": # test the functions - testmod(name ='avgpooling', verbose = True) + testmod(name='avgpooling', verbose=True) # Loading the image image = Image.open("path_to_image") From 03340658348079080e0d86b112e318d25bc0bdc0 Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Tue, 16 Nov 2021 21:19:56 +0530 Subject: [PATCH 08/13] Update computer_vision/pooling_functions.py Co-authored-by: Christian Clauss --- computer_vision/pooling_functions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index 98145136afc6..10ad4e8690da 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -2,7 +2,6 @@ # Importing the libraries import numpy as np from PIL import Image -from doctest import testmod # Maxpooling Function From 6b8c1274ec8335102b9a04448cb0a156957a0a8d Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Tue, 16 Nov 2021 21:20:22 +0530 Subject: [PATCH 09/13] Update computer_vision/pooling_functions.py Co-authored-by: Christian Clauss --- computer_vision/pooling_functions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index 10ad4e8690da..4c243c0ca8c1 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -118,7 +118,8 @@ def avgpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: # Main Function if __name__ == "__main__": - # test the functions + from doctest import testmod + testmod(name='avgpooling', verbose=True) # Loading the image From aeaada0247e4dcd3aa8433df5c4d5cdef92b2e7f Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Tue, 16 Nov 2021 21:20:47 +0530 Subject: [PATCH 10/13] Update computer_vision/pooling_functions.py Co-authored-by: Christian Clauss --- computer_vision/pooling_functions.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index 4c243c0ca8c1..f94321861ce2 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -128,10 +128,7 @@ def avgpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix - # Parameters - size = 3 - stride = 2 - Image.fromarray(maxpooling(np.array(image), size, stride)).show() + Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix From 6d1f8064aa9e40c0b80517c047481a519cf6624c Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Tue, 16 Nov 2021 21:21:01 +0530 Subject: [PATCH 11/13] Update computer_vision/pooling_functions.py Co-authored-by: Christian Clauss --- computer_vision/pooling_functions.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index f94321861ce2..0a3641dd1f3e 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -133,7 +133,4 @@ def avgpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix - # Parameters - size = 3 - stride = 2 - Image.fromarray(avgpooling(np.array(image), size, stride)).show() + Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show() From 7b8eac6d1e50939990cd0a77b6bff2ee41c5a3ce Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Tue, 16 Nov 2021 21:26:33 +0530 Subject: [PATCH 12/13] Update pooling_functions.py --- computer_vision/pooling_functions.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index 0a3641dd1f3e..3e8a623441b2 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -3,9 +3,8 @@ import numpy as np from PIL import Image -# Maxpooling Function - +# Maxpooling Function def maxpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: """ This function is used to perform maxpooling on the input array of 2D matrix(image) @@ -28,8 +27,8 @@ def maxpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: raise ValueError("The input array is not a square matrix") i = 0 j = 0 - max_mat_i = 0 - max_mat_j = 0 + mat_i = 0 + mat_j = 0 # compute the shape of the output matrix maxpool_shape = (arr.shape[0] - size) // stride + 1 @@ -45,18 +44,18 @@ def maxpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix - updated_arr[max_mat_i][max_mat_j] = np.max(arr[i:i + size, j:j + size]) + updated_arr[mat_i][mat_j] = np.max(arr[i:i + size, j:j + size]) # shift the pooling matrix by stride of column pixels j += stride - max_mat_j += 1 + mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride - max_mat_i += 1 + mat_i += 1 # reset the column index to 0 j = 0 - max_mat_j = 0 + mat_j = 0 return updated_arr From eb776b95911a0bbb65a619b8fb29c617bbcdef05 Mon Sep 17 00:00:00 2001 From: Navaneeth Sharma <63489382+Navaneeth-Sharma@users.noreply.github.com> Date: Tue, 16 Nov 2021 21:48:05 +0530 Subject: [PATCH 13/13] Formatting pooling.py --- computer_vision/pooling_functions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/computer_vision/pooling_functions.py b/computer_vision/pooling_functions.py index 3e8a623441b2..09beabcba82d 100644 --- a/computer_vision/pooling_functions.py +++ b/computer_vision/pooling_functions.py @@ -44,7 +44,7 @@ def maxpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix - updated_arr[mat_i][mat_j] = np.max(arr[i:i + size, j:j + size]) + updated_arr[mat_i][mat_j] = np.max(arr[i : i + size, j : j + size]) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 @@ -100,7 +100,7 @@ def avgpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: if j + size > arr.shape[1]: break # compute the average of the pooling matrix - updated_arr[mat_i][mat_j] = int(np.average(arr[i:i + size, j:j + size])) + updated_arr[mat_i][mat_j] = int(np.average(arr[i : i + size, j : j + size])) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 @@ -119,7 +119,7 @@ def avgpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: if __name__ == "__main__": from doctest import testmod - testmod(name='avgpooling', verbose=True) + testmod(name="avgpooling", verbose=True) # Loading the image image = Image.open("path_to_image")