網站首頁 編程語言 正文
Yolov5如何更換BiFPN?
第一步:修改common.py
將如下代碼添加到common.py
文件中
# BiFPN # 兩個特征圖add操作 class BiFPN_Add2(nn.Module): def __init__(self, c1, c2): super(BiFPN_Add2, self).__init__() # 設置可學習參數 nn.Parameter的作用是:將一個不可訓練的類型Tensor轉換成可以訓練的類型parameter # 并且會向宿主模型注冊該參數 成為其一部分 即model.parameters()會包含這個parameter # 從而在參數優化的時候可以自動一起優化 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) # Fast normalized fusion return self.conv(self.silu(weight[0] * x[0] + weight[1] * x[1] + weight[2] * x[2]))
第二步:修改yolo.py
在parse_model
函數中找到elif m is Concat:
語句,在其后面加上BiFPN_Add
相關語句
elif m is Concat: c2 = sum(ch[x] for x in f) # 添加bifpn_add結構 elif m in [BiFPN_Add2, BiFPN_Add3]: c2 = max([ch[x] for x in f])
第三步:修改train.py
將BiFPN_Add2和BiFPN_Add3函數中定義的w參數,加入g1
g = [], [], [] # optimizer parameter groups bn = tuple(v for k, v in nn.__dict__.items() if 'Norm' in k) # normalization layers, i.e. BatchNorm2d() for v in model.modules(): # hasattr: 測試指定的對象是否具有給定的屬性,返回一個布爾值 if hasattr(v, 'bias') and isinstance(v.bias, nn.Parameter): # bias g[2].append(v.bias) if isinstance(v, bn): # weight (no decay) g[1].append(v.weight) elif hasattr(v, 'weight') and isinstance(v.weight, nn.Parameter): # weight (with decay) g[0].append(v.weight) # BiFPN_Concat elif isinstance(v, BiFPN_Add2) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter): g[1].append(v.w) elif isinstance(v, BiFPN_Add3) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter): g[1].append(v.w)
導入BiFPN_Add3
, BiFPN_Add2
from models.common import BiFPN_Add3, BiFPN_Add2
第四步:修改yolov5.yaml
將Concat
全部換成BiFPN_Add
注意:BiFPN_Add本質是add操作,因此輸入層通道數、feature map要完全對應
2022.8.25 官方也提供了BiFPN,可以嘗試用官方的
關于5m加BiFPN的文件我已經更新到了我的Git
總結
原文鏈接:https://blog.csdn.net/weixin_43694096/article/details/125148552
相關推薦
- 2022-08-27 JQuery自定義模態框效果_jquery
- 2022-02-22 rcp異常org.eclipse.swt.SWTException: Invalid thread
- 2021-12-08 C++數組的定義詳情_C 語言
- 2022-05-22 分享幾種python?變量合并方法_python
- 2022-03-30 Python機器學習應用之樸素貝葉斯篇_python
- 2022-09-08 執行go?vendor第三方包版本沖突問題解決_Golang
- 2022-05-12 python2中input()漏洞
- 2022-04-11 解決git 錯誤error: failed to push some refs to......
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支