Pytorch实现U-net细胞分割
生活随笔
收集整理的這篇文章主要介紹了
Pytorch实现U-net细胞分割
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網絡結構
U-net是MICCAI2015的專門針對醫學影像分割設計的網絡結構,直到今天還是很好用,效果任然很好。網絡的左半部分是Encode
部分,此部分將輸入圖像逐步下采樣得到高層的語義信息。右半部分是Decoder部分,將Encode提取的語義特征逐步恢復為原始圖像的尺寸,中間是跳躍連接,將下采樣過程中提取到的不同level的特征加入到Decoder中,得到更好的分割結果。
數據集介紹
數據集使用的是ISBI細胞分割數據集,訓練集總共就三十張圖像,少的可憐,U-net比其他網絡強大的地方就在此,小數據集上也能得到很不錯的結果。
應該是將細胞邊界給分割出來。
U-net網絡代碼:
# !/usr/bin/python3 # -*- coding:utf-8 -*- # Author:WeiFeng Liu # @Time: 2021/12/9 下午12:58import torch import torch.nn as nn import torch.nn.functional as Fclass DoubleConv(nn.Module):"""unet的編碼器中,每一個level都會有兩層卷積和Relu"""def __init__(self, in_channels, out_channels):super(DoubleConv,self).__init__()self.double_conv = nn.Sequential(nn.Conv2d(in_channels, out_channels,kernel_size=3,padding=1),nn.BatchNorm2d(out_channels),nn.ReLU(inplace=True),nn.Conv2d(out_channels,out_channels,kernel_size=3,padding=1),nn.BatchNorm2d(out_channels),nn.ReLU(inplace=True),)def forward(self,x):return self.double_conv(x)class downsample(nn.Module):"""下采樣 maxpool + DoubleConv"""def __init__(self, in_channels, out_channels):super(downsample,self).__init__()self.maxpool_conv = nn.Sequential(nn.MaxPool2d(2),#feature map 減半DoubleConv(in_channels,out_channels),)def forward(self, x):return self.maxpool_conv(x)class upsample(nn.Module):"""upsample, 使用雙線性插值或者反卷積"""def __init__(self, in_channels,out_channels,bilinear = True):super(upsample,self).__init__()if bilinear:self.upsample = nn.Upsample(scale_factor=2, mode='bilinear',align_corners=True)else:self.upsample = nn.ConvTranspose2d(in_channels//2, out_channels//2,kernel_size=2,stride=2)self.conv = DoubleConv(in_channels,out_channels)def forward(self,x1,x2):""":param x1: decoder feature:param x2: encoder feature:return:"""x1 = self.upsample(x1)diff_y = torch.tensor([x2.size()[2] - x1.size()[2]])diff_x = torch.tensor([x2.size()[3] - x1.size()[3]])#將x1與x2的特征圖對齊后concatx1 = F.pad(x1, [diff_x//2,diff_x - diff_x//2,diff_y//2,diff_y - diff_y // 2])x = torch.cat([x2,x1],dim=1)return self.conv(x)class output_conv(nn.Module):def __init__(self,in_channels,out_channels):super(output_conv, self).__init__()self.conv = nn.Conv2d(in_channels,out_channels,kernel_size=1)def forward(self,x):return self.conv(x)class UNET(nn.Module):def __init__(self,n_channels,n_classes,bilinear = True):""":param n_channels: input channel:param n_classes: segmentation classes:param bilinear: upsample tpye"""super(UNET,self).__init__()self.n_channels = n_channelsself.n_classes = n_classesself.bilinear = bilinearself.init = DoubleConv(n_channels,64)self.downsample1 = downsample(64,128)self.downsample2 = downsample(128,256)self.downsample3 = downsample(256,512)self.downsample4 = downsample(512,512)self.upsample1 = upsample(1024,256,bilinear)self.upsample2 = upsample(512,128,bilinear)self.upsample3 = upsample(256,64,bilinear)self.upsample4 = upsample(128,64,bilinear)self.outconv = output_conv(64,n_classes)def forward(self,x):x1 = self.init(x)x2 = self.downsample1(x1)x3 = self.downsample2(x2)x4 = self.downsample3(x3)x5 = self.downsample4(x4)x = self.upsample1(x5,x4)x = self.upsample2(x, x3)x = self.upsample3(x,x2)x = self.upsample4(x, x1)res = self.outconv(x)return res完整訓練代碼和數據可以去我的github上面下載:https://github.com/SPECTRELWF/Semantic-Segmentation
個人主頁:liuweifeng.top:8090
測試結果:
三十張圖像訓練30個epoch得到的結果,還是很好的。
總結
以上是生活随笔為你收集整理的Pytorch实现U-net细胞分割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OPENCV-PYTHON将.GIF格式
- 下一篇: Pytorch实现U-net视网膜血管分