ReflectionPad2d

nn.ReflectionPad2d(padding)

  • padding: (int or tuple) padding的尺寸,如果是int,使用相同的padding填充边界。如果是(tuple),按照**(left, right, top, bottom)**的顺序来填充

以矩阵边缘的元素为对称轴,将矩阵中的元素对称的填充到最外围

  • input: (N, C, Hi, Wi) or (C, Hi, Wi)
  • output: (N, C, Ho, Wo) or (C, Ho, Wo)

实例

注意:如果镜像填充的数据比原来的数据多,就会报错。比如原来H=3,pad如果是5,就会报错

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
import torch
import torch.nn as nn

input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
print(input)
'''
tensor([[[[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]]]])
'''

m1 = nn.ReflectionPad2d(2)
print(m1(input))
'''
tensor([[[[8., 7., 6., 7., 8., 7., 6.],
[5., 4., 3., 4., 5., 4., 3.],
[2., 1., 0., 1., 2., 1., 0.],
[5., 4., 3., 4., 5., 4., 3.],
[8., 7., 6., 7., 8., 7., 6.],
[5., 4., 3., 4., 5., 4., 3.],
[2., 1., 0., 1., 2., 1., 0.]]]])
'''

m2 = nn.ReflectionPad2d((1, 1, 2, 0))
print(m2(input))
'''
tensor([[[[7., 6., 7., 8., 7.],
[4., 3., 4., 5., 4.],
[1., 0., 1., 2., 1.],
[4., 3., 4., 5., 4.],
[7., 6., 7., 8., 7.]]]])
'''

m3 = nn.ReflectionPad2d((5, 0, 0, 0))
print(m3(input)) # wrong
'''
RuntimeError: Argument #4: Padding size should be less than the corresponding
input dimension, but got: padding (5, 0) at dimension 3 of input 4
'''
Error: Not Found