yolox-backbone详解之Focus(含代码

本文详细介绍了CSPDarknet中的Focus模块,该模块用于将宽度和高度信息融合到通道空间。通过四个patch的拼接实现下采样效果,与YOLOv3的等效层进行比较。博客提供了相关的代码示例,解释了如何使用Focus层以及其在YOLOv5中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值