SilverIce Toolbox
Back to course

Stage 4 / Chapter 18

第18章:目标检测与 YOLO | Chapter 18: Object Detection & YOLO

阶段定位 | Stage: 第四阶段 — ML 策略与 CNN 预计学时 | Duration: 4~5 小时

---

学习目标 | Learning Objectives

中文:

  • 理解目标检测与图像分类的核心区别
  • 掌握 IoU(交并比)的计算与意义
  • 掌握 Non-Maximum Suppression(NMS)的算法流程
  • 理解 YOLO 的单阶段检测思想
  • 理解 Anchor Boxes 的作用与多尺度检测

English:

  • Understand core differences between object detection and image classification
  • Master IoU computation and meaning
  • Master Non-Maximum Suppression algorithm
  • Understand YOLO's single-stage detection philosophy
  • Understand Anchor Boxes and multi-scale detection

---

18.1 从分类到检测 | From Classification to Detection

中文解释

图像分类

输入: 一张图片
输出: 一个类别标签(如"猫")

目标定位

输入: 一张图片
输出: 类别 + 边界框 (bx, by, bh, bw)

目标检测

输入: 一张图片
输出: 多个 (类别, 边界框)

三种方法对比

方法思路速度精度
滑动窗口用分类器扫描所有位置极慢
两阶段(R-CNN)先找候选区域,再分类
单阶段(YOLO)一次前向传播完成检测极快中高

English Explanation

Classification → Localization → Detection:

  • Classification: one label
  • Localization: label + bounding box
  • Detection: multiple (label, box) pairs

Methods: sliding window (slow), two-stage (accurate), single-stage (fast)

---

18.2 IoU 与 NMS | IoU & NMS

中文解释

IoU(Intersection over Union)

衡量两个框的重叠程度:

IoU = 交集面积 / 并集面积
IoU判断
0完全不重叠
0.5常用阈值,认为检测正确
1.0完全重合

NMS(Non-Maximum Suppression)

问题:同一物体被多个框检测,产生冗余。

算法:

1. 按置信度排序所有框
2. 取置信度最高的框,保留
3. 删除与该框 IoU > 阈值的所有框
4. 重复直到没有框剩下

English Explanation

IoU: overlap measure. >0.5 typically means correct detection.

NMS: keep highest-confidence box, remove overlapping duplicates.

---

18.3 YOLO:You Only Look Once

中文解释

核心思想

把图像分成 S×S 的网格,每个网格直接预测:

  • 是否有物体(置信度)
  • 边界框坐标(相对于网格)
  • 类别概率

单次前向传播完成所有预测!

输出张量

(S, S, B×5 + C)
  • S×S:网格数
  • B:每个网格预测的框数
  • 5:x, y, w, h, confidence
  • C:类别数

Anchor Boxes

不同物体有不同形状(人瘦高,车矮宽)。预定义多种形状的 anchor:

Anchor 1: (宽, 高) = (1, 3)   ← 适合瘦高物体
Anchor 2: (宽, 高) = (3, 1)   ← 适合矮宽物体

每个网格预测多个 anchor,分别负责不同形状的物体。

English Explanation

YOLO: divide image into S×S grid, each cell predicts boxes directly.

Anchor boxes: predefined shapes to handle objects of different aspect ratios.

---

18.4 完整实现:IoU 与 NMS

代码案例

python
import numpy as np

def compute_iou(box1, box2):
    """
    box = [x1, y1, x2, y2]
    """
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    x2 = min(box1[2], box2[2])
    y2 = min(box1[3], box2[3])

    inter_area = max(0, x2 - x1) * max(0, y2 - y1)
    box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])
    box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])
    union_area = box1_area + box2_area - inter_area

    return inter_area / (union_area + 1e-6)

def nms(boxes, scores, iou_threshold=0.5):
    """Non-Maximum Suppression"""
    indices = np.argsort(scores)[::-1]
    keep = []

    while len(indices) > 0:
        current = indices[0]
        keep.append(current)

        if len(indices) == 1:
            break

        current_box = boxes[current]
        other_boxes = boxes[indices[1:]]

        ious = np.array([compute_iou(current_box, b) for b in other_boxes])
        mask = ious <= iou_threshold
        indices = indices[1:][mask]

    return keep

# ========== 测试 ==========
print("=" * 50)
print("IoU 与 NMS 测试")
print("=" * 50)

# IoU 示例
box_a = [100, 100, 200, 200]
box_b = [150, 150, 250, 250]
iou = compute_iou(box_a, box_b)
print(f"\nBox A: {box_a}")
print(f"Box B: {box_b}")
print(f"IoU: {iou:.3f}")

# 可视化理解
inter_w = min(box_a[2], box_b[2]) - max(box_a[0], box_b[0])
inter_h = min(box_a[3], box_b[3]) - max(box_a[1], box_b[1])
print(f"交集: {inter_w}×{inter_h}={inter_w*inter_h}")

union = (100*100) + (100*100) - (inter_w*inter_h)
print(f"并集: {union}")
print(f"IoU = {inter_w*inter_h}/{union} = {iou:.3f}")

# NMS 测试
boxes = np.array([
    [100, 100, 210, 210],   # 目标1,高置信度
    [105, 105, 215, 215],   # 目标1,冗余框
    [300, 300, 400, 400],   # 目标2
    [103, 102, 212, 208],   # 目标1,冗余框
])
scores = np.array([0.95, 0.88, 0.75, 0.82])

print(f"\n检测框:")
for i, (box, score) in enumerate(zip(boxes, scores)):
    print(f"  Box {i}: {box}, score={score}")

keep = nms(boxes, scores, iou_threshold=0.5)
print(f"\nNMS 后保留的索引: {keep}")
print("说明:Box 0 置信度最高,Box 1 和 3 与 Box 0 重叠度高被抑制")
print("      Box 2 是另一个目标,保留")

输出:

==================================================
IoU 与 NMS 测试
==================================================

Box A: [100, 100, 200, 200]
Box B: [150, 150, 250, 250]
IoU: 0.143

交集: 50×50=2500
并集: 17500
IoU = 2500/17500 = 0.143

检测框:
  Box 0: [100, 100, 210, 210], score=0.95
  Box 1: [105, 105, 215, 215], score=0.88
  Box 2: [300, 300, 400, 400], score=0.75
  Box 3: [103, 102, 212, 208], score=0.82

NMS 后保留的索引: [0, 2]
说明:Box 0 置信度最高,Box 1 和 3 与 Box 0 重叠度高被抑制
      Box 2 是另一个目标,保留

---

本章总结 | Chapter Summary

中文:

  • 目标检测 = 分类 + 定位 + 多物体
  • YOLO 单阶段检测:网格直接预测框,速度极快
  • IoU 衡量框重叠度,>0.5 认为检测正确
  • NMS 去除冗余框,保留最高置信度
  • Anchor Boxes 预定义形状,处理不同长宽比物体

English:

  • Detection = classification + localization + multiple objects
  • YOLO: single-stage, grid predicts boxes directly
  • IoU measures overlap, >0.5 = correct
  • NMS removes redundant boxes
  • Anchor boxes handle different aspect ratios

---

课后练习 | Homework

  1. IoU 边界情况:计算两个框完全包含、完全分离、完全相同时的 IoU。
  1. NMS 变体:实现 Soft-NMS(降低重叠框的置信度而非直接删除),对比与标准 NMS 的效果。
  1. YOLO 输出格式:假设 S=7, B=2, C=20。计算输出张量大小。如果输入 448×448,每个网格负责多大区域?
  1. Anchor 设计:COCO 数据集中物体长宽比分布大致为 1:1, 1:2, 2:1。设计 3 个 anchor 尺寸。
  1. mAP 计算:了解 mean Average Precision 的计算方法。为什么检测任务不用准确率而用 mAP?