1. 程式人生 > 其它 >pytorch之torch.nn.Conv2d()函式詳解

pytorch之torch.nn.Conv2d()函式詳解

文章目錄

一、官方文件介紹

nn.Conv2d:對由多個輸入平面(多通道)組成的輸入訊號進行二維卷積

二、torch.nn.Conv2d()函式詳解

引數詳解
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
引數引數型別
in_channels int Number of channels in the input image 輸入影象通道數
out_channels int Number of channels produced by the convolution 卷積產生的通道數
kernel_size (int or tuple) Size of the convolving kernel 卷積核尺寸,可以設為1個int型數或者一個(int, int)型的元組。例如(2,3)是高2寬3卷積核
stride (int or tuple, optional) Stride of the convolution. Default: 1
卷積步長,預設為1。可以設為1個int型數或者一個(int, int)型的元組。
padding (int or tuple, optional) Zero-padding added to both sides of the input. Default: 0 填充操作,控制padding_mode的數目。
padding_mode (string, optional) ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’ padding模式,預設為Zero-padding 。
dilation (int or tuple, optional)
Spacing between kernel elements. Default: 1 擴張操作:控制kernel點(卷積核點)的間距,預設值:1。
groups (int, optional) Number of blocked connections from input channels to output channels. Default: 1 group引數的作用是控制分組卷積,預設不分組,為1組。
bias (bool, optional) If True, adds a learnable bias to the output. Default: True 為真,則在輸出中新增一個可學習的偏差。預設:True。

引數dilation——擴張卷積(也叫空洞卷積)

dilation操作動圖演示如下:
Dilated Convolution with a 3 x 3 kernel and dilation rate 2
擴張卷積核為3×3,擴張率為2

引數groups——分組卷積

Group Convolution顧名思義,則是對輸入feature map進行分組,然後每組分別卷積。假設輸入feature map的尺寸仍為C ∗ H ∗ W ,輸出feature map的數量為N個,如果設定要分成G個groups,則每組的輸入feature map數量為C G \frac{C}{G}GC,每組的輸出feature map數量為N G \frac{N}{G}GN,每個卷積核的尺寸為C G ∗ K ∗ K \frac{C}{G}∗K∗KGCKK,卷積核的總數仍為N個,每組的卷積核數量為N G \frac{N}{G}GN,卷積核只與其同組的輸入map進行卷積,卷積核的總引數量為N ∗ C G ∗ K ∗ K N∗\frac{C}{G}∗K∗KNGCKK,可見,總引數量減少為原來的1 G \frac{1}{G}G1,其連線方式如下圖右所示,group1輸出map數為2,有2個卷積核,每個卷積核的channel數為4,與group1的輸入map的channel數相同,卷積核只與同組的輸入map卷積,而不與其他組的輸入map卷積。

三、程式碼例項

import torch

x = torch.randn(3,1,5,4)
print(x)

conv = torch.nn.Conv2d(1,4,(2,3))
res = conv(x)

print(res.shape)    # torch.Size([3, 4, 4, 2])
輸入:x[ batch_size, channels, height_1, width_1 ]
batch_size,一個batch中樣本的個數 3
channels,通道數,也就是當前層的深度 1
height_1, 圖片的高 5
width_1, 圖片的寬 4

卷積操作:Conv2d[ channels, output, height_2, width_2 ]
channels,通道數,和上面保持一致,也就是當前層的深度 1
output ,輸出的深度 4【需要4個filter】
height_2,卷積核的高 2
width_2,卷積核的寬 3

輸出:res[ batch_size,output, height_3, width_3 ]
batch_size,,一個batch中樣例的個數,同上 3
output, 輸出的深度 4
height_3, 卷積結果的高度 4
width_3,卷積結果的寬度 2

一個樣本卷積示例:

親測有效!!!