網站首頁 編程語言 正文
Unet是一個最近比較火的網絡結構。它的理論已經有很多大佬在討論了。本文主要從實際操作的層面,講解pytorch從頭開始搭建UNet++的過程。
Unet++代碼
網絡架構
黑色部分是Backbone,是原先的UNet。
綠色箭頭為上采樣,藍色箭頭為密集跳躍連接。
綠色的模塊為密集連接塊,是經過左邊兩個部分拼接操作后組成的
Backbone
2個3x3的卷積,padding=1。
class VGGBlock(nn.Module):
def __init__(self, in_channels, middle_channels, out_channels):
super().__init__()
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(in_channels, middle_channels, 3, padding=1)
self.bn1 = nn.BatchNorm2d(middle_channels)
self.conv2 = nn.Conv2d(middle_channels, out_channels, 3, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
return out
上采樣
圖中的綠色箭頭,上采樣使用雙線性插值。
雙線性插值就是有兩個變量的插值函數的線性插值擴展,其核心思想是在兩個方向分別進行一次線性插值
torch.nn.Upsample(size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)
參數說明:
①size:可以用來指定輸出空間的大小,默認是None;
②scale_factor:比例因子,比如scale_factor=2意味著將輸入圖像上采樣2倍,默認是None;
③mode:用來指定上采樣算法,有’nearest’、 ‘linear’、‘bilinear’、‘bicubic’、‘trilinear’,默認是’nearest’。上采樣算法在本文中會有詳細理論進行講解;
④align_corners:如果True,輸入和輸出張量的角像素對齊,從而保留這些像素的值,默認是False。此處True和False的區別本文中會有詳細的理論講解;
⑤recompute_scale_factor:如果recompute_scale_factor是True,則必須傳入scale_factor并且scale_factor用于計算輸出大小。計算出的輸出大小將用于推斷插值的新比例。請注意,當scale_factor為浮點數時,由于舍入和精度問題,它可能與重新計算的scale_factor不同。如果recompute_scale_factor是False,那么size或scale_factor將直接用于插值。
class Up(nn.Module):
def __init__(self):
super().__init__()
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
def forward(self, x1, x2):
x1 = self.up(x1)
# input is CHW
diffY = torch.tensor([x2.size()[2] - x1.size()[2]])
diffX = torch.tensor([x2.size()[3] - x1.size()[3]])
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
diffY // 2, diffY - diffY // 2])
x = torch.cat([x2, x1], dim=1)
return x
下采樣
圖中的黑色箭頭,采用的是最大池化。
self.pool = nn.MaxPool2d(2, 2)
深度監督
所示,該結構下有4個分支,可以分為兩種模式。
精確模式:4個分支取平均值結果
快速模式:只選擇一個分支,其余被剪枝
if self.deep_supervision:
output1 = self.final1(x0_1)
output2 = self.final2(x0_2)
output3 = self.final3(x0_3)
output4 = self.final4(x0_4)
return [output1, output2, output3, output4]
else:
output = self.final(x0_4)
return output
網絡架構代碼
class NestedUNet(nn.Module):
def __init__(self, num_classes=1, input_channels=1, deep_supervision=False, **kwargs):
super().__init__()
nb_filter = [32, 64, 128, 256, 512]
self.deep_supervision = deep_supervision
self.pool = nn.MaxPool2d(2, 2)
self.up = Up()
self.conv0_0 = VGGBlock(input_channels, nb_filter[0], nb_filter[0])
self.conv1_0 = VGGBlock(nb_filter[0], nb_filter[1], nb_filter[1])
self.conv2_0 = VGGBlock(nb_filter[1], nb_filter[2], nb_filter[2])
self.conv3_0 = VGGBlock(nb_filter[2], nb_filter[3], nb_filter[3])
self.conv4_0 = VGGBlock(nb_filter[3], nb_filter[4], nb_filter[4])
self.conv0_1 = VGGBlock(nb_filter[0]+nb_filter[1], nb_filter[0], nb_filter[0])
self.conv1_1 = VGGBlock(nb_filter[1]+nb_filter[2], nb_filter[1], nb_filter[1])
self.conv2_1 = VGGBlock(nb_filter[2]+nb_filter[3], nb_filter[2], nb_filter[2])
self.conv3_1 = VGGBlock(nb_filter[3]+nb_filter[4], nb_filter[3], nb_filter[3])
self.conv0_2 = VGGBlock(nb_filter[0]*2+nb_filter[1], nb_filter[0], nb_filter[0])
self.conv1_2 = VGGBlock(nb_filter[1]*2+nb_filter[2], nb_filter[1], nb_filter[1])
self.conv2_2 = VGGBlock(nb_filter[2]*2+nb_filter[3], nb_filter[2], nb_filter[2])
self.conv0_3 = VGGBlock(nb_filter[0]*3+nb_filter[1], nb_filter[0], nb_filter[0])
self.conv1_3 = VGGBlock(nb_filter[1]*3+nb_filter[2], nb_filter[1], nb_filter[1])
self.conv0_4 = VGGBlock(nb_filter[0]*4+nb_filter[1], nb_filter[0], nb_filter[0])
if self.deep_supervision:
self.final1 = nn.Conv2d(nb_filter[0], num_classes, kernel_size=1)
self.final2 = nn.Conv2d(nb_filter[0], num_classes, kernel_size=1)
self.final3 = nn.Conv2d(nb_filter[0], num_classes, kernel_size=1)
self.final4 = nn.Conv2d(nb_filter[0], num_classes, kernel_size=1)
else:
self.final = nn.Conv2d(nb_filter[0], num_classes, kernel_size=1)
def forward(self, input):
x0_0 = self.conv0_0(input)
x1_0 = self.conv1_0(self.pool(x0_0))
x0_1 = self.conv0_1(self.up(x1_0, x0_0))
x2_0 = self.conv2_0(self.pool(x1_0))
x1_1 = self.conv1_1(self.up(x2_0, x1_0))
x0_2 = self.conv0_2(self.up(x1_1, torch.cat([x0_0, x0_1], 1)))
x3_0 = self.conv3_0(self.pool(x2_0))
x2_1 = self.conv2_1(self.up(x3_0, x2_0))
x1_2 = self.conv1_2(self.up(x2_1, torch.cat([x1_0, x1_1], 1)))
x0_3 = self.conv0_3(self.up(x1_2, torch.cat([x0_0, x0_1, x0_2], 1)))
x4_0 = self.conv4_0(self.pool(x3_0))
x3_1 = self.conv3_1(self.up(x4_0, x3_0))
x2_2 = self.conv2_2(self.up(x3_1, torch.cat([x2_0, x2_1], 1)))
x1_3 = self.conv1_3(self.up(x2_2, torch.cat([x1_0, x1_1, x1_2], 1)))
x0_4 = self.conv0_4(self.up(x1_3, torch.cat([x0_0, x0_1, x0_2, x0_3], 1)))
if self.deep_supervision:
output1 = self.final1(x0_1)
output2 = self.final2(x0_2)
output3 = self.final3(x0_3)
output4 = self.final4(x0_4)
return [output1, output2, output3, output4]
else:
output = self.final(x0_4)
return output
原文鏈接:https://blog.csdn.net/qq128252/article/details/127610581
相關推薦
- 2022-04-20 C語言數據結構與算法時間空間復雜度基礎實踐_C 語言
- 2022-04-29 python?tkinter實現學生信息管理系統_python
- 2022-11-17 Python操作MongoDB的教程詳解(插,查,改,排,刪)_python
- 2022-09-01 Nginx?部署的虛擬主機使用?Let's?Encrypt?加密?https的方法_nginx
- 2022-05-10 開發跨域問題的解決
- 2022-04-17 SpringBoot中,redis的key和value為基本類型時需要注意的
- 2024-01-08 Spring AOP 切面@Around注解的具體使用
- 2023-12-22 uni-app微信小程序分包
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支