1,本文介绍
BiFPN(Bidirectional Feature Pyramid Network)是一种增强特征金字塔网络(FPN)的方法,旨在改善多尺度特征融合。BiFPN的主要创新点包括:
-
双向特征融合:与传统FPN仅在自下而上的方向进行特征融合不同,BiFPN引入了双向融合机制。它不仅从低层特征向高层传递信息,还从高层特征向低层传递信息,这种双向流动增强了特征的表达能力。
-
加权特征融合:BiFPN通过加权融合机制,自动调整不同尺度特征的重要性,从而实现了更精准的特征融合。这种加权策略使得特征的利用更加高效,有效提升了目标检测性能。
-
高效的特征处理:BiFPN采用了高效的计算结构来减少计算开销,同时保持了良好的特征融合效果。通过简化和优化计算流程,BiFPN提高了网络的运行速度和精度。
整体上,BiFPN在多尺度特征处理和融合上比传统FPN更具优势,提升了图像识别和目标检测的性能。
关于BiFPN的详细介绍可以看论文:https://arxiv.org/pdf/1911.09070.pdf
本文将讲解如何将BiFPN融合进yolov8
话不多说,上代码!
2, 将BiFPN融合进yolov8
2.1 步骤一
找到如下的目录'ultralytics/nn/modules',然后在这个目录下创建一个BiFPN.py文件,文件名字可以根据你自己的习惯起,然后将BiFPN的核心代码复制进去。
import torch.nn as nn
import torch
class swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)
class Bi_FPN(nn.Module):
def __init__(self, length):
super().__init__()
self.weight = nn.Parameter(torch.ones(length, dtype=torch.float32), requires_grad=True)
self.swish = swish()
self.epsilon = 0.0001
def forward(self, x):
weights = self.weight / (torch.sum(self.swish(self.weight), dim=0) + self.epsilon) # 权重归一化处理
weighted_feature_maps = [weights[i] * x[i] for i in range(len(x))]
stacked_feature_maps = torch.stack(weighted_feature_maps, dim=0)
result = torch.sum(stacked_feature_maps, dim=0)
return result
2.2 步骤二
在task.py导入我们的模块
from .modules.BiFPN import Bi_FPN
2.3 步骤三
在task.py的parse_model方法里面注册我们的模块
elif m in {Bi_FPN}:
length = len([ch[x] for x in f])
args = [length]
到此注册成功,复制后面的yaml文件直接运行即可
yaml文件
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
# YOLOv8.0n backbone
backbone:
# [from, repeats, module, args]
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
- [-1, 3, C2f, [128, True]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 6, C2f, [256, True]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 6, C2f, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 3, C2f, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
# YOLOv8.0n head
head:
- [4, 1, Conv, [512]] # 10-P3/8
- [6, 1, Conv, [512]] # 11-P4/16
- [9, 1, Conv, [512]] # 12-P5/32
- [-1, 1, nn.Upsample, [None, 2, 'nearest']] # 13 P5->P4
- [[-1, 11], 1, Bi_FPN, []] # 14
- [-1, 3, C2f, [512]] # 15-P4/16
- [-1, 1, nn.Upsample, [None, 2, 'nearest']] # 16 P4->P3
- [[-1, 10], 1, Bi_FPN, []] # 17
- [-1, 3, C2f, [512]] # 18-P3/8
- [1, 1, Conv, [512, 3, 2]] # 19 P2->P3
- [[-1, 10, 18], 1, Bi_FPN, []] # 20
- [-1, 3, C2f, [256]] # 21-P3/8
- [-1, 1, Conv, [512, 3, 2]] # 22 P3->P4
- [[-1, 11, 15], 1, Bi_FPN, []] # 23
- [-1, 3, C2f, [512]] # 24-P4/16
- [-1, 1, Conv, [512, 3, 2]] # 25 P4->P5
- [[-1, 12], 1, Bi_FPN, []] # 26
- [-1, 3, C2f, [1024]] # 27-P5/32
- [[21, 24, 27], 1, Detect, [nc]] # Detect(P3, P4, P5)
不知不觉已经看完了哦,动动小手留个点赞收藏吧--_--