yolov5和yolox中的focus模块

YOLO里面输入的图像会先进入Focus模块,该模块主要是实现没有信息丢失的下采样

很形象的一张图:

 


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)

    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)

上面的实现是来自YOLOX。

参考链接:

 

posted @ 2022-06-29 22:24  海_纳百川  阅读(840)  评论(0编辑  收藏  举报
本站总访问量