卷积层ConvolutionLayer正向传导的目标层往往是池化层PoolingLayer。池化层通过降采样来降低卷积层输出的特征向量,同时改善结果,不易出现过拟合。最常用的降采样方法有均值采样(取区域平均值作为降采样值)、最大值采样(取区域最大值作为降采样值)和随机采样(取区域内随机一个像素)等。
PoolingLayer类从Layer基类单一继承而来,没有派生其它子类。具体定义在pooling_layer.hpp中,
template <typename Dtype>
class PoolingLayer : public Layer<Dtype> {
public:
explicit PoolingLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual inline const char* type() const { return "Pooling"; }
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int MinTopBlobs() const { return 1; }
// 最大值采样可以额外输出一个Blob,所以MaxTopBlobs返回2
virtual inline int MaxTopBlobs() const {
return (this->layer_param_.pooling_param().pool() ==
PoolingParameter_PoolMethod_MAX) ? 2 : 1;
}
protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
// 卷积区域尺寸
int kernel_h_, kernel_w_;
// 卷积平移步幅
int stride_h_, stride_w_;
// 图像补齐像素数
int pad_h_, pad_w_;
// 通道
int channels_;
// 输入图像尺寸
int height_, width_;
// 池化后尺寸
int pooled_height_, pooled_width_;
// 是否全区域池化(将整幅图像降采样为1x1)
bool global_pooling_;
// 随机采样点索引
Blob<Dtype> rand_idx_;
// 最大值采样点索引
Blob<int> max_idx_;
};
具体实现在pooling_layer.cpp中,
template <typename Dtype>
void PoolingLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
PoolingParameter pool_param = this->layer_param_.pooling_param();
if (pool_param.global_pooling()) {
CHECK(!(pool_param.has_kernel_size() ||
pool_param.has_kernel_h() || pool_param.has_kernel_w()))
<< "With Global_pooling: true Filter size cannot specified";
} else {
CHECK(!pool_param.has_kernel_size() !=
!(pool_param.has_kernel_h() && pool_param.has_kernel_w()))
<< "Filter size is kernel_size OR kernel_h and kernel_w; not both";
CHECK(pool_param