Pytorch Four Boundary Filling Methods (Padding)

1. Fill before choosing convolution (strongly recommended)

Young students highly recommend that you do not use the filling method of convolution anymore, which is simple but has too many defects.It is not possible to decide the boundary with different filling from the top to the bottom, and the boundary with different filling from the left to the right according to your own needs; and it is easy to get artifacts in the case of zero boundary filling, which has a greater impact on the experimental results.It is better to replace Padding in convolution with Padding before convolution. Four padding methods are listed below (zero padding, constant padding, mirror padding, copy padding).

You won't need to talk about younger babies. Please look down on the guest officers~~

2. Zero fill for boundary fill

Zero filling is a special case of constant filling, which is similar to filling in convolution, but is more flexible than convolution filling, so we can fill the corresponding 0 element up, down, left, right, and so on.

2.1 Code

 1 import torch
 2 import torch.nn as nn
 3 
 4 
 5 # ==================  Zero Fill  ==================
 6 def conv_ZeroPad2d():
 7     # Define a four-dimensional data:(batchSize, channel, height, width)
 8     data = torch.tensor([[[[1, 2, 3],
 9                            [4, 5, 6],
10                            [7, 8, 9]]]]).float()
11     print("data_shape: ", data.shape)
12     print("data: ", data)
13     # Zero Fill, Fill at Boundary n 0,They are left, right, top and bottom
14     ZeroPad = nn.ZeroPad2d(padding=(1, 2, 1, 2))
15     data1 = ZeroPad(data)
16     print("data1_shape: ", data1.shape)
17     print("data1: ", data1)
18 
19 
20 if __name__ == '__main__':
21     conv_ZeroPad2d()

2.2 Results

You can see that one column of 0 elements is filled on the left, two columns of 0 elements on the right, one column of 0 elements on the top, and two columns of 0 elements on the bottom.

          

3. Constant Filling for Boundary Filling

Constant padding, which allows you to fill the specified elements up, down, left, and right as you want.

3.1 Code

 1 import torch
 2 
 3 
 4 # ================== Constant Fill ==================
 5 def conv_ConstantPad2d():
 6     # Define a four-dimensional data:(batchSize, channel, height, width)
 7     data = torch.tensor([[[[1, 2, 3],
 8                            [4, 5, 6],
 9                            [7, 8, 9]]]]).float()
10     print("data_shape: ", data.shape)
11     print("data: ", data)
12     # For a given paper fill, the 0 fill is a column of constant light fills: left, right, top, bottom
13     ConstantPad = nn.ConstantPad2d(padding=(1, 2, 1, 2), value=10)
14     data1 = ConstantPad(data)
15     print("data1_shape: ", data1.shape)
16     print("data1: ", data1)
17 
18 
19 if __name__ == '__main__':
20     conv_ConstantPad2d()

 

3.2 Results

You can see that there is one column with 10 elements on the left, two columns with 10 elements on the right, one column with 10 elements on the top, and two columns with 10 elements on the bottom.

       

4. Mirror Filling with Boundary Filling

Mirror filling is based on symmetry.

4.1 Code

 1 import torch 
 2 
 3 
 4 # ================== Mirror Fill ==================
 5 def conv_ReflectionPad2d():
 6     # Define a four-dimensional data:(batchSize, channel, height, width)
 7     data = torch.tensor([[[[1, 2, 3],
 8                            [4, 5, 6],
 9                            [7, 8, 9]]]]).float()
10     print("data_shape: ", data.shape)
11     print("data: ", data)
12     # Copy Boundary n Second, left, right, top and bottom
13     ReflectionPad = nn.ReflectionPad2d(padding=(1, 2, 1, 2))
14     data1 = ReflectionPad(data)
15     print("data1_shape: ", data1.shape)
16     print("data1: ", data1)
17 
18 
19 if __name__ == '__main__':
20     conv_ReflectionPad2d()

4.2 Results

 

5. Duplicate Filling of Boundary Filling

Duplicate filling is highly recommended by younger students.Copy filling is the filling by copying the outermost boundary elements so that the filling elements are close to the boundary elements and have minimal impact on the experimental results.

5.1 Code

 1 import torch
 2 
 3 
 4 # ================== Repeat Fill ==================
 5 def conv_ReplicationPad2d():
 6     # Define a four-dimensional data:(batchSize, channel, height, width)
 7     data = torch.tensor([[[[1, 2, 3],
 8                            [4, 5, 6],
 9                            [7, 8, 9]]]]).float()
10     print("data_shape: ", data.shape)
11     print("data: ", data)
12     # Fill with symmetrical pixels,They are left, right, top and bottom
13     ReplicationPad = nn.ReplicationPad2d(padding=(1, 2, 1, 2))
14     data1 = ReplicationPad(data)
15     print("data1_shape: ", data1.shape)
16     print("data1: ", data1)
17 
18 
19 if __name__ == '__main__':
20     conv_ReplicationPad2d()

5.2 Results

You can see that the filled element is the same as the outermost boundary element.

  

6. Summary

Efforts to love everyone around you do not necessarily pay off, but they do not!Give street performers money and not bargain with the vendors who are still laying out late at night.May my blog help you (*^^*) (*^^*)!

If the guest officer likes the kid's garden, remember to pay attention to the kid. The kid will keep updating (#^. ^#)(#^. ^#)!

Tags: Python

Posted on Sun, 10 May 2020 12:55:07 -0400 by warran