mxnet
spatial_pool.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_SPATIAL_POOL_H_
8 #define MSHADOW_EXTENSION_SPATIAL_POOL_H_
9 #include <algorithm>
10 #include "../extension.h"
11 namespace mshadow {
12 namespace expr {
20 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
21 struct PoolingExp:
22  public MakeTensorExp<PoolingExp<Reducer, SrcExp, DType, srcdim>,
23  SrcExp, srcdim, DType> {
25  const SrcExp &src_;
39  PoolingExp(const SrcExp &src,
40  index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x)
41  : src_(src), ksize_y_(ksize_y), ksize_x_(ksize_x),
42  kstride_y_(kstride_y), kstride_x_(kstride_x) {
44  CHECK(sshape[srcdim - 1] >= ksize_x && sshape[srcdim - 2] >= ksize_y)
45  << "PoolingExp: kernel must be smaller than image";
46  this->src_height_ = sshape[srcdim - 2];
47  this->src_width_ = sshape[srcdim - 1];
48  this->shape_ = sshape;
49  this->shape_[srcdim - 2] = (src_height_ - ksize_y) / kstride_y + 1;
50  this->shape_[srcdim - 1] = (src_width_ - ksize_x) / kstride_x + 1;
51  }
53  PoolingExp(const SrcExp &src, Shape<2> pshape,
54  index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x)
55  : src_(src), ksize_y_(ksize_y), ksize_x_(ksize_x),
56  kstride_y_(kstride_y), kstride_x_(kstride_x) {
58  CHECK(sshape[srcdim - 1] >= ksize_x && sshape[srcdim - 2] >= ksize_y)
59  << "PoolingExp: kernel must be smaller than image";
60  this->src_height_ = sshape[srcdim - 2];
61  this->src_width_ = sshape[srcdim - 1];
62  this->shape_ = sshape;
63  this->shape_[srcdim - 2] = pshape[0];
64  this->shape_[srcdim - 1] = pshape[1];
65  }
66 };
80 template<typename Reducer, typename SrcExp, typename DType, int etype>
83  index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x) {
85  ::Error_Expression_Does_Not_Meet_Dimension_Req();
87  (src.self(), ksize_y, ksize_x, kstride_y, kstride_x);
88 }
103 template<typename Reducer, typename SrcExp,
104  typename DType, int etype>
107  index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x) {
109  ::Error_Expression_Does_Not_Meet_Dimension_Req();
111  (src.self(), pshape, ksize_y, ksize_x, kstride_y, kstride_x);
112 }
113 //----------------------
114 // Execution plan
115 //----------------------
116 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
117 struct Plan<PoolingExp< Reducer, SrcExp, DType, srcdim>, DType> {
118  public:
120  : src_(MakePlan(e.src_)),
124  new_height_(e.shape_[srcdim - 2]) {}
125  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
126  using namespace std;
127  const index_t py = i % new_height_;
128  const index_t y_start = py * kstride_y_;
129  const index_t y_end = min(y_start + ksize_y_, src_height_);
130  const index_t px = j;
131  const index_t x_start = px * kstride_x_;
132  const index_t x_end = min(x_start + ksize_x_, src_width_);
133  const index_t c = i / new_height_;
134 
135  DType res; Reducer::SetInitValue(res);
136  for (index_t y = y_start; y < y_end; ++y) {
137  for (index_t x = x_start; x < x_end; ++x) {
138  Reducer::Reduce(res, src_.Eval(c * src_height_ + y, x));
139  }
140  }
141  return res;
142  }
143 
144  private:
148  const index_t new_height_;
149 };
150 } // namespace expr
151 } // namespace mshadow
152 #endif // MSHADOW_EXTENSION_SPATIAL_POOL_H_
index_t ksize_x_
kernel size in width
Definition: spatial_pool.h:29
Definition: expr_engine-inl.h:40
index_t src_height_
source height shape[1]
Definition: spatial_pool.h:35
used to help static type check
Definition: expr_engine-inl.h:312
index_t ksize_y_
kernel size in height
Definition: spatial_pool.h:27
Definition: optional.h:241
PoolingExp< Reducer, SrcExp, DType, ExpInfo< SrcExp >::kDim > pool(const Exp< SrcExp, DType, etype > &src, index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x)
pooling subregion results together
Definition: spatial_pool.h:82
PoolingExp(const SrcExp &src, index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x)
constructor
Definition: spatial_pool.h:39
PoolingExp(const SrcExp &src, Shape< 2 > pshape, index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x)
constructor, specify shape
Definition: spatial_pool.h:53
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:204
int32_t index_t
type that will be used for index
Definition: base.h:291
index_t kstride_y_
kernel stride in y directory
Definition: spatial_pool.h:31
index_t kstride_x_
kernel stride in x directory
Definition: spatial_pool.h:33
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
index_t src_width_
source width shape[0]
Definition: spatial_pool.h:37
const SubType & self(void) const
Definition: expression.h:64
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:221
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:25
namespace for mshadow
Definition: base.h:282
const SrcExp & src_
source operand
Definition: spatial_pool.h:25
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:29
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: spatial_pool.h:125
Plan(const PoolingExp< Reducer, SrcExp, DType, srcdim > &e)
Definition: spatial_pool.h:119
pooling expression, do reduction over local patches of a image
Definition: spatial_pool.h:21