YOLO 算法作业(CS230)

本文介绍YOLO(You Only Look Once)算法在自驾车检测系统中的应用。YOLO模型通过一次前向传播就能进行物体检测,实现高准确性和实时性。模型输入为608x608图像,输出为19x19x5x85的编码,包含每个网格的5个锚框信息。非极大抑制用于过滤低得分的预测框,减少重叠的检测结果。最后,将编码输出通过过滤和非极大抑制处理,得到最终的检测结果。

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

1 - Problem Statement

You are working on a self-driving car. As a critical component of this project, you'd like to first build a car detection system. To collect data, you've mounted a camera to the hood (meaning the front) of the car, which takes pictures of the road ahead every few seconds while you drive around.

 Pictures taken from a car-mounted camera while driving around Silicon Valley.
We would like to especially thank [drive.ai](https://www.drive.ai/) for providing this dataset! Drive.ai is a company building the brains of self-driving vehicles.

 You've gathered all these images into a folder and have labelled them by drawing bounding boxes around every car you found. Here's an example of what your bounding boxes look like.

If you have 80 classes that you want YOLO to recognize, you can represent the class label 𝑐 either as an integer from 1 to 80, or as an 80-dimensional vector (with 80 numbers) one component of which is 1 and the rest of which are 0. The video lectures had used the latter representation; in this notebook, we will use both representations, depending on which is more convenient for a particular step.

In this exercise, you will learn how YOLO works, then apply it to car detection. Because the YOLO model is very computationally expensive to train, we will load pre-trained weights for you to use.

2 - YOLO

YOLO ("you only look once") is a popular algoritm because it achieves high accuracy while also being able to run in real-time. This algorithm "only looks once" at the image in the sense that it requires only one forward propagation pass through the network to make predictions. After non-max suppression, it then outputs recognized objects together with the bounding boxes.

2.1 - Model details

First things to know:

  • The input is a batch of images of shape (m, 608, 608, 3)
  • The output is a list of bounding boxes along with the recognized classes. Each bounding box is represented by 6 numbers  (𝑝𝑐,𝑏𝑥,𝑏𝑦,𝑏ℎ,𝑏𝑤,𝑐)  as explained above. If you expand 𝑐c into an 80-dimensional vector, each bounding box is then represented by 85 numbers.

We will use 5 anchor boxes. So you can think of the YOLO architecture as the following: IMAGE (m, 608, 608, 3) -> DEEP CNN -> ENCODING (m, 19, 19, 5, 85).

Lets look in greater detail at what this encoding represents.

 If the center/midpoint of an object falls into a grid cell, that grid cell is responsible for detecting that object.

Since we are using 5 anchor boxes, each of the 19 x19 cells thus encodes information about 5 boxes. Anchor boxes are defined only by their width and height.

For simplicity, we will flatten the last two last dimensions of the shape (19, 19, 5, 85) encoding. So the output of the Deep CNN is (19, 19, 425).

 Now, for each box (of each cell) we will compute the following elementwise product and extract a probability that the box contains a certain class.

Here's one way to visualize what YOLO is predicting on an image:

  • For each of the 19x19 grid cells, find the maximum of the probability scores (taking a max across both the 5 anchor boxes and across different classes).
  • Color that grid cell according to what object that grid cell considers the most likely.

Doing this results in this picture:

 Note that this visualization isn't a core part of the YOLO algorithm itself for making predictions; it's just a nice way of visualizing an intermediate result of the algorithm.

Another way to visualize YOLO's output is to plot the bounding boxes that it outputs. Doing that results in a visualization like this:

 In the figure above, we plotted only boxes that the model had assigned a high probability to, but this is still too many boxes. You'd like to filter the algorithm's output down to a much smaller number of detected objects. To do so, you'll use non-max suppression. Specifically, you'll carry out these steps:

  • Get rid of boxes with a low score (meaning, the box is not very confident about detecting a class)
  • Select only one box when several boxes overlap with each other and detect the same object.

2.2 - Filtering with a threshold on class scores

You are going to apply a first filter by thresholding. You would like to get rid of any box for which the class "score" is less than a chosen threshold.

The model gives you a total of 19x19x5x85 numbers, with each box described by 85 numbers. It'll be convenient to rearrange the (19,19,5,85) (or (19,19,425)) dimensional tensor into the following variables:

  • box_confidence: tensor of shape (19×19,5,1)(19×19,5,1) containing 𝑝𝑐pc (confidence probability that there's some object) for each of the 5 boxes predicted in each of the 19x19 cells.
  • boxes: tensor of shape (19×19,5,4)(19×19,5,4) containing (𝑏𝑥,𝑏𝑦,𝑏ℎ,𝑏𝑤)(bx,by,bh,bw) for each of the 5 boxes per cell.
  • box_class_probs: tensor of shape (19×19,5,80)(19×19,5,80) containing the detection probabilities (𝑐1,𝑐2,...𝑐80)(c1,c2,...c80) for each of the 80 classes for each of the 5 boxes per cell.

Exercise: Implement yolo_filter_boxes().

  1. Compute box scores by doing the elementwise product as described in Figure 4. The following code may help you choose the right operator:
    a = np.random.randn(19*19, 5, 1)
    b = np.random.randn(19*19, 5, 80)
    c = a * b # shape of c will be (19*19, 5, 80)
  2. For each box, find:
    • the index of the class with the maximum box score (Hint) (Be careful with what axis you choose; consider using axis=-1)
    • the corresponding box score (Hint) (Be careful with what axis you choose; consider using axis=-1)
  3. Create a mask by using a threshold. As a reminder: ([0.9, 0.3, 0.4, 0.5, 0.1] < 0.4) returns: [False, True, False, False, True]. Th
### CS-FPN 改进 YOLO 网络结构的方法 #### 背景介绍 YOLO系列作为实时目标检测的经典算法之一,其核心在于速度与精度之间的平衡。然而,传统的FPN(Feature Pyramid Network)虽然能够很好地处理多尺度目标检测问题,但在轻量化模型中存在一定的局限性。为了进一步提升YOLO模型的性能,研究者提出了多种改进版的FPN架构,其中CS-FPN是一种针对边缘计算场景设计的轻量级特征金字塔网络。 #### CS-FPN 的主要特点 CS-FPN的核心设计理念是在保持较低计算复杂度的同时,增强对不同尺度目标的检测能力。以下是CS-FPN的主要改进点: 1. **卷积替代** 在传统FPN的基础上,CS-FPN将所有的标准 \(3 \times 3\) 卷积替换为CSL-Module[^2]。这种模块不仅保留了原有的空间信息提取能力,还引入了额外的通道注意力机制,使得网络能够在更少的参数下实现更高的特征表示能力。 2. **中尺度层引入** CSL-FPN在两个相邻的尺度层之间新增了一个中间尺度层。这一设计有助于在网络的不同层次间传递更多信息,特别是在小目标检测方面表现突出。通过这种方式,CS-FPN增强了模型对于跨尺度目标的理解能力。 3. **分阶段特征融合** 特征融合过程分为扩展阶段和重复阶段两部分: - 扩展阶段:在原有尺度基础上增加新的中间尺度层。 - 重复阶段:采用交替的方式进行特征融合,即仅在特定时刻选择奇数层或偶数层参与融合操作。这种方法减少了冗余计算,同时提升了效率。 #### 如何应用 CS-FPN 到 YOLO 中? 要将CS-FPN集成到YOLO框架中,可以按照以下方式调整网络结构: 1. **基础骨干网适配** 首先,确保所使用的YOLO版本支持自定义颈部(neck)模块的设计。例如,在YOLOv5YOLOX等较新版本中,可以通过配置文件轻松替换默认的PANet或其他颈部组件。 2. **替换现有 FPN/PAN 结构** 使用CS-FPN代替原生的FPN或PANet结构。具体来说,需要重新定义三个关键部分: - 替换所有\(3 \times 3\)卷积单元为CSL-Modules; - 添加必要的中间尺度层以促进跨尺度交互; - 修改特征融合逻辑以适应交替模式。 3. **训练与调优** 完成上述结构调整后,需对整个网络进行充分预热并微调超参数设置。考虑到CS-FPN可能带来不同的收敛特性,建议适当延长学习率调度周期,并监控验证集上的mAP指标变化趋势。 ```python import torch.nn as nn class CSLModule(nn.Module): def __init__(self, in_channels, out_channels): super(CSLModule, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): return self.relu(self.bn(self.conv(x))) def build_csl_fpn(backbone_features): layers = [] for i in range(len(backbone_features)-1): csl_module = CSLModule(backbone_features[i], backbone_features[i+1]) mid_scale_layer = MidScaleLayer() # 自定义中间尺度层类 fusion_logic = FeatureFusionLogic() # 自定义特征融合逻辑类 layers.append(csl_module) layers.append(mid_scale_layer) layers.append(fusion_logic) return nn.Sequential(*layers) ``` #### 性能评估 实验结果显示,当CS-FPN应用于YOLO模型时,可以在几乎不增加额外推理开销的前提下显著改善小目标检测效果。这得益于CSL-Module的有效性和多层次特征融合策略的优势。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值