###################### BiFPN ###################################
# BiFPN
# 两个特征图add操作
class BiFPN_Add2(nn.Module):
def __init__(self, c1, c2):
super(BiFPN_Add2, self).__init__()
self.w = nn.Parameter(torch.ones(2, dtype=torch.float32), requires_grad=True)
self.epsilon = 0.0001
self.conv = nn.Conv2d(c1, c2, kernel_size=1, stride=1, padding=0)
self.silu = nn.SiLU()
def forward(self, x):
w = self.w
weight = w / (torch.sum(w, dim=0) + self.epsilon)
return self.conv(self.silu(weight[0] * x[0] + weight[1] * x[1]))
# 三个特征图add操作
class BiFPN_Add3(nn.Module):
def __init__(self, c1, c2):
super(BiFPN_Add3, self).__init__()
self.w = nn.Parameter(torch.ones(3, dtype=torch.float32), requires_grad=True)
self.epsilon = 0.0001
self.conv = nn.Conv2d(c1, c2, kernel_size=1, stride=1, padding=0)
self.silu = nn.SiLU()
def forward(self, x):
w = self.w
weight = w / (torch.sum(w, dim=0) + self.epsilon)
return self.conv(self.silu(weight[0] * x[0] + weight[1] * x[1] + weight[2] * x[2]))
###################### BiFPN ###################################
1.在ultralytics/nn/modules/block.py文件的最后加入上述代码。并在文件上方的__all__中加入下述标框代码
2.在ultralytics/nn/modules/__init__.py中加入下述标框代码
3.ultralytics/nn/tasks.py中加入下述标框代码
4.在ultralytics/cfg/models/v8目录下创建yolov8-BiFPN.yaml文件,复制下述代码。
# Ultralytics YOLO
标签:__,Conv,nn,self,yolov8,EffectiveSE,模块,BiFPN,C2f
From: https://blog.csdn.net/qq_63632802/article/details/137077456