计算机视觉与深度学习融合的群养猪行为识别与分类算法【附算法】
✨ 长期致力于计算机视觉、深度学习、攻击识别、多物体玩耍识别、饮水和玩耍饮水器分类、进食识别、行为量化研究工作擅长数据搜集与处理、建模仿真、程序编写、仿真设计。✅ 专业定制毕设、代码✅如需沟通交流点击《获取方式》1时空注意力卷积网络攻击检测构建基于SlowFast双路网络与通道时空注意力融合的模型命名为SF-AT。快速路径以高帧率30fps捕获运动细节慢速路径以低帧率6fps捕获全局语义。引入通道时空注意力模块在时空维度上自适应校准特征权重。在自建群养猪视频数据集上训练包含二百四十小时标注数据攻击行为片段标注七千二百段。训练采用交叉熵损失和在线难例挖掘初始学习率零点零零一共六十轮。测试集上攻击识别准确率达到百分之九十八点二误报率百分之二点三。相比基线SlowFast网络准确率提高四点一个百分点。模型在Jetson Xavier NX边缘设备上推理速度为二十五帧每秒满足实时监控要求。采用时序动作定位网络进一步输出攻击起止时间平均时间交并比达到零点七八。2多目标交互图卷积玩耍行为识别针对猪与多种玩具篮球、链条、橡胶棒的交互设计图卷积网络GCN-Play。将每头猪的头部、躯干、四肢关键点检测使用HRNet和玩具位置作为图节点边表示空间距离和交互可能性。图卷积层数三层隐层维度一百二十八。在七百段玩耍视频上训练识别玩耍与非玩耍的准确率为百分之九十六点五对玩具类型分类准确率为百分之九十一点三。进一步引入时间卷积网络捕捉动作序列实现对玩耍强度的量化轻度、中度、剧烈kappa系数为零点八六。该方法首次实现了对群体中多物体玩耍行为的自动识别玩耍持续时间与人工记录相关系数零点九四。3轻量化双流网络饮水分辨与进食时长估计为解决饮水与玩饮水器难以区分的问题设计双流网络WaterNet。空间流采用MobileNetV3处理单帧图像识别饮水区域时间流采用时序差分卷积处理光流图识别头部摆动模式。融合层使用门控机制自适应加权。在保育猪圈采集数据标注饮水事件三千二百次玩饮水器事件一千八百次。模型分类准确率百分之九十三点四其中饮水召回率百分之九十五点二。进食识别方面基于YOLOv5检测猪头部和食槽区域提取头部与食槽的交并比序列采用隐半马尔可夫模型分割进食回合。进食时间估计与人工测量对比绝对误差小于七秒每餐。模型已部署到猪场自动饲喂系统实时生成每头猪的日采食时长曲线帮助早期发现病弱个体。import torch import torch.nn as nn import torch.nn.functional as F from torchvision.models import mobilenet_v3_small class SlowFast_AT(nn.Module): def __init__(self, num_classes2): super().__init__() self.slow_path mobilenet_v3_small(pretrainedTrue) self.fast_path mobilenet_v3_small(pretrainedTrue) # replace first conv to accept different temporal strides self.slow_path.features[0][0] nn.Conv2d(3, 16, kernel_size7, stride2) self.fast_path.features[0][0] nn.Conv2d(3, 16, kernel_size7, stride1) # temporal attention module self.temporal_attn nn.Sequential( nn.AdaptiveAvgPool3d((1,1,1)), nn.Conv3d(1280, 64, kernel_size1), nn.ReLU(), nn.Conv3d(64, 1280, kernel_size1), nn.Sigmoid() ) self.fc nn.Linear(1280*2, num_classes) def forward(self, x_slow, x_fast): # x_slow shape: (B,3,T_slow,H,W) , T_slow 8 # x_fast shape: (B,3,T_fast,H,W) , T_fast 32 b,_,t_s, h,w x_slow.shape x_slow x_slow.permute(0,2,1,3,4).reshape(b*t_s, 3, h, w) out_slow self.slow_path.features(x_slow) out_slow out_slow.view(b, t_s, -1).mean(dim1) b,_,t_f, h,w x_fast.shape x_fast x_fast.permute(0,2,1,3,4).reshape(b*t_f, 3, h, w) out_fast self.fast_path.features(x_fast) out_fast out_fast.view(b, t_f, -1).mean(dim1) concat torch.cat([out_slow, out_fast], dim1) attn self.temporal_attn(concat.unsqueeze(-1).unsqueeze(-1)) return self.fc(concat * attn.squeeze()) class GCN_Play(nn.Module): def __init__(self, in_features64, hidden128, num_classes2): super().__init__() self.gc1 nn.Linear(in_features, hidden) self.gc2 nn.Linear(hidden, hidden) self.fc nn.Linear(hidden, num_classes) self.adj None def forward(self, x, adj): # x: (batch, nodes, in_features) x F.relu(self.gc1(x)) x torch.bmm(adj, x) # graph convolution x F.relu(self.gc2(x)) x torch.bmm(adj, x) x x.mean(dim1) return self.fc(x) class WaterNet(nn.Module): def __init__(self): super().__init__() self.spatial mobilenet_v3_small(pretrainedTrue) self.temporal nn.Sequential( nn.Conv3d(3, 32, kernel_size(3,3,3), padding1), nn.ReLU(), nn.AdaptiveAvgPool3d((1,1,1)) ) self.gate nn.Sigmoid() self.fc nn.Linear(128032, 2) def forward(self, rgb_frame, optical_flow_clip): spatial_feat self.spatial.features(rgb_frame).mean([2,3]) flow_feat self.temporal(optical_flow_clip).view(rgb_frame.size(0), -1) alpha self.gate(spatial_feat) fused alpha * spatial_feat (1-alpha) * flow_feat return self.fc(fused)