From fa9667a24019242f76f7eb712d6b070abaaf8ebe Mon Sep 17 00:00:00 2001 From: xiaoxuheng <73049735+xiaoxuheng@users.noreply.github.com> Date: Sat, 24 Dec 2022 17:33:25 +0800 Subject: [PATCH] Add files via upload --- nn/Pooling.py | 66 +++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/nn/Pooling.py b/nn/Pooling.py index 924b126..199751a 100644 --- a/nn/Pooling.py +++ b/nn/Pooling.py @@ -2,36 +2,39 @@ import numpy as np -from Test_Parameter import Test_Parameter -from Module import Module +from .Parameter import Parameter +from .Module import Module class Pooling(Module): - def __init__(self, row, col, poolsize, poolstride, mode="max"): - self.parameter = Test_Parameter((row, col)) + def __init__(self, pool_size=2, mode="max"): self.inputs = [] self.data = None - self.poolsize = poolsize - self.poolstride = poolstride + self.pool_size = pool_size + self.pool_stride = pool_size self.mode = mode - self.maxpos = [] + self.max_pos = [] + self.gradient = None def forward(self, x): self.inputs.append(x) - if self.mode == "max": - self.data = self.pooling() + self.data = self.pooling() return self def backward(self, grad): - temp = copy.copy(self.inputs) - in_row, in_col = np.shape(self.inputs) - for i in range(0, in_row): - for j in range(0, in_col): - if self.maxpos.count((i, j)) == 0: - temp[i][j] = 0 - self.parameter.gradient = temp + self.gradient = copy.copy(self.inputs[0].data) + in_row, in_col = np.shape(self.inputs[0].data) + if self.mode == "max": + for i in range(0, in_row): + for j in range(0, in_col): + if self.max_pos.count((i, j)) == 0: + self.gradient[i][j] = 0 + elif self.mode == "mean": + for i in range(0, in_row): + for j in range(0, in_col): + self.gradient[i, j] = self.data[int(i/self.pool_size), int(j/self.pool_size)]/self.pool_size/self.pool_size if isinstance(self.inputs[0], Module): - self.inputs[0].backward(self.parameter.gradient) + self.inputs[0].backward(self.gradient) def __call__(self, x): return self.forward(x) @@ -48,11 +51,11 @@ def pooling(self): Padding mode - 'edge' """ # inputMap sizes - in_row, in_col = np.shape(self.inputs) + in_row, in_col = np.shape(self.inputs[0].data) # outputMap sizes - out_row, out_col = int(np.floor(in_row / self.poolstride)), int(np.floor(in_col / self.poolstride)) - row_remainder, col_remainder = np.mod(in_row, self.poolstride), np.mod(in_col, self.poolstride) + out_row, out_col = int(np.floor(in_row / self.pool_stride)), int(np.floor(in_col / self.pool_stride)) + row_remainder, col_remainder = np.mod(in_row, self.pool_stride), np.mod(in_col, self.pool_stride) if row_remainder != 0: out_row += 1 if col_remainder != 0: @@ -60,27 +63,28 @@ def pooling(self): outputMap = np.zeros((out_row, out_col)) # padding - temp_map = np.lib.pad(self.inputs, ((0, self.poolsize - row_remainder), (0, self.poolsize - col_remainder)), 'edge') + temp_map = np.lib.pad(self.inputs[0].data, ((0, self.pool_size - row_remainder), (0, self.pool_size - col_remainder)), 'edge') - # max pooling + # pooling if self.mode == "max": for r_idx in range(0, out_row): for c_idx in range(0, out_col): - startX = c_idx * self.poolstride - startY = r_idx * self.poolstride - poolField = temp_map[startY:startY + self.poolsize, startX:startX + self.poolsize] + startX = c_idx * self.pool_stride + startY = r_idx * self.pool_stride + poolField = temp_map[startY:startY + self.pool_size, startX:startX + self.pool_size] poolOut = np.max(poolField) - temp = np.where(self.inputs == poolOut) - self.maxpos.append((temp[0][0], temp[1][0])) + temp = np.where(temp_map[startY:startY + self.pool_size, startX:startX + self.pool_size] == poolOut) + self.max_pos.append((startY+temp[0][0], startX+temp[1][0])) outputMap[r_idx, c_idx] = poolOut elif self.mode == "mean": for r_idx in range(0, out_row): for c_idx in range(0, out_col): - startX = c_idx * self.poolstride - startY = r_idx * self.poolstride - poolField = temp_map[startY:startY + self.poolsize, startX:startX + self.poolsize] + startX = c_idx * self.pool_stride + startY = r_idx * self.pool_stride + poolField = temp_map[startY:startY + self.pool_size, startX:startX + self.pool_size] poolOut = np.sum(poolField) - outputMap[r_idx, c_idx] = poolOut / self.poolsize / self.poolsize + mean_value = poolOut / self.pool_size / self.pool_size + outputMap[r_idx, c_idx] = mean_value # retrun outputMap return outputMap