> For the complete documentation index, see [llms.txt](https://baozoulin.gitbook.io/neural-networks-and-deep-learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://baozoulin.gitbook.io/neural-networks-and-deep-learning/di-si-men-ke-juan-ji-shen-jing-wang-luo-convolutional-neural-networks/convolutional-neural-networks/object-detection/39-yolo-suan-fa-ff08-putting-it-together-yolo-algorithm.md).

# 3.9 YOLO 算法（Putting it together: YOLO algorithm）

假设要在图片中检测行人、汽车，同时使用两种不同的Anchor box

**训练集：**

* 输入X：同样大小的完整图片
* 目标Y：使用$$3\times3$$网格划分，输出大小$$3\times3\times2\times8$$，或者$$3\times3\times16$$
* 对不同格子中的小图，定义目标输出向量Y

[\
![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/36ff927836cfcd7fee9413e2d34757d8.png)](https://legacy.gitbook.com/book/baozou/neural-networks-and-deep-learning/edit#)

编号2目标向量$$y =\begin{bmatrix} 0 & ? & ? & ? & ? & ? & ? & ? & 1 & b\_{x} & b\_{y} & b\_{h} \&b\_{w} & 0 & 1 & 0 \end{bmatrix}^{T}$$，假设训练集中对于车子有一个边界框（编号3），水平方向更长一点，红框和**anchor box 2**的交并比更高，车子和向量的下半部分相关

**模型预测：**

输入与训练集中相同大小的图片，然后训练一个卷积网络，遍历9个格子，得到每个格子中不同的输出结果：$$3\times3\times2\times8$$

[\
![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/e23084f4a75246f08ea4cedef55f60ab.png)](https://legacy.gitbook.com/book/baozou/neural-networks-and-deep-learning/edit#)

**运行非最大值抑制（NMS）：**

1. 假设使用了2个Anchor box，每一个网格都会得到预测输出的2个bounding boxes，其中一个$$P\_{c}$$比较高
2. 抛弃概率$$P\_{c}$$值低的预测bounding boxes
3. 对每个对象分别使用NMS算法得到最终的预测边界框

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/23256c4b7b28d62d34a744f5fb5e9c3b.png)](https://legacy.gitbook.com/book/baozou/neural-networks-and-deep-learning/edit#)

[\
![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/66f8cf8e55eadc1ac01f773515bfbc45.png)](https://legacy.gitbook.com/book/baozou/neural-networks-and-deep-learning/edit#)

如果有三个对象检测类别，希望检测行人，汽车和摩托车：对于每个类别单独运行非极大值抑制，处理预测结果所属类别的边界框，用非极大值抑制来处理行人类别、车子类别、摩托车类别，运行三次来得到最终的预测结果
