Spaces:
Running
Running
🔧 [Update] the config, remove conv, using Pool
Browse files- yolo/config/model/v7-base.yaml +10 -10
- yolo/model/module.py +19 -65
- yolo/model/yolo.py +1 -1
yolo/config/model/v7-base.yaml
CHANGED
|
@@ -31,8 +31,8 @@ model:
|
|
| 31 |
source: [-1, -3, -5, -6]
|
| 32 |
- Conv:
|
| 33 |
args: {out_channels: 256, kernel_size: 1}
|
| 34 |
-
-
|
| 35 |
-
args: {}
|
| 36 |
- Conv:
|
| 37 |
args: {out_channels: 128, kernel_size: 1}
|
| 38 |
- Conv:
|
|
@@ -60,8 +60,8 @@ model:
|
|
| 60 |
tags: 8x
|
| 61 |
- Conv:
|
| 62 |
args: {out_channels: 512, kernel_size: 1}
|
| 63 |
-
-
|
| 64 |
-
args: {}
|
| 65 |
- Conv:
|
| 66 |
args: {out_channels: 256, kernel_size: 1}
|
| 67 |
- Conv:
|
|
@@ -89,8 +89,8 @@ model:
|
|
| 89 |
- Conv:
|
| 90 |
args: {out_channels: 1024, kernel_size: 1}
|
| 91 |
tags: 16x
|
| 92 |
-
-
|
| 93 |
-
args: {}
|
| 94 |
- Conv:
|
| 95 |
args: {out_channels: 512, kernel_size: 1}
|
| 96 |
- Conv:
|
|
@@ -173,8 +173,8 @@ model:
|
|
| 173 |
source: [-1, -2, -3, -4, -5, -6]
|
| 174 |
- Conv:
|
| 175 |
args: {out_channels: 128, kernel_size: 1}
|
| 176 |
-
-
|
| 177 |
-
args: {}
|
| 178 |
- Conv:
|
| 179 |
args: {out_channels: 128, kernel_size: 1}
|
| 180 |
- Conv:
|
|
@@ -201,8 +201,8 @@ model:
|
|
| 201 |
source: [-1, -2, -3, -4, -5, -6]
|
| 202 |
- Conv:
|
| 203 |
args: {out_channels: 256, kernel_size: 1}
|
| 204 |
-
-
|
| 205 |
-
args: {}
|
| 206 |
- Conv:
|
| 207 |
args: {out_channels: 256, kernel_size: 1}
|
| 208 |
- Conv:
|
|
|
|
| 31 |
source: [-1, -3, -5, -6]
|
| 32 |
- Conv:
|
| 33 |
args: {out_channels: 256, kernel_size: 1}
|
| 34 |
+
- Pool:
|
| 35 |
+
args: {padding: 0}
|
| 36 |
- Conv:
|
| 37 |
args: {out_channels: 128, kernel_size: 1}
|
| 38 |
- Conv:
|
|
|
|
| 60 |
tags: 8x
|
| 61 |
- Conv:
|
| 62 |
args: {out_channels: 512, kernel_size: 1}
|
| 63 |
+
- Pool:
|
| 64 |
+
args: {padding: 0}
|
| 65 |
- Conv:
|
| 66 |
args: {out_channels: 256, kernel_size: 1}
|
| 67 |
- Conv:
|
|
|
|
| 89 |
- Conv:
|
| 90 |
args: {out_channels: 1024, kernel_size: 1}
|
| 91 |
tags: 16x
|
| 92 |
+
- Pool:
|
| 93 |
+
args: {padding: 0}
|
| 94 |
- Conv:
|
| 95 |
args: {out_channels: 512, kernel_size: 1}
|
| 96 |
- Conv:
|
|
|
|
| 173 |
source: [-1, -2, -3, -4, -5, -6]
|
| 174 |
- Conv:
|
| 175 |
args: {out_channels: 128, kernel_size: 1}
|
| 176 |
+
- Pool:
|
| 177 |
+
args: {padding: 0}
|
| 178 |
- Conv:
|
| 179 |
args: {out_channels: 128, kernel_size: 1}
|
| 180 |
- Conv:
|
|
|
|
| 201 |
source: [-1, -2, -3, -4, -5, -6]
|
| 202 |
- Conv:
|
| 203 |
args: {out_channels: 256, kernel_size: 1}
|
| 204 |
+
- Pool:
|
| 205 |
+
args: {padding: 0}
|
| 206 |
- Conv:
|
| 207 |
args: {out_channels: 256, kernel_size: 1}
|
| 208 |
- Conv:
|
yolo/model/module.py
CHANGED
|
@@ -11,7 +11,13 @@ class Conv(nn.Module):
|
|
| 11 |
"""A basic convolutional block that includes convolution, batch normalization, and activation."""
|
| 12 |
|
| 13 |
def __init__(
|
| 14 |
-
self,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
):
|
| 16 |
super().__init__()
|
| 17 |
kwargs.setdefault("padding", auto_pad(kernel_size, **kwargs))
|
|
@@ -26,7 +32,7 @@ class Conv(nn.Module):
|
|
| 26 |
class Pool(nn.Module):
|
| 27 |
"""A generic pooling block supporting 'max' and 'avg' pooling methods."""
|
| 28 |
|
| 29 |
-
def __init__(self, method: str = "max", kernel_size: _size_2_t =
|
| 30 |
super().__init__()
|
| 31 |
kwargs.setdefault("padding", auto_pad(kernel_size, **kwargs))
|
| 32 |
pool_classes = {"max": nn.MaxPool2d, "avg": nn.AvgPool2d}
|
|
@@ -80,7 +86,7 @@ class SPPELAN(nn.Module):
|
|
| 80 |
neck_channels = neck_channels or out_channels // 2
|
| 81 |
|
| 82 |
self.conv1 = Conv(in_channels, neck_channels, kernel_size=1)
|
| 83 |
-
self.pools = nn.ModuleList([Pool("max", 5,
|
| 84 |
self.conv5 = Conv(4 * neck_channels, out_channels, kernel_size=1)
|
| 85 |
|
| 86 |
def forward(self, x: Tensor) -> Tensor:
|
|
@@ -93,49 +99,6 @@ class SPPELAN(nn.Module):
|
|
| 93 |
#### -- ####
|
| 94 |
|
| 95 |
|
| 96 |
-
# basic
|
| 97 |
-
class Conv(nn.Module):
|
| 98 |
-
# basic convlution
|
| 99 |
-
def __init__(
|
| 100 |
-
self,
|
| 101 |
-
in_channels,
|
| 102 |
-
out_channels,
|
| 103 |
-
kernel_size,
|
| 104 |
-
stride=1,
|
| 105 |
-
padding=None,
|
| 106 |
-
dilation=1,
|
| 107 |
-
groups=1,
|
| 108 |
-
act=nn.SiLU(),
|
| 109 |
-
bias=False,
|
| 110 |
-
auto_padding=True,
|
| 111 |
-
padding_mode="zeros",
|
| 112 |
-
):
|
| 113 |
-
|
| 114 |
-
super().__init__()
|
| 115 |
-
|
| 116 |
-
# not yet handle the case when dilation is a tuple
|
| 117 |
-
if auto_padding:
|
| 118 |
-
if isinstance(kernel_size, int):
|
| 119 |
-
padding = (dilation * (kernel_size - 1) + 1) // 2
|
| 120 |
-
else:
|
| 121 |
-
padding = [(dilation * (k - 1) + 1) // 2 for k in kernel_size]
|
| 122 |
-
|
| 123 |
-
self.conv = nn.Conv2d(
|
| 124 |
-
in_channels, out_channels, kernel_size, stride, padding, groups=groups, dilation=dilation, bias=bias
|
| 125 |
-
)
|
| 126 |
-
self.bn = nn.BatchNorm2d(out_channels)
|
| 127 |
-
self.act = act if isinstance(act, nn.Module) else nn.Identity()
|
| 128 |
-
|
| 129 |
-
def forward(self, x):
|
| 130 |
-
return self.act(self.bn(self.conv(x)))
|
| 131 |
-
|
| 132 |
-
def forward_fuse(self, x):
|
| 133 |
-
return self.act(self.conv(x))
|
| 134 |
-
|
| 135 |
-
# to be implement
|
| 136 |
-
# def fuse_conv_bn(self):
|
| 137 |
-
|
| 138 |
-
|
| 139 |
# RepVGG
|
| 140 |
class RepConv(nn.Module):
|
| 141 |
# https://github.com/DingXiaoH/RepVGG
|
|
@@ -145,8 +108,8 @@ class RepConv(nn.Module):
|
|
| 145 |
|
| 146 |
super().__init__()
|
| 147 |
self.deploy = deploy
|
| 148 |
-
self.conv1 = Conv(in_channels, out_channels, kernel_size, stride, groups=groups,
|
| 149 |
-
self.conv2 = Conv(in_channels, out_channels, 1, stride, groups=groups,
|
| 150 |
self.act = act if isinstance(act, nn.Module) else nn.Identity()
|
| 151 |
|
| 152 |
def forward(self, x):
|
|
@@ -420,29 +383,20 @@ class Concat(nn.Module):
|
|
| 420 |
return torch.cat(x, self.dim)
|
| 421 |
|
| 422 |
|
| 423 |
-
class MaxPool(nn.Module):
|
| 424 |
-
def __init__(self, kernel_size: int = 2):
|
| 425 |
-
super().__init__()
|
| 426 |
-
self.pool_layer = nn.MaxPool2d(kernel_size=kernel_size, stride=kernel_size)
|
| 427 |
-
|
| 428 |
-
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 429 |
-
return self.pool_layer(x)
|
| 430 |
-
|
| 431 |
-
|
| 432 |
# TODO: check if Mit
|
| 433 |
class SPPCSPConv(nn.Module):
|
| 434 |
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
| 435 |
def __init__(self, in_channels, out_channels, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)):
|
| 436 |
super(SPPCSPConv, self).__init__()
|
| 437 |
c_ = int(2 * out_channels * e) # hidden channels
|
| 438 |
-
self.cv1 = Conv(in_channels, c_, 1
|
| 439 |
-
self.cv2 = Conv(in_channels, c_, 1
|
| 440 |
-
self.cv3 = Conv(c_, c_, 3
|
| 441 |
-
self.cv4 = Conv(c_, c_, 1
|
| 442 |
-
self.m = nn.ModuleList([
|
| 443 |
-
self.cv5 = Conv(4 * c_, c_, 1
|
| 444 |
-
self.cv6 = Conv(c_, c_, 3
|
| 445 |
-
self.cv7 = Conv(2 * c_, out_channels, 1
|
| 446 |
|
| 447 |
def forward(self, x):
|
| 448 |
x1 = self.cv4(self.cv3(self.cv1(x)))
|
|
|
|
| 11 |
"""A basic convolutional block that includes convolution, batch normalization, and activation."""
|
| 12 |
|
| 13 |
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
in_channels: int,
|
| 16 |
+
out_channels: int,
|
| 17 |
+
kernel_size: _size_2_t,
|
| 18 |
+
*,
|
| 19 |
+
activation: Optional[str] = "SiLU",
|
| 20 |
+
**kwargs
|
| 21 |
):
|
| 22 |
super().__init__()
|
| 23 |
kwargs.setdefault("padding", auto_pad(kernel_size, **kwargs))
|
|
|
|
| 32 |
class Pool(nn.Module):
|
| 33 |
"""A generic pooling block supporting 'max' and 'avg' pooling methods."""
|
| 34 |
|
| 35 |
+
def __init__(self, method: str = "max", kernel_size: _size_2_t = 2, **kwargs):
|
| 36 |
super().__init__()
|
| 37 |
kwargs.setdefault("padding", auto_pad(kernel_size, **kwargs))
|
| 38 |
pool_classes = {"max": nn.MaxPool2d, "avg": nn.AvgPool2d}
|
|
|
|
| 86 |
neck_channels = neck_channels or out_channels // 2
|
| 87 |
|
| 88 |
self.conv1 = Conv(in_channels, neck_channels, kernel_size=1)
|
| 89 |
+
self.pools = nn.ModuleList([Pool("max", 5, stride=1) for _ in range(3)])
|
| 90 |
self.conv5 = Conv(4 * neck_channels, out_channels, kernel_size=1)
|
| 91 |
|
| 92 |
def forward(self, x: Tensor) -> Tensor:
|
|
|
|
| 99 |
#### -- ####
|
| 100 |
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# RepVGG
|
| 103 |
class RepConv(nn.Module):
|
| 104 |
# https://github.com/DingXiaoH/RepVGG
|
|
|
|
| 108 |
|
| 109 |
super().__init__()
|
| 110 |
self.deploy = deploy
|
| 111 |
+
self.conv1 = Conv(in_channels, out_channels, kernel_size, stride=stride, groups=groups, activation=False)
|
| 112 |
+
self.conv2 = Conv(in_channels, out_channels, 1, stride=stride, groups=groups, activation=False)
|
| 113 |
self.act = act if isinstance(act, nn.Module) else nn.Identity()
|
| 114 |
|
| 115 |
def forward(self, x):
|
|
|
|
| 383 |
return torch.cat(x, self.dim)
|
| 384 |
|
| 385 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
# TODO: check if Mit
|
| 387 |
class SPPCSPConv(nn.Module):
|
| 388 |
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
| 389 |
def __init__(self, in_channels, out_channels, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)):
|
| 390 |
super(SPPCSPConv, self).__init__()
|
| 391 |
c_ = int(2 * out_channels * e) # hidden channels
|
| 392 |
+
self.cv1 = Conv(in_channels, c_, 1)
|
| 393 |
+
self.cv2 = Conv(in_channels, c_, 1)
|
| 394 |
+
self.cv3 = Conv(c_, c_, 3)
|
| 395 |
+
self.cv4 = Conv(c_, c_, 1)
|
| 396 |
+
self.m = nn.ModuleList([Pool(method="max", kernel_size=x, stride=1, padding=x // 2) for x in k])
|
| 397 |
+
self.cv5 = Conv(4 * c_, c_, 1)
|
| 398 |
+
self.cv6 = Conv(c_, c_, 3)
|
| 399 |
+
self.cv7 = Conv(2 * c_, out_channels, 1)
|
| 400 |
|
| 401 |
def forward(self, x):
|
| 402 |
x1 = self.cv4(self.cv3(self.cv1(x)))
|
yolo/model/yolo.py
CHANGED
|
@@ -72,7 +72,7 @@ class YOLO(nn.Module):
|
|
| 72 |
def get_out_channels(self, layer_type: str, layer_args: dict, output_dim: list, source: Union[int, list]):
|
| 73 |
if "Conv" in layer_type:
|
| 74 |
return layer_args["out_channels"]
|
| 75 |
-
if layer_type in ["
|
| 76 |
return output_dim[source]
|
| 77 |
if layer_type == "Concat":
|
| 78 |
return sum(output_dim[idx] for idx in source)
|
|
|
|
| 72 |
def get_out_channels(self, layer_type: str, layer_args: dict, output_dim: list, source: Union[int, list]):
|
| 73 |
if "Conv" in layer_type:
|
| 74 |
return layer_args["out_channels"]
|
| 75 |
+
if layer_type in ["Pool", "UpSample"]:
|
| 76 |
return output_dim[source]
|
| 77 |
if layer_type == "Concat":
|
| 78 |
return sum(output_dim[idx] for idx in source)
|