一、CSPDarknet
1.Focus
Focus:下采样效果,关于facus的理解https://blog.csdn.net/qq_39056987/article/details/112712817
原issue的链接https://github.com/ultralytics/yolov5/discussions/3181
m1 = Focus(3, 64, 3) # YOLOv5 single layer
m2 = nn.Sequential(Conv(3, 32, 3, 1), Conv(32, 64, 3, 2), Bottleneck(64, 64)) # YOLOv3 equivalent layers
在索引中使用...可以表示任意多的维度。
a = torch.rand(4,3,28,28)
print(a[:, 1, ...].shape)#torch.Size([4, 28, 28])
print(a[..., :2].shape)#torch.Size([4, 3, 28, 2])
print(a[0, ..., ::2].shape)#torch.Size([3, 28, 14])
class Focus(nn.Module):
"""Focus width and height information into channel space."""
def __init__(self, in_channels, out_channels, ksize=1, stride=1, act="silu"):
super().__init__()
self.conv = BaseConv(in_channels * 4, out_channels, ksize, stride, act=act) # 这里输入通道变成了4倍
def forward(self, x):
# shape of x (b,c,w,h) -> y(b,4c,w/2,h/2)
patch_top_left = x[..., ::2, ::2]
patch_top_right = x[..., ::2, 1::2]
patch_bot_left = x[..., 1::2, ::2]
patch_bot_right = x[..., 1::2, 1::2]
x = torch.cat(
(
patch_top_left,
patch_bot_left,
patch_top_right,
patch_bot_right,
),
dim=1,
)
return self.conv(x)