PyTorch中的padding(邊緣填充)操作

簡介

我們知道,在對圖像執行卷積操作時,如果不對圖像邊緣進行填充,卷積核將無法到達圖像邊緣的像素,而且卷積前後圖像的尺寸也會發生變化,這會造成許多麻煩。

因此現在各大深度學習框架的卷積層實現上基本都配備了padding操作,以保證圖像輸入輸出前後的尺寸大小不變。例如,若卷積核大小爲3x3,那麼就應該設定padding=1,即填充1層邊緣像素;若卷積核大小爲7x7,那麼就應該設定padding=3,填充3層邊緣像素;也就是padding大小一般設定爲核大小的一半。在pytorch的卷積層定義中,默認的padding爲零填充。

self.conv = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=7, padding=3)

 

padding的種類及其pytorch定義

padding,即邊緣填充,可以分爲四類:零填充常數填充鏡像填充重複填充

1.零填充

對圖像或者張量的邊緣進行補零填充操作:

class ZeroPad2d(ConstantPad2d):
    # Pads the input tensor boundaries with zero.
    def __init__(self, padding):
        super(ZeroPad2d, self).__init__(padding, 0)

2.常數填充

定義一個常數來對圖像或者張量的邊緣進行填充,若該常數等於0則等價於零填充。

class ConstantPad2d(_ConstantPadNd):
    # Pads the input tensor boundaries with a constant value.
    def __init__(self, padding, value):
        super(ConstantPad2d, self).__init__(value)
        self.padding = _quadruple(padding)

3.鏡像填充

對圖像或者張量的邊緣進行鏡像對稱的填充,示例如下:

>>> m = nn.ReflectionPad2d(2)
>>> input = torch.arange(9).reshape(1, 1, 3, 3)
>>> input

(0 ,0 ,.,.) =
  0  1  2
  3  4  5
  6  7  8
[torch.FloatTensor of size (1,1,3,3)]

>>> m(input)

(0 ,0 ,.,.) =
   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
class ReflectionPad2d(_ReflectionPadNd):
    # Pads the input tensor using the reflection of the input boundary.

    def __init__(self, padding):
        super(ReflectionPad2d, self).__init__()
        self.padding = _quadruple(padding)

4.重複填充

對圖像或者張量的邊緣進行重複填充,就是說直接用邊緣的像素值來填充。示例如下:

>>> m = nn.ReplicationPad2d(2)
>>> input = torch.arange(9).reshape(1, 1, 3, 3)
>>> input

(0 ,0 ,.,.) =
    0  1  2
    3  4  5
    6  7  8
[torch.FloatTensor of size (1,1,3,3)]

>>> m(input)

(0 ,0 ,.,.) =
    0   0   0   1   2   2   2
    0   0   0   1   2   2   2
    0   0   0   1   2   2   2
    3   3   3   4   5   5   5
    6   6   6   7   8   8   8
    6   6   6   7   8   8   8
    6   6   6   7   8   8   8
[torch.FloatTensor of size (1,1,7,7)]
class ReplicationPad2d(_ReplicationPadNd):
    # Pads the input tensor using replication of the input boundary.

    def __init__(self, padding):
        super(ReplicationPad2d, self).__init__()
        self.padding = _quadruple(padding)

 

實際應用

在許多計算機視覺任務中,例如圖像分類,zero padding已經能夠滿足要求。但是不結合實際地亂用也是不行的。比方說,在圖像增強/圖像生成領域,zero padding可能會導致邊緣出現僞影,如下所示:

可以清楚地看到圖像上邊緣的僞影

 

這時候,可以改用鏡像填充來代替零填充操作。我們定義一個新的padding層,然後把卷積層裏的padding參數置爲0.

具體寫法如下:

class DEMO(nn.Module):

    def __init__(self):
        super(DEMO, self).__init__()
        self.pad = nn.ReflectionPad2d(1)
        self.conv = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3, padding=0)

    def forward(self, x):
        x = self.pad(x)
        x = self.conv(x)
        return F.relu(x)

以低光照增強任務爲例,最終對比效果如下圖。零填充會產生邊緣僞影,而鏡像填充很好地緩解了這一效應。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章