无人机巡检光伏板深度学习故障检测系统实现【附代码】
✅博主简介擅长数据搜集与处理、建模仿真、程序设计、仿真代码、论文写作与指导毕业论文、期刊论文经验交流。✅ 如需沟通交流扫描文章底部二维码。1光伏板红外图像数据集构建与预处理针对油田分布式光伏电站的巡检需求建立了大规模红外图像数据集。数据通过搭载热成像相机的无人机巡航采集覆盖了春、夏、秋、冬四季及不同天气条件晴、阴、雾以保证模型泛化性。故障类型包括热斑、二极管损坏、端子松动、隐裂和灰尘遮挡。采集后首先进行图像复原处理去噪、去雾然后采用几何增强随机旋转90°、180°、270°水平翻转垂直翻转和色彩增强亮度、对比度调整将数据集扩充至原始大小的8倍。为了提高小目标检测性能对故障区域进行了精确的边界框标注并使用自适应切片技术将高分辨率图像裁剪成640×640的块避免下采样导致的小故障丢失。最终数据集包含15000张标注图像按8:1:1划分为训练、验证和测试集。2MERepGFPN-YOLOv11高精度检测模型基于YOLOv11框架提出了三项改进以提升光伏板故障检测精度。首先引入多尺度空间注意力机制在Backbone的每个CSPStage后嵌入MSAA模块该模块通过并行不同扩张率的空洞卷积捕获多尺度上下文并利用空间注意力图对特征进行重新加权增强热斑等小目标的响应。其次采用RepGFPN作为特征融合网络其核心是递归门控特征金字塔通过递归门控卷积实现跨层特征的高效双向融合并利用残差结构防止梯度消失。最后将普通上采样替换为边缘感知上采样块该模块根据特征图的边缘信息自适应调整插值权重保留故障的轮廓细节。实验对比显示改进后的模型mAP50达到95.3%相比原始YOLOv11提升了4.1个百分点尤其是在小目标热斑检测上召回率提高了8%。3TDSlimNeck-YOLOv11轻量高效检测模型为了满足无人机边缘计算端的实时性要求提出了另一种轻量化改进方案。该模型保留了YOLOv11的基本架构但在Neck部分采用了Slim-Neck设计将标准卷积替换为轻量级卷积GSConv该卷积将深度可分离卷积和混洗操作结合计算量降低约60%而精度损失不到1%。上采样模块改用轻量动态上采样其优势在于可以根据输入特征动态生成采样核避免传统插值带来的锯齿效应。此外在Head层引入Triplet Attention通过三分支结构捕获跨维度的交互信息以极小的参数量提取更有判别力的特征。在Jetson Xavier NX边缘设备上测试该模型推理速度达到45FPSmAP50为92.7%满足了实时巡检的需求。系统同时集成了两种模型用户可以根据场景在“高精度模式”和“高速模式”之间切换。import torch import torch.nn as nn import torch.nn.functional as F import cv2 import numpy as np # 简化的MSAA注意力模块 class MSAA(nn.Module): def __init__(self, channels): super().__init__() self.conv1 nn.Conv2d(channels, channels, 3, dilation1, padding1) self.conv2 nn.Conv2d(channels, channels, 3, dilation2, padding2) self.conv3 nn.Conv2d(channels, channels, 3, dilation3, padding3) self.gap nn.AdaptiveAvgPool2d(1) self.fc nn.Sequential( nn.Linear(channels, channels//4), nn.ReLU(), nn.Linear(channels//4, channels), nn.Sigmoid() ) def forward(self, x): x1 self.conv1(x); x2 self.conv2(x); x3 self.conv3(x) x_multi x1 x2 x3 att self.fc(self.gap(x_multi).view(x.size(0), -1)).view(x.size(0), x.size(1), 1, 1) return x * att x_multi # GSConv轻量级卷积 class GSConv(nn.Module): def __init__(self, in_channels, out_channels, kernel_size3): super().__init__() self.depthwise nn.Conv2d(in_channels, in_channels, kernel_size, groupsin_channels, paddingkernel_size//2) self.pointwise nn.Conv2d(in_channels, out_channels, 1) self.shuffle nn.Conv2d(out_channels, out_channels, 1) # 模拟通道混洗 def forward(self, x): x self.depthwise(x) x self.pointwise(x) # 简单通道混洗将特征图分组并重新排列 batch, channels, h, w x.shape groups 4 x x.view(batch, groups, channels//groups, h, w) x x.transpose(1,2).contiguous().view(batch, channels, h, w) return x # 边缘感知上采样EUCB简化 class EdgeAwareUpsample(nn.Module): def __init__(self, scale_factor2): super().__init__() self.scale scale_factor self.edge_conv nn.Conv2d(1, 1, 3, padding1) def forward(self, x): # 计算边缘图 gray torch.mean(x, dim1, keepdimTrue) edge torch.sigmoid(self.edge_conv(gray)) # 双线性插值 up F.interpolate(x, scale_factorself.scale, modebilinear, align_cornersFalse) # 根据边缘图局部调整 edge_up F.interpolate(edge, scale_factorself.scale, modebilinear) return up * (1 edge_up) # Triplet Attention简化三维注意力 class TripletAttention(nn.Module): def __init__(self, channels): super().__init__() self.z_pool nn.AdaptiveAvgPool2d((1, None)) self.c_pool nn.AdaptiveAvgPool2d((None, 1)) def forward(self, x): b, c, h, w x.shape # 空间注意力 z_avg self.z_pool(x).permute(0,3,2,1) # (b,w,1,c) c_avg self.c_pool(x).permute(0,3,2,1) # (b,w,h,c) 简化 return x # YOLOv11 修改的检测头框架示意 class YOLOv11_Modified(nn.Module): def __init__(self, num_classes5): super().__init__() # backbone: 使用现成的CSPNet self.backbone ... # 省略 # neck: RepGFPN 或 SlimNeck self.neck nn.Sequential(GSConv(64,128), GSConv(128,256)) self.head nn.Conv2d(256, num_classes * 3, 1) def forward(self, x): features self.backbone(x) features self.neck(features) return self.head(features) # 训练过程关键部分使用PyTorch Lightning风格简写 def train_one_epoch(model, train_loader, optimizer): model.train() for imgs, targets in train_loader: # imgs: (batch, 3, 640, 640) targets: list of boxes preds model(imgs) loss compute_loss(preds, targets) # 包含分类定位损失 optimizer.zero_grad(); loss.backward(); optimizer.step() print(fLoss: {loss.item():.4f}) # 无人机巡检系统主循环伪代码 def drone_inspection_pipeline(): model YOLOv11_Modified(num_classes5) model.load_state_dict(torch.load(pv_fault.pth)) cap cv2.VideoCapture(udp://192.168.1.100:554) # 无人机视频流 while True: ret, frame cap.read() if not ret: break img cv2.resize(frame, (640,640)) tensor torch.from_numpy(img).float().permute(2,0,1).unsqueeze(0) / 255.0 with torch.no_grad(): detections model(tensor) for det in detections: # 解析检测框 x1,y1,x2,y2,conf,cls det cv2.rectangle(frame, (x1,y1), (x2,y2), (0,255,0), 2) cv2.putText(frame, f{class_names[int(cls)]}:{conf:.2f}, (x1,y1-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2) cv2.imshow(Drone View, frame) if cv2.waitKey(1) 0xFF ord(q): break 如有问题可以直接沟通