当前位置:网站首页>How to change the panet layer in yolov5 to bifpn
How to change the panet layer in yolov5 to bifpn
2022-07-03 02:26:00 【Yisu cloud】
How to yolov5 Medium PANet Layer changed to BiFPN
Today, I want to share with you how to yolov5 Medium PANet Layer changed to BiFPN Relevant knowledge points of , Detailed content , Clear logic , I believe most people still know too much about this knowledge , So share this article for your reference , I hope you will gain something after reading this article , Now let's take a look .
One 、Add
1. stay common.py Then add the following code
# combination BiFPN Set learnable parameters Learn the weights of different branches # Two branches add operation class BiFPN_Add2(nn.Module): def __init__(self, c1, c2): super(BiFPN_Add2, self).__init__() # Set learnable parameters nn.Parameter The role of is : Will be a non trainable type Tensor Convert to a training type parameter # And the parameter will be registered with the host model Be part of it namely model.parameters() Will include this parameter # Thus, it can be automatically optimized together when the parameters are optimized 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])) # Three branches add operation 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) # Normalize the weights # Fast normalized fusion return self.conv(self.silu(weight[0] * x[0] + weight[1] * x[1] + weight[2] * x[2]))
2.yolov5s.yaml Make changes
# YOLOv5 ???? by Ultralytics, GPL-3.0 license # Parametersnc: 80 # number of classesdepth_multiple: 0.33 # model depth multiplewidth_multiple: 0.50 # layer channel multipleanchors: - [10,13, 16,30, 33,23] # P3/8 - [30,61, 62,45, 59,119] # P4/16 - [116,90, 156,198, 373,326] # P5/32 # YOLOv5 v6.0 backbonebackbone: # [from, number, module, args] [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 [-1, 3, C3, [128]], [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 [-1, 6, C3, [256]], [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 [-1, 9, C3, [512]], [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 [-1, 3, C3, [1024]], [-1, 1, SPPF, [1024, 5]], # 9 ] # YOLOv5 v6.0 BiFPN headhead: [[-1, 1, Conv, [512, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 6], 1, BiFPN_Add2, [256, 256]], # cat backbone P4 [-1, 3, C3, [512, False]], # 13 [-1, 1, Conv, [256, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 4], 1, BiFPN_Add2, [128, 128]], # cat backbone P3 [-1, 3, C3, [256, False]], # 17 (P3/8-small) [-1, 1, Conv, [512, 3, 2]], # in order to BiFPN correct add, adjustment channel Count [[-1, 13, 6], 1, BiFPN_Add3, [256, 256]], # cat P4 <--- BiFPN change Be careful v5s The number of channels is half of the default parameter [-1, 3, C3, [512, False]], # 20 (P4/16-medium) [-1, 1, Conv, [512, 3, 2]], [[-1, 10], 1, BiFPN_Add2, [256, 256]], # cat head P5 [-1, 3, C3, [1024, False]], # 23 (P5/32-large) [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) ]
3. modify yolo.py, stay parse_model Find in the function elif m is Concat: sentence , Add... After it BiFPN_Add Related statements :

# add to bifpn_add structure elif m in [BiFPN_Add2, BiFPN_Add3]: c2 = max([ch[x] for x in f])
4. modify train.py, Add... To the optimizer BiFPN Weight parameter of
take BiFPN_Add2 and BiFPN_Add3 Function w Parameters , Join in g1

# BiFPN_Concat elif isinstance(v, BiFPN_Add2) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter): g1.append(v.w) elif isinstance(v, BiFPN_Add3) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter): g1.append(v.w)
Then import these two packages

Two 、Concat
1. stay common.py Then add the following code
# combination BiFPN Set learnable parameters Learn the weights of different branches # Two branches concat operation class BiFPN_Concat2(nn.Module): def __init__(self, dimension=1): super(BiFPN_Concat2, self).__init__() self.d = dimension self.w = nn.Parameter(torch.ones(2, dtype=torch.float32), requires_grad=True) self.epsilon = 0.0001 def forward(self, x): w = self.w weight = w / (torch.sum(w, dim=0) + self.epsilon) # Normalize the weights # Fast normalized fusion x = [weight[0] * x[0], weight[1] * x[1]] return torch.cat(x, self.d) # Three branches concat operation class BiFPN_Concat3(nn.Module): def __init__(self, dimension=1): super(BiFPN_Concat3, self).__init__() self.d = dimension # Set learnable parameters nn.Parameter The role of is : Will be a non trainable type Tensor Convert to a training type parameter # And the parameter will be registered with the host model Be part of it namely model.parameters() Will include this parameter # Thus, it can be automatically optimized together when the parameters are optimized self.w = nn.Parameter(torch.ones(3, dtype=torch.float32), requires_grad=True) self.epsilon = 0.0001 def forward(self, x): w = self.w weight = w / (torch.sum(w, dim=0) + self.epsilon) # Normalize the weights # Fast normalized fusion x = [weight[0] * x[0], weight[1] * x[1], weight[2] * x[2]] return torch.cat(x, self.d)
2.yolov5s.yaml Make changes
# YOLOv5 ???? by Ultralytics, GPL-3.0 license # Parametersnc: 80 # number of classesdepth_multiple: 0.33 # model depth multiplewidth_multiple: 0.50 # layer channel multipleanchors: - [10,13, 16,30, 33,23] # P3/8 - [30,61, 62,45, 59,119] # P4/16 - [116,90, 156,198, 373,326] # P5/32 # YOLOv5 v6.0 backbonebackbone: # [from, number, module, args] [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 [-1, 3, C3, [128]], [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 [-1, 6, C3, [256]], [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 [-1, 9, C3, [512]], [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 [-1, 3, C3, [1024]], [-1, 1, SPPF, [1024, 5]], # 9 ] # YOLOv5 v6.0 BiFPN headhead: [[-1, 1, Conv, [512, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 6], 1, BiFPN_Concat2, [1]], # cat backbone P4 <--- BiFPN change [-1, 3, C3, [512, False]], # 13 [-1, 1, Conv, [256, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 4], 1, BiFPN_Concat2, [1]], # cat backbone P3 <--- BiFPN change [-1, 3, C3, [256, False]], # 17 (P3/8-small) [-1, 1, Conv, [256, 3, 2]], [[-1, 14, 6], 1, BiFPN_Concat3, [1]], # cat P4 <--- BiFPN change [-1, 3, C3, [512, False]], # 20 (P4/16-medium) [-1, 1, Conv, [512, 3, 2]], [[-1, 10], 1, BiFPN_Concat2, [1]], # cat head P5 <--- BiFPN change [-1, 3, C3, [1024, False]], # 23 (P5/32-large) [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) ]
3. modify yolo.py, stay parse_model Find in the function elif m is Concat: sentence , Add... After it BiFPN_Concat Related statements :

# add to bifpn_concat structure elif m in [Concat, BiFPN_Concat2, BiFPN_Concat3]: c2 = sum(ch[x] for x in f)
4. modify train.py, Add... To the optimizer BiFPN Weight parameter of
Add compound formula as above (Add)
# BiFPN_Concat elif isinstance(v, BiFPN_Concat2) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter): g1.append(v.w) elif isinstance(v, BiFPN_Concat3) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter): g1.append(v.w)
That's all “ How to yolov5 Medium PANet Layer changed to BiFPN” All the content of this article , Thank you for reading ! I believe you will gain a lot after reading this article , Xiaobian will update different knowledge for you every day , If you want to learn more , Please pay attention to the Yisu cloud industry information channel .
边栏推荐
- Unrecognized SSL message, plaintext connection?
- CFdiv2-Fixed Point Guessing-(區間答案二分)
- The Sandbox阐释对元宇宙平台的愿景
- SPI机制
- Iptables layer 4 forwarding
- 詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信
- oauth2.0鉴权,登录访问 “/oauth/token”,请求头Authorization(basicToken)如何取值???
- udp接收队列以及多次初始化的测试
- GBase 8c系统表-pg_conversion
- Kotlin middle process understanding and Practice (I)
猜你喜欢

通达OA v12流程中心

Memory pool (understand the process of new developing space from the perspective of kernel)

Tongda OA V12 process center

oauth2.0鉴权,登录访问 “/oauth/token”,请求头Authorization(basicToken)如何取值???

Detailed analysis of micro service component sentinel (hystrix)

搭建私有云盘 cloudreve
【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)

错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决

stm32F407-------DMA

Distributed transaction solution
随机推荐
[Hcia]No.15 Vlan间通信
Awk from introduction to earth (0) overview of awk
Cfdiv2 fixed point guessing- (interval answer two points)
GBase 8c系统表pg_cast
各国Web3现状与未来
[Yu Yue education] reference materials of chemical experiment safety knowledge of University of science and technology of China
Cfdiv2 Fixed Point Guessing - (2 points for Interval answer)
【教程】chrome關閉跨域策略cors、samesite,跨域帶上cookie
【教程】chrome关闭跨域策略cors、samesite,跨域带上cookie
Detailed introduction to the deployment and usage of the Nacos registry
错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
PyTorch 卷积网络正则化 DropBlock
Gbase 8C system table PG_ collation
Memory pool (understand the process of new developing space from the perspective of kernel)
UDP receive queue and multiple initialization test
Machine learning process and method
Awk from getting started to being buried (2) understand the built-in variables and the use of variables in awk
GBase 8c系统表-pg_collation
Current situation and future of Web3 in various countries
Tongda OA V12 process center