在没有出现sppnet之前,RCNN使用corp和warp来对图片进行大小调整,这种操作会造成图片信息失真和信息丢失。sppnet这个模型推出来之后(关于这个网络的描述,可以看看之前写的一篇理解:http://www.cnblogs.com/gongxijun/p/7172134.html),rg大神沿用了sppnet的思路到他的下一个模型中fast-rcnn中,但是roi_pooling和sppnet的思路虽然相同,但是实现方式还是不同的.我们看一下网络参数:

layer {
name: "roi_pool5"
type: "ROIPooling"
bottom: "conv5_3"
bottom: "rois"
top: "pool5"
roi_pooling_param {
pooled_w:
pooled_h:
spatial_scale: 0.0625 # /
}

结合源代码,作者借助了sppnet的空域金字塔pool方式,但是和sppnet并不同的是,作者在这里只使用了(pooled_w,pooled_h)这个尺度,来将得到的每一个特征图分成(pooled_w,pooled_h),然后对每一块进行max_pooling取值,最后得到一个n*7*7固定大小的特征图。

 // ------------------------------------------------------------------
// Fast R-CNN
// Copyright (c) 2015 Microsoft
// Licensed under The MIT License [see fast-rcnn/LICENSE for details]
// Written by Ross Girshick
// ------------------------------------------------------------------ #include <cfloat> #include "caffe/fast_rcnn_layers.hpp" using std::max;
using std::min;
using std::floor;
using std::ceil; namespace caffe { template <typename Dtype>
void ROIPoolingLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
ROIPoolingParameter roi_pool_param = this->layer_param_.roi_pooling_param();
CHECK_GT(roi_pool_param.pooled_h(), )
<< "pooled_h must be > 0";
CHECK_GT(roi_pool_param.pooled_w(), )
<< "pooled_w must be > 0";
pooled_height_ = roi_pool_param.pooled_h(); //定义网络的大小
pooled_width_ = roi_pool_param.pooled_w();
spatial_scale_ = roi_pool_param.spatial_scale();
LOG(INFO) << "Spatial scale: " << spatial_scale_;
} template <typename Dtype>
void ROIPoolingLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
channels_ = bottom[]->channels();
height_ = bottom[]->height();
width_ = bottom[]->width();
top[]->Reshape(bottom[]->num(), channels_, pooled_height_,
pooled_width_);
max_idx_.Reshape(bottom[]->num(), channels_, pooled_height_,
pooled_width_);
} template <typename Dtype>
void ROIPoolingLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[]->cpu_data();
const Dtype* bottom_rois = bottom[]->cpu_data();//获取roidb信息(n,x1,y1,x2,y2)
// Number of ROIs
int num_rois = bottom[]->num();//候选目标的个数
int batch_size = bottom[]->num();//特征图的维度,vgg16的conv5之后为512
int top_count = top[]->count();//需要输出的值个数
Dtype* top_data = top[]->mutable_cpu_data();
caffe_set(top_count, Dtype(-FLT_MAX), top_data);
int* argmax_data = max_idx_.mutable_cpu_data();
caffe_set(top_count, -, argmax_data); // For each ROI R = [batch_index x1 y1 x2 y2]: max pool over R
for (int n = ; n < num_rois; ++n) {
int roi_batch_ind = bottom_rois[];
int roi_start_w = round(bottom_rois[] * spatial_scale_);//缩小16倍,将候选区域在原始坐标中的位置,映射到conv_5特征图上
int roi_start_h = round(bottom_rois[] * spatial_scale_);
int roi_end_w = round(bottom_rois[] * spatial_scale_);
int roi_end_h = round(bottom_rois[] * spatial_scale_);
CHECK_GE(roi_batch_ind, );
CHECK_LT(roi_batch_ind, batch_size); int roi_height = max(roi_end_h - roi_start_h + , );//得到候选区域在特征图上的大小
int roi_width = max(roi_end_w - roi_start_w + , );
const Dtype bin_size_h = static_cast<Dtype>(roi_height)
/ static_cast<Dtype>(pooled_height_);//计算如果需要划分成(pooled_height_,pooled_weight_)这么多块,那么每一个块的大小(bin_size_w,bin_size_h);
const Dtype bin_size_w = static_cast<Dtype>(roi_width)
/ static_cast<Dtype>(pooled_width_); const Dtype* batch_data = bottom_data + bottom[]->offset(roi_batch_ind);//获取当前维度的特征图数据,比如一共有(n,x1,x2,x3,x4)的数据,拿到第一块特征图的数据 for (int c = ; c < channels_; ++c) {
for (int ph = ; ph < pooled_height_; ++ph) {
for (int pw = ; pw < pooled_width_; ++pw) {
// Compute pooling region for this output unit:
// start (included) = floor(ph * roi_height / pooled_height_)
// end (excluded) = ceil((ph + 1) * roi_height / pooled_height_)
int hstart = static_cast<int>(floor(static_cast<Dtype>(ph)
* bin_size_h)); //计算每一块的位置
int wstart = static_cast<int>(floor(static_cast<Dtype>(pw)
* bin_size_w));
int hend = static_cast<int>(ceil(static_cast<Dtype>(ph + )
* bin_size_h));
int wend = static_cast<int>(ceil(static_cast<Dtype>(pw + )
* bin_size_w)); hstart = min(max(hstart + roi_start_h, ), height_);
hend = min(max(hend + roi_start_h, ), height_);
wstart = min(max(wstart + roi_start_w, ), width_);
wend = min(max(wend + roi_start_w, ), width_); bool is_empty = (hend <= hstart) || (wend <= wstart); const int pool_index = ph * pooled_width_ + pw;
if (is_empty) {
top_data[pool_index] = ;
argmax_data[pool_index] = -;
} for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
const int index = h * width_ + w;
if (batch_data[index] > top_data[pool_index]) {
top_data[pool_index] = batch_data[index]; //在取每一块中的最大值,就是max_pooling操作.
argmax_data[pool_index] = index;
}
}
}
}
}
// Increment all data pointers by one channel
batch_data += bottom[]->offset(, );
top_data += top[]->offset(, );
argmax_data += max_idx_.offset(, );
}
// Increment ROI data pointer
bottom_rois += bottom[]->offset();
}
} template <typename Dtype>
void ROIPoolingLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
NOT_IMPLEMENTED;
} #ifdef CPU_ONLY
STUB_GPU(ROIPoolingLayer);
#endif INSTANTIATE_CLASS(ROIPoolingLayer);
REGISTER_LAYER_CLASS(ROIPooling); } // namespace caffe

进过以上的操作过后,就得到了固定大小的特征图啦,然后就可以进行全连接操作了. 但愿我说明白了.

---完.

最新文章

  1. 【代码笔记】iOS-替换电话号码中间4位为-号
  2. OpenGL变换
  3. sqlite 下载的 ZIP 包的区别
  4. NOI2014 动物园
  5. Python connect zookeeper use the kazoo module
  6. 精美实用的jQuery插件精选
  7. 微信网页授权获取code链接
  8. C#句柄使用
  9. ebs清除并法管理器所清除的表
  10. [RM 状态机详解3]RMContainer状态机详解
  11. 在HTML页面中加载js文件和css文件的方法
  12. eclipse导入项目之后有感叹号
  13. windows10下面部署nginx(解决文件名中文乱码问题)
  14. slot
  15. Configuring SSL for SAP Host Agent on UNIX
  16. C++实现文件内字符数、单词数、行数的统计
  17. 转sql server新增、修改字段语句(整理)
  18. 解决HTML5提出的新的元素不被IE6-8识别的解决办法
  19. MVC| NuGet安装相关工具包
  20. JavaWeb开发中采用FreeMarker生成Excel表格

热门文章

  1. 201521123093 java 第五周学习总结
  2. 201521123113 《Java程序设计》第2周学习总结
  3. 201521123010 《Java程序设计》第9周学习总结
  4. 201521123049 《JAVA程序设计》 第9周学习总结
  5. 201521123012 《Java程序设计》第十周学习总结
  6. Ajax跨域问题的出现和解决
  7. JavaSE(十)之Map总结
  8. WebUtils复用代码【request2Bean、UUID】
  9. Oracle--新建用户以及赋予的权限
  10. oc __weak和__strong的区别