Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Outputtensor([[[[5., 5., 0., 8., 7., 8., 1., 1.],[5., 5., 0., 8., 7., 8., 1., 1.],[1., 1., 9., 5., 0., 7., 7., 7.],[6., 6., 0., 2., 4., 6., 6., 6.],[9., 9., 7., 6., 6., 8., 4., 4.],[8., 8., 3., 8., 5., 1., 3., 3.],[7., 7., 2., 7., 0., 1., 0., 0.],[7., 7., 2., 7., 0., 1., 0., 0.]]]])In reflection padding, it gets a bit trickier. It is like the outer columns and rows areused as axes for the reflection. So, the left padded column (forget about thecorners for now) reflects the second column (since the first column is the axis ofreflection). The same reasoning goes for the right padded column. Similarly, the toppadded row reflects the second row (since the first row is the axis of reflection),and the same reasoning goes for the bottom padded row. The values used in thereflection are in a darker shade of orange. The corners have the same values as theintersection of the reflected rows and columns of the original image. Hopefully,the image can convey the idea better than my words.In PyTorch, you can use the functional form F.pad() with mode="reflect", or usethe module version nn.ReflectionPad2d:reflection_padder = nn.ReflectionPad2d(padding=1)reflection_padder(image)Outputtensor([[[[9., 1., 9., 5., 0., 7., 7., 7.],[0., 5., 0., 8., 7., 8., 1., 8.],[9., 1., 9., 5., 0., 7., 7., 7.],[0., 6., 0., 2., 4., 6., 6., 6.],[7., 9., 7., 6., 6., 8., 4., 8.],[3., 8., 3., 8., 5., 1., 3., 1.],[2., 7., 2., 7., 0., 1., 0., 1.],[3., 8., 3., 8., 5., 1., 3., 1.]]]])In circular padding, the left-most (right-most) column gets copied as the right (left)Convolutions | 361

padded column (forget about the corners for now too). Similarly, the top-most(bottom-most) row gets copied as the bottom (top) padded row. The cornersreceive the values of the diametrically opposed corner: The top-left padded pixelreceives the value of the bottom-right corner of the original image. Once again, thevalues used in the padding are in a darker shade of orange.In PyTorch, you must use the functional form F.pad() with mode="circular" sincethere is no module version of the circular padding (at time of writing):F.pad(image, pad=(1, 1, 1, 1), mode='circular')Outputtensor([[[[0., 7., 2., 7., 0., 1., 0., 7.],[1., 5., 0., 8., 7., 8., 1., 5.],[7., 1., 9., 5., 0., 7., 7., 1.],[6., 6., 0., 2., 4., 6., 6., 6.],[4., 9., 7., 6., 6., 8., 4., 9.],[3., 8., 3., 8., 5., 1., 3., 8.],[0., 7., 2., 7., 0., 1., 0., 7.],[1., 5., 0., 8., 7., 8., 1., 5.]]]])By padding an image, it is possible to get resulting images with the same shape asinput images, or even larger, should you choose to stuff more and more rows andcolumns into the input image. Assuming we’re doing symmetrical padding of size p,the resulting shape is given by the formula below:Equation 5.4 - Shape after a convolution with stride and paddingWe’re basically extending the original dimensions by 2p pixels each.A REAL FilterEnough with the identity filter! Let’s try an edge detector [91] filter from traditionalcomputer vision for a change:362 | Chapter 5: Convolutions

Output

tensor([[[[5., 5., 0., 8., 7., 8., 1., 1.],

[5., 5., 0., 8., 7., 8., 1., 1.],

[1., 1., 9., 5., 0., 7., 7., 7.],

[6., 6., 0., 2., 4., 6., 6., 6.],

[9., 9., 7., 6., 6., 8., 4., 4.],

[8., 8., 3., 8., 5., 1., 3., 3.],

[7., 7., 2., 7., 0., 1., 0., 0.],

[7., 7., 2., 7., 0., 1., 0., 0.]]]])

In reflection padding, it gets a bit trickier. It is like the outer columns and rows are

used as axes for the reflection. So, the left padded column (forget about the

corners for now) reflects the second column (since the first column is the axis of

reflection). The same reasoning goes for the right padded column. Similarly, the top

padded row reflects the second row (since the first row is the axis of reflection),

and the same reasoning goes for the bottom padded row. The values used in the

reflection are in a darker shade of orange. The corners have the same values as the

intersection of the reflected rows and columns of the original image. Hopefully,

the image can convey the idea better than my words.

In PyTorch, you can use the functional form F.pad() with mode="reflect", or use

the module version nn.ReflectionPad2d:

reflection_padder = nn.ReflectionPad2d(padding=1)

reflection_padder(image)

Output

tensor([[[[9., 1., 9., 5., 0., 7., 7., 7.],

[0., 5., 0., 8., 7., 8., 1., 8.],

[9., 1., 9., 5., 0., 7., 7., 7.],

[0., 6., 0., 2., 4., 6., 6., 6.],

[7., 9., 7., 6., 6., 8., 4., 8.],

[3., 8., 3., 8., 5., 1., 3., 1.],

[2., 7., 2., 7., 0., 1., 0., 1.],

[3., 8., 3., 8., 5., 1., 3., 1.]]]])

In circular padding, the left-most (right-most) column gets copied as the right (left)

Convolutions | 361

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!