同步操作将从 July1921/Algorithms-Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# Source : https://computersciencewiki.org/index.php/Max-pooling_/_Pooling# Importing the librariesimport numpy as npfrom PIL import Image# Maxpooling Functiondef 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:arr: numpy arraysize: size of pooling matrixstride: the number of pixels shifts over the input matrixReturns:numpy array of maxpooled matrixSample 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([[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]:raise ValueError("The input array is not a square matrix")i = 0j = 0mat_i = 0mat_j = 0# compute the shape of the output matrixmaxpool_shape = (arr.shape[0] - size) // stride + 1# initialize the output matrix with zeros of shape maxpool_shapeupdated_arr = np.zeros((maxpool_shape, maxpool_shape))while i < arr.shape[0]:if i + size > arr.shape[0]:# if the end of the matrix is reached, breakbreakwhile j < arr.shape[1]:# if the end of the matrix is reached, breakif j + size > arr.shape[1]:break# compute the maximum of the pooling matrixupdated_arr[mat_i][mat_j] = np.max(arr[i : i + size, j : j + size])# shift the pooling matrix by stride of column pixelsj += stridemat_j += 1# shift the pooling matrix by stride of row pixelsi += stridemat_i += 1# reset the column index to 0j = 0mat_j = 0return updated_arr# Averagepooling Functiondef 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)Args:arr: numpy arraysize: size of pooling matrixstride: the number of pixels shifts over the input matrixReturns:numpy array of avgpooled matrixSample 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 = 0j = 0mat_i = 0mat_j = 0# compute the shape of the output matrixavgpool_shape = (arr.shape[0] - size) // stride + 1# initialize the output matrix with zeros of shape avgpool_shapeupdated_arr = np.zeros((avgpool_shape, avgpool_shape))while i < arr.shape[0]:# if the end of the matrix is reached, breakif i + size > arr.shape[0]:breakwhile j < arr.shape[1]:# if the end of the matrix is reached, breakif j + size > arr.shape[1]:break# compute the average of the pooling matrixupdated_arr[mat_i][mat_j] = int(np.average(arr[i : i + size, j : j + size]))# shift the pooling matrix by stride of column pixelsj += stridemat_j += 1# shift the pooling matrix by stride of row pixelsi += stridemat_i += 1# reset the column index to 0j = 0mat_j = 0return updated_arr# Main Functionif __name__ == "__main__":from doctest import testmodtestmod(name="avgpooling", verbose=True)# Loading the imageimage = Image.open("path_to_image")# Converting the image to numpy array and maxpooling, displaying the result# Ensure that the image is a square matrixImage.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 matrixImage.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。