> 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/35-bounding-boxyu-ce-ff08-bounding-box-predictions.md).

# 3.5 Bounding Box预测（Bounding box predictions）

滑动窗口法的卷积实现算法效率很高，但不能输出最精准的边界框

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/cd3e263bf279739afe62eb8730b4e167.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/cd3e263bf279739afe62eb8730b4e167.png)

输入图像是100×100的，用3×3网格，实际实现时会用更精细的网格（19×19）。使用图像分类和定位算法

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/e4bebc707829a1610572f43f8e0995c9.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/e4bebc707829a1610572f43f8e0995c9.png)

编号1什么也没有，左上格子的标签向量$$y$$是$$\begin{bmatrix}0\ ?\ ?\ ?\ ?\ ?\ ?\ ? \end{bmatrix}$$。其他什么也没有的格子都一样

图中有两个对象，**YOLO**算法做的是取两个对象的中点，将对象分配给包含对象中点的格子。即使中心格子（编号5）同时有两辆车的一部分，分类标签$$y$$也为$$y= \begin{bmatrix}0\ ?\ ?\ ?\ ?\ ?\ ?\ ? \end{bmatrix}$$。编号4目标标签$$y= \begin{bmatrix} 1\ b\_{x}\ b\_{y}\ b\_{h}\ b\_{w}\ 0\ 1\ 0 \end{bmatrix}$$，编号6类似

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/fb08477cd3937f7df0deddc1de1d2920.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/fb08477cd3937f7df0deddc1de1d2920.png)

3×3中9个格子都对应一个8维输出目标向量$$y$$，其中一些值可以是**dont care-s**（即？）所以总的目标输出尺寸就是3×3×8

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/98633e9df22fd06cfc21af2e7d39bbb6.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/98633e9df22fd06cfc21af2e7d39bbb6.png)

如果要训练一个输入为100×100×3的神经网络，输入图像通过普通的卷积网络，卷积层，最大池化层等等，最后映射到一个3×3×8输出尺寸。然后用反向传播训练神经网络，将任意输入$$x$$映射到输出向量$$y$$

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/404fdcba2685b830ae3718d348ab1d75.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/404fdcba2685b830ae3718d348ab1d75.png)

这个算法的优点在于神经网络可以输出精确的边界框，测试的时候有要做的是喂入输入图像$$x$$，然后跑正向传播，直到得到输出$$y$$。然后3×3位置对应的9个输出，只要每个格子中对象数目没有超过1个，这个算法应该是没问题的。但实践中会使用更精细的19×19网格，输出就是19×19×8，多个对象分配到同一个格子得概率就小得多

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/a6d32959543c502ee18765cf20495bc2.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/a6d32959543c502ee18765cf20495bc2.png)

即使对象可以横跨多个格子，也只会被分配到9个格子其中之一，或者19×19网络的其中一个格子。在19×19网格中，两个对象的中点（图中蓝色点所示）处于同一个格子的概率就会更低。

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/b6b6ca6167596a180c7bab7296ea850c.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/b6b6ca6167596a180c7bab7296ea850c.png)

优点：

* 显式地输出边界框坐标，可以具有任意宽高比，并且能输出更精确的坐标，不会受到滑动窗口分类器的步长大小限制
* 并没有在3×3网格上跑9次算法，而是单次卷积实现，但在处理这3×3计算中很多计算步骤是共享的，所以这个算法效率很高
* 因为是卷积实现，运行速度非常快，可以达到实时识别

如何编码这些边界框$$b\_{x}$$、$$b\_{y}$$、$$b\_{h}$$和$$b\_{w}$$：

在**YOLO**算法中，编号1约定左上点是$$(0,0)$$，右下点是$$(1,1)$$，橙色中点的位置$$b\_{x}$$大概是0.4，$$b\_{y}$$大概是0.3，$$b\_{w}$$是0.9，$$b\_{h}$$是0.5。$$b\_{x}$$、$$b\_{y}$$、$$b\_{h}$$和$$b\_{w}$$单位是相对于格子尺寸的比例，所以$$b\_{x}$$和$$b\_{y}$$必须在0和1之间，因为从定义上看，橙色点位于对象分配到格子的范围内，如果它不在0和1之间，即它在方块外，那么这个对象就应该分配到另一个格子上。这个值（$$b\_{h}$$和$$b\_{w}$$）可能会大于1，特别是如果有一辆汽车的边界框是这样的（编号3所示），那么边界框的宽度和高度有可能大于1

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/0d7ee9b9f455338a8724520841223b11.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/0d7ee9b9f455338a8724520841223b11.png)
