news 2026/4/16 13:05:59

语义分割实战——基于EGEUNet神经网络印章分割系统3:含训练测试代码、数据集和GUI交互界面

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
语义分割实战——基于EGEUNet神经网络印章分割系统3:含训练测试代码、数据集和GUI交互界面

第一步:准备数据

印章分割-深度学习图像分割数据集

印章分割数据,可直接应用到一些常用深度学习分割算法中,比如FCN、Unet、SegNet、DeepLabV1、DeepLabV2、DeepLabV3、DeepLabV3+、PSPNet、RefineNet、HRnet、Mask R-CNN、Segformer、DUCK-Net模型等
数据集总共有2000对图片,数据质量非常高,甚至可应用到工业落地的项目中

第二步:搭建模型

本文选择EGEUNet,其网络结构分别如下:

第三步:训练代码

1)损失函数为:dice_loss + focal_loss

2)网络代码:

class EGEUNet(nn.Module): def __init__(self, num_classes=1, input_channels=3, c_list=[8, 16, 24, 32, 48, 64], bridge=True, gt_ds=False): super().__init__() self.bridge = bridge self.gt_ds = gt_ds self.encoder1 = nn.Sequential( nn.Conv2d(input_channels, c_list[0], 3, stride=1, padding=1), ) self.encoder2 = nn.Sequential( nn.Conv2d(c_list[0], c_list[1], 3, stride=1, padding=1), ) self.encoder3 = nn.Sequential( nn.Conv2d(c_list[1], c_list[2], 3, stride=1, padding=1), ) self.encoder4 = nn.Sequential( Grouped_multi_axis_Hadamard_Product_Attention(c_list[2], c_list[3]), ) self.encoder5 = nn.Sequential( Grouped_multi_axis_Hadamard_Product_Attention(c_list[3], c_list[4]), ) self.encoder6 = nn.Sequential( Grouped_multi_axis_Hadamard_Product_Attention(c_list[4], c_list[5]), ) if bridge: self.GAB1 = group_aggregation_bridge(c_list[1], c_list[0]) self.GAB2 = group_aggregation_bridge(c_list[2], c_list[1]) self.GAB3 = group_aggregation_bridge(c_list[3], c_list[2]) self.GAB4 = group_aggregation_bridge(c_list[4], c_list[3]) self.GAB5 = group_aggregation_bridge(c_list[5], c_list[4]) print('group_aggregation_bridge was used') if gt_ds: self.gt_conv1 = nn.Sequential(nn.Conv2d(c_list[4], 1, 1)) self.gt_conv2 = nn.Sequential(nn.Conv2d(c_list[3], 1, 1)) self.gt_conv3 = nn.Sequential(nn.Conv2d(c_list[2], 1, 1)) self.gt_conv4 = nn.Sequential(nn.Conv2d(c_list[1], 1, 1)) self.gt_conv5 = nn.Sequential(nn.Conv2d(c_list[0], 1, 1)) print('gt deep supervision was used') self.decoder1 = nn.Sequential( Grouped_multi_axis_Hadamard_Product_Attention(c_list[5], c_list[4]), ) self.decoder2 = nn.Sequential( Grouped_multi_axis_Hadamard_Product_Attention(c_list[4], c_list[3]), ) self.decoder3 = nn.Sequential( Grouped_multi_axis_Hadamard_Product_Attention(c_list[3], c_list[2]), ) self.decoder4 = nn.Sequential( nn.Conv2d(c_list[2], c_list[1], 3, stride=1, padding=1), ) self.decoder5 = nn.Sequential( nn.Conv2d(c_list[1], c_list[0], 3, stride=1, padding=1), ) self.ebn1 = nn.GroupNorm(4, c_list[0]) self.ebn2 = nn.GroupNorm(4, c_list[1]) self.ebn3 = nn.GroupNorm(4, c_list[2]) self.ebn4 = nn.GroupNorm(4, c_list[3]) self.ebn5 = nn.GroupNorm(4, c_list[4]) self.dbn1 = nn.GroupNorm(4, c_list[4]) self.dbn2 = nn.GroupNorm(4, c_list[3]) self.dbn3 = nn.GroupNorm(4, c_list[2]) self.dbn4 = nn.GroupNorm(4, c_list[1]) self.dbn5 = nn.GroupNorm(4, c_list[0]) self.final = nn.Conv2d(c_list[0], num_classes, kernel_size=1) self.apply(self._init_weights) def _init_weights(self, m): if isinstance(m, nn.Linear): trunc_normal_(m.weight, std=.02) if isinstance(m, nn.Linear) and m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Conv1d): n = m.kernel_size[0] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.Conv2d): fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels fan_out //= m.groups m.weight.data.normal_(0, math.sqrt(2.0 / fan_out)) if m.bias is not None: m.bias.data.zero_() def forward(self, x): out = F.gelu(F.max_pool2d(self.ebn1(self.encoder1(x)), 2, 2)) t1 = out # b, c0, H/2, W/2 out = F.gelu(F.max_pool2d(self.ebn2(self.encoder2(out)), 2, 2)) t2 = out # b, c1, H/4, W/4 out = F.gelu(F.max_pool2d(self.ebn3(self.encoder3(out)), 2, 2)) t3 = out # b, c2, H/8, W/8 out = F.gelu(F.max_pool2d(self.ebn4(self.encoder4(out)), 2, 2)) t4 = out # b, c3, H/16, W/16 out = F.gelu(F.max_pool2d(self.ebn5(self.encoder5(out)), 2, 2)) t5 = out # b, c4, H/32, W/32 out = F.gelu(self.encoder6(out)) # b, c5, H/32, W/32 t6 = out out5 = F.gelu(self.dbn1(self.decoder1(out))) # b, c4, H/32, W/32 if self.gt_ds: gt_pre5 = self.gt_conv1(out5) t5 = self.GAB5(t6, t5, gt_pre5) gt_pre5 = F.interpolate(gt_pre5, scale_factor=32, mode='bilinear', align_corners=True) else: t5 = self.GAB5(t6, t5) out5 = torch.add(out5, t5) # b, c4, H/32, W/32 out4 = F.gelu(F.interpolate(self.dbn2(self.decoder2(out5)), scale_factor=(2, 2), mode='bilinear', align_corners=True)) # b, c3, H/16, W/16 if self.gt_ds: gt_pre4 = self.gt_conv2(out4) t4 = self.GAB4(t5, t4, gt_pre4) gt_pre4 = F.interpolate(gt_pre4, scale_factor=16, mode='bilinear', align_corners=True) else: t4 = self.GAB4(t5, t4) out4 = torch.add(out4, t4) # b, c3, H/16, W/16 out3 = F.gelu(F.interpolate(self.dbn3(self.decoder3(out4)), scale_factor=(2, 2), mode='bilinear', align_corners=True)) # b, c2, H/8, W/8 if self.gt_ds: gt_pre3 = self.gt_conv3(out3) t3 = self.GAB3(t4, t3, gt_pre3) gt_pre3 = F.interpolate(gt_pre3, scale_factor=8, mode='bilinear', align_corners=True) else: t3 = self.GAB3(t4, t3) out3 = torch.add(out3, t3) # b, c2, H/8, W/8 out2 = F.gelu(F.interpolate(self.dbn4(self.decoder4(out3)), scale_factor=(2, 2), mode='bilinear', align_corners=True)) # b, c1, H/4, W/4 if self.gt_ds: gt_pre2 = self.gt_conv4(out2) t2 = self.GAB2(t3, t2, gt_pre2) gt_pre2 = F.interpolate(gt_pre2, scale_factor=4, mode='bilinear', align_corners=True) else: t2 = self.GAB2(t3, t2) out2 = torch.add(out2, t2) # b, c1, H/4, W/4 out1 = F.gelu(F.interpolate(self.dbn5(self.decoder5(out2)), scale_factor=(2, 2), mode='bilinear', align_corners=True)) # b, c0, H/2, W/2 if self.gt_ds: gt_pre1 = self.gt_conv5(out1) t1 = self.GAB1(t2, t1, gt_pre1) gt_pre1 = F.interpolate(gt_pre1, scale_factor=2, mode='bilinear', align_corners=True) else: t1 = self.GAB1(t2, t1) out1 = torch.add(out1, t1) # b, c0, H/2, W/2 out0 = F.interpolate(self.final(out1), scale_factor=(2, 2), mode='bilinear', align_corners=True) # b, num_class, H, W if self.gt_ds: return (torch.sigmoid(gt_pre5), torch.sigmoid(gt_pre4), torch.sigmoid(gt_pre3), torch.sigmoid(gt_pre2), torch.sigmoid(gt_pre1)), torch.sigmoid(out0) else: return torch.sigmoid(out0)

第四步:统计一些指标(训练过程中的loss和miou)

第五步:搭建GUI界面

第六步:整个工程的内容

项目完整文件下载请见演示与介绍视频的简介处给出:➷➷➷

https://www.bilibili.com/video/BV1Qpz4BfECo/

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 12:59:21

五大智能建站工具真实测评:不画饼,真能上线那种

最近在搞一个支付平台的项目,我前端水平一般,实在不想花几周去啃UI框架。干脆试了一圈AI建站工具,结果还真发现了几个能打的——不光能生成页面,连后台逻辑、数据对接都能搞定,几分钟就能搭出能跑起来的应用。 下面这…

作者头像 李华
网站建设 2026/4/11 22:47:56

人群仿真软件:Vadere_(6).Vadere中的障碍物设置

障碍物设置 在人群仿真软件Vadere中,障碍物的设置对于模拟真实环境中的各种情况至关重要。障碍物可以是墙壁、柱子、家具等,这些障碍物会影响行人的行走路径和行为。本节将详细介绍如何在Vadere中设置障碍物,包括障碍物的定义、添加、编辑和…

作者头像 李华
网站建设 2026/4/13 9:21:44

免费工艺流程模板下载_在线编辑建筑/汽车/物流工艺流程图图表

良功绘图网站 (https://www.lghuitu.com ) 在建筑、汽车、物流等行业的日常运营与项目推进中,工艺流程可视化是提升效率、规范管理的核心手段。无论是建筑工程的施工管控、汽车制造的生产线优化,还是物流行业的仓储配送调度,一份清晰、规范的…

作者头像 李华
网站建设 2026/4/16 12:42:24

AI金融风控:智能反欺诈与个性化理财

一、AI金融风控的核心价值与行业背景 随着数字金融的快速迭代,金融欺诈手段呈现智能化、隐蔽化、团伙化趋势,传统风控模式面临严峻挑战。2023年某电商平台披露数据显示,全年因恶意刷单、账号盗刷等造成的损失超2.3亿元,某银行信用…

作者头像 李华
网站建设 2026/4/16 9:15:12

MCP与A2A深度解析:AI系统集成与智能体协作的未来之路

MCP与A2A是AI系统集成的两大关键协议。MCP解决AI安全调用工具与数据的问题,提供资源、工具和提示模板三大能力;A2A则规范智能体间的协作,通过任务、消息和产物等组件实现高效接力。它们共同构成AI系统的分层规范,将开发范式从&quo…

作者头像 李华
网站建设 2026/4/13 6:16:36

网络考试点下一步出错怎么办?试试这样做

场景:家中网络课学习完毕考试,遇到点下一步出错问题 解决方案:打开显示所有题号的面板,点击面板上的题号跳转 注:我这里题号面板是点击进度条打开的,各人根据实际情况找下

作者头像 李华