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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use max;
use ;
// `AvgPool2dSame`
//
// # Reference Python
//
// ```python,ignore
// # Calculate asymmetric TensorFlow-like 'SAME' padding for a convolution
// def get_same_padding(x: int, kernel_size: int, stride: int, dilation: int):
// if isinstance(x, torch.Tensor):
// return torch.clamp(((x / stride).ceil() - 1) * stride + (kernel_size - 1) * dilation + 1 - x, min=0)
// else:
// return max((math.ceil(x / stride) - 1) * stride + (kernel_size - 1) * dilation + 1 - x, 0)
//
// # Dynamically pad input x with 'SAME' padding for conv with specified args
// def pad_same(
// x,
// kernel_size: List[int],
// stride: List[int],
// dilation: List[int] = (1, 1),
// value: float = 0,
// ):
// ih, iw = x.size()[-2:]
// pad_h = get_same_padding(ih, kernel_size[0], stride[0], dilation[0])
// pad_w = get_same_padding(iw, kernel_size[1], stride[1], dilation[1])
// x = F.pad(x, (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2), value=value)
// return x
//
//
// def avg_pool2d_same(
// x: torch.Tensor,
// kernel_size: List[int],
// stride: List[int],
// padding: List[int] = (0, 0),
// ceil_mode: bool = False,
// count_include_pad: bool = True,
// ):
// # FIXME how to deal with count_include_pad vs not for external padding?
// x = pad_same(x, kernel_size, stride)
// return F.avg_pool2d(x, kernel_size, stride, (0, 0), ceil_mode, count_include_pad)
//
// class AvgPool2d(nn.Module):
// def __init__(
// self,
// kernel_size: _size_2_t,
// stride: Optional[_size_2_t] = None,
// padding: _size_2_t = 0,
// ceil_mode: bool = False,
// count_include_pad: bool = True,
// divisor_override: Optional[int] = None,
// ) -> None:
// super().__init__()
// self.kernel_size = kernel_size
// self.stride = stride if (stride is not None) else kernel_size
// self.padding = padding
// self.ceil_mode = ceil_mode
// self.count_include_pad = count_include_pad
// self.divisor_override = divisor_override
//
// def forward(self, input: Tensor) -> Tensor:
// return F.avg_pool2d(
// input,
// self.kernel_size,
// self.stride,
// self.padding,
// self.ceil_mode,
// self.count_include_pad,
// self.divisor_override,
// )
//
// class AvgPool2dSame(nn.AvgPool2d):
// """Tensorflow like 'SAME' wrapper for 2D average pooling."""
// def __init__(
// self,
// kernel_size: _size_2_t,
// stride: Optional[_size_2_t] = None,
// padding: _size_2_t = 0,
// ceil_mode=False,
// count_include_pad=True,
// ):
// super(AvgPool2dSame, self).__init__(
// kernel_size=kernel_size,
// stride=stride,
// padding=(0, 0), # padding is dropped, is this a bug?
// ceil_mode=ceil_mode,
// count_include_pad=count_include_pad,
// )
//
// def forward(self, x):
// x = pad_same(x, self.kernel_size, self.stride)
// return F.avg_pool2d(
// x,
// self.kernel_size,
// self.stride,
// self.padding,
// self.ceil_mode,
// self.count_include_pad,
// )
// ```
/// [`AvgPool2dSame`] Configuration.
/// `AvgPool2dSame` Layer.
/// Calculate asymmetric TensorFlow-like 'SAME' padding for a convolution.
/// Dynamically pad input x with 'SAME' padding for conv with specified args.