惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News | PayPal Newsroom
S
Security Affairs
T
Tor Project blog
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Help Net Security
Help Net Security
U
Unit 42
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
Cisco Talos Blog
Cisco Talos Blog
量子位
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
有赞技术团队
有赞技术团队
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
腾讯CDC
AI
AI
Last Week in AI
Last Week in AI
Latest news
Latest news
Google Online Security Blog
Google Online Security Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
V2EX - 技术
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell

StudyingLover's Blog

Diffusion Policy笔记 rwkv笔记 act笔记 nanovllm-block_manager opencode多智能体 nanobot-pre-train nanobot-rl nanobot-sft nanobot-checkpoint_manager nanobot-gpt nanobot-mid-train Vision Mamba (Vim)笔记 BPE演示 最后一遍学习Transformer 下载根服务器解析记录 Dynaseal A Backend-Controlled LLM API Key Distribution Scheme with Constrained Invocation Parameters 判断链表有环 王道25数据结构勘误 关于perplexity的open-sourcing-r1-1776 AI为什么不像人类一样进行多轮对话 新博客改造日记和功能测试 linuxqq只显示登陆背景图 数字设计和计算机体系结构(机械工业出版社)勘误(自制) Dynaseal:面向未来端侧llm agent的llm api key分发机制 A Definitive Guide to Markdown Style This post is using MDX, Where you can embed JSX and Astro components RT-Patch学习 pydantic实现的LLM ReAct fastapi 和 uvicorn 设置监听 ipv6 pydantic+openai+json 控制大模型输出的最佳范式 解决 Matplotlib Scatter 不支持 Marker 列表的问题:mscatter 实现 roofline model zhipuAI接口兼容openai 在docker部署fastapi宝塔里使用nginx反代套上cloudflare获取请求的真实ip clion搭建libbpf-bootstrap开发环境 coze+coze-discord-proxy+ChatNextWebUI实现AI自由 安卓内核时间使用的是UTC时间 colab运行google最新开源模型Gemma Sora技术报告 视频生成模型作为世界模拟器 笔记 archlinux flutter开发踩坑 fastapi集成google auth登录 linux下NTFS磁盘报错输入输出错误 Venn-Abers 预测器 基于Venn-Abers预测器的系统日志异常检测方法_顾兆军 手机平板远程访问kvm虚拟机的windows phi-2弱智吧测评 poe的gemini pro或是百度开发 google gemini api使用 google gemini api申请 构建用于复杂数据处理的高效UDP服务器和客户端 matplotlib中文字体渲染 TruFor笔记和代码复现 深入分析:GitHub Trending 项目 "multipleWindow3dScene" pua大模型 ggml教程|mnist手写体识别量化推理 xgboost2.0最佳实践 xgboost使用GPU最佳实践 马踏棋盘 cloudlflare推理llama2 docker搭建elasticsearch并使用python连接 FreeU-文字生成图片的免费午餐笔记 使用xgboost的c接口推理模型 Archlinux使用CMake调用xgboost的c接口 m2cgen生成机器学习c语言推理代码 xgboost模型序列化存储并推理 speculative-sampling笔记 prompt2model笔记 RoboTAP笔记 自建obsidian同步服务 MediaPipe即将推出图像生成服务 Dual-Stream Diffusion Net for Text-to-Video Generation笔记 ViT在DDPM取代UNet(DiT) arch4edu搞崩了我的flutter LISA(推理分割)笔记 在终端绘制GPU显存使用曲线 GPTBot介绍 arch蓝牙无法连接 GPU部署llama-cpp-python(llama.cpp通用) 花式求GCD 使用llama构建一个蜜罐(前端) 使用llama构建一个蜜罐(后端) llama-cpp-python快速上手 快速上手llama2.c(更新版) Paper Gestalt笔记 DINO-v2笔记 快速上手llama2.c AnyDoor笔记 Archlinux安装scrcpy加载共享库出错 error while loading shared libraries:libusb-1.0.so.0:wrong ELF class:ELFCLASS32 npc_gzip笔记 python调用c++函数 Filesystem type ntfs3,ntfs not configured in kernel open_clip编码图像和文本 PicGo配置CloudflareR2图片储存 ArchlinuxGnome快捷键打开终端 clip-interrogator代码解析 GroundingDINO安装报错解决 2023华为鲲鹏畅想日暨西安高新国际会议中心零食午饭测评 RoboMaster开源仓库汇总(长期更新) 没有手都可以在腾讯云创建镜像 I3D笔记
YOLOv5 目标检测笔记
About the Author StudyingLover · 2026-01-01 · via StudyingLover's Blog

# YOLOv5 Architecture,图源:https://docs.ultralytics.com/yolov5/tutorials/architecture_description/

第一部分:Backbone (主干网络) —— 提取特征的“鹰眼”

Backbone 的任务是把图片不断的下采样(Downsample),让特征图越来越小,但包含的语义信息越来越丰富(从“这是线条”变成“这是车轮”再到“这是车”)。

YOLOv5 的 Backbone 是 CSPDarknet 的改进版。

1. Focus 层 (但在 v6.0 版本已被替换)

  • 注意: 在你要学习的代码(v6.0/v6.1)中,原来的 Focus 模块已经被一个简单的 Conv(卷积核大小 6,步长 2)取代了。
  • 代码对应: models/yolov5s.yaml 的第 0 层:[-1, 1, Conv, [64, 6, 2, 2]]
  • 作用: 这是一个 6x6 的卷积,步长为 2。它直接把图片的宽和高减半,通道数从 3 扩充到 64。这是效率极高的下采样。

2. CBS 模块 (Conv-BatchNorm-SiLU)

这是网络里最基本的“细胞”。你在 YAML 里看到的 Conv 其实不是 pytorch 原生的卷积,而是封装过的。

  • 代码对应: models/common.py 中的 class Conv
  • 构成: Conv2d + BatchNorm2d + SiLU (Sigmoid Linear Unit 激活函数)。
  • 细节: SiLU () 是这一代 YOLO 的标配,比 ReLU 更平滑,梯度传导更好。
class Conv(nn.Module):
    """Applies a convolution, batch normalization, and activation function to an input tensor in a neural network."""

    default_act = nn.SiLU()  # default activation

    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
        """Initializes a standard convolution layer with optional batch normalization and activation."""
        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):
        """Applies a convolution followed by batch normalization and an activation function to the input tensor `x`."""
        return self.act(self.bn(self.conv(x)))

    def forward_fuse(self, x):
        """Applies a fused convolution and activation function to the input tensor `x`."""
        return self.act(self.conv(x))

3. C3 模块 (CSP Bottleneck with 3 convolutions) —— 核心组件

这是 YOLOv5 的灵魂,用来替换原来的 ResBlock。它的全称是 CSPNet (Cross Stage Partial Network) 结构。

  • 代码对应: models/common.py 中的 class C3
  • 原理(一定要看懂这个): 输入信号进入 C3 后被分流成两条路:
  1. 主路: 经过一系列残差组件(Bottlenecks)。
  2. 旁路: 经过一个简单的卷积。
  3. 汇合: 最后两路 Concat(拼接)在一起。
class C3(nn.Module):
    """Implements a CSP Bottleneck module with three convolutions for enhanced feature extraction in neural networks."""

    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
        """Initializes C3 module with options for channel count, bottleneck repetition, shortcut usage, group
        convolutions, and expansion.
        """
        super().__init__()
        c_ = int(c2 * e)  # hidden channels
        self.cv1 = Conv(c1, c_, 1, 1)
        self.cv2 = Conv(c1, c_, 1, 1)
        self.cv3 = Conv(2 * c_, c2, 1)  # optional act=FReLU(c2)
        self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))

    def forward(self, x):
        """Performs forward propagation using concatenated outputs from two convolutions and a Bottleneck sequence."""
        return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))

YOLOv5 C3 Module Structure

Input x

Path 1 (主路)

Conv 1x1
(self.cv1)

Bottleneck x N

ResBlock: Conv 1x1 → Conv 3x3
(Shortcut + Residual)

Path 2 (辅路)

Conv 1x1
(self.cv2)

Feature Bypass

Concat (拼接)
dim = 1

Conv 1x1 Output
(self.cv3)

  • 为什么这么做? 这让梯度在反向传播时有“高速公路”可以走(旁路),同时主路又能专心提取深层特征。它在减少参数量的同时,防止了网络太深导致的梯度消失。
  • YAML 里的体现: 比如 [-1, 3, C3, [128]],意思是这一层有 3 个 Bottleneck 串联,输出通道是 128。

4. SPPF (Spatial Pyramid Pooling - Fast)

放在 Backbone 的最末端(第 9 层)。

  • 代码对应: models/common.py 中的 class SPPF
  • 结构: 它不像以前的 SPP 并行跑 3 个不同大小的池化核,而是串行跑 3 次 5x5 的 MaxPool。
  • 公式: Output = Concat(Input, Pool(Input), Pool(Pool(Input)), Pool(Pool(Pool(Input))))。
  • 作用: 不管图片里物体是大是小,通过这种多次池化的融合,都能把特征提取出来(多尺度融合),而且速度飞快。
class SPPF(nn.Module):
    """Implements a fast Spatial Pyramid Pooling (SPPF) layer for efficient feature extraction in YOLOv5 models."""

    def __init__(self, c1, c2, k=5):
        """Initializes YOLOv5 SPPF layer with given channels and kernel size for YOLOv5 model, combining convolution and
        max pooling.

        Equivalent to SPP(k=(5, 9, 13)).
        """
        super().__init__()
        c_ = c1 // 2  # hidden channels
        self.cv1 = Conv(c1, c_, 1, 1)
        self.cv2 = Conv(c_ * 4, c2, 1, 1)
        self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)

    def forward(self, x):
        """Processes input through a series of convolutions and max pooling operations for feature extraction."""
        x = self.cv1(x)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")  # suppress torch 1.9.0 max_pool2d() warning
            y1 = self.m(x)
            y2 = self.m(y1)
            return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))

YOLOv5 SPPF Module Structure

(Serial Pooling = Larger Receptive Field)

Input x

Conv 1x1 (cv1)
Channel Halved (c_)

Serial MaxPool Pipeline

  • Each step expands the receptive field by 4 (5x5 kernel)

Concat ( x, y1, y2, y3 )
dim = 1 (Channel Stacking)

Conv 1x1 Output (cv2)
Fusion & Dim Reduction


第二部分:Neck (颈部) —— 特征融合的“集散地”

如果 Backbone 只是负责看,Neck 就负责“把看到的各种细节拼起来”。YOLOv5 使用了 FPN (Feature Pyramid Network) + PAN (Path Aggregation Network) 结构。

想象一下:

  • P3 层(浅层):分辨率高,适合看小物体(细节多)。
  • P5 层(深层):分辨率低,适合看大物体(语义强)。

Neck 的工作就是让 P3 和 P5 互相交流。

1. 自顶向下 (FPN 路径) —— 传达语义

  • 流程: 1. 拿 Backbone 最后的输出(P5),先上采样(Upsample,变大)。
  1. 然后和上一层(P4)的特征进行 Concat(拼接)。
  2. 再经过 C3 融合。
  3. 继续上采样,和 P3 拼接。
  • 目的: 让底层的特征图(看小物体用的)也能拥有高层的语义信息(知道“这是什么”)。

2. 自底向上 (PAN 路径) —— 传达定位

  • 流程: 1. 刚才融合好的浅层特征,再通过卷积(stride=2)下采样(变小)。
  1. 和深层特征再次 Concat
  • 目的: 让高层的特征图(看大物体用的)也能拥有底层的定位信息(知道“准确轮廓在哪”)。

第三部分:Head (头部) —— 最终判决

最后是 Detect 模块,它负责根据 Neck 提供的 3 张特征图(大、中、小)输出结果。

  • 代码对应: models/yolo.py 中的 class Detect

  • 输入: 3 个特征图(P3, P4, P5)。

  • P3 (80x80): 负责检测小物体。

  • P4 (40x40): 负责检测中物体。

  • P5 (20x20): 负责检测大物体。 (注:假设输入图片是 640x640)

  • 1x1 卷积: 每个特征图都会通过一个 1x1 卷积,将通道数调整为:

  • Anchors (na): 通常是 3(每个网格预设 3 个框)。

  • 5: 代表 。

  • Classes (nc): 比如 COCO 数据集是 80。

  • 所以对于 COCO,输出通道是 3×(5+80)=2553 \times (5 + 80) = 255

总结图谱 (Memory Map)

为了方便记忆,你可以把整个流程浓缩成这一串动作:

  1. 图片进 (640x640x3)
  2. 切片 (Conv 6x6) -> 320x320
  3. 下采样与提炼 (Conv + C3) -> 160 -> 80 -> 40 -> 20 (这里就是 P3, P4, P5)
  4. SPPF (在 P5 处做多尺度增强)
  5. FPN 上采样 (让 P3 看到 P5 的语义)
  6. PAN 下采样 (让 P5 找回 P3 的定位)
  7. Detect (在 P3, P4, P5 三个层面上同时预测)

后处理

把 3 个头的输出拼在一起,运行NMS

  • 注意:训练模型的时候,是不需要 NMS 的。网络吐出 3 个头的预测结果后,直接拿着它们去和Labels 对比算误差(Loss),然后反向传播更新参数。