diff --git a/code/chapter09_computer-vision/9.8_rcnn.ipynb b/code/chapter09_computer-vision/9.8_rcnn.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..15554b7021e2cd712d712f5451e3cf3c26097fc7 --- /dev/null +++ b/code/chapter09_computer-vision/9.8_rcnn.ipynb @@ -0,0 +1,128 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 9.8 区域卷积神经网络(R-CNN)系列" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.4.0a0+6b959ee\n" + ] + } + ], + "source": [ + "import torch\n", + "import torchvision\n", + "\n", + "print(torchvision.__version__)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 9.8.2 Fast R-CNN" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[[[ 0., 1., 2., 3.],\n", + " [ 4., 5., 6., 7.],\n", + " [ 8., 9., 10., 11.],\n", + " [12., 13., 14., 15.]]]])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X = torch.arange(16, dtype=torch.float).view(1, 1, 4, 4)\n", + "X" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "rois = torch.tensor([[0, 0, 0, 20, 20], [0, 0, 10, 30, 30]], dtype=torch.float)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[[[ 5., 6.],\n", + " [ 9., 10.]]],\n", + "\n", + "\n", + " [[[ 9., 11.],\n", + " [13., 15.]]]])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torchvision.ops.roi_pool(X, rois, output_size=(2, 2), spatial_scale=0.1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/README.md b/docs/README.md index 6ccf737448dd02a973549a1ece5b29a5455b9a35..c19d0b221b8290cfabc6bb633d6782b4f5f28f1c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -112,6 +112,7 @@ docsify serve docs * [9.4 锚框](chapter09_computer-vision/9.4_anchor.md) * [9.5 多尺度目标检测](chapter09_computer-vision/9.5_multiscale-object-detection.md) * [9.6 目标检测数据集(皮卡丘)](chapter09_computer-vision/9.6_object-detection-dataset.md) + * [9.8 区域卷积神经网络(R-CNN)系列](chapter09_computer-vision/9.8_rcnn.md) * 待更新... * 10\. 自然语言处理 * [10.1 词嵌入(word2vec)](chapter10_natural-language-processing/10.1_word2vec.md) diff --git a/docs/_sidebar.md b/docs/_sidebar.md index d8729b6dfccd34cd31e9e7c22e1d44dbfbc4c80c..3ee794a7141e444000a8c07217a8f1230ce18ba9 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -74,6 +74,7 @@ * [9.4 锚框](chapter09_computer-vision/9.4_anchor.md) * [9.5 多尺度目标检测](chapter09_computer-vision/9.5_multiscale-object-detection.md) * [9.6 目标检测数据集(皮卡丘)](chapter09_computer-vision/9.6_object-detection-dataset.md) + * [9.8 区域卷积神经网络(R-CNN)系列](chapter09_computer-vision/9.8_rcnn.md) * 待更新... * 10\. 自然语言处理 * [10.1 词嵌入(word2vec)](chapter10_natural-language-processing/10.1_word2vec.md) diff --git a/docs/chapter09_computer-vision/9.8_rcnn.md b/docs/chapter09_computer-vision/9.8_rcnn.md new file mode 100644 index 0000000000000000000000000000000000000000..846ece0593f154746d5b94a5db44dc4fe0055378 --- /dev/null +++ b/docs/chapter09_computer-vision/9.8_rcnn.md @@ -0,0 +1,146 @@ +# 9.8 区域卷积神经网络(R-CNN)系列 + + +区域卷积神经网络(region-based CNN或regions with CNN features,R-CNN)是将深度模型应用于目标检测的开创性工作之一 [1]。在本节中,我们将介绍R-CNN和它的一系列改进方法:快速的R-CNN(Fast R-CNN)[3]、更快的R-CNN(Faster R-CNN)[4] 以及掩码R-CNN(Mask R-CNN)[5]。限于篇幅,这里只介绍这些模型的设计思路。 + + +## 9.8.1 R-CNN + +R-CNN首先对图像选取若干提议区域(如锚框也是一种选取方法)并标注它们的类别和边界框(如偏移量)。然后,用卷积神经网络对每个提议区域做前向计算抽取特征。之后,我们用每个提议区域的特征预测类别和边界框。图9.5描述了R-CNN模型。 + +
+ +
+
图9.5 R-CNN模型
+ +具体来说,R-CNN主要由以下4步构成。 + +1. 对输入图像使用选择性搜索(selective search)来选取多个高质量的提议区域 [2]。这些提议区域通常是在多个尺度下选取的,并具有不同的形状和大小。每个提议区域将被标注类别和真实边界框。 +1. 选取一个预训练的卷积神经网络,并将其在输出层之前截断。将每个提议区域变形为网络需要的输入尺寸,并通过前向计算输出抽取的提议区域特征。 +1. 将每个提议区域的特征连同其标注的类别作为一个样本,训练多个支持向量机对目标分类。其中每个支持向量机用来判断样本是否属于某一个类别。 +1. 将每个提议区域的特征连同其标注的边界框作为一个样本,训练线性回归模型来预测真实边界框。 + +R-CNN虽然通过预训练的卷积神经网络有效抽取了图像特征,但它的主要缺点是速度慢。想象一下,我们可能从一张图像中选出上千个提议区域,对该图像做目标检测将导致上千次的卷积神经网络的前向计算。这个巨大的计算量令R-CNN难以在实际应用中被广泛采用。 + + +## 9.8.2 Fast R-CNN + +R-CNN的主要性能瓶颈在于需要对每个提议区域独立抽取特征。由于这些区域通常有大量重叠,独立的特征抽取会导致大量的重复计算。Fast R-CNN对R-CNN的一个主要改进在于只对整个图像做卷积神经网络的前向计算。 + +图9.6描述了Fast R-CNN模型。 + +
+ +
+
图9.6 Fast R-CNN模型
+ +它的主要计算步骤如下。 + +1. 与R-CNN相比,Fast R-CNN用来提取特征的卷积神经网络的输入是整个图像,而不是各个提议区域。而且,这个网络通常会参与训练,即更新模型参数。设输入为一张图像,将卷积神经网络的输出的形状记为$1 \times c \times h_1 \times w_1$。 +1. 假设选择性搜索生成$n$个提议区域。这些形状各异的提议区域在卷积神经网络的输出上分别标出形状各异的兴趣区域。这些兴趣区域需要抽取出形状相同的特征(假设高和宽均分别指定为$h_2$和$w_2$)以便于连结后输出。Fast R-CNN引入兴趣区域池化(Region of Interest Pooling,RoI池化)层,将卷积神经网络的输出和提议区域作为输入,输出连结后的各个提议区域抽取的特征,形状为$n \times c \times h_2 \times w_2$。 +1. 通过全连接层将输出形状变换为$n \times d$,其中超参数$d$取决于模型设计。 +1. 预测类别时,将全连接层的输出的形状再变换为$n \times q$并使用softmax回归($q$为类别个数)。预测边界框时,将全连接层的输出的形状变换为$n \times 4$。也就是说,我们为每个提议区域预测类别和边界框。 + +Fast R-CNN中提出的兴趣区域池化层跟我们在5.4节(池化层)中介绍过的池化层有所不同。在池化层中,我们通过设置池化窗口、填充和步幅来控制输出形状。而兴趣区域池化层对每个区域的输出形状是可以直接指定的,例如,指定每个区域输出的高和宽分别为$h_2$和$w_2$。假设某一兴趣区域窗口的高和宽分别为$h$和$w$,该窗口将被划分为形状为$h_2 \times w_2$的子窗口网格,且每个子窗口的大小大约为$(h/h_2) \times (w/w_2)$。任一子窗口的高和宽要取整,其中的最大元素作为该子窗口的输出。因此,兴趣区域池化层可从形状各异的兴趣区域中均抽取出形状相同的特征。 + +图9.7中,我们在$4 \times 4$的输入上选取了左上角的$3\times 3$区域作为兴趣区域。对于该兴趣区域,我们通过$2\times 2$兴趣区域池化层得到一个$2\times 2$的输出。4个划分后的子窗口分别含有元素0、1、4、5(5最大),2、6(6最大),8、9(9最大),10。 + +
+ +
+
图9.7 2×2兴趣区域池化层
+ +我们使用`ROIPooling`函数来演示兴趣区域池化层的计算。假设卷积神经网络抽取的特征`X`的高和宽均为4且只有单通道。 + +``` python +import torch +import torchvision + +X = torch.arange(16, dtype=torch.float).view(1, 1, 4, 4) +X +``` +输出: +``` +tensor([[[[ 0., 1., 2., 3.], + [ 4., 5., 6., 7.], + [ 8., 9., 10., 11.], + [12., 13., 14., 15.]]]]) +``` + +假设图像的高和宽均为40像素。再假设选择性搜索在图像上生成了两个提议区域:每个区域由5个元素表示,分别为区域目标类别、左上角的$x$和$y$轴坐标以及右下角的$x$和$y$轴坐标。 + +``` python +rois = torch.tensor([[0, 0, 0, 20, 20], [0, 0, 10, 30, 30]], dtype=torch.float) +``` + +由于`X`的高和宽是图像的高和宽的$1/10$,以上两个提议区域中的坐标先按`spatial_scale`自乘0.1,然后在`X`上分别标出兴趣区域`X[:,:,0:3,0:3]`和`X[:,:,1:4,0:4]`。最后对这两个兴趣区域分别划分子窗口网格并抽取高和宽为2的特征。 + +``` python +torchvision.ops.roi_pool(X, rois, output_size=(2, 2), spatial_scale=0.1) +``` +输出: +``` +tensor([[[[ 5., 6.], + [ 9., 10.]]], + + [[[ 9., 11.], + [13., 15.]]]]) +``` + +## 9.8.3 Faster R-CNN + +Fast R-CNN通常需要在选择性搜索中生成较多的提议区域,以获得较精确的目标检测结果。Faster R-CNN提出将选择性搜索替换成区域提议网络(region proposal network),从而减少提议区域的生成数量,并保证目标检测的精度。 + + +
+ +
+
图9.8 Faster R-CNN模型
+ + +图9.8描述了Faster R-CNN模型。与Fast R-CNN相比,只有生成提议区域的方法从选择性搜索变成了区域提议网络,而其他部分均保持不变。具体来说,区域提议网络的计算步骤如下。 + +1. 使用填充为1的$3\times 3$卷积层变换卷积神经网络的输出,并将输出通道数记为$c$。这样,卷积神经网络为图像抽取的特征图中的每个单元均得到一个长度为$c$的新特征。 +1. 以特征图每个单元为中心,生成多个不同大小和宽高比的锚框并标注它们。 +1. 用锚框中心单元长度为$c$的特征分别预测该锚框的二元类别(含目标还是背景)和边界框。 +1. 使用非极大值抑制,从预测类别为目标的预测边界框中移除相似的结果。最终输出的预测边界框即兴趣区域池化层所需要的提议区域。 + + +值得一提的是,区域提议网络作为Faster R-CNN的一部分,是和整个模型一起训练得到的。也就是说,Faster R-CNN的目标函数既包括目标检测中的类别和边界框预测,又包括区域提议网络中锚框的二元类别和边界框预测。最终,区域提议网络能够学习到如何生成高质量的提议区域,从而在减少提议区域数量的情况下也能保证目标检测的精度。 + + +## 9.8.4 Mask R-CNN + +如果训练数据还标注了每个目标在图像上的像素级位置,那么Mask R-CNN能有效利用这些详尽的标注信息进一步提升目标检测的精度。 + +
+ +
+
图9.9 Mask R-CNN模型
+ +如图9.9所示,Mask R-CNN在Faster R-CNN的基础上做了修改。Mask R-CNN将兴趣区域池化层替换成了兴趣区域对齐层,即通过双线性插值(bilinear interpolation)来保留特征图上的空间信息,从而更适于像素级预测。兴趣区域对齐层的输出包含了所有兴趣区域的形状相同的特征图。它们既用来预测兴趣区域的类别和边界框,又通过额外的全卷积网络预测目标的像素级位置。我们将在9.10节(全卷积网络)介绍如何使用全卷积网络预测图像中像素级的语义。 + + + +## 小结 + +* R-CNN对图像选取若干提议区域,然后用卷积神经网络对每个提议区域做前向计算抽取特征,再用这些特征预测提议区域的类别和边界框。 +* Fast R-CNN对R-CNN的一个主要改进在于只对整个图像做卷积神经网络的前向计算。它引入了兴趣区域池化层,从而令兴趣区域能够抽取出形状相同的特征。 +* Faster R-CNN将Fast R-CNN中的选择性搜索替换成区域提议网络,从而减少提议区域的生成数量,并保证目标检测的精度。 +* Mask R-CNN在Faster R-CNN基础上引入一个全卷积网络,从而借助目标的像素级位置进一步提升目标检测的精度。 + + +## 参考文献 + +[1] Girshick, R., Donahue, J., Darrell, T., & Malik, J. (2014). Rich feature hierarchies for accurate object detection and semantic segmentation. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 580-587). + +[2] Uijlings, J. R., Van De Sande, K. E., Gevers, T., & Smeulders, A. W. (2013). Selective search for object recognition. International journal of computer vision, 104(2), 154-171. + +[3] Girshick, R. (2015). Fast r-cnn. arXiv preprint arXiv:1504.08083. + +[4] Ren, S., He, K., Girshick, R., & Sun, J. (2015). Faster r-cnn: Towards real-time object detection with region proposal networks. In Advances in neural information processing systems (pp. 91-99). + +[5] He, K., Gkioxari, G., Dollár, P., & Girshick, R. (2017, October). Mask r-cnn. In Computer Vision (ICCV), 2017 IEEE International Conference on (pp. 2980-2988). IEEE. + +----------- +> 注:除代码外本节与原书基本相同,[原书传送门](http://zh.d2l.ai/chapter_computer-vision/rcnn.html) diff --git a/docs/img/chapter09/9.8_fast-rcnn.svg b/docs/img/chapter09/9.8_fast-rcnn.svg new file mode 100644 index 0000000000000000000000000000000000000000..4378bee73a4bb0b8d304d2d64e1627ea8661f471 --- /dev/null +++ b/docs/img/chapter09/9.8_fast-rcnn.svg @@ -0,0 +1,167 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/img/chapter09/9.8_faster-rcnn.svg b/docs/img/chapter09/9.8_faster-rcnn.svg new file mode 100644 index 0000000000000000000000000000000000000000..8f64cd7d211f9f6817c57b015afbfafc3b1afbb5 --- /dev/null +++ b/docs/img/chapter09/9.8_faster-rcnn.svg @@ -0,0 +1,233 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/img/chapter09/9.8_mask-rcnn.svg b/docs/img/chapter09/9.8_mask-rcnn.svg new file mode 100644 index 0000000000000000000000000000000000000000..7b329849cf87c20c14f0a9daffc6f14926c14d67 --- /dev/null +++ b/docs/img/chapter09/9.8_mask-rcnn.svg @@ -0,0 +1,184 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/img/chapter09/9.8_r-cnn.svg b/docs/img/chapter09/9.8_r-cnn.svg new file mode 100644 index 0000000000000000000000000000000000000000..40d6f85e58ee17bfc28a70e505d19aaf4a367402 --- /dev/null +++ b/docs/img/chapter09/9.8_r-cnn.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/docs/img/chapter09/9.8_roi.svg b/docs/img/chapter09/9.8_roi.svg new file mode 100644 index 0000000000000000000000000000000000000000..ae74064db2a976a7e6e054a82bf0f04797f3ef67 --- /dev/null +++ b/docs/img/chapter09/9.8_roi.svg @@ -0,0 +1,206 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +