Ultralytics:解读Focus模块

Ultralytics:解读Focus模块

前言

相关介绍

Ultralytics 简介

Ultralytics 基于多年的计算机视觉和人工智能基础研究,创建了最先进的 (SOTA) YOLO 模型。我们的模型不断更新性能和灵活性,快速、准确且易于使用。他们擅长对象检测、跟踪、实例分割、语义分割、图像分类和姿势估计任务。

前提条件

  • 熟悉Python、Pytorch

实验环境

bash 复制代码
Package                  Version
------------------------ ------------
Python                   3.11.8
absl-py                  2.4.0
accelerate               1.13.0
annotated-doc            0.0.4
anyio                    4.13.0
calflops                 0.3.2
certifi                  2026.4.22
charset-normalizer       3.4.7
click                    8.3.3
colorama                 0.4.6
contourpy                1.3.3
cycler                   0.12.1
filelock                 3.29.0
flatbuffers              25.12.19
fonttools                4.62.1
fsspec                   2026.4.0
grpcio                   1.80.0
h11                      0.16.0
hf-xet                   1.5.0
httpcore                 1.0.9
httpx                    0.28.1
huggingface_hub          1.14.0
idna                     3.15
Jinja2                   3.1.6
kiwisolver               1.5.0
Markdown                 3.10.2
markdown-it-py           4.2.0
MarkupSafe               3.0.3
matplotlib               3.10.9
mdurl                    0.1.2
ml_dtypes                0.5.0
mpmath                   1.3.0
networkx                 3.6.1
numpy                    1.26.4
nvidia-cublas-cu12       12.8.3.14
nvidia-cuda-cupti-cu12   12.8.57
nvidia-cuda-nvrtc-cu12   12.8.61
nvidia-cuda-runtime-cu12 12.8.57
nvidia-cudnn-cu12        9.7.1.26
nvidia-cufft-cu12        11.3.3.41
nvidia-cufile-cu12       1.13.0.11
nvidia-curand-cu12       10.3.9.55
nvidia-cusolver-cu12     11.7.2.55
nvidia-cusparse-cu12     12.5.7.53
nvidia-cusparselt-cu12   0.6.3
nvidia-nccl-cu12         2.26.2
nvidia-nvjitlink-cu12    12.8.61
nvidia-nvtx-cu12         12.8.55
onnx                     1.19.0
onnxruntime-gpu          1.26.0
onnxslim                 0.1.94
opencv-python            4.6.0.66
packaging                26.2
pillow                   12.2.0
pip                      24.0
polars                   1.40.1
polars-runtime-32        1.40.1
protobuf                 7.34.1
psutil                   7.2.2
pycocotools              2.0.11
Pygments                 2.20.0
pyparsing                3.3.2
python-dateutil          2.9.0.post0
PyYAML                   6.0.3
regex                    2026.5.9
requests                 2.34.1
rich                     15.0.0
safetensors              0.7.0
scipy                    1.16.0
setuptools               65.5.0
shellingham              1.5.4
six                      1.17.0
sympy                    1.14.0
tabulate                 0.10.0
tensorboard              2.20.0
tensorboard-data-server  0.7.2
tokenizers               0.22.2
torch                    2.7.1+cu128
torchaudio               2.7.1+cu128
torchvision              0.22.1+cu128
tqdm                     4.67.3
transformers             5.8.1
triton                   3.3.1
typer                    0.25.1
typing_extensions        4.15.0
ultralytics              8.4.58
ultralytics-thop         2.0.19
urllib3                  2.7.0
Werkzeug                 3.1.8

Focus(特征聚焦模块)

Focus 是 YOLOv5 中引入的一种下采样模块,用于在保留空间信息的同时增加通道数。其核心思想是将输入特征图在空间上每隔一个像素进行采样 ,得到四个子图,然后在通道维度上拼接 ,从而使通道数变为原来的 4 倍,空间尺寸减半。接着通过一个标准卷积层(Conv)进行特征变换。这种操作相当于用步长为 2 的卷积处理原始图像,但在 YOLOv5 的设计中被认为能更好地保留信息。不过,在现代 YOLOv8 中,该模块已被简化为直接使用步长为 2 的卷积,因为后者同样有效且更高效。


代码实现

python 复制代码
import cv2
import math
import torch
import numpy as np
import matplotlib.pyplot as plt
from torch import nn

def autopad(k, p=None, d=1):  # kernel, padding, dilation
    """Pad to 'same' shape outputs."""
    if d > 1:
        k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k]  # actual kernel-size
    if p is None:
        p = k // 2 if isinstance(k, int) else [x // 2 for x in k]  # auto-pad
    return p


class Conv(nn.Module):
    """Standard convolution module with batch normalization and activation.

    Attributes:
        conv (nn.Conv2d): Convolutional layer.
        bn (nn.BatchNorm2d): Batch normalization layer.
        act (nn.Module): Activation function layer.
        default_act (nn.Module): Default activation function (SiLU).
    """

    default_act = nn.SiLU()  # default activation

    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
        """Initialize Conv layer with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p (int, optional): Padding.
            g (int): Groups.
            d (int): Dilation.
            act (bool | nn.Module): Activation function.
        """
        super().__init__()
        self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
        self.bn = nn.BatchNorm2d(c2)
        self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()

    def forward(self, x):
        """Apply convolution, batch normalization and activation to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        """
        return self.act(self.bn(self.conv(x)))

    def forward_fuse(self, x):
        """Apply convolution and activation without batch normalization.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        """
        return self.act(self.conv(x))

class Focus(nn.Module):
    """Focus module for concentrating feature information.

    Slices input tensor into 4 parts and concatenates them in the channel dimension.

    Attributes:
        conv (Conv): Convolution layer.
    """

    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):
        """Initialize Focus module with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p (int, optional): Padding.
            g (int): Groups.
            act (bool | nn.Module): Activation function.
        """
        super().__init__()
        self.conv = Conv(c1 * 4, c2, k, s, p, g, act=act)
        # self.contract = Contract(gain=2)

    def forward(self, x):
        """Apply Focus operation and convolution to input tensor.

        Input shape is (B, C, H, W) and output shape is (B, c2, H/2, W/2).

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        """
        return self.conv(torch.cat((x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]), 1))
        # return self.conv(self.contract(x))

功能

  • 空间下采样 :通过隔点采样,将特征图的高和宽各缩小一半(即 H / 2 × W / 2 H/2 \times W/2 H/2×W/2)。
  • 通道数扩充 :将四个子图在通道维度拼接,通道数变为 4 × c 1 4 \times c_1 4×c1。
  • 特征变换 :拼接后的特征图通过一个 Conv 层(包含卷积、BN、激活)输出指定通道数 c 2 c_2 c2。
  • 等效于步长为 2 的卷积:在数学上,Focus 操作与使用步长为 2、核大小可调的标准卷积等价(当卷积核为 3×3 等时),但 Focus 的分片拼接过程可能对梯度流有一定影响。

初始化参数

参数 类型 说明
c1 int 输入通道数
c2 int 输出通道数
k int 卷积核大小(默认 1)
s int 卷积步长(默认 1)
p int / None 填充(若为 None,由 autopad 自动计算)
g int 分组数(默认 1)
act bool / nn.Module 激活函数(与 Conv 一致)

需要注意的是,Focus 本身不包含步长参数(s 仅用于后续卷积),其下采样是通过切片操作固定的(2 倍下采样)。若需其他下采样倍数,需修改切片方式。


前向方法

  • forward(x):对输入 x 进行四个方向的隔点采样,拼接后在通道维度进行卷积。

具体切片操作为:

  • x[..., ::2, ::2]:从 (0,0) 开始,每隔一个像素采样。
  • x[..., 1::2, ::2]:从 (1,0) 开始。
  • x[..., ::2, 1::2]:从 (0,1) 开始。
  • x[..., 1::2, 1::2]:从 (1,1) 开始。

然后将这四个张量在第 1 维(通道维)拼接,得到形状 (B, 4*C, H/2, W/2),再送入 self.conv


使用示例

python 复制代码
if __name__ == '__main__':
    # 1. 读取图像
    img_path = "cat_640x640.png"
    img_bgr = cv2.imread(img_path)
    if img_bgr is None:
        raise FileNotFoundError(f"图片 {img_path} 不存在!")

    # 2. 转为张量 (1,3,640,640)
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    img_tensor = torch.from_numpy(img_rgb).float().permute(2, 0, 1).unsqueeze(0)

    # 3. 创建 Focus 层:输入3通道,输出16通道,卷积核3×3,步长1(下采样由Focus本身完成)
    focus_layer = Focus(c1=3, c2=16, k=3, s=1, p=None)  # p=None 自动填充,保持卷积后尺寸不变

    # 4. 前向传播
    with torch.no_grad():
        out = focus_layer(img_tensor)
    print("输出形状:", out.shape)  # 预期 torch.Size([1, 16, 320, 320])

    # 5. 可视化第一个通道
    feat_map = out[0, 0, :, :].cpu().numpy()
    feat_map = (feat_map - feat_map.min()) / (feat_map.max() - feat_map.min() + 1e-8)
    feat_map = (feat_map * 255).astype(np.uint8)

    plt.figure(figsize=(10, 5))
    plt.subplot(1, 2, 1)
    plt.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
    plt.title("Original (640x640)")
    plt.axis("off")

    plt.subplot(1, 2, 2)
    plt.imshow(feat_map, cmap='gray')
    plt.title("Focus Output (320x320, Ch0)")
    plt.axis("off")

    plt.tight_layout()
    plt.savefig("focus_output.png", dpi=150)
    # plt.show()
    print("可视化已保存为 focus_output.png")

输出示例

复制代码
输出形状: torch.Size([1, 16, 320, 320])
可视化已保存为 focus_output.png

流程示意图


代码解读

__init__ 方法
  • self.conv = Conv(c1 * 4, c2, k, s, p, g, act=act):创建一个 Conv 模块,其输入通道数为 c1*4(因为切片拼接后通道数为原来的 4 倍),输出通道数为 c2,其他参数(k, s, p, g, act)直接透传。
  • 注释掉的 self.contract 是 Ultralytics 早期版本中的另一种实现方式(使用 Contract 层),但当前版本改用直接切片拼接。
forward 方法
  • 使用 torch.cat 在通道维度(dim=1)拼接四个切片。
  • 拼接后调用 self.conv 进行特征变换。

注意事项

  1. 与普通步长为 2 的卷积等效性

    • 当卷积核大小为 3×3、步长 2 时,Focus 与直接使用 Conv(c1, c2, k=3, s=2) 在数学上不等价 (因为 Focus 先切片再卷积,感受野略有不同),但二者都能实现下采样。实际上,YOLOv5 作者认为 Focus 能更好地保留信息,但后续实验表明直接卷积也能达到相近效果,因此 YOLOv8 已移除 Focus,改用 Conv(k=3, s=2)
  2. 输入尺寸必须为偶数

    • 由于切片需要 HW 能被 2 整除,否则会导致尺寸不一致或报错。
  3. 计算开销

    • Focus 的切片拼接操作本身无参数,但会使通道数膨胀 4 倍,增加后续卷积的输入通道数,从而增加参数量和计算量(相比直接步长为 2 的卷积,Focus 的卷积输入通道数较大,但实际 FLOPs 与直接卷积相比,在不同设置下可能有所不同,通常 Focus 略高)。
  4. 适用场景

    • 主要应用于早期 YOLO 系列(如 YOLOv5)的骨干网络第一层,用于将输入图像快速下采样到 320×320(640→320)。对于新项目,建议直接使用 Conv(k=3, s=2)
  5. ConvTranspose 的关系

    • 两者方向相反:Focus 下采样,ConvTranspose 上采样。

优缺点

优点
  1. 信息保留充分:通过分别采样四个子区域,保留了原始图像中相邻像素的局部信息,可能有助于小目标检测。
  2. 结构简单:仅需切片和拼接,无额外参数,易于实现。
  3. 加速收敛 :在某些数据集上,Focus 被观察到能使训练更快收敛(但该优势在 YOLOv8 中被认为不明显)。
缺点
  1. 计算成本较高 :通道数膨胀为 4 倍,导致后续卷积参数量增加(Conv 的输入通道为 4*c1,而直接步长为 2 的卷积输入通道仅为 c1)。
  2. 输入尺寸受限:必须为偶数尺寸,否则需填充或裁剪。
  3. 与主流设计趋势不符:现代轻量级网络(如 YOLOv8)倾向于使用直接卷积,因其在硬件上更高效,且精度差异可忽略。
  4. 不易扩展:若需其他下采样倍数(如 4 倍),需自定义切片逻辑,不够灵活。

在 YOLOv8 中,Focus 已被弃用,取而代之的是在第一个卷积层直接使用 Conv(c1, c2, k=3, s=2)。若您在旧版 YOLOv5 项目中看到该模块,可保留;若为新建项目,建议使用标准卷积下采样。

参考文献

1 https://docs.ultralytics.com/

2 https://github.com/ultralytics/ultralytics.git