详细的Faster R-CNN源码解析之ROI-Pooling逐行代码解析

好的文章怕没了,先转载过来。。。。。。。


详细的Faster R-CNN源码解析之ROI-Pooling逐行代码解析

原创  2018年04月20日 19:45:56
  • 205

   在笔者的上一篇博客中,解析了Faster R-CNN中的RPN代码,在本篇博客中,笔者详细地解析一下ROI-Pooling代码。为大家讲解2015年Fast R-CNN的核心贡献(ROI Pooling被Faster R-CNN沿用)ROI Pooling的实现原理。(笔者其实一年半之前就看过这个代码,只是当时没有写到博客上,感慨.jpg)

   在代码解析正式开始之前,笔者声明几点:

1. 本篇博客解析的ROI-Pooling代码分为两个框架下实现的,第一个当然是Ross Girshick实现的py-faster-rcnn中使用的ROI-Pooling代码,工程代码链接地址https://github.com/rbgirshick/py-faster-rcnn,ROI-Pooling代码链接地址https://github.com/rbgirshick/caffe-fast-rcnn/tree/0dcd397b29507b8314e252e850518c5695efbb83,后者打开是一个包含ROI-Pooling的caffe源码包,关于ROI-Pooling代码详见include/caffe/fast_rcnn_layers.hpp,src/caffe/proto/caffe.proto和src/caffe/layers/roi_pooling_layers.cpp以及src/caffe/layers/roi_pooling_layers.cu等四个文件。第二个是tensorflow实现的Faster R-CNN,工程链接地址https://github.com/kevinjliang/tf-Faster-RCNN,ROI-Pooling代码在Lib/roi_pool.py文件中。

2. 在大家看代码解析之前请大家完全明了ROI-Pooling的原理,有以下几个途径:

1) 直接进行Fast R-CNN论文阅读,链接https://arxiv.org/abs/1504.08083

2) 可以参考一下笔者的博客,里面对ROI-Pooling做出了解析,详询以下链接:

实例分割模型Mask R-CNN详解:从R-CNN,Fast R-CNN,Faster R-CNN再到Mask R-CNN

3) Region of interest pooling explained

   下面开始正文:

   首先是原版的ROI-Pooling的实现,是用c++语言实现的,在caffe框架下运行,代码分成四个部分。

   第一个部分是caffe.proto文件中的定义:

[cpp]  view plain  copy
  1. // Message that stores parameters used by ROIPoolingLayer  
  2. message ROIPoolingParameter {  
  3.   // Pad, kernel size, and stride are all given as a single value for equal  
  4.   // dimensions in height and width or as Y, X pairs.  
  5.   optional uint32 pooled_h = 1 [default = 0];//The pooled output height: roi pool过后的特征的高  
  6.   optional uint32 pooled_w = 2 [default = 0];//The pooled output width: roi pool过后的特征的宽  
  7.   // Multiplicative spatial scale factor to translate ROI coords from their  
  8.   // input scale to the scale used when pooling  
  9.   optional float spatial_scale = 3 [default = 1];//共享卷积层输出的Feature Map相较于原图缩小的尺度(1/16)  
  10. }  

   上面的proto文件定义了三个参数,roi_pool输出特征的,输出特征的和共享卷积层输出的特征相较于原图长宽缩小的尺度,如共享卷积层使用的是ResNet50,那么该值就是1/16。

   第二个部分是fast_rcnn_layers.hpp中的定义:

[cpp]  view plain  copy
  1. /* ROIPoolingLayer - Region of Interest Pooling Layer 
  2. */  
  3. template <typename Dtype>  
  4. class ROIPoolingLayer : public Layer<Dtype> {  
  5.  public:  
  6.   explicit ROIPoolingLayer(const LayerParameter& param)  
  7.       : Layer<Dtype>(param) {} //空的构造函数继承父类  
  8.   virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,  
  9.       const vector<Blob<Dtype>*>& top); //Layer初始化函数,实现详见cpp文件  
  10.   virtual void Reshape(const vector<Blob<Dtype>*>& bottom,  
  11.       const vector<Blob<Dtype>*>& top); //Reshape函数,实现详见cpp文件  
  12.   
  13.   virtual inline const char* type() const { return "ROIPooling"; }  
  14.   
  15.   //bottom blob最小和最大数量都为2,第一个bottom blob是共享卷积层输出的特征,第二个是roi的信息  
  16.   virtual inline int MinBottomBlobs() const { return 2; }  
  17.   virtual inline int MaxBottomBlobs() const { return 2; }  
  18.   //top blob最小和最大数量都为1,表示pool过后的roi特征  
  19.   virtual inline int MinTopBlobs() const { return 1; }  
  20.   virtual inline int MaxTopBlobs() const { return 1; }  
  21.   
  22.  protected:  
  23.   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,  
  24.       const vector<Blob<Dtype>*>& top); //cpu前传  
  25.   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,  
  26.       const vector<Blob<Dtype>*>& top); //cpu反传  
  27.   virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,  
  28.       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); //gpu前传  
  29.   virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,  
  30.       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); //gpu反传  
  31.   
  32.   int channels_; //记录共享卷积层输出的特征的channels  
  33.   int height_; //记录共享卷积层输出的特征的高  
  34.   int width_; //记录共享卷积层输出的特征的宽  
  35.   int pooled_height_; //记录pool过后的特征的高  
  36.   int pooled_width_; //记录pool过后的特征的宽  
  37.   Dtype spatial_scale_; //记录共享特征图相比于原图缩小的尺度,如果共享卷积层的使用的是ResNet50,那么就为1/16  
  38.   Blob<int> max_idx_; //记录roi_pool过后得到的特征中的每个值在共享卷积层输出的特征图中的位置索引  
  39. };  

   在头文件定义中,除了caffe的四个标配LayerSetUp,Reshape,Forward和Backward四个模块定义之外,还有一些别的定义,详见上面的代码片最下方。其中有一个不显眼的Blob<int> 类型的变量max_idx_,先告诉大家这是ROI_Pooling中非常重要的变量之一,记录了roi pool过后的特征中的各个值在共享卷积层输出的特征图上的位置,这在反传过程中会起到核心作用。

   第三个部分是roi_pooling_layer.cpp中的LayerSetUp,Reshape和Forward_cpu部分

[cpp]  view plain  copy
  1. // ------------------------------------------------------------------  
  2. // Fast R-CNN  
  3. // Copyright (c) 2015 Microsoft  
  4. // Licensed under The MIT License [see fast-rcnn/LICENSE for details]  
  5. // Written by Ross Girshick  
  6. // ------------------------------------------------------------------  
  7.   
  8. #include <cfloat>  
  9.   
  10. #include "caffe/fast_rcnn_layers.hpp"  
  11.   
  12. using std::max;  
  13. using std::min;  
  14. using std::floor;  
  15. using std::ceil;  
  16.   
  17. namespace caffe {  
  18.   
  19. template <typename Dtype>  
  20. void ROIPoolingLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,  
  21.       const vector<Blob<Dtype>*>& top) {  
  22.   ROIPoolingParameter roi_pool_param = this->layer_param_.roi_pooling_param();  
  23.   CHECK_GT(roi_pool_param.pooled_h(), 0)  
  24.       << "pooled_h must be > 0";  
  25.   CHECK_GT(roi_pool_param.pooled_w(), 0)  
  26.       << "pooled_w must be > 0";  
  27.   //LayerSetUp函数,主要做了一件事,就是给三个变量赋值,这三个变量在caffe.proto文件里面指定:  
  28.   pooled_height_ = roi_pool_param.pooled_h();//第一个是roi pool过后的高  
  29.   pooled_width_ = roi_pool_param.pooled_w();//第二个是roi pool过后的宽  
  30.   spatial_scale_ = roi_pool_param.spatial_scale();//第三个是共享卷积层输出的Feature Map相较于原图缩小的尺度  
  31.   LOG(INFO) << "Spatial scale: " << spatial_scale_;  
  32. }  
  33.   
  34. template <typename Dtype>  
  35. void ROIPoolingLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,  
  36.       const vector<Blob<Dtype>*>& top) {  
  37.   channels_ = bottom[0]->channels();//得到共享卷积的层输出的特征图的channel数量  
  38.   height_ = bottom[0]->height();//得到共享卷积的层输出的特征图的高  
  39.   width_ = bottom[0]->width();//得到共享卷积的层输出的特征图的宽  
  40.   top[0]->Reshape(bottom[1]->num(), channels_, pooled_height_,  
  41.       pooled_width_);//top[0]表示pool过后的特征,第一维为roi的数量(即bottom[1]->num())  
  42.   max_idx_.Reshape(bottom[1]->num(), channels_, pooled_height_,  
  43.       pooled_width_);//max_idx_表示pool过后的特征中的每一个值在原共享特征图上的索引,第一维亦为roi的数量(即bottom[1]->num())  
  44. }  
  45.   
  46. //cpu前传函数  
  47. template <typename Dtype>  
  48. void ROIPoolingLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,  
  49.       const vector<Blob<Dtype>*>& top) {  
  50.   const Dtype* bottom_data = bottom[0]->cpu_data();//bottom_data表示共享卷积层输出的特征图  
  51.   const Dtype* bottom_rois = bottom[1]->cpu_data();//bottom_rois表示rois的信息,一共5维,第一维表示rois在训练batch中的图片索引,后四维表示rois的坐标信息  
  52.   // Number of ROIs  
  53.   int num_rois = bottom[1]->num();//roi的总数  
  54.   int batch_size = bottom[0]->num();//batch_size表示一次训练中输入的图片数量,因为roi并不是都存在于同一张图片上  
  55.   int top_count = top[0]->count();//top_count表示top[0]的全部容量(N*C*H*W)  
  56.   Dtype* top_data = top[0]->mutable_cpu_data();//使用top_data指针索引top[0]  
  57.   caffe_set(top_count, Dtype(-FLT_MAX), top_data);//初始化top_data(即top[0]),将其初始值(共top_count个)置为最小值(-FLT_MAX)  
  58.   int* argmax_data = max_idx_.mutable_cpu_data();//使用argmax_data指针索引max_idx_  
  59.   caffe_set(top_count, -1, argmax_data);//初始化argmax_data(即max_idx_),将其初始值(共top_count个)置为-1  
  60.   
  61.   // For each ROI R = [batch_index x1 y1 x2 y2]: max pool over R (rois的五维数据的说明)  
  62.   for (int n = 0; n < num_rois; ++n) {//第一个for循环,遍历rois  
  63.     int roi_batch_ind = bottom_rois[0];//找到该roi对应的训练图片在训练batch中的索引  
  64.     int roi_start_w = round(bottom_rois[1] * spatial_scale_);//得到该roi的x1(左上角x坐标)在共享卷积层输出的特征图上面的位置(注意使用了round取整)  
  65.     int roi_start_h = round(bottom_rois[2] * spatial_scale_);//得到该roi的y1(左上角y坐标)在共享卷积层输出的特征图上面的位置(注意使用了round取整)  
  66.     int roi_end_w = round(bottom_rois[3] * spatial_scale_);//得到该roi的x2(右下角x坐标)在共享卷积层输出的特征图上面的位置(注意使用了round取整)  
  67.     int roi_end_h = round(bottom_rois[4] * spatial_scale_);//得到该roi的y2(右下角x坐标)在共享卷积层输出的特征图上面的位置(注意使用了round取整)  
  68.     //下面两行代码核验该roi必须要出现在训练batch的某一张图片中  
  69.     CHECK_GE(roi_batch_ind, 0);//核验roi_batch_ind是否大于等于零  
  70.     CHECK_LT(roi_batch_ind, batch_size);//核验roi_batch_ind是否小于batch_size  
  71.   
  72.     int roi_height = max(roi_end_h - roi_start_h + 1, 1);//得到该roi在共享卷积层输出的特征图上面的高  
  73.     int roi_width = max(roi_end_w - roi_start_w + 1, 1);//得到该roi在共享卷积层输出的特征图上面的宽  
  74.     const Dtype bin_size_h = static_cast<Dtype>(roi_height)  
  75.                              / static_cast<Dtype>(pooled_height_);//得到roi_pool的时候该roi在高方向上被划分的段数  
  76.     const Dtype bin_size_w = static_cast<Dtype>(roi_width)  
  77.                              / static_cast<Dtype>(pooled_width_);//得到roi_pool的时候该roi在宽方向上被划分的段数  
  78.   
  79.     const Dtype* batch_data = bottom_data + bottom[0]->offset(roi_batch_ind);//找到该roi对应的batch中的那张图片  
  80.   
  81.     for (int c = 0; c < channels_; ++c) {//第二个for循环,遍历channels  
  82.       for (int ph = 0; ph < pooled_height_; ++ph) {//第三个for循环,遍历pooled_height_,即pool过后的roi的高  
  83.         for (int pw = 0; pw < pooled_width_; ++pw) {//第四个for循环,遍历pooled_width_,即pool过后的roi的宽  
  84.           // Compute pooling region for this output unit:  
  85.           //  start (included) = floor(ph * roi_height / pooled_height_)  
  86.           //  end (excluded) = ceil((ph + 1) * roi_height / pooled_height_)  
  87.           //hstart,wstart,hend,wend构成了在得到roi_pool输出中每一个值的过程中在共享卷积层输出的特征图上面的检索区域的坐标,下称"该值"  
  88.           int hstart = static_cast<int>(floor(static_cast<Dtype>(ph)  
  89.                                               * bin_size_h));//找到该值对应的检索区域在共享卷积层输出的特征图上面的高开始的偏移坐标  
  90.           int wstart = static_cast<int>(floor(static_cast<Dtype>(pw)  
  91.                                               * bin_size_w));//找到该值对应的检索区域在共享卷积层输出的特征图上面的宽开始的偏移坐标  
  92.           int hend = static_cast<int>(ceil(static_cast<Dtype>(ph + 1)  
  93.                                            * bin_size_h));//找到该值对应的检索区域在共享卷积层输出的特征图上面的高结束的偏移坐标  
  94.           int wend = static_cast<int>(ceil(static_cast<Dtype>(pw + 1)  
  95.                                            * bin_size_w));//找到该值对应的检索区域在共享卷积层输出的特征图上面的宽结束的偏移坐标  
  96.   
  97.           hstart = min(max(hstart + roi_start_h, 0), height_);//找到该值对应的检索区域在共享卷积层输出的特征图上面的高开始坐标  
  98.           hend = min(max(hend + roi_start_h, 0), height_);//找到该值对应的检索区域在共享卷积层输出的特征图上面的宽开始坐标  
  99.           wstart = min(max(wstart + roi_start_w, 0), width_);//找到该值对应的检索区域在共享卷积层输出的特征图上面的高结束坐标  
  100.           wend = min(max(wend + roi_start_w, 0), width_);//找到该值对应的检索区域在共享卷积层输出的特征图上面的宽结束坐标  
  101.   
  102.           bool is_empty = (hend <= hstart) || (wend <= wstart);//如果说该roi的尺寸非法,即(hend <= hstart)或者(wend <= wstart),is_empty就为true  
  103.   
  104.           const int pool_index = ph * pooled_width_ + pw;//找到该值对应的pool_index  
  105.           if (is_empty) {  
  106.             top_data[pool_index] = 0;//如果is_empty为true,那么该值就为0  
  107.             argmax_data[pool_index] = -1;//如果is_empty为true,那么该值对应的max_idx_为-1  
  108.           }  
  109.   
  110.           for (int h = hstart; h < hend; ++h) {//第五个for循环,遍历该值对应的检索区域的高  
  111.             for (int w = wstart; w < wend; ++w) {//第六个for循环,遍历该值对应的检索区域的宽  
  112.               const int index = h * width_ + w;//找到检索区域中正被比较的值在共享卷积层输出的特征图上面的位置索引  
  113.               if (batch_data[index] > top_data[pool_index]) {  
  114.                 top_data[pool_index] = batch_data[index];//该值对应检索区域中的最大值  
  115.                 argmax_data[pool_index] = index;//记录该值对应的共享特征图上面的位置  
  116.               }  
  117.             }  
  118.           }  
  119.         }  
  120.       }//结束第三至第六个for循环  
  121.       // Increment all data pointers by one channel  
  122.       batch_data += bottom[0]->offset(0, 1);//做完了一个channel,将batch_data索引指针移动到下一个channel继续pool  
  123.       top_data += top[0]->offset(0, 1);//做完了一个channel,将输出索引指针移动到下一个channel继续接收pool的输出  
  124.       argmax_data += max_idx_.offset(0, 1);//做完了一个channel,将max_idx_索引指针移动到下一个channel继续接收坐标记录  
  125.     }//结束第二个for循环  
  126.     // Increment ROI data pointer  
  127.     bottom_rois += bottom[1]->offset(1);//处理完了一个roi,接着处理下一个roi  
  128.   }//结束第一个for循环  
  129. }  
  130.   
  131. //Backward_cpu没有实现,在.cu文件上实现的gpu版本  
  132. template <typename Dtype>  
  133. void ROIPoolingLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,  
  134.       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {  
  135.   NOT_IMPLEMENTED;  
  136. }  
  137.   
  138.   
  139. #ifdef CPU_ONLY  
  140. STUB_GPU(ROIPoolingLayer);  
  141. #endif  
  142.   
  143. INSTANTIATE_CLASS(ROIPoolingLayer);  
  144. REGISTER_LAYER_CLASS(ROIPooling);  
  145.   
  146. }  // namespace caffe  

   在上面的代码中,我们可以看到,Forward_cpu函数核心是使用六个for循环实现。大家可以看到,ROI_Pooling是依次对每一个ROI实现的(第一个for循环),在得到一个ROI的信息之后,首先要找到该ROI对应的训练batch中的图片的特征在bottom[0]中的位置,即batch_data。然后,对于逐个通道(第二个for循环)将每一个通道上的ROI区域划分成pooled_height_×pooled_width_份(第三个和第四个for循环),将每一份中(hstart, hend, wstart和wend规定的区域)的最大值填入该通道roi_pool的输出区域(第五个和第六个for循环找最大值)。请大家注意,在做上述操作的同时,前传函数还记录了ROI Pooling的输出中的每一个值在共享卷积层输出的FeatureMap上面的位置,通过argmax_data指针记录,并保存在max_idx_里面。记录输出值的位置这个操作相当关键相当关键相当关键因为在反传的时候,梯度只会反传到相应位置的值上面。最后,对于反传的cpu版本,作者并没有实现,而是实现在gpu版本上。

   第四个部分是roi_pooling_layer.cu中的Forward_gpu和Backward_gpu部分:

[cpp]  view plain  copy
  1. // ------------------------------------------------------------------  
  2. // Fast R-CNN  
  3. // Copyright (c) 2015 Microsoft  
  4. // Licensed under The MIT License [see fast-rcnn/LICENSE for details]  
  5. // Written by Ross Girshick  
  6. // ------------------------------------------------------------------  
  7.   
  8. #include <cfloat>  
  9.   
  10. #include "caffe/fast_rcnn_layers.hpp"  
  11.   
  12. using std::max;  
  13. using std::min;  
  14.   
  15. namespace caffe {  
  16.   
  17. //以下是roi_pooling前传的gpu版本,和cpu版本差别不大,主要在gpu上变成了多线程实现。原理请详见roi_pooling_layer.cpp文件。  
  18. template <typename Dtype>  
  19. __global__ void ROIPoolForward(const int nthreads, const Dtype* bottom_data,  
  20.     const Dtype spatial_scale, const int channels, const int height,  
  21.     const int width, const int pooled_height, const int pooled_width,  
  22.     const Dtype* bottom_rois, Dtype* top_data, int* argmax_data) {  
  23.   CUDA_KERNEL_LOOP(index, nthreads) {  
  24.     // (n, c, ph, pw) is an element in the pooled output  
  25.     int pw = index % pooled_width;  
  26.     int ph = (index / pooled_width) % pooled_height;  
  27.     int c = (index / pooled_width / pooled_height) % channels;  
  28.     int n = index / pooled_width / pooled_height / channels;  
  29.   
  30.     bottom_rois += n * 5;  
  31.     int roi_batch_ind = bottom_rois[0];  
  32.     int roi_start_w = round(bottom_rois[1] * spatial_scale);  
  33.     int roi_start_h = round(bottom_rois[2] * spatial_scale);  
  34.     int roi_end_w = round(bottom_rois[3] * spatial_scale);  
  35.     int roi_end_h = round(bottom_rois[4] * spatial_scale);  
  36.   
  37.     // Force malformed ROIs to be 1x1  
  38.     int roi_width = max(roi_end_w - roi_start_w + 1, 1);  
  39.     int roi_height = max(roi_end_h - roi_start_h + 1, 1);  
  40.     Dtype bin_size_h = static_cast<Dtype>(roi_height)  
  41.                        / static_cast<Dtype>(pooled_height);  
  42.     Dtype bin_size_w = static_cast<Dtype>(roi_width)  
  43.                        / static_cast<Dtype>(pooled_width);  
  44.   
  45.     int hstart = static_cast<int>(floor(static_cast<Dtype>(ph)  
  46.                                         * bin_size_h));  
  47.     int wstart = static_cast<int>(floor(static_cast<Dtype>(pw)  
  48.                                         * bin_size_w));  
  49.     int hend = static_cast<int>(ceil(static_cast<Dtype>(ph + 1)  
  50.                                      * bin_size_h));  
  51.     int wend = static_cast<int>(ceil(static_cast<Dtype>(pw + 1)  
  52.                                      * bin_size_w));  
  53.   
  54.     // Add roi offsets and clip to input boundaries  
  55.     hstart = min(max(hstart + roi_start_h, 0), height);  
  56.     hend = min(max(hend + roi_start_h, 0), height);  
  57.     wstart = min(max(wstart + roi_start_w, 0), width);  
  58.     wend = min(max(wend + roi_start_w, 0), width);  
  59.     bool is_empty = (hend <= hstart) || (wend <= wstart);  
  60.   
  61.     // Define an empty pooling region to be zero  
  62.     Dtype maxval = is_empty ? 0 : -FLT_MAX;  
  63.     // If nothing is pooled, argmax = -1 causes nothing to be backprop'd  
  64.     int maxidx = -1;  
  65.     bottom_data += (roi_batch_ind * channels + c) * height * width;  
  66.     for (int h = hstart; h < hend; ++h) {  
  67.       for (int w = wstart; w < wend; ++w) {  
  68.         int bottom_index = h * width + w;  
  69.         if (bottom_data[bottom_index] > maxval) {  
  70.           maxval = bottom_data[bottom_index];  
  71.           maxidx = bottom_index;  
  72.         }  
  73.       }  
  74.     }  
  75.     top_data[index] = maxval;  
  76.     argmax_data[index] = maxidx;  
  77.   }  
  78. }  
  79.   
  80. template <typename Dtype>  
  81. void ROIPoolingLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,  
  82.       const vector<Blob<Dtype>*>& top) {  
  83.   const Dtype* bottom_data = bottom[0]->gpu_data();  
  84.   const Dtype* bottom_rois = bottom[1]->gpu_data();  
  85.   Dtype* top_data = top[0]->mutable_gpu_data();  
  86.   int* argmax_data = max_idx_.mutable_gpu_data();  
  87.   int count = top[0]->count();  
  88.   // NOLINT_NEXT_LINE(whitespace/operators)  
  89.   ROIPoolForward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(  
  90.       count, bottom_data, spatial_scale_, channels_, height_, width_,  
  91.       pooled_height_, pooled_width_, bottom_rois, top_data, argmax_data);  
  92.   CUDA_POST_KERNEL_CHECK;  
  93. }  
  94.   
  95.   
  96.   
  97. //以上是roi_pooling前传的gpu版本,和cpu版本差别不大,主要在gpu上变成了多线程实现。原理请详见roi_pooling_layer.cpp文件。  
  98. //——————————————————————————————————分割线——————————————————————————————————————————  
  99. //..................................分割线..........................................  
  100. //----------------------------------分割线------------------------------------------  
  101.   
  102.   
  103. //以下是roi_pooling反传的gpu版本。  
  104. template <typename Dtype>  
  105. __global__ void ROIPoolBackward(const int nthreads, const Dtype* top_diff,  
  106.     const int* argmax_data, const int num_rois, const Dtype spatial_scale,  
  107.     const int channels, const int height, const int width,  
  108.     const int pooled_height, const int pooled_width, Dtype* bottom_diff,  
  109.     const Dtype* bottom_rois) {  
  110.   CUDA_KERNEL_LOOP(index, nthreads) {//第一个LOOP  
  111.     // (n, c, h, w) coords in bottom data  
  112.     //对于共享卷积层输出的每一个值,找到对应的w,h,c,n坐标  
  113.     int w = index % width;  
  114.     int h = (index / width) % height;  
  115.     int c = (index / width / height) % channels;  
  116.     int n = index / width / height / channels;  
  117.   
  118.     Dtype gradient = 0;  
  119.     // Accumulate gradient over all ROIs that pooled this element  
  120.     //下面开始累积梯度  
  121.     for (int roi_n = 0; roi_n < num_rois; ++roi_n) {//第一个for循环,遍历所有roi  
  122.       const Dtype* offset_bottom_rois = bottom_rois + roi_n * 5;//得到一个roi的信息  
  123.       int roi_batch_ind = offset_bottom_rois[0];//得到这个roi对应的训练batch的图片索引  
  124.       // Skip if ROI's batch index doesn't match n  
  125.       if (n != roi_batch_ind) {//如果这个roi对应的图片不是目前累积梯度的特征值所在的图片,直接continue  
  126.         continue;  
  127.       }  
  128.   
  129.       int roi_start_w = round(offset_bottom_rois[1] * spatial_scale);//得到该roi的x1(左上角x坐标)在共享卷积层输出的特征图上面的位置  
  130.       int roi_start_h = round(offset_bottom_rois[2] * spatial_scale);//得到该roi的y1(左上角y坐标)在共享卷积层输出的特征图上面的位置  
  131.       int roi_end_w = round(offset_bottom_rois[3] * spatial_scale);//得到该roi的x2(右下角x坐标)在共享卷积层输出的特征图上面的位置  
  132.       int roi_end_h = round(offset_bottom_rois[4] * spatial_scale);//得到该roi的y2(右下角x坐标)在共享卷积层输出的特征图上面的位置  
  133.   
  134.       // Skip if ROI doesn't include (h, w)  
  135.       const bool in_roi = (w >= roi_start_w && w <= roi_end_w &&  
  136.                            h >= roi_start_h && h <= roi_end_h);  
  137.       if (!in_roi) {  
  138.         continue;  
  139.       }//判断一下roi的坐标是否合法,如果不合法直接continue  
  140.   
  141.       int offset = (roi_n * channels + c) * pooled_height * pooled_width;//找到这个roi对应的顶层梯度blob的偏移量  
  142.       const Dtype* offset_top_diff = top_diff + offset;//找到这个roi对应的顶层梯度blob中的位置  
  143.       const int* offset_argmax_data = argmax_data + offset;//找到这个roi对应的pooled_feature在共享卷积层输出的特征里面的位置,梯度反传的时候非常重要  
  144.   
  145.       // Compute feasible set of pooled units that could have pooled  
  146.       // this bottom unit  
  147.   
  148.       // Force malformed ROIs to be 1x1  
  149.       int roi_width = max(roi_end_w - roi_start_w + 1, 1);//得到这个roi在共享卷积层输出的特征图上面的宽  
  150.       int roi_height = max(roi_end_h - roi_start_h + 1, 1);//得到这个roi在共享卷积层输出的特征图上面的高  
  151.   
  152.       Dtype bin_size_h = static_cast<Dtype>(roi_height)  
  153.                          / static_cast<Dtype>(pooled_height);//得到roi_pool的时候这个roi在高方向上被划分的段数  
  154.       Dtype bin_size_w = static_cast<Dtype>(roi_width)  
  155.                          / static_cast<Dtype>(pooled_width);//得到roi_pool的时候这个roi在宽方向上被划分的段数  
  156.   
  157.       int phstart = floor(static_cast<Dtype>(h - roi_start_h) / bin_size_h);//得到共享特征图上的某个值对应的在roi_pool的输出上的高起始坐标  
  158.       int phend = ceil(static_cast<Dtype>(h - roi_start_h + 1) / bin_size_h);//得到共享特征图上的某个值对应的在roi_pool的输出上的高结束坐标  
  159.       int pwstart = floor(static_cast<Dtype>(w - roi_start_w) / bin_size_w);//得到共享特征图上的某个值对应的在roi_pool的输出上的宽起始坐标  
  160.       int pwend = ceil(static_cast<Dtype>(w - roi_start_w + 1) / bin_size_w);//得到共享特征图上的某个值对应的在roi_pool的输出上的宽结束坐标  
  161.   
  162.       phstart = min(max(phstart, 0), pooled_height);//得到共享特征图上的某个值对应的在roi_pool的输出上的最终的高起始坐标,对上面求出的坐标做判断,必须要大于0,和小于pooled_height,意思是该点在这个roi之内,下同  
  163.       phend = min(max(phend, 0), pooled_height);//得到共享特征图上的某个值对应的在roi_pool的输出上的最终的高结束坐标  
  164.       pwstart = min(max(pwstart, 0), pooled_width);//得到共享特征图上的某个值对应的在roi_pool的输出上的最终的宽起始坐标  
  165.       pwend = min(max(pwend, 0), pooled_width);//得到共享特征图上的某个值对应的在roi_pool的输出上的最终的宽结束坐标  
  166.   
  167.       for (int ph = phstart; ph < phend; ++ph) {//第二个for循环,遍历共享特征图上的某个值对应的对应的roi_pool输出区域的高的范围  
  168.         for (int pw = pwstart; pw < pwend; ++pw) {//第三个for循环,遍历共享特征图上的某个值对应的对应的roi_pool输出区域的宽的范围  
  169.           if (offset_argmax_data[ph * pooled_width + pw] == (h * width + w)) {  
  170.             gradient += offset_top_diff[ph * pooled_width + pw];  
  171.           }//一旦发现记录的roi_pool输出得到的特征上对应的共享特征图上的位置等同于目前被累积梯度的共享特征图上的位置,就在目前的共享特征图上的位置累积top传下来的该点对应的梯度  
  172.         }//结束第三个for循环  
  173.       }//结束第二个for循环  
  174.     }//结束第一个for循环  
  175.     bottom_diff[index] = gradient;//最终得到共享特征图上某一点对应的总的梯度  
  176.   }//结束LOOP  
  177. }  
  178.   
  179. template <typename Dtype>  
  180. void ROIPoolingLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,  
  181.       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {  
  182.   if (!propagate_down[0]) {  
  183.     return;  
  184.   }//如果不需反传,直接return  
  185.   const Dtype* bottom_rois = bottom[1]->gpu_data();//bottom_rois对应rois的信息  
  186.   const Dtype* top_diff = top[0]->gpu_diff();//top_diff对应顶层的梯度  
  187.   Dtype* bottom_diff = bottom[0]->mutable_gpu_diff();//bottom_diff对应需要传到共享卷积层的梯度  
  188.   const int count = bottom[0]->count();//count对应bottom[0]的容量(即共享卷积层输出的特征的容量)  
  189.   caffe_gpu_set(count, Dtype(0.), bottom_diff);//将bottom[0]的梯度全部初始化为零  
  190.   const int* argmax_data = max_idx_.gpu_data();//argmax_data对应roi_pool输出得到的特征上每一个值对应的共享卷积层输出的特征图上的位置  
  191.   // NOLINT_NEXT_LINE(whitespace/operators)  
  192.   //下面是具体的反传函数  
  193.   ROIPoolBackward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(  
  194.       count, top_diff, argmax_data, top[0]->num(), spatial_scale_, channels_,  
  195.       height_, width_, pooled_height_, pooled_width_, bottom_diff, bottom_rois);  
  196.   CUDA_POST_KERNEL_CHECK;  
  197. }  
  198.   
  199. INSTANTIATE_LAYER_GPU_FUNCS(ROIPoolingLayer);  
  200.   
  201. }  // namespace caffe  

   大家可以看到,上述代码中,ROI Pooling的反传主要是通过一个CUDA_KERNEL_LOOP和三个for循环实现。最外层的CUDA_KERNEL_LOOP遍历了共享卷积层输出的特征图上面的每一个位置的梯度。然后,第一个for循环表示是逐ROI进行第二个和第三个for循环表示的是从高和宽方向遍历ROI_Pooling的输出blob。在这里有个问题,channel哪去了?大家可以看到channel已经被融合在第一个CUDA_KERNEL_LOOP中了,也就是说反传中的梯度累积是对共享卷积层输出的特征图中的每一个位置进行的。对于共享卷积层输出的特征图中的每一个位置的梯度,找到其可能出现在ROI_Pool输出的特征图上的位置并进行位置匹配,一旦发现共享卷积层输出的特征图中这个值的位置和在前传的过程中记录的位置相同,则将该ROI_Pool输出的特征图上的位置对应的梯度累积到共享卷积层输出的特征图中的那个位置上。

   总的来说,既然ROI Pooling的输出是从共享卷积层输出的特征图上面选择的,那么,在反传的时候,梯度也自然反传到相应的被选择的位置上面。

   以上四个部分,就是Ross Girshick的py-faster-rcnn工程里面使用的ROI Pooling代码,总的来说还是相当复杂的,代码确实让人眼花缭乱。不过,在理解透彻了ROI Pooling的原理之后再看这个代码,就比较轻松了。

   下面,我们来看一下tensorflow中实现的ROI Pooling的代码:

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-  
  2. """ 
  3. Created on Wed Jan 11 14:29:57 2017 
  4.  
  5. @author: Kevin Liang 
  6.  
  7. ROI pooling layer: Using tensorflow's crop_and_resize function as replacement. 
  8. crop_and_resize expects box indices in normalized coordinates. 
  9.  
  10. Convolutional feature maps are cropped to a constant size of (14,14) and then 
  11. maxpooled to (7x7) 
  12. """  
  13.   
  14. import tensorflow as tf  
  15.   
  16. #featureMaps指的是共享卷积层输出的特征图  
  17. #rois指的是需要被pool的框的坐标,shape[M, 5],后一维中5个值中的第一个值指的是该框在某一个训练batch中的图片索引  
  18. #im_dims指的是图片的尺度,shape[N, 2],N指的是batch_size,这里固定为1  
  19. def roi_pool(featureMaps,rois,im_dims):     
  20.     ''''' 
  21.     Regions of Interest (ROIs) from the Region Proposal Network (RPN) are  
  22.     formatted as: 
  23.     (image_id, x1, y1, x2, y2) 
  24.      
  25.     Note: Since mini-batches are sampled from a single image, image_id = 0s 
  26.     '''  
  27.     with tf.variable_scope('roi_pool'):  
  28.         # Image that the ROI is taken from (minibatch of 1 means these will all be 0)  
  29.         box_ind = tf.cast(rois[:,0],dtype=tf.int32)#在这里取到所有需要pool的框在训练batch中的对应图片序号,由于batch_size为1,因此box_ind里面的值都为0  
  30.           
  31.         # ROI box coordinates. Must be normalized and ordered to [y1, x1, y2, x2]  
  32.         boxes = rois[:,1:] #在这里取到所有的需要pool的框 shape[N, 4]  
  33.         normalization = tf.cast(tf.stack([im_dims[:,1],im_dims[:,0],im_dims[:,1],im_dims[:,0]],axis=1),dtype=tf.float32) #在这里取到归一化框的坐标时需要的图片尺度  
  34.         boxes = tf.div(boxes,normalization) #在这里归一化框的坐标  
  35.         boxes = tf.stack([boxes[:,1],boxes[:,0],boxes[:,3],boxes[:,2]],axis=1)  # y1, x1, y2, x2 在这里交换框的坐标(x1, y1, x2, y2)->(y1, x1, y2, x2)  
  36.           
  37.         # ROI pool output size  
  38.         crop_size = tf.constant([14,14]) #在这里规定初始pool过后输出的尺度  
  39.           
  40.         # ROI pool  
  41.     #进行ROI pool,之所以需要归一化框的坐标是因为tf接口的要求  
  42.         pooledFeatures = tf.image.crop_and_resize(image=featureMaps, boxes=boxes, box_ind=box_ind, crop_size=crop_size)  
  43.           
  44.         # Max pool to (7x7)  
  45.     #用2×2的滑动窗口进行最大池化操作,输出的尺度是7×7  
  46.         pooledFeatures = tf.nn.max_pool(pooledFeatures, ksize=[1221], strides=[1221], padding='SAME')  
  47.   
  48.     return pooledFeatures  

   非常意外,ROI Pooling在tensorflow中已经被规范在了框架里面,使用tf.image.crop_and_resize函数实现。


   在tf.image.crop_and_resize函数中,image就是共享卷积层输出的特征图。


   boxes指的就是ROI的坐标,不过需要换成[y1, x1, y2, x2]的顺序,并且要归一化。可见在代码中做出了交换并使用tf.div进行了归一化。


   box_ind就是rois在训练batch中的图片索引数组


   crop_size指的是ROI Pooling后输出的特征图的大小,上面的代码中是14×14。


   最后则是一些resize的方式等参数,默认是采用双线性插值方式进行resize。


   在上面的代码中,是先将ROI pool成了14×14的大小,再使用tf.nn.max_pool将其转化成了7×7的大小。

   不得不说,tf的实现方式还是和caffe原版的不太一样,主要体现在caffe原版实现的时候,是将特征图的逐通道上面的ROI区域划成了若干份,并从每一份中选取了最大值,也就是说,每一个通道的选值情况是不一样的!而tensorflow实现的ROI Pooling是先直接将ROI区域对应的特征割出来,并按照某一尺度(14×14)进行双线性插值resize,再使用tf.nn.max_pool将特征转化为更小的维度(7×7)。

   到这里,ROI Pooling代码解析就接近尾声了,笔者想说的是,第一,Caffe代码虽然复杂,但是笔者坚持认为Caffe这框架确实是深入理解深度学习的好法宝,因为有很多机会能看到许多模块的底层实现代码。第二,R-CNN,Fast R-CNN和Faster R-CNN作为深度学习计算机视觉领域中的革命性成果,其代码是相当值得学习吸收的,在最开始阅读的时候不可避免也会有一些挑战,但是,还是要静下心来仔细分析。只要了解了算法原理,再仔细阅读分析算法代码,就能对原理更加深理解,最终达到完全理解算法的目的。

   最后,对于ROI Pooling代码的分析,如果各位读者朋友们认为存在疏漏,欢迎在评论区指出与讨论,笔者不胜感激。

   欢迎阅读笔者后续博客,各位读者朋友的支持与鼓励是我最大的动力!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值