SegNet在Camvid的复现 pytorch实现

1.数据来源:

https://github.com/alexgkendall/SegNet-Tutorial

2.网络参数选择

代码参考B导:憨批的语义分割重制版6——Pytorch 搭建自己的Unet语义分割平台_Bubbliiiing的博客-CSDN博客憨批的语义分割13——Pytorch 搭建自己的Unet语义分割平台注意事项学习前言什么是Unet模型代码下载Unet实现思路一、预测部分1、主干网络介绍2、加强特征提取结构3、利用特征获得预测结果二、训练部分1、训练文件详解2、LOSS解析训练自己的Unet模型注意事项这是重新构建了的Unet语义分割网络,主要是文件框架上的构建,还有代码的实现,和之前的语义分割网络相比,更加完整也更清晰一些。建议还是学习这个版本的Unet。学习前言还是快乐的pytorch人。什么是Unet模型Unet是一个https://blog.csdn.net/weixin_44791964/article/details/108866828

输入尺寸:512*512

Epoch:100

损失函数:CEloss

pytorch实现segnet:


import torch
import torch.nn as nn
import torch.nn.functional as F
# from collections import OrderedDict
#Encoder模块
 
class Encoder(nn.Module):
    def __init__(self):
        super(Encoder,self).__init__()
        #前13层是VGG16的前13层,分为5个stage
        #因为在下采样时要保存最大池化层的索引, 方便起见, 池化层不写在stage中
        self.stage_1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
        )
        
        self.stage_2 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
        )
        
        self.stage_3 = nn.Sequential(
            nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(),
        )     
        
        self.stage_4 = nn.Sequential(
            nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
        )   
        
        self.stage_5 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
        )     
        
    def forward(self, x):
        #用来保存各层的池化索引
        pool_indices = []
        x = x.float()
        
        x = self.stage_1(x)
        #pool_indice_1保留了第一个池化层的索引
        x, pool_indice_1 = nn.MaxPool2d( 2, stride=2, return_indices=True)(x)
        pool_indices.append(pool_indice_1)
        
        x = self.stage_2(x)
        x, pool_indice_2 = nn.MaxPool2d(2, stride=2, return_indices=True)(x)
        pool_indices.append(pool_indice_2)
        
        x = self.stage_3(x)
        x, pool_indice_3 = nn.MaxPool2d(2, stride=2, return_indices=True)(x)
        pool_indices.append(pool_indice_3)   
        
        x = self.stage_4(x)
        x, pool_indice_4 = nn.MaxPool2d(2, stride=2, return_indices=True)(x)
        pool_indices.append(pool_indice_4)
        
        x = self.stage_5(x)
        x, pool_indice_5 = nn.MaxPool2d(2, stride=2, return_indices=True)(x)
        pool_indices.append(pool_indice_5)
        
        return x, pool_indices
    
    
#SegNet网络, Encoder-Decoder
class SegNet(nn.Module):
    def __init__(self, num_classes):
        super(SegNet, self).__init__()
        #加载Encoder
        self.encoder = Encoder()
       #上采样 从下往上, 1->2->3->4->5
        self.upsample_1 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
        )
        
        self.upsample_2 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(),
        )
        
        self.upsample_3 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
        )
        
        self.upsample_4 = nn.Sequential(
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
        )
        
        self.upsample_5 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.Conv2d(64, num_classes, kernel_size=3, stride=1, padding=1),
        )   
        
    def forward(self, x):
        x, pool_indices = self.encoder(x)
        
        #池化索引上采样
        x = nn.MaxUnpool2d(2, 2, padding=0)(x, pool_indices[4])
        x = self.upsample_1(x)
        
        x = nn.MaxUnpool2d(2, 2, padding=0)(x, pool_indices[3])
        x = self.upsample_2(x) 
        
        x = nn.MaxUnpool2d(2, 2, padding=0)(x, pool_indices[2])
        x = self.upsample_3(x)
        
        x = nn.MaxUnpool2d(2, 2, padding=0)(x, pool_indices[1])
        x = self.upsample_4(x)
        
        x = nn.MaxUnpool2d(2, 2, padding=0)(x, pool_indices[0])
        x = self.upsample_5(x)
        
        return x

3.网络结果:

各类iou指标:

 预测结果:

4.总结:

预测会出现许多噪点,效果不太好,上述指标是在val上的结果,论文上指标好像是50几左右,整体来说还是unet比较扛一点,

用resnet_unet的miou都达到66%miou,

Biformer_tiny版本的unet更达到了72.4%的Miou。(参数量比rensnet_unet少一倍)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 要使用SegNet PyTorch版本来训练自己的数据集,需要按照以下步骤进行操作。 首先,将自己的数据集准备好。数据集应包含带有相应标签的图像。确保所有图像的分辨率一致,并且标签图像与输入图像大小相匹配。 接下来,下载SegNet PyTorch版本的源代码,并配置所需的环境。PyTorch的安装是必需的,你可以根据自己的系统进行安装。此外,还需要安装其他可能需要的依赖项。 然后,将准备好的数据集分为训练集和测试集。确保训练集与测试集的标签图像都包含在对应的文件夹中,并且文件名与其对应的输入图像相同。 接下来,修改SegNet源代码以适应自己的数据集。在训练和测试过程中,需要根据数据集的类别数量修改网络的输出通道数,并根据输入图像的大小调整网络的输入尺寸。 在修改好源代码后,进行训练。使用训练集数据来训练网络,并调整超参数以达到更好的性能。可以通过调节批次大小、学习率和迭代次数等来调整训练速度和准确性。 训练完成后,可以使用测试集数据来评估网络的性能。查看网络在测试集上每个类别的预测结果,并计算准确性、精确度和召回率等评价指标。 最后,可以使用训练好的SegNet模型来对未知图像进行预测。加载模型并对待预测图像进行处理,最后得到图像的分割结果。 以上就是使用SegNet PyTorch版本训练自己的数据集的基本步骤。通过适应自己的数据集和调整超参数,可以获得更好的语义分割模型。 ### 回答2: SegNet是一种用于图像语义分割的深度学习模型,其可以用于将输入图像分为不同的语义类别。如果要在PyTorch中使用SegNet模型,需要先准备自己的数据集并对其进行相应的处理。 首先,数据集需要包括输入图像和对应的标签图像。输入图像作为模型的输入,标签图像包含每个像素的语义类别信息。可以使用图像标注工具如labelImg对图像进行手动标注,或者使用已有的语义标注数据集。 接下来,需要将数据集分为训练集和验证集。可以按照一定的比例将数据集划分为两部分,其中一部分用于模型的训练,另一部分用于验证模型的性能。 然后,需要对数据集进行预处理。预处理的步骤包括图像的缩放、归一化和图像增强等。在PyTorch中,使用torchvision.transforms中的函数可以方便地进行这些处理。 接下来,需要定义数据加载器。可以使用PyTorch的DataLoader类读取预处理后的数据集,并将其提供给模型进行训练和验证。 在开始训练之前,需要加载SegNet模型。在PyTorch中,可以通过torchvision.models中的函数加载预定义的SegNet模型。可以选择预训练好的模型权重,或者将模型初始化为随机权重。 然后,需要定义损失函数和优化器。对于语义分割问题,常用的损失函数是交叉熵损失函数。可以使用torch.nn.CrossEntropyLoss定义损失函数。优化器可以选择Adam或SGD等常用的优化算法。 最后,开始模型的训练和验证。使用torch.nn.Module类创建SegNet模型的子类,并实现其forward函数。然后,通过迭代训练集的每个批次,使用损失函数计算损失,并使用优化器更新模型的参数。在每个epoch结束后,使用验证集评估模型的性能。 以上就是在PyTorch中使用SegNet模型进行图像语义分割的基本流程。通过按照上述步骤对自己的数据集进行处理,即可使用SegNet模型训练和验证自己的图像语义分割任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值