分割 bisenetv2笔记

训练心得,没有u2net收敛的快,效果好。

2021年的:

bisenetv2/bisenetv2_new.py at main · git-luo66/bisenetv2 · GitHub

地址2,2019年的:

BiseNetv2-pytorch/BiseNet.py at master · Soulempty/BiseNetv2-pytorch · GitHub

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.model_zoo as modelzoo# backbone_url = 'https://github.com/CoinCheung/BiSeNet/releases/download/0.0.0/backbone_v2.pth'class ConvBNReLU(nn.Module):def __init__(self, in_chan, out_chan, ks=3, stride=1, padding=1,dilation=1, groups=1, bias=False):super(ConvBNReLU, self).__init__()self.conv = nn.Conv2d(in_chan, out_chan, kernel_size=ks, stride=stride,padding=padding, dilation=dilation,groups=groups, bias=bias)self.bn = nn.BatchNorm2d(out_chan)self.relu = nn.ReLU(inplace=True)def forward(self, x):feat = self.conv(x)feat = self.bn(feat)feat = self.relu(feat)return featclass UpSample(nn.Module):def __init__(self, n_chan, factor=2):super(UpSample, self).__init__()out_chan = n_chan * factor * factorself.proj = nn.Conv2d(n_chan, out_chan, 1, 1, 0)self.up = nn.PixelShuffle(factor)self.init_weight()def forward(self, x):feat = self.proj(x)feat = self.up(feat)return featdef init_weight(self):nn.init.xavier_normal_(self.proj.weight, gain=1.)class DetailBranch(nn.Module):def __init__(self):super(DetailBranch, self).__init__()self.S1 = nn.Sequential(ConvBNReLU(3, 64, 3, stride=2),ConvBNReLU(64, 64, 3, stride=1),)self.S2 = nn.Sequential(ConvBNReLU(64, 64, 3, stride=2),ConvBNReLU(64, 64, 3, stride=1),ConvBNReLU(64, 64, 3, stride=1),)self.S3 = nn.Sequential(ConvBNReLU(64, 128, 3, stride=2),ConvBNReLU(128, 128, 3, stride=1),ConvBNReLU(128, 128, 3, stride=1),)def forward(self, x):feat = self.S1(x)feat = self.S2(feat)feat = self.S3(feat)return featclass StemBlock(nn.Module):def __init__(self):super(StemBlock, self).__init__()self.conv = ConvBNReLU(3, 16, 3, stride=2)self.left = nn.Sequential(ConvBNReLU(16, 8, 1, stride=1, padding=0),ConvBNReLU(8, 16, 3, stride=2),)self.right = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, ceil_mode=False)self.fuse = ConvBNReLU(32, 16, 3, stride=1)def forward(self, x):feat = self.conv(x)feat_left = self.left(feat)feat_right = self.right(feat)feat = torch.cat([feat_left, feat_right], dim=1)feat = self.fuse(feat)return featclass CEBlock(nn.Module):def __init__(self):super(CEBlock, self).__init__()self.bn = nn.BatchNorm2d(128)self.conv_gap = ConvBNReLU(128, 128, 1, stride=1, padding=0)#TODO: in paper here is naive conv2d, no bn-reluself.conv_last = ConvBNReLU(128, 128, 3, stride=1)def forward(self, x):feat = torch.mean(x, dim=(2, 3), keepdim=True)feat = self.bn(feat)feat = self.conv_gap(feat)feat = feat + xfeat = self.conv_last(feat)return featclass GELayerS1(nn.Module):def __init__(self, in_chan, out_chan, exp_ratio=6):super(GELayerS1, self).__init__()mid_chan = in_chan * exp_ratioself.conv1 = ConvBNReLU(in_chan, in_chan, 3, stride=1)self.dwconv = nn.Sequential(nn.Conv2d(in_chan, mid_chan, kernel_size=3, stride=1,padding=1, groups=in_chan, bias=False),nn.BatchNorm2d(mid_chan),nn.ReLU(inplace=True), # not shown in paper)self.conv2 = nn.Sequential(nn.Conv2d(mid_chan, out_chan, kernel_size=1, stride=1,padding=0, bias=False),nn.BatchNorm2d(out_chan),)self.conv2[1].last_bn = Trueself.relu = nn.ReLU(inplace=True)def forward(self, x):feat = self.conv1(x)feat = self.dwconv(feat)feat = self.conv2(feat)feat = feat + xfeat = self.relu(feat)return featclass GELayerS2(nn.Module):def __init__(self, in_chan, out_chan, exp_ratio=6):super(GELayerS2, self).__init__()mid_chan = in_chan * exp_ratioself.conv1 = ConvBNReLU(in_chan, in_chan, 3, stride=1)self.dwconv1 = nn.Sequential(nn.Conv2d(in_chan, mid_chan, kernel_size=3, stride=2,padding=1, groups=in_chan, bias=False),nn.BatchNorm2d(mid_chan),)self.dwconv2 = nn.Sequential(nn.Conv2d(mid_chan, mid_chan, kernel_size=3, stride=1,padding=1, groups=mid_chan, bias=False),nn.BatchNorm2d(mid_chan),nn.ReLU(inplace=True), # not shown in paper)self.conv2 = nn.Sequential(nn.Conv2d(mid_chan, out_chan, kernel_size=1, stride=1,padding=0, bias=False),nn.BatchNorm2d(out_chan),)self.conv2[1].last_bn = Trueself.shortcut = nn.Sequential(nn.Conv2d(in_chan, in_chan, kernel_size=3, stride=2,padding=1, groups=in_chan, bias=False),nn.BatchNorm2d(in_chan),nn.Conv2d(in_chan, out_chan, kernel_size=1, stride=1,padding=0, bias=False),nn.BatchNorm2d(out_chan),)self.relu = nn.ReLU(inplace=True)def forward(self, x):feat = self.conv1(x)feat = self.dwconv1(feat)feat = self.dwconv2(feat)feat = self.conv2(feat)shortcut = self.shortcut(x)feat = feat + shortcutfeat = self.relu(feat)return featclass SegmentBranch(nn.Module):def __init__(self):super(SegmentBranch, self).__init__()self.S1S2 = StemBlock()self.S3 = nn.Sequential(GELayerS2(16, 32),GELayerS1(32, 32),)self.S4 = nn.Sequential(GELayerS2(32, 64),GELayerS1(64, 64),)self.S5_4 = nn.Sequential(GELayerS2(64, 128),GELayerS1(128, 128),GELayerS1(128, 128),GELayerS1(128, 128),)self.S5_5 = CEBlock()def forward(self, x):feat2 = self.S1S2(x)feat3 = self.S3(feat2)feat4 = self.S4(feat3)feat5_4 = self.S5_4(feat4)feat5_5 = self.S5_5(feat5_4)return feat2, feat3, feat4, feat5_4, feat5_5class BGALayer(nn.Module):def __init__(self):super(BGALayer, self).__init__()self.left1 = nn.Sequential(nn.Conv2d(128, 128, kernel_size=3, stride=1,padding=1, groups=128, bias=False),nn.BatchNorm2d(128),nn.Conv2d(128, 128, kernel_size=1, stride=1,padding=0, bias=False),)self.left2 = nn.Sequential(nn.Conv2d(128, 128, kernel_size=3, stride=2,padding=1, bias=False),nn.BatchNorm2d(128),nn.AvgPool2d(kernel_size=3, stride=2, padding=1, ceil_mode=False))self.right1 = nn.Sequential(nn.Conv2d(128, 128, kernel_size=3, stride=1,padding=1, bias=False),nn.BatchNorm2d(128),)self.right2 = nn.Sequential(nn.Conv2d(128, 128, kernel_size=3, stride=1,padding=1, groups=128, bias=False),nn.BatchNorm2d(128),nn.Conv2d(128, 128, kernel_size=1, stride=1,padding=0, bias=False),)self.up1 = nn.Upsample(scale_factor=4)self.up2 = nn.Upsample(scale_factor=4)##TODO: does this really has no relu?self.conv = nn.Sequential(nn.Conv2d(128, 128, kernel_size=3, stride=1,padding=1, bias=False),nn.BatchNorm2d(128),nn.ReLU(inplace=True), # not shown in paper)def forward(self, x_d, x_s):dsize = x_d.size()[2:]left1 = self.left1(x_d)left2 = self.left2(x_d)right1 = self.right1(x_s)right2 = self.right2(x_s)right1 = self.up1(right1)left = left1 * torch.sigmoid(right1)right = left2 * torch.sigmoid(right2)right = self.up2(right)out = self.conv(left + right)return outclass SegmentHead(nn.Module):def __init__(self, in_chan, mid_chan, n_classes, up_factor=8, aux=True):super(SegmentHead, self).__init__()self.conv = ConvBNReLU(in_chan, mid_chan, 3, stride=1)self.drop = nn.Dropout(0.1)self.up_factor = up_factorout_chan = n_classesmid_chan2 = up_factor * up_factor if aux else mid_chanup_factor = up_factor // 2 if aux else up_factorself.conv_out = nn.Sequential(nn.Sequential(nn.Upsample(scale_factor=2),ConvBNReLU(mid_chan, mid_chan2, 3, stride=1)) if aux else nn.Identity(),nn.Conv2d(mid_chan2, out_chan, 1, 1, 0, bias=True),nn.Upsample(scale_factor=up_factor, mode='bilinear', align_corners=False))def forward(self, x):feat = self.conv(x)feat = self.drop(feat)feat = self.conv_out(feat)return featclass BiSeNetV2(nn.Module):def __init__(self, n_classes, aux_mode='train'):super(BiSeNetV2, self).__init__()self.aux_mode = aux_modeself.detail = DetailBranch()self.segment = SegmentBranch()self.bga = BGALayer()## TODO: what is the number of mid chan ?self.head = SegmentHead(128, 1024, n_classes, up_factor=8, aux=False)if self.aux_mode == 'train':self.aux2 = SegmentHead(16, 128, n_classes, up_factor=4)self.aux3 = SegmentHead(32, 128, n_classes, up_factor=8)self.aux4 = SegmentHead(64, 128, n_classes, up_factor=16)self.aux5_4 = SegmentHead(128, 128, n_classes, up_factor=32)self.init_weights()def forward(self, x):size = x.size()[2:]feat_d = self.detail(x)feat2, feat3, feat4, feat5_4, feat_s = self.segment(x)feat_head = self.bga(feat_d, feat_s)logits = self.head(feat_head)if self.aux_mode == 'train':logits_aux2 = self.aux2(feat2)logits_aux3 = self.aux3(feat3)logits_aux4 = self.aux4(feat4)logits_aux5_4 = self.aux5_4(feat5_4)return [logits, logits_aux2, logits_aux3, logits_aux4, logits_aux5_4]elif self.aux_mode == 'eval':return logits,elif self.aux_mode == 'pred':pred = logits.argmax(dim=1)return predelse:raise NotImplementedErrordef init_weights(self):for name, module in self.named_modules():if isinstance(module, (nn.Conv2d, nn.Linear)):nn.init.kaiming_normal_(module.weight, mode='fan_out')if not module.bias is None: nn.init.constant_(module.bias, 0)elif isinstance(module, nn.modules.batchnorm._BatchNorm):if hasattr(module, 'last_bn') and module.last_bn:nn.init.zeros_(module.weight)else:nn.init.ones_(module.weight)nn.init.zeros_(module.bias)# self.load_pretrain()# def load_pretrain(self):#     state = modelzoo.load_url(backbone_url)#     for name, child in self.named_children():#         if name in state.keys():#             child.load_state_dict(state[name], strict=True)def get_params(self):def add_param_to_list(mod, wd_params, nowd_params):for param in mod.parameters():if param.dim() == 1:nowd_params.append(param)elif param.dim() == 4:wd_params.append(param)else:print(name)wd_params, nowd_params, lr_mul_wd_params, lr_mul_nowd_params = [], [], [], []for name, child in self.named_children():if 'head' in name or 'aux' in name:add_param_to_list(child, lr_mul_wd_params, lr_mul_nowd_params)else:add_param_to_list(child, wd_params, nowd_params)return wd_params, nowd_params, lr_mul_wd_params, lr_mul_nowd_paramsif __name__ == "__main__":#  x = torch.randn(16, 3, 1024, 2048)#  detail = DetailBranch()#  feat = detail(x)#  print('detail', feat.size())##  x = torch.randn(16, 3, 1024, 2048)#  stem = StemBlock()#  feat = stem(x)#  print('stem', feat.size())##  x = torch.randn(16, 128, 16, 32)#  ceb = CEBlock()#  feat = ceb(x)#  print(feat.size())##  x = torch.randn(16, 32, 16, 32)#  ge1 = GELayerS1(32, 32)#  feat = ge1(x)#  print(feat.size())##  x = torch.randn(16, 16, 16, 32)#  ge2 = GELayerS2(16, 32)#  feat = ge2(x)#  print(feat.size())##  left = torch.randn(16, 128, 64, 128)#  right = torch.randn(16, 128, 16, 32)#  bga = BGALayer()#  feat = bga(left, right)#  print(feat.size())##  x = torch.randn(16, 128, 64, 128)#  head = SegmentHead(128, 128, 19)#  logits = head(x)#  print(logits.size())##  x = torch.randn(16, 3, 1024, 2048)#  segment = SegmentBranch()#  feat = segment(x)[0]#  print(feat.size())#input = torch.randn(1, 3, 512, 512)#.cuda()model = BiSeNetV2(n_classes=1)#.cuda()model.eval()model_path = "dicenet.pth"torch.save(model.state_dict(), model_path)import osimport timefsize = os.path.getsize(model_path)fsize = fsize / float(1024 * 1024)print(f"model size {round(fsize, 2)} m")for i in range(10):start = time.time()output = model(input)print('output.size ', output[0].size(), time.time() - start)  # , output[0].size(), output[1].size(), output[2].size())del output

分割 bisenetv2笔记相关推荐

  1. 使用Pytorch搭建U-Net网络并基于DRIVE数据集训练(语义分割)学习笔记

    使用Pytorch搭建U-Net网络并基于DRIVE数据集训练(语义分割)学习笔记 https://www.bilibili.com/video/BV1rq4y1w7xM?spm_id_from=33 ...

  2. 点云分割学习笔记2022

    PointCloudSegmentation 点云分割 PointCloudSegmentation测试笔记_AI视觉网奇的博客-CSDN博客 代码地址:https://github.com/Liya ...

  3. 检测跟踪分割网络笔记

    模型247.97m GitHub - SysCV/pcan: Prototypical Cross-Attention Networks for Multiple Object Tracking an ...

  4. 分割BiSeNet笔记

    BiSeNet 有attention层 有Fusion层 据说能到105帧率 核心网络resne18 两年前的,不是这个 https://github.com/zllrunning/face-pars ...

  5. 基于能量或过零率的实时语音分割--学习笔记

    重要假设/基础:采集到的音频在起始处有一小段静音,长度为几百毫秒,这是我们估计静音阈值E0的基础,也是语音降噪的基础. 1. 能量energy 选区一定数量的音频帧,计算其平均能量值,然后加上一个经验 ...

  6. 霹雳吧啦Wz语义分割学习笔记P3

    P3.FCN网络结构详解 1.前言 Fully Convolutional Networks for Semantic Segmentation 2015CVPR https://arxiv.org/ ...

  7. 深度学习语义分割论文笔记(待完善)

    在深度学习这一块儿,我最开始使用Unet进行了冠状动脉血管的分割 再后来,我尝试改进Unet,改进损失函数,让网络能有不错的效果 再后来,看到了注意力机制,读了attention unet, 于是,我 ...

  8. 语义分割学习笔记(一)

    文章目录 一.区别 二.代码演示原图像与mask融合 三.数据处理 一.区别 语义分割:每个像素都打上标签(这个像素是人,树,背景等)语义分割只区分类别,不区分类别中的具体单元 实例分割:不光要区分类 ...

  9. kaggle 2018 data science bowl 细胞核分割学习笔记

    一. 获奖者解决方案 1. 第一名解决方案(Unet 0.631) 主要的贡献 targets: 预测touching borders,将问题作为instance分割 loss function:组合 ...

最新文章

  1. AI一分钟 | 李开复:AI创业公司估值今年会降20%~30%;谷歌让搜索结果加载速度提升两倍...
  2. 云场景实践研究第74期:科沃斯
  3. 《系统集成项目管理工程师》必背100个知识点-31WBS的分解原则
  4. haproxy 负载_负载测试HAProxy(第1部分)
  5. 深度学习的实用层面 —— 1.5 为什么正则化可以减少过拟合
  6. swagger的使用(com.spring4all)
  7. ecshop 后台添加评论_技术小白如何添加服务号模板消息?服务号的模板消息功能到底该怎么使用?...
  8. SQL实战之查找所有已经分配部门的员工的last_name和first_name
  9. CentOS 7以yum方式安装zabbix3.2及配置文件详解
  10. Python安装geopandas库
  11. 大端模式和小端模式的再理解
  12. 王者-甄别同一板块强弱股的方法【真假美猴王】
  13. 常见物联网操作系统介绍
  14. qcom usb驱动下载_艾肯Mobile Q驱动-艾肯Mobile Q usb外置声卡驱动下载v1.35.20 官方最新版-西西软件下载...
  15. XtraReport控件详细属性说明
  16. 10. 微型计算机常用的显示器有哪几类及其工作原理,四川自考07311《多媒体技术》全真模拟试题(十)...
  17. Python OpenCV开发MR智能人脸识别打卡系统(四、服务模块设计)
  18. zabbix监控-企业微信webhook告警并使用markdown格式
  19. Imply方式安装0.15.0版本Druid和实例(hdfs2druid)分享
  20. OSChina 周四乱弹 ——过节上班没关系,老王他休息!

热门文章

  1. 使用sqlmap 绕过防火墙进行注入测试
  2. Linux安装配置php7+nginx
  3. 用symbol来获得ShadowSSDT的原始地址和函数名
  4. Android开发--FileInputStream/OutStream/Sdcard写入
  5. java设计模式---工厂方法模式
  6. linux系统调用挂钩方法总结
  7. mysql怎么在海量数据上ddl_浅谈MySQL Online DDL(中)
  8. java io 读取配置文件_java读取配置文件 - tomzhao2008的个人空间 - OSCHINA - 中文开源技术交流社区...
  9. 百度面试 中缀表达式变成后缀表达式
  10. python对共轭复数的定义_python print出共轭复数的方法详解