|
1 | 1 | from .module import Module |
2 | 2 | from .utils import _quadruple, _ntuple |
3 | | -from .._functions.padding import ConstantPad2d as F_ConstantPad2d |
| 3 | +from .. import functional as F |
4 | 4 |
|
5 | 5 | # TODO: grad_output size asserts in THNN |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class ReflectionPad2d(Module): |
| 9 | + r"""Pads the input tensor using the reflection of the input boundary. |
| 10 | +
|
| 11 | + Args: |
| 12 | + padding (int, tuple): the size of the padding. If is int, uses the same |
| 13 | + padding in all boundaries. If a 4-tuple, uses (paddingLeft, paddingRight, paddingTop, paddingBottom) |
| 14 | +
|
| 15 | + Shape: |
| 16 | + - Input: :math:`(N, C, H_{in}, W_{in})` |
| 17 | + - Output: :math:`(N, C, H_{out}, W_{out})` where |
| 18 | + :math:`H_{out} = H_{in} + paddingTop + paddingBottom` |
| 19 | + :math:`W_{out} = W_{in} + paddingLeft + paddingRight` |
| 20 | +
|
| 21 | + Examples:: |
| 22 | +
|
| 23 | + >>> m = nn.ReflectionPad2d(3) |
| 24 | + >>> input = autograd.Variable(torch.randn(16, 3, 320, 480)) |
| 25 | + >>> output = m(input) |
| 26 | + >>> # using different paddings |
| 27 | + >>> m = nn.ReflectionPad2d((3, 3, 6, 6)) |
| 28 | + >>> output = m(input) |
| 29 | +
|
| 30 | + """ |
9 | 31 |
|
10 | 32 | def __init__(self, padding): |
11 | 33 | super(ReflectionPad2d, self).__init__() |
12 | 34 | self.padding = _quadruple(padding) |
13 | 35 |
|
14 | 36 | def forward(self, input): |
15 | | - return self._backend.ReflectionPad2d(*self.padding)(input) |
| 37 | + return F.pad(input, self.padding, 'reflect') |
16 | 38 |
|
17 | 39 | def __repr__(self): |
18 | 40 | return self.__class__.__name__ + ' ' + str(self.padding) |
19 | 41 |
|
20 | 42 |
|
21 | 43 | class ReplicationPad2d(Module): |
| 44 | + r"""Pads the input tensor using replication of the input boundary. |
| 45 | +
|
| 46 | + Args: |
| 47 | + padding (int, tuple): the size of the padding. If is int, uses the same |
| 48 | + padding in all boundaries. If a 4-tuple, uses (paddingLeft, paddingRight, paddingTop, paddingBottom) |
| 49 | +
|
| 50 | + Shape: |
| 51 | + - Input: :math:`(N, C, H_{in}, W_{in})` |
| 52 | + - Output: :math:`(N, C, H_{out}, W_{out})` where |
| 53 | + :math:`H_{out} = H_{in} + paddingTop + paddingBottom` |
| 54 | + :math:`W_{out} = W_{in} + paddingLeft + paddingRight` |
| 55 | +
|
| 56 | + Examples:: |
| 57 | +
|
| 58 | + >>> m = nn.ReplicationPad2d(3) |
| 59 | + >>> input = autograd.Variable(torch.randn(16, 3, 320, 480)) |
| 60 | + >>> output = m(input) |
| 61 | + >>> # using different paddings |
| 62 | + >>> m = nn.ReplicationPad2d((3, 3, 6, 6)) |
| 63 | + >>> output = m(input) |
| 64 | +
|
| 65 | + """ |
22 | 66 |
|
23 | 67 | def __init__(self, padding): |
24 | 68 | super(ReplicationPad2d, self).__init__() |
25 | 69 | self.padding = _quadruple(padding) |
26 | 70 |
|
27 | 71 | def forward(self, input): |
28 | | - return self._backend.ReplicationPad2d(*self.padding)(input) |
| 72 | + return F.pad(input, self.padding, 'replicate') |
29 | 73 |
|
30 | 74 | def __repr__(self): |
31 | 75 | return self.__class__.__name__ + ' ' + str(self.padding) |
32 | 76 |
|
33 | 77 |
|
34 | 78 | class ReplicationPad3d(Module): |
| 79 | + r"""Pads the input tensor using replication of the input boundary. |
| 80 | +
|
| 81 | + Args: |
| 82 | + padding (int, tuple): the size of the padding. If is int, uses the same |
| 83 | + padding in all boundaries. If a 6-tuple, uses (paddingLeft, paddingRight, |
| 84 | + paddingTop, paddingBottom, paddingFront, paddingBack) |
| 85 | +
|
| 86 | + Shape: |
| 87 | + - Input: :math:`(N, C, D_{in}, H_{in}, W_{in})` |
| 88 | + - Output: :math:`(N, C, D_{out}, H_{out}, W_{out})` where |
| 89 | + :math:`D_{out} = D_{in} + paddingFront + paddingBack` |
| 90 | + :math:`H_{out} = H_{in} + paddingTop + paddingBottom` |
| 91 | + :math:`W_{out} = W_{in} + paddingLeft + paddingRight` |
| 92 | +
|
| 93 | + Examples:: |
| 94 | +
|
| 95 | + >>> m = nn.ReplicationPad3d(3) |
| 96 | + >>> input = autograd.Variable(torch.randn(16, 3, 8, 320, 480)) |
| 97 | + >>> output = m(input) |
| 98 | + >>> # using different paddings |
| 99 | + >>> m = nn.ReplicationPad3d((3, 3, 6, 6, 1, 1)) |
| 100 | + >>> output = m(input) |
| 101 | +
|
| 102 | + """ |
35 | 103 |
|
36 | 104 | def __init__(self, padding): |
37 | 105 | super(ReplicationPad3d, self).__init__() |
38 | 106 | self.padding = _ntuple(6)(padding) |
39 | 107 |
|
40 | 108 | def forward(self, input): |
41 | | - return self._backend.ReplicationPad3d(*self.padding)(input) |
| 109 | + return F.pad(input, self.padding, 'replicate') |
42 | 110 |
|
43 | 111 | def __repr__(self): |
44 | 112 | return self.__class__.__name__ + ' ' + str(self.padding) |
45 | 113 |
|
46 | 114 |
|
47 | 115 | class ZeroPad2d(Module): |
| 116 | + r"""Pads the input tensor boundaries with zero. |
| 117 | +
|
| 118 | + Args: |
| 119 | + padding (int, tuple): the size of the padding. If is int, uses the same |
| 120 | + padding in all boundaries. If a 4-tuple, uses (paddingLeft, paddingRight, paddingTop, paddingBottom) |
| 121 | +
|
| 122 | + Shape: |
| 123 | + - Input: :math:`(N, C, H_{in}, W_{in})` |
| 124 | + - Output: :math:`(N, C, H_{out}, W_{out})` where |
| 125 | + :math:`H_{out} = H_{in} + paddingTop + paddingBottom` |
| 126 | + :math:`W_{out} = W_{in} + paddingLeft + paddingRight` |
| 127 | +
|
| 128 | + Examples:: |
| 129 | +
|
| 130 | + >>> m = nn.ZeroPad2d(3) |
| 131 | + >>> input = autograd.Variable(torch.randn(16, 3, 320, 480)) |
| 132 | + >>> output = m(input) |
| 133 | + >>> # using different paddings |
| 134 | + >>> m = nn.ZeroPad2d((3, 3, 6, 6)) |
| 135 | + >>> output = m(input) |
| 136 | +
|
| 137 | + """ |
48 | 138 |
|
49 | 139 | def __init__(self, padding): |
50 | 140 | super(ZeroPad2d, self).__init__() |
51 | 141 | self.padding = _quadruple(padding) |
52 | 142 |
|
53 | 143 | def forward(self, input): |
54 | | - return F_ConstantPad2d(pad=self.padding, value=0)(input) |
| 144 | + return F.pad(input, self.padding, 'constant', 0) |
55 | 145 |
|
56 | 146 | def __repr__(self): |
57 | 147 | return self.__class__.__name__ + ' ' + str(self.padding) |
58 | 148 |
|
59 | 149 |
|
60 | 150 | class ConstantPad2d(Module): |
| 151 | + r"""Pads the input tensor boundaries with a constant value. |
| 152 | +
|
| 153 | + Args: |
| 154 | + padding (int, tuple): the size of the padding. If is int, uses the same |
| 155 | + padding in all boundaries. If a 4-tuple, uses (paddingLeft, paddingRight, paddingTop, paddingBottom) |
| 156 | +
|
| 157 | + Shape: |
| 158 | + - Input: :math:`(N, C, H_{in}, W_{in})` |
| 159 | + - Output: :math:`(N, C, H_{out}, W_{out})` where |
| 160 | + :math:`H_{out} = H_{in} + paddingTop + paddingBottom` |
| 161 | + :math:`W_{out} = W_{in} + paddingLeft + paddingRight` |
| 162 | +
|
| 163 | + Examples:: |
| 164 | +
|
| 165 | + >>> m = nn.ConstantPad2d(3, 3.5) |
| 166 | + >>> input = autograd.Variable(torch.randn(16, 3, 320, 480)) |
| 167 | + >>> output = m(input) |
| 168 | + >>> # using different paddings |
| 169 | + >>> m = nn.ConstantPad2d((3, 3, 6, 6), 3.5) |
| 170 | + >>> output = m(input) |
| 171 | +
|
| 172 | + """ |
61 | 173 |
|
62 | 174 | def __init__(self, padding, value): |
63 | 175 | super(ConstantPad2d, self).__init__() |
64 | 176 | self.padding = _quadruple(padding) |
65 | 177 | self.value = value |
66 | 178 |
|
67 | 179 | def forward(self, input): |
68 | | - return F_ConstantPad2d(pad=self.padding, value=self.value)(input) |
| 180 | + return F.pad(input, self.padding, 'constant', self.value) |
69 | 181 |
|
70 | 182 | def __repr__(self): |
71 | 183 | return self.__class__.__name__ + ' ' + str(self.padding) |
0 commit comments