您好,登錄后才能下訂單哦!
這篇文章主要講解了“F.conv2d pytorch卷積計(jì)算方式是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“F.conv2d pytorch卷積計(jì)算方式是什么”吧!
Pytorch里一般小寫(xiě)的都是函數(shù)式的接口,相應(yīng)的大寫(xiě)的是類(lèi)式接口。
函數(shù)式的更加low-level一些,如果不需要做特別復(fù)雜的配置只需要用類(lèi)式接口就夠了。
nn.Conved是2D卷積層,而F.conv2d是2D卷積操作。
import torch from torch.nn import functional as F """手動(dòng)定義卷積核(weight)和偏置""" w = torch.rand(16, 3, 5, 5) # 16種3通道的5乘5卷積核 b = torch.rand(16) # 和卷積核種類(lèi)數(shù)保持一致(不同通道共用一個(gè)bias) """定義輸入樣本""" x = torch.randn(1, 3, 28, 28) # 1張3通道的28乘28的圖像 """2D卷積得到輸出""" out = F.conv2d(x, w, b, stride=1, padding=1) # 步長(zhǎng)為1,外加1圈padding,即上下左右各補(bǔ)了1圈的0, print(out.shape) out = F.conv2d(x, w, b, stride=2, padding=2) # 步長(zhǎng)為2,外加2圈padding print(out.shape) out = F.conv2d(x, w) # 步長(zhǎng)為1,默認(rèn)不padding, 不夠的舍棄,所以對(duì)于28*28的圖片來(lái)說(shuō),算完之后變成了24*24 print(out.shape)
在DSSINet發(fā)現(xiàn)又用到了空洞卷積dilated convolution
mu1 = F.conv2d(img1, window , padding=padd, dilation=dilation, groups=channel)
Dilated/Atrous convolution或者是convolution with holes從字面上就很好理解,是在標(biāo)準(zhǔn)的convolution map里注入空洞,以此來(lái)增加感受野reception field。
相比原來(lái)的正常卷積,空洞卷積多了一個(gè)超參數(shù)dilation rate,指的是kernel的間隔數(shù)量(正常的卷積是dilation rate=1)
正常圖像的卷積為
空洞卷積為
現(xiàn)在我們?cè)賮?lái)看下卷積本身,并了解他背后的設(shè)計(jì)直覺(jué),以下主要探討空洞卷積在語(yǔ)義分割(semantic segmentation)的應(yīng)用。
1、up-sampling/pooling layer(e.g. bilinear interpolation) is deterministic(not learnable)
2、內(nèi)部數(shù)據(jù)結(jié)構(gòu)丟失,空間層級(jí)化信息丟失。
3、小物體信息無(wú)法重建(假設(shè)有4個(gè)pooling layer,則任何小于2^4=16 pixel的物體信息將理論上無(wú)法重建)
在這樣問(wèn)題的存在下,語(yǔ)義分割問(wèn)題一直處于瓶頸期無(wú)法再明顯提高精度,而dilated convolution 的設(shè)計(jì)就良好的避免了這些問(wèn)題。
對(duì)于dilated convolution,我們已經(jīng)可以發(fā)現(xiàn)他的優(yōu)點(diǎn),即內(nèi)部數(shù)據(jù)結(jié)構(gòu)的保留和避免使用down_sampling這樣的特性。但是完全基于dilated convolution的結(jié)構(gòu)如何設(shè)計(jì)則是一個(gè)新的問(wèn)題。
pytorch中空洞卷積分為兩類(lèi),一類(lèi)是正常圖像的卷積,另一類(lèi)是池化時(shí)候。
空洞卷積的目的是為了在擴(kuò)大感受野的同時(shí),不降低圖片分辨率和不引入額外參數(shù)及計(jì)算量(一般在CNN中擴(kuò)大感受野都需要使用S》1的conv或者pooling,導(dǎo)致分辨率降低,不利于segmentation,如果使用大卷積核,確實(shí)可以達(dá)到增大感受野,但是會(huì)引入額外的參數(shù)及計(jì)算量)。
import torch import torch.nn.functional as F # 小括號(hào)里面有幾個(gè)[]就代表是幾維數(shù)據(jù) input = torch.tensor([[1,2,0,3,1], [0,1,2,3,1], [1,2,1,0,0], [5,2,3,1,1], [2,1,0,1,1]]) kernel = torch.tensor([[1,2,1], [0,1,0], [2,1,0]]) input = torch.reshape(input,(1,1,5,5)) kernel = torch.reshape(kernel,(1,1,3,3)) # stride代表的是步長(zhǎng)的意思,即每次卷積核向左或者向下移動(dòng)多少步進(jìn)行相乘 # 因?yàn)閏onv2d的input和weight對(duì)應(yīng)的tensor是[batch,channel,h,w],所以上述才將它們進(jìn)行reshape output = F.conv2d(input,kernel,stride=1) print(output) output = F.conv2d(input,kernel,stride=2) print(output) # padding代表的是向上下左右填充的行列數(shù),里面數(shù)字填寫(xiě)0 output3 = F.conv2d(input,kernel,stride=1,padding=1) print(output3)
import torch import torchvision from torch.utils.data import DataLoader from torch import nn from torch.nn import Conv2d from torch.utils.tensorboard import SummaryWriter dataset = torchvision.datasets.CIFAR10('./torchvision_dataset', train=False, download=False, transform=torchvision.transforms.ToTensor()) # 準(zhǔn)備好數(shù)據(jù)集就放在dataloader中進(jìn)行加載 dataloader = DataLoader(dataset, batch_size=64) # 開(kāi)始定義一個(gè)卷積類(lèi) class Zkl(nn.Module): def __init__(self): super(Zkl, self).__init__() self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0) def forward(self,x): x = self.conv1(x) return x writer = SummaryWriter("nn_conv2d") zkl = Zkl() # print(zkl) step = 0 for data in dataloader: imgs,target = data output = zkl(imgs) #print(imgs.shape) #print(output.shape) writer.add_images('nn_conv2d_input',imgs,step) #因?yàn)檩敵鍪?個(gè)通道,tensorboard無(wú)法解析,所以需要reshape三個(gè)通道 output = torch.reshape(output,(-1,3,30,30)) writer.add_images('nn_conv2d_output',output,step) step+=1 writer.close()
感謝各位的閱讀,以上就是“F.conv2d pytorch卷積計(jì)算方式是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)F.conv2d pytorch卷積計(jì)算方式是什么這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。