提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
一、DenseNet

二、代码
找了2个代码,还考虑用H-DenseU-Net的代码。
- https://github.com/stefano-malacrino/DenseUNet-pytorch
- https://github.com/THUHoloLab/Dense-U-net
第一次编写代码,感觉很多东西有点冗余,之后在优化,可以运行。(●’◡’●)
import torch
import torch.nn as nn
class conv_block(nn.Module):
def __init__(self, ch_in, ch_out):
super(conv_block, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
nn.BatchNorm2d(ch_out),
nn.ReLU(inplace=True),
nn.Conv2d(ch_out, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
nn.BatchNorm2d(ch_out),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.conv(x)
return x
class up_conv(nn.Module):
def __init__(self, ch_in, ch_out):
super(up_conv, self).__init__()
self.up = nn.Sequential(
nn.Upsample(scale_factor=2),
nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
nn.BatchNorm2d(ch_out),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.up(x)
return x
class Conv_Block(nn.Module):
def __init__(self, ch_in, ch_out):
super(Conv_Block, self).__init__()
self.conv = nn.Sequential(
nn.BatchNorm2d(ch_in),
nn.ReLU(inplace=True),
nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1)
)
def forward(self, x):
x = self.conv(x)
return x
class dens_block(nn.Module):
def __init__(self,ch_in,ch_out):
super(dens_block, self).__init__()#这三个相同吗????
self.conv1 = Conv_Block(ch_in,ch_out)
self.conv2 = Conv_Block(ch_out+ch_in, ch_out)
self.conv3 = Conv_Block(ch_out*2 + ch_in, ch_out)
def forward(self,input_tensor):
x1 = self.conv1(input_tensor)
add1 = torch.cat([x1,input_tensor

本文介绍了作者构建DenseU-Net模型的过程,包括代码实现和遇到的问题。作者首先分享了DenseNet的基础知识,然后提供了DenseU-Net的PyTorch代码,并展示了模型结构。在实践中,作者尝试了不同的网络配置,如DenseNet121结合U-Net,但遇到了过拟合和低准确率的问题。最后,作者调整了模型并实现了基于DenseNet的DenseU-Net,用于图像分割任务。
最低0.47元/天 解锁文章
1855





