| 77 | |
| 78 | template <typename Dtype> |
| 79 | void PoolingLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |
| 80 | const vector<Blob<Dtype>*>& top) { |
| 81 | CHECK_EQ(4, bottom[0]->num_axes()) << "Input must have 4 axes, " |
| 82 | << "corresponding to (num, channels, height, width)"; |
| 83 | channels_ = bottom[0]->channels(); |
| 84 | height_ = bottom[0]->height(); |
| 85 | width_ = bottom[0]->width(); |
| 86 | if (global_pooling_) { |
| 87 | kernel_h_ = bottom[0]->height(); |
| 88 | kernel_w_ = bottom[0]->width(); |
| 89 | } |
| 90 | pooled_height_ = static_cast<int>(ceil(static_cast<float>( |
| 91 | height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1; |
| 92 | pooled_width_ = static_cast<int>(ceil(static_cast<float>( |
| 93 | width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1; |
| 94 | if (pad_h_ || pad_w_) { |
| 95 | // If we have padding, ensure that the last pooling starts strictly |
| 96 | // inside the image (instead of at the padding); otherwise clip the last. |
| 97 | if ((pooled_height_ - 1) * stride_h_ >= height_ + pad_h_) { |
| 98 | --pooled_height_; |
| 99 | } |
| 100 | if ((pooled_width_ - 1) * stride_w_ >= width_ + pad_w_) { |
| 101 | --pooled_width_; |
| 102 | } |
| 103 | CHECK_LT((pooled_height_ - 1) * stride_h_, height_ + pad_h_); |
| 104 | CHECK_LT((pooled_width_ - 1) * stride_w_, width_ + pad_w_); |
| 105 | } |
| 106 | top[0]->Reshape(bottom[0]->num(), channels_, pooled_height_, |
| 107 | pooled_width_); |
| 108 | if (top.size() > 1) { |
| 109 | top[1]->ReshapeLike(*top[0]); |
| 110 | } |
| 111 | // If max pooling, we will initialize the vector index part. |
| 112 | if (this->layer_param_.pooling_param().pool() == |
| 113 | PoolingParameter_PoolMethod_MAX && top.size() == 1) { |
| 114 | max_idx_.Reshape(bottom[0]->num(), channels_, pooled_height_, |
| 115 | pooled_width_); |
| 116 | } |
| 117 | // If stochastic pooling, we will initialize the random index part. |
| 118 | if (this->layer_param_.pooling_param().pool() == |
| 119 | PoolingParameter_PoolMethod_STOCHASTIC) { |
| 120 | rand_idx_.Reshape(bottom[0]->num(), channels_, pooled_height_, |
| 121 | pooled_width_); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // TODO(Yangqing): Is there a faster way to do pooling in the channel-first |
| 126 | // case? |