創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務器提供商,新人活動買多久送多久,劃算不套路!
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比定興網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式定興網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋定興地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。這期內(nèi)容當中小編將會給大家?guī)碛嘘Ppytorch實現(xiàn)unet網(wǎng)絡的方法,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
設計神經(jīng)網(wǎng)絡的一般步驟:
1. 設計框架
2. 設計骨干網(wǎng)絡
Unet網(wǎng)絡設計的步驟:
1. 設計Unet網(wǎng)絡工廠模式
2. 設計編解碼結(jié)構
3. 設計卷積模塊
4. unet實例模塊
Unet網(wǎng)絡最重要的特征:
1. 編解碼結(jié)構。
2. 解碼結(jié)構,比FCN更加完善,采用連接方式。
3. 本質(zhì)是一個框架,編碼部分可以使用很多圖像分類網(wǎng)絡。
示例代碼:
import torch import torch.nn as nn class Unet(nn.Module): #初始化參數(shù):Encoder,Decoder,bridge #bridge默認值為無,如果有參數(shù)傳入,則用該參數(shù)替換None def __init__(self,Encoder,Decoder,bridge = None): super(Unet,self).__init__() self.encoder = Encoder(encoder_blocks) self.decoder = Decoder(decoder_blocks) self.bridge = bridge def forward(self,x): res = self.encoder(x) out,skip = res[0],res[1,:] if bridge is not None: out = bridge(out) out = self.decoder(out,skip) return out #設計編碼模塊 class Encoder(nn.Module): def __init__(self,blocks): super(Encoder,self).__init__() #assert:斷言函數(shù),避免出現(xiàn)參數(shù)錯誤 assert len(blocks) > 0 #nn.Modulelist():模型列表,所有的參數(shù)可以納入網(wǎng)絡,但是沒有forward函數(shù) self.blocks = nn.Modulelist(blocks) def forward(self,x): skip = [] for i in range(len(self.blocks) - 1): x = self.blocks[i](x) skip.append(x) res = [self.block[i+1](x)] #列表之間可以通過+號拼接 res += skip return res #設計Decoder模塊 class Decoder(nn.Module): def __init__(self,blocks): super(Decoder, self).__init__() assert len(blocks) > 0 self.blocks = nn.Modulelist(blocks) def ceter_crop(self,skips,x): _,_,height1,width2 = skips.shape() _,_,height2,width3 = x.shape() #對圖像進行剪切處理,拼接的時候保持對應size參數(shù)一致 ht,wt = min(height1,height2),min(width2,width3) dh2 = (height1 - height2)//2 if height1 > height2 else 0 dw1 = (width2 - width3)//2 if width2 > width3 else 0 dh3 = (height2 - height1)//2 if height2 > height1 else 0 dw2 = (width3 - width2)//2 if width3 > width2 else 0 return skips[:,:,dh2:(dh2 + ht),dw1:(dw1 + wt)],\ x[:,:,dh3:(dh3 + ht),dw2 : (dw2 + wt)] def forward(self, skips,x,reverse_skips = True): assert len(skips) == len(blocks) - 1 if reverse_skips is True: skips = skips[: : -1] x = self.blocks[0](x) for i in range(1, len(self.blocks)): skip = skips[i-1] x = torch.cat(skip,x,1) x = self.blocks[i](x) return x #定義了一個卷積block def unet_convs(in_channels,out_channels,padding = 0): #nn.Sequential:與Modulelist相比,包含了forward函數(shù) return nn.Sequential( nn.Conv2d(in_channels, out_channels, kernal_size = 3, padding = padding, bias = False), nn.BatchNorm2d(outchannels), nn.ReLU(inplace = True), nn.Conv2d(in_channels, out_channels, kernal_size=3, padding=padding, bias=False), nn.BatchNorm2d(outchannels), nn.ReLU(inplace=True), ) #實例化Unet模型 def unet(in_channels,out_channels): encoder_blocks = [unet_convs(in_channels, 64),\ nn.Sequential(nn.Maxpool2d(kernal_size = 2, stride = 2, ceil_mode = True),\ unet_convs(64,128)), \ nn.Sequential(nn.Maxpool2d(kernal_size=2, stride=2, ceil_mode=True), \ unet_convs(128, 256)), nn.Sequential(nn.Maxpool2d(kernal_size=2, stride=2, ceil_mode=True), \ unet_convs(256, 512)), ] bridge = nn.Sequential(unet_convs(512, 1024)) decoder_blocks = [nn.conTranpose2d(1024, 512), \ nn.Sequential(unet_convs(1024, 512), nn.conTranpose2d(512, 256)),\ nn.Sequential(unet_convs(512, 256), nn.conTranpose2d(256, 128)), \ nn.Sequential(unet_convs(512, 256), nn.conTranpose2d(256, 128)), \ nn.Sequential(unet_convs(256, 128), nn.conTranpose2d(128, 64)) ] return Unet(encoder_blocks,decoder_blocks,bridge)