Skip to content

Commit

Permalink
Create my_pooling.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dongliangchang authored Feb 11, 2020
1 parent f2761ba commit 972eede
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions my_pooling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import torch
import numpy as np
import random
from torch.autograd import Variable
from torch.nn.modules.module import Module
from torch.nn.modules.utils import _single, _pair, _triple
import torch.nn.functional as F
from torch.nn.parameter import Parameter



class my_MaxPool2d(Module):


def __init__(self, kernel_size, stride=None, padding=0, dilation=1,
return_indices=False, ceil_mode=False):
super(my_MaxPool2d, self).__init__()
self.kernel_size = kernel_size
self.stride = stride or kernel_size
self.padding = padding
self.dilation = dilation
self.return_indices = return_indices
self.ceil_mode = ceil_mode

def forward(self, input):
input = input.transpose(3,1)


input = F.max_pool2d(input, self.kernel_size, self.stride,
self.padding, self.dilation, self.ceil_mode,
self.return_indices)
input = input.transpose(3,1).contiguous()

return input

def __repr__(self):
kh, kw = _pair(self.kernel_size)
dh, dw = _pair(self.stride)
padh, padw = _pair(self.padding)
dilh, dilw = _pair(self.dilation)
padding_str = ', padding=(' + str(padh) + ', ' + str(padw) + ')' \
if padh != 0 or padw != 0 else ''
dilation_str = (', dilation=(' + str(dilh) + ', ' + str(dilw) + ')'
if dilh != 0 and dilw != 0 else '')
ceil_str = ', ceil_mode=' + str(self.ceil_mode)
return self.__class__.__name__ + '(' \
+ 'kernel_size=(' + str(kh) + ', ' + str(kw) + ')' \
+ ', stride=(' + str(dh) + ', ' + str(dw) + ')' \
+ padding_str + dilation_str + ceil_str + ')'


class my_AvgPool2d(Module):
def __init__(self, kernel_size, stride=None, padding=0, ceil_mode=False,
count_include_pad=True):
super(my_AvgPool2d, self).__init__()
self.kernel_size = kernel_size
self.stride = stride or kernel_size
self.padding = padding
self.ceil_mode = ceil_mode
self.count_include_pad = count_include_pad

def forward(self, input):
input = input.transpose(3,1)
input = F.avg_pool2d(input, self.kernel_size, self.stride,
self.padding, self.ceil_mode, self.count_include_pad)
input = input.transpose(3,1).contiguous()

return input


def __repr__(self):
return self.__class__.__name__ + '(' \
+ 'kernel_size=' + str(self.kernel_size) \
+ ', stride=' + str(self.stride) \
+ ', padding=' + str(self.padding) \
+ ', ceil_mode=' + str(self.ceil_mode) \
+ ', count_include_pad=' + str(self.count_include_pad) + ')'


m = my_MaxPool2d((1, 32), stride=(1, 32))
input = Variable(torch.randn(3, 2208, 7, 7))
output = m(input)
print(output.size())

0 comments on commit 972eede

Please sign in to comment.