参考:https://blog.csdn.net/lanran2/article/details/60143861/
如果真的以一个10x10的小目标来算的话,来,我们走一走:
(1)首先将10x10的小目标映射到最终的特征图上,将小目标的x1,y1,x2,y2都处以16,不能整除的取整(round 返回四舍五入的整数),边长为10的小目标可能会出现x1=x2或者y1=y2的情况,接下来计算HW,HW的最小值为1,所以小目标的HW肯定是一了。
(2)将一 除以七,嵌套的三个for循环里面,计算hstar,hend,wstar,wend,对该小目标肯定都是0,1,0,1。对应特征图上的一个像素,后面的也都在一个像素内找最大值。
// ------------------------------------------------------------------
// 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 {
//LayerSetUp 输入参数
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(), 0)
<< "pooled_h must be > 0";
CHECK_GT(roi_pool_param.pooled_w(), 0)
<< "pooled_w must be > 0";
//经过pooling后的feature map的高
pooled_height_ = roi_pool_param.pooled_h();
//经过pooling之后的 feature map 的宽
pooled_width_ = roi_pool_param.pooled_w();
//输入图片与feature map 之间的比值,这个feature map 指的是roi pooling层的输入
spatial_scale_ = roi_pool_param.spatial_scale();
LOG(INFO) << "Spatial scale: " << spatial_scale_;
}
//reshape
template <typename Dtype>
void ROIPoolingLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
//bottom[0]指的是输入的feature map
channels_ = bottom[0]->channels();
height_ = bottom[0]->height();
width_ = bottom[0]->width();
//设置输出的形状NCHW,N=ROI的个数,C=channels_,H=pooled_height_,W=pooled_width_
top[0]->Reshape(bottom[1]->num(), channels_, pooled_height_,
pooled_width_);
//max_idx_的形状与top一致
max_idx_.Reshape(bottom[1]->num(), channels_, pooled_height_,
pooled_width_);
}
//forward
template <typename Dtype>
void ROIPoolingLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
//两部分输入
const Dtype* bottom_data = bottom[0]->cpu_data();
const Dtype* bottom_rois = bottom[1]->cpu_data();
// Number of ROIs
int num_rois = bottom[1]->num();
int batch_size = bottom[0]->num();
int top_count = top[0]->count();
Dtype* top_data = top[0]->mutable_cpu_data();
caffe_set(top_count, Dtype(-FLT_MAX), top_data);
int* argmax_data = max_idx_.mutable_cpu_data();
caffe_set(top_count, -1, argmax_data);
// For each ROI R = [batch_index x1 y1 x2 y2]: max pool over R
for (int n = 0; n < num_rois; ++n) {
int roi_batch_ind = bottom_rois[0];//roi_batch_ind指的是roi的索引
//将roi的坐标映射到特征图上,round,取整
int roi_start_w = round(bottom_rois[1] * spatial_scale_);
int roi_start_h = round(bottom_rois[2] * spatial_scale_);
int roi_end_w = round(bottom_rois[3] * spatial_scale_);
int roi_end_h = round(bottom_rois[4] * spatial_scale_);
CHECK_GE(roi_batch_ind, 0);
CHECK_LT(roi_batch_ind, batch_size);
//计算 roi的高和宽,宽和高最小为一
int roi_height = max(roi_end_h - roi_start_h + 1, 1);
int roi_width = max(roi_end_w - roi_start_w + 1, 1);
//将 roi 的高和宽除以pooling之后的超参数高和宽,得到比例。
const Dtype bin_size_h = static_cast<Dtype>(roi_height)
/ static_cast<Dtype>(pooled_height_);
const Dtype bin_size_w = static_cast<Dtype>(roi_width)
/ static_cast<Dtype>(pooled_width_);
//找到对应 roi 的 feature map ,应该是裁剪下来了,后面循环中用到
//如果input data的batch size为1,那么roi batch ind 等于0
const Dtype* batch_data = bottom_data + bottom[0]->offset(roi_batch_ind);
//pooling 的过程是针对每一个channel的,需要循环遍历
for (int c = 0; c < channels_; ++c) {
//计算output的每一个值,所以需要遍历一遍output,然后求出所有值
for (int ph = 0; ph < pooled_height_; ++ph) {
for (int pw = 0; 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));//floor(x)返回不大于x的最大整数值。
int wstart = static_cast<int>(floor(static_cast<Dtype>(pw)
* bin_size_w));//
int hend = static_cast<int>(ceil(static_cast<Dtype>(ph + 1)
* bin_size_h));//ceil(x)返回不小于x的最小整数值(然后转换为double型)。
int wend = static_cast<int>(ceil(static_cast<Dtype>(pw + 1)
* bin_size_w));
hstart = min(max(hstart + roi_start_h, 0), height_);
hend = min(max(hend + roi_start_h, 0), height_);
wstart = min(max(wstart + roi_start_w, 0), width_);
wend = min(max(wend + roi_start_w, 0), width_);
bool is_empty = (hend <= hstart) || (wend <= wstart);
const int pool_index = ph * pooled_width_ + pw;
if (is_empty) {
top_data[pool_index] = 0;
argmax_data[pool_index] = -1;
}
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];
argmax_data[pool_index] = index;
}
}
}
}
}
// Increment all data pointers by one channel
batch_data += bottom[0]->offset(0, 1);
top_data += top[0]->offset(0, 1);
argmax_data += max_idx_.offset(0, 1);
}
// Increment ROI data pointer
bottom_rois += bottom[1]->offset(1);
}
}
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