Skip to content

Commit a1e5f9a

Browse files
authored
New model.yaml activation: field (#9371)
* New model.yaml `activation:` field Add optional model yaml activation field to define model-wide activations, i.e.: ```yaml activation: nn.LeakyReLU(0.1) # activation with arguments activation: nn.SiLU() # activation with no arguments ``` Signed-off-by: Glenn Jocher <[email protected]> * Update yolo.py Signed-off-by: Glenn Jocher <[email protected]> * Add example models * l to m models * update * Add yolov5s-LeakyReLU.yaml * Update yolov5s-LeakyReLU.yaml Signed-off-by: Glenn Jocher <[email protected]> Signed-off-by: Glenn Jocher <[email protected]>
1 parent 58ad5ca commit a1e5f9a

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

models/common.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ def autopad(k, p=None, d=1): # kernel, padding, dilation
3939

4040
class Conv(nn.Module):
4141
# Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation)
42+
act = nn.SiLU() # default activation
43+
4244
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
4345
super().__init__()
4446
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
4547
self.bn = nn.BatchNorm2d(c2)
46-
self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
48+
self.act = self.act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
4749

4850
def forward(self, x):
4951
return self.act(self.bn(self.conv(x)))
@@ -54,8 +56,8 @@ def forward_fuse(self, x):
5456

5557
class DWConv(Conv):
5658
# Depth-wise convolution
57-
def __init__(self, c1, c2, k=1, s=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
58-
super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
59+
def __init__(self, c1, c2, k=1, s=1, d=1, act=True): # ch_in, ch_out, kernel, stride, dilation, activation
60+
super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), d=d, act=act)
5961

6062

6163
class DWConvTranspose2d(nn.ConvTranspose2d):

models/hub/yolov5s-LeakyReLU.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2+
3+
# Parameters
4+
nc: 80 # number of classes
5+
activation: nn.LeakyReLU(0.1) # <----- Conv() activation used throughout entire YOLOv5 model
6+
depth_multiple: 0.33 # model depth multiple
7+
width_multiple: 0.50 # layer channel multiple
8+
anchors:
9+
- [10,13, 16,30, 33,23] # P3/8
10+
- [30,61, 62,45, 59,119] # P4/16
11+
- [116,90, 156,198, 373,326] # P5/32
12+
13+
# YOLOv5 v6.0 backbone
14+
backbone:
15+
# [from, number, module, args]
16+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18+
[-1, 3, C3, [128]],
19+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20+
[-1, 6, C3, [256]],
21+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22+
[-1, 9, C3, [512]],
23+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
24+
[-1, 3, C3, [1024]],
25+
[-1, 1, SPPF, [1024, 5]], # 9
26+
]
27+
28+
# YOLOv5 v6.0 head
29+
head:
30+
[[-1, 1, Conv, [512, 1, 1]],
31+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
32+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
33+
[-1, 3, C3, [512, False]], # 13
34+
35+
[-1, 1, Conv, [256, 1, 1]],
36+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
37+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
38+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
39+
40+
[-1, 1, Conv, [256, 3, 2]],
41+
[[-1, 14], 1, Concat, [1]], # cat head P4
42+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
43+
44+
[-1, 1, Conv, [512, 3, 2]],
45+
[[-1, 10], 1, Concat, [1]], # cat head P5
46+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
47+
48+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
49+
]

models/yolo.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,12 @@ def _from_yaml(self, cfg):
297297

298298

299299
def parse_model(d, ch): # model_dict, input_channels(3)
300+
# Parse a YOLOv5 model.yaml dictionary
300301
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
301-
anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple']
302+
anchors, nc, gd, gw, act = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'], d.get('activation')
303+
if act:
304+
Conv.act = eval(act) # redefine default activation, i.e. Conv.act = nn.SiLU()
305+
LOGGER.info(f"{colorstr('activation:')} {act}") # print
302306
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
303307
no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
304308

0 commit comments

Comments
 (0)