专业行业网站开发报价,长沙市网站推广哪家专业,大连网站制作公司费用多少,网站设计与制作前景第一步#xff1a;准备数据
头发分割数据#xff0c;总共有10276张图片#xff0c;里面的像素值为0和1#xff0c;所以看起来全部是黑的#xff0c;不影响使用 第二步#xff1a;搭建模型
级联模式
通常多个类似U-Net按顺序堆叠#xff0c;以建立级联模型#xff0c…第一步准备数据
头发分割数据总共有10276张图片里面的像素值为0和1所以看起来全部是黑的不影响使用 第二步搭建模型
级联模式
通常多个类似U-Net按顺序堆叠以建立级联模型并可归纳为(Uxn-Net)n是重复U-Net模块的数目带来的问题是计算和内存开销被n放大了。如DocUNet, CU-Net网络等如下图所示为DocUNet网络的构成 U型嵌套模式
作者提出一种不同的U型结构叠加模型。我们的指数表示法是指嵌套的U型结构而不是级联叠加。理论上可以将指数n设为任意正整数实现单级或多级嵌套U型结构。但是嵌套层太多的体系结构过于复杂无法在实际中实现和应用。
我们将n设为2来构建U2-Net是一个两层嵌套的U型结构如图5所示。它的顶层是一个由11 stages图5中的立方体组成的大U型结构每一stage由一个配置良好的RSU填充。因此嵌套的U结构可以更有效的提取stage内的多尺度特征和聚集阶段的多层次特征。 第三步代码
1损失函数为交叉熵损失函数
2网络代码
import torch
import torch.nn as nn
from torchvision import models
import torch.nn.functional as Fclass REBNCONV(nn.Module):def __init__(self,in_ch3,out_ch3,dirate1):super(REBNCONV,self).__init__()self.conv_s1 nn.Conv2d(in_ch,out_ch,3,padding1*dirate,dilation1*dirate)self.bn_s1 nn.BatchNorm2d(out_ch)self.relu_s1 nn.ReLU(inplaceTrue)def forward(self,x):hx xxout self.relu_s1(self.bn_s1(self.conv_s1(hx)))return xout## upsample tensor src to have the same spatial size with tensor tar
def _upsample_like(src,tar):src F.upsample(src,sizetar.shape[2:],modebilinear)return src### RSU-7 ###
class RSU7(nn.Module):#UNet07DRES(nn.Module):def __init__(self, in_ch3, mid_ch12, out_ch3):super(RSU7,self).__init__()self.rebnconvin REBNCONV(in_ch,out_ch,dirate1)self.rebnconv1 REBNCONV(out_ch,mid_ch,dirate1)self.pool1 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv2 REBNCONV(mid_ch,mid_ch,dirate1)self.pool2 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv3 REBNCONV(mid_ch,mid_ch,dirate1)self.pool3 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv4 REBNCONV(mid_ch,mid_ch,dirate1)self.pool4 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv5 REBNCONV(mid_ch,mid_ch,dirate1)self.pool5 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv6 REBNCONV(mid_ch,mid_ch,dirate1)self.rebnconv7 REBNCONV(mid_ch,mid_ch,dirate2)self.rebnconv6d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv5d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv4d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv3d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv2d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv1d REBNCONV(mid_ch*2,out_ch,dirate1)def forward(self,x):hx xhxin self.rebnconvin(hx)hx1 self.rebnconv1(hxin)hx self.pool1(hx1)hx2 self.rebnconv2(hx)hx self.pool2(hx2)hx3 self.rebnconv3(hx)hx self.pool3(hx3)hx4 self.rebnconv4(hx)hx self.pool4(hx4)hx5 self.rebnconv5(hx)hx self.pool5(hx5)hx6 self.rebnconv6(hx)hx7 self.rebnconv7(hx6)hx6d self.rebnconv6d(torch.cat((hx7,hx6),1))hx6dup _upsample_like(hx6d,hx5)hx5d self.rebnconv5d(torch.cat((hx6dup,hx5),1))hx5dup _upsample_like(hx5d,hx4)hx4d self.rebnconv4d(torch.cat((hx5dup,hx4),1))hx4dup _upsample_like(hx4d,hx3)hx3d self.rebnconv3d(torch.cat((hx4dup,hx3),1))hx3dup _upsample_like(hx3d,hx2)hx2d self.rebnconv2d(torch.cat((hx3dup,hx2),1))hx2dup _upsample_like(hx2d,hx1)hx1d self.rebnconv1d(torch.cat((hx2dup,hx1),1))return hx1d hxin### RSU-6 ###
class RSU6(nn.Module):#UNet06DRES(nn.Module):def __init__(self, in_ch3, mid_ch12, out_ch3):super(RSU6,self).__init__()self.rebnconvin REBNCONV(in_ch,out_ch,dirate1)self.rebnconv1 REBNCONV(out_ch,mid_ch,dirate1)self.pool1 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv2 REBNCONV(mid_ch,mid_ch,dirate1)self.pool2 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv3 REBNCONV(mid_ch,mid_ch,dirate1)self.pool3 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv4 REBNCONV(mid_ch,mid_ch,dirate1)self.pool4 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv5 REBNCONV(mid_ch,mid_ch,dirate1)self.rebnconv6 REBNCONV(mid_ch,mid_ch,dirate2)self.rebnconv5d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv4d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv3d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv2d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv1d REBNCONV(mid_ch*2,out_ch,dirate1)def forward(self,x):hx xhxin self.rebnconvin(hx)hx1 self.rebnconv1(hxin)hx self.pool1(hx1)hx2 self.rebnconv2(hx)hx self.pool2(hx2)hx3 self.rebnconv3(hx)hx self.pool3(hx3)hx4 self.rebnconv4(hx)hx self.pool4(hx4)hx5 self.rebnconv5(hx)hx6 self.rebnconv6(hx5)hx5d self.rebnconv5d(torch.cat((hx6,hx5),1))hx5dup _upsample_like(hx5d,hx4)hx4d self.rebnconv4d(torch.cat((hx5dup,hx4),1))hx4dup _upsample_like(hx4d,hx3)hx3d self.rebnconv3d(torch.cat((hx4dup,hx3),1))hx3dup _upsample_like(hx3d,hx2)hx2d self.rebnconv2d(torch.cat((hx3dup,hx2),1))hx2dup _upsample_like(hx2d,hx1)hx1d self.rebnconv1d(torch.cat((hx2dup,hx1),1))return hx1d hxin### RSU-5 ###
class RSU5(nn.Module):#UNet05DRES(nn.Module):def __init__(self, in_ch3, mid_ch12, out_ch3):super(RSU5,self).__init__()self.rebnconvin REBNCONV(in_ch,out_ch,dirate1)self.rebnconv1 REBNCONV(out_ch,mid_ch,dirate1)self.pool1 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv2 REBNCONV(mid_ch,mid_ch,dirate1)self.pool2 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv3 REBNCONV(mid_ch,mid_ch,dirate1)self.pool3 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv4 REBNCONV(mid_ch,mid_ch,dirate1)self.rebnconv5 REBNCONV(mid_ch,mid_ch,dirate2)self.rebnconv4d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv3d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv2d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv1d REBNCONV(mid_ch*2,out_ch,dirate1)def forward(self,x):hx xhxin self.rebnconvin(hx)hx1 self.rebnconv1(hxin)hx self.pool1(hx1)hx2 self.rebnconv2(hx)hx self.pool2(hx2)hx3 self.rebnconv3(hx)hx self.pool3(hx3)hx4 self.rebnconv4(hx)hx5 self.rebnconv5(hx4)hx4d self.rebnconv4d(torch.cat((hx5,hx4),1))hx4dup _upsample_like(hx4d,hx3)hx3d self.rebnconv3d(torch.cat((hx4dup,hx3),1))hx3dup _upsample_like(hx3d,hx2)hx2d self.rebnconv2d(torch.cat((hx3dup,hx2),1))hx2dup _upsample_like(hx2d,hx1)hx1d self.rebnconv1d(torch.cat((hx2dup,hx1),1))return hx1d hxin### RSU-4 ###
class RSU4(nn.Module):#UNet04DRES(nn.Module):def __init__(self, in_ch3, mid_ch12, out_ch3):super(RSU4,self).__init__()self.rebnconvin REBNCONV(in_ch,out_ch,dirate1)self.rebnconv1 REBNCONV(out_ch,mid_ch,dirate1)self.pool1 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv2 REBNCONV(mid_ch,mid_ch,dirate1)self.pool2 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.rebnconv3 REBNCONV(mid_ch,mid_ch,dirate1)self.rebnconv4 REBNCONV(mid_ch,mid_ch,dirate2)self.rebnconv3d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv2d REBNCONV(mid_ch*2,mid_ch,dirate1)self.rebnconv1d REBNCONV(mid_ch*2,out_ch,dirate1)def forward(self,x):hx xhxin self.rebnconvin(hx)hx1 self.rebnconv1(hxin)hx self.pool1(hx1)hx2 self.rebnconv2(hx)hx self.pool2(hx2)hx3 self.rebnconv3(hx)hx4 self.rebnconv4(hx3)hx3d self.rebnconv3d(torch.cat((hx4,hx3),1))hx3dup _upsample_like(hx3d,hx2)hx2d self.rebnconv2d(torch.cat((hx3dup,hx2),1))hx2dup _upsample_like(hx2d,hx1)hx1d self.rebnconv1d(torch.cat((hx2dup,hx1),1))return hx1d hxin### RSU-4F ###
class RSU4F(nn.Module):#UNet04FRES(nn.Module):def __init__(self, in_ch3, mid_ch12, out_ch3):super(RSU4F,self).__init__()self.rebnconvin REBNCONV(in_ch,out_ch,dirate1)self.rebnconv1 REBNCONV(out_ch,mid_ch,dirate1)self.rebnconv2 REBNCONV(mid_ch,mid_ch,dirate2)self.rebnconv3 REBNCONV(mid_ch,mid_ch,dirate4)self.rebnconv4 REBNCONV(mid_ch,mid_ch,dirate8)self.rebnconv3d REBNCONV(mid_ch*2,mid_ch,dirate4)self.rebnconv2d REBNCONV(mid_ch*2,mid_ch,dirate2)self.rebnconv1d REBNCONV(mid_ch*2,out_ch,dirate1)def forward(self,x):hx xhxin self.rebnconvin(hx)hx1 self.rebnconv1(hxin)hx2 self.rebnconv2(hx1)hx3 self.rebnconv3(hx2)hx4 self.rebnconv4(hx3)hx3d self.rebnconv3d(torch.cat((hx4,hx3),1))hx2d self.rebnconv2d(torch.cat((hx3d,hx2),1))hx1d self.rebnconv1d(torch.cat((hx2d,hx1),1))return hx1d hxin##### U^2-Net ####
class U2NET(nn.Module):def __init__(self,in_ch3,out_ch1):super(U2NET,self).__init__()self.stage1 RSU7(in_ch,32,64)self.pool12 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage2 RSU6(64,32,128)self.pool23 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage3 RSU5(128,64,256)self.pool34 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage4 RSU4(256,128,512)self.pool45 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage5 RSU4F(512,256,512)self.pool56 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage6 RSU4F(512,256,512)# decoderself.stage5d RSU4F(1024,256,512)self.stage4d RSU4(1024,128,256)self.stage3d RSU5(512,64,128)self.stage2d RSU6(256,32,64)self.stage1d RSU7(128,16,64)self.side1 nn.Conv2d(64,out_ch,3,padding1)self.side2 nn.Conv2d(64,out_ch,3,padding1)self.side3 nn.Conv2d(128,out_ch,3,padding1)self.side4 nn.Conv2d(256,out_ch,3,padding1)self.side5 nn.Conv2d(512,out_ch,3,padding1)self.side6 nn.Conv2d(512,out_ch,3,padding1)self.outconv nn.Conv2d(6,out_ch,1)def forward(self,x):hx x#stage 1hx1 self.stage1(hx)hx self.pool12(hx1)#stage 2hx2 self.stage2(hx)hx self.pool23(hx2)#stage 3hx3 self.stage3(hx)hx self.pool34(hx3)#stage 4hx4 self.stage4(hx)hx self.pool45(hx4)#stage 5hx5 self.stage5(hx)hx self.pool56(hx5)#stage 6hx6 self.stage6(hx)hx6up _upsample_like(hx6,hx5)#-------------------- decoder --------------------hx5d self.stage5d(torch.cat((hx6up,hx5),1))hx5dup _upsample_like(hx5d,hx4)hx4d self.stage4d(torch.cat((hx5dup,hx4),1))hx4dup _upsample_like(hx4d,hx3)hx3d self.stage3d(torch.cat((hx4dup,hx3),1))hx3dup _upsample_like(hx3d,hx2)hx2d self.stage2d(torch.cat((hx3dup,hx2),1))hx2dup _upsample_like(hx2d,hx1)hx1d self.stage1d(torch.cat((hx2dup,hx1),1))#side outputd1 self.side1(hx1d)d2 self.side2(hx2d)d2 _upsample_like(d2,d1)d3 self.side3(hx3d)d3 _upsample_like(d3,d1)d4 self.side4(hx4d)d4 _upsample_like(d4,d1)d5 self.side5(hx5d)d5 _upsample_like(d5,d1)d6 self.side6(hx6)d6 _upsample_like(d6,d1)d0 self.outconv(torch.cat((d1,d2,d3,d4,d5,d6),1))#return F.sigmoid(d0), F.sigmoid(d1), F.sigmoid(d2), F.sigmoid(d3), F.sigmoid(d4), F.sigmoid(d5), F.sigmoid(d6)return d0, d1, d2, d3, d4, d5, d6class U2NET_half(nn.Module):def __init__(self,in_ch3,out_ch1):super(U2NET_half,self).__init__()self.stage1 RSU7(in_ch,16,32)self.pool12 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage2 RSU6(32,16,64)self.pool23 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage3 RSU5(64,32,128)self.pool34 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage4 RSU4(128,64,256)self.pool45 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage5 RSU4F(256,128,256)self.pool56 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage6 RSU4F(256,128,256)# decoderself.stage5d RSU4F(512,128,256)self.stage4d RSU4(512,64,128)self.stage3d RSU5(256,32,64)self.stage2d RSU6(128,16,32)self.stage1d RSU7(64,8,32)self.side1 nn.Conv2d(32,out_ch,3,padding1)self.side2 nn.Conv2d(32,out_ch,3,padding1)self.side3 nn.Conv2d(64,out_ch,3,padding1)self.side4 nn.Conv2d(128,out_ch,3,padding1)self.side5 nn.Conv2d(256,out_ch,3,padding1)self.side6 nn.Conv2d(256,out_ch,3,padding1)self.outconv nn.Conv2d(6,out_ch,1)def forward(self,x):hx x#stage 1hx1 self.stage1(hx)hx self.pool12(hx1)#stage 2hx2 self.stage2(hx)hx self.pool23(hx2)#stage 3hx3 self.stage3(hx)hx self.pool34(hx3)#stage 4hx4 self.stage4(hx)hx self.pool45(hx4)#stage 5hx5 self.stage5(hx)hx self.pool56(hx5)#stage 6hx6 self.stage6(hx)hx6up _upsample_like(hx6,hx5)#-------------------- decoder --------------------hx5d self.stage5d(torch.cat((hx6up,hx5),1))hx5dup _upsample_like(hx5d,hx4)hx4d self.stage4d(torch.cat((hx5dup,hx4),1))hx4dup _upsample_like(hx4d,hx3)hx3d self.stage3d(torch.cat((hx4dup,hx3),1))hx3dup _upsample_like(hx3d,hx2)hx2d self.stage2d(torch.cat((hx3dup,hx2),1))hx2dup _upsample_like(hx2d,hx1)hx1d self.stage1d(torch.cat((hx2dup,hx1),1))#side outputd1 self.side1(hx1d)d2 self.side2(hx2d)d2 _upsample_like(d2,d1)d3 self.side3(hx3d)d3 _upsample_like(d3,d1)d4 self.side4(hx4d)d4 _upsample_like(d4,d1)d5 self.side5(hx5d)d5 _upsample_like(d5,d1)d6 self.side6(hx6)d6 _upsample_like(d6,d1)d0 self.outconv(torch.cat((d1,d2,d3,d4,d5,d6),1))#return F.sigmoid(d0), F.sigmoid(d1), F.sigmoid(d2), F.sigmoid(d3), F.sigmoid(d4), F.sigmoid(d5), F.sigmoid(d6)return d0, d1, d2, d3, d4, d5, d6### U^2-Net small ###
class U2NETP(nn.Module):def __init__(self,in_ch3,out_ch1):super(U2NETP,self).__init__()self.stage1 RSU7(in_ch,16,64)self.pool12 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage2 RSU6(64,16,64)self.pool23 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage3 RSU5(64,16,64)self.pool34 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage4 RSU4(64,16,64)self.pool45 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage5 RSU4F(64,16,64)self.pool56 nn.MaxPool2d(2,stride2,ceil_modeTrue)self.stage6 RSU4F(64,16,64)# decoderself.stage5d RSU4F(128,16,64)self.stage4d RSU4(128,16,64)self.stage3d RSU5(128,16,64)self.stage2d RSU6(128,16,64)self.stage1d RSU7(128,16,64)self.side1 nn.Conv2d(64,out_ch,3,padding1)self.side2 nn.Conv2d(64,out_ch,3,padding1)self.side3 nn.Conv2d(64,out_ch,3,padding1)self.side4 nn.Conv2d(64,out_ch,3,padding1)self.side5 nn.Conv2d(64,out_ch,3,padding1)self.side6 nn.Conv2d(64,out_ch,3,padding1)self.outconv nn.Conv2d(6,out_ch,1)def forward(self,x):hx x#stage 1hx1 self.stage1(hx)hx self.pool12(hx1)#stage 2hx2 self.stage2(hx)hx self.pool23(hx2)#stage 3hx3 self.stage3(hx)hx self.pool34(hx3)#stage 4hx4 self.stage4(hx)hx self.pool45(hx4)#stage 5hx5 self.stage5(hx)hx self.pool56(hx5)#stage 6hx6 self.stage6(hx)hx6up _upsample_like(hx6,hx5)#decoderhx5d self.stage5d(torch.cat((hx6up,hx5),1))hx5dup _upsample_like(hx5d,hx4)hx4d self.stage4d(torch.cat((hx5dup,hx4),1))hx4dup _upsample_like(hx4d,hx3)hx3d self.stage3d(torch.cat((hx4dup,hx3),1))hx3dup _upsample_like(hx3d,hx2)hx2d self.stage2d(torch.cat((hx3dup,hx2),1))hx2dup _upsample_like(hx2d,hx1)hx1d self.stage1d(torch.cat((hx2dup,hx1),1))#side outputd1 self.side1(hx1d)d2 self.side2(hx2d)d2 _upsample_like(d2,d1)d3 self.side3(hx3d)d3 _upsample_like(d3,d1)d4 self.side4(hx4d)d4 _upsample_like(d4,d1)d5 self.side5(hx5d)d5 _upsample_like(d5,d1)d6 self.side6(hx6)d6 _upsample_like(d6,d1)d0 self.outconv(torch.cat((d1,d2,d3,d4,d5,d6),1))# return F.sigmoid(d0), F.sigmoid(d1), F.sigmoid(d2), F.sigmoid(d3), F.sigmoid(d4), F.sigmoid(d5), F.sigmoid(d6)return d0, d1, d2, d3, d4, d5, d6第四步搭建GUI界面 第五步整个工程的内容
有训练代码和训练好的模型以及训练过程提供数据提供GUI界面代码
代码见基于Pytorch框架的深度学习U2Net网络天空语义精细分割系统源码 有问题可以私信或者留言有问必答