forked from lixiang-ucas/FSODM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pooling.py
60 lines (46 loc) · 1.9 KB
/
pooling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import torch
import numpy as np
from torch.nn.modules.module import Module
from torch.nn.modules.utils import _single, _pair, _triple
from torch.nn import functional as F
class GlobalMaxPool2d(Module):
def __init__(self, stride=None, padding=0, dilation=1,
return_indices=False, ceil_mode=False):
super(GlobalMaxPool2d, self).__init__()
self.stride = stride or 1
self.padding = padding
self.dilation = dilation
self.return_indices = return_indices
self.ceil_mode = ceil_mode
def extra_repr(self):
return 'global max pooling, stride={stride}, padding={padding}' \
', dilation={dilation}, ceil_mode={ceil_mode}'.format(**self.__dict__)
def forward(self, input):
kernel_size = input.size(-1)
return F.max_pool2d(input, kernel_size, self.stride,
self.padding, self.dilation, self.ceil_mode,
self.return_indices)
class GlobalAvgPool2d(Module):
def __init__(self, stride=None, padding=0, dilation=1,
return_indices=False, ceil_mode=False):
super(GlobalAvgPool2d, self).__init__()
self.stride = stride or 1
self.padding = padding
self.dilation = dilation
self.return_indices = return_indices
self.ceil_mode = ceil_mode
def extra_repr(self):
return 'global avg pooling'
def forward(self, input):
# kernel_size = input.size(-1)
return F.adaptive_avg_pool2d(input, 1)
class Split(Module):
def __init__(self, splits):
super(Split, self).__init__()
self.splits = splits
def extra_repr(self):
return 'split layer, splits={splits}'.format(**self.__dict__)
def forward(self, input):
splits = np.cumsum([0] + self.splits)
xs = [input[:,splits[i]:splits[i+1],:,:].contiguous() for i in range(len(splits) - 1)]
return xs