Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxuheng authored Dec 24, 2022
1 parent 9994040 commit fa9667a
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions nn/Pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -48,39 +51,40 @@ 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:
out_col += 1
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

0 comments on commit fa9667a

Please sign in to comment.