日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

PSPnet模型结构及实现代码

發(fā)布時間:2024/1/4 综合教程 21 生活家
生活随笔 收集整理的這篇文章主要介紹了 PSPnet模型结构及实现代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


該模塊融合了4種不同金字塔尺度的特征,第一行紅色是最粗糙的特征–全局池化生成單個bin輸出,后面三行是不同尺度的池化特征。

為了保證全局特征的權(quán)重,如果金字塔共有N個級別,則在每個級別后使用1×1 1×11×1的卷積將對于級別通道降為原本的1/N。再通過雙線性插值獲得未池化前的大小,最終concat到一起。












1 import torch 2 import torch.nn.functional as F 3 from torch import nn 4 from torchvision import models 5 6 from utils import initialize_weights 7 from utils.misc import Conv2dDeformable 8 from .config import res101_path 9 10 //金字塔模塊,將從前面卷積結(jié)構(gòu)提取的特征分別進(jìn)行不同的池化操作,得到不同感受野以及全局語境信息(或者叫做不同層級的信息) 11 class _PyramidPoolingModule(nn.Module): 12 def __init__(self, in_dim, reduction_dim, setting): 13 super(_PyramidPoolingModule, self).__init__() 14 self.features = [] 15 for s in setting: //對應(yīng)不同的池化操作,單個bin,多個bin 16 self.features.append(nn.Sequential( 17 nn.AdaptiveAvgPool2d(s), 18 nn.Conv2d(in_dim, reduction_dim, kernel_size=1, bias=False), 19 nn.BatchNorm2d(reduction_dim, momentum=.95), 20 nn.ReLU(inplace=True) 21 )) 22 self.features = nn.ModuleList(self.features) 23 24 def forward(self, x): 25 x_size = x.size() 26 out = [x] 27 for f in self.features: 28 out.append(F.upsample(f(x), x_size[2:], mode='bilinear')) 29 out = torch.cat(out, 1) 30 return out 31 32 //整個pspnet網(wǎng)絡(luò)的結(jié)構(gòu) 33 class PSPNet(nn.Module): 34 def __init__(self, num_classes, pretrained=True, use_aux=True): 35 super(PSPNet, self).__init__() 36 self.use_aux = use_aux 37 resnet = models.resnet101() //采用resnet101作為骨干模型,提取特征 38 if pretrained: 39 resnet.load_state_dict(torch.load(res101_path)) 40 self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool) 41 self.layer1, self.layer2, self.layer3, self.layer4 = resnet.layer1, resnet.layer2, resnet.layer3, resnet.layer4 42     //設(shè)置帶洞卷積的參數(shù)(dilation),以及卷積的參數(shù) 43 for n, m in self.layer3.named_modules(): 44 if 'conv2' in n: 45 m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1) 46 elif 'downsample.0' in n: 47 m.stride = (1, 1) 48 for n, m in self.layer4.named_modules(): 49 if 'conv2' in n: 50 m.dilation, m.padding, m.stride = (4, 4), (4, 4), (1, 1) 51 elif 'downsample.0' in n: 52 m.stride = (1, 1) 53     //加入ppm模塊,以及最后的連接層(卷積) 54 self.ppm = _PyramidPoolingModule(2048, 512, (1, 2, 3, 6)) 55 self.final = nn.Sequential( 56 nn.Conv2d(4096, 512, kernel_size=3, padding=1, bias=False), 57 nn.BatchNorm2d(512, momentum=.95), 58 nn.ReLU(inplace=True), 59 nn.Dropout(0.1), 60 nn.Conv2d(512, num_classes, kernel_size=1) 61 ) 62 63 if use_aux: 64 self.aux_logits = nn.Conv2d(1024, num_classes, kernel_size=1) 65 initialize_weights(self.aux_logits) 66 # 初始化權(quán)重 67 initialize_weights(self.ppm, self.final) 68 69 def forward(self, x): 70 x_size = x.size() 71 x = self.layer0(x) 72 x = self.layer1(x) 73 x = self.layer2(x) 74 x = self.layer3(x) 75 if self.training and self.use_aux: 76 aux = self.aux_logits(x) 77 x = self.layer4(x) 78 x = self.ppm(x) 79 x = self.final(x) 80 if self.training and self.use_aux: 81 return F.upsample(x, x_size[2:], mode='bilinear'), F.upsample(aux, x_size[2:], mode='bilinear') 82 return F.upsample(x, x_size[2:], mode='bilinear')

總結(jié)

以上是生活随笔為你收集整理的PSPnet模型结构及实现代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。