mxnet
channel_pool.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_CHANNEL_POOL_H_
8 #define MSHADOW_EXTENSION_CHANNEL_POOL_H_
9 #include <algorithm>
10 #include "../extension.h"
11 namespace mshadow {
12 namespace expr {
21 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
23  public MakeTensorExp<ChannelPoolingExp<Reducer, SrcExp, DType, srcdim>,
24  SrcExp, srcdim, DType> {
26  const SrcExp &src_;
35  ChannelPoolingExp(const SrcExp &src, index_t nsize, index_t stride, index_t pad)
36  : src_(src), nsize_(nsize), stride_(stride), pad_(pad) {
38  this->src_channel_ = this->shape_[srcdim - 3];
39  CHECK_GE(this->shape_[srcdim - 3], nsize_)
40  << "chpool: local size must be smaller than nchannels";
41  this->shape_[srcdim - 3] = (this->src_channel_ - nsize + pad * 2 + 1) / stride;
42  }
43 };
55 template<typename Reducer, typename SrcExp, typename DType, int etype>
59  ::Error_Expression_Does_Not_Meet_Dimension_Req();
60  CHECK_EQ(nsize % 2, 1U) << "chpool: if no pad is specified, local size must be odd";
61  return ChannelPoolingExp<Reducer, SrcExp,
62  DType, ExpInfo<SrcExp>::kDim>(src.self(), nsize, 1, nsize / 2);
63 }
64 
65 template<typename Reducer, typename SrcExp, typename DType, int etype>
69  ::Error_Expression_Does_Not_Meet_Dimension_Req();
70  return ChannelPoolingExp<Reducer, SrcExp,
71  DType, ExpInfo<SrcExp>::kDim>(src.self(), nsize, stride, pad);
72 }
73 
74 //----------------------
75 // Execution plan
76 //----------------------
77 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
78 struct Plan<ChannelPoolingExp<Reducer, SrcExp, DType, srcdim>, DType> {
79  public:
81  : src_(MakePlan(e.src_)), channel_(e.shape_[srcdim - 3]),
82  height_(e.shape_[srcdim - 2]), width_(e.shape_[srcdim - 1]),
83  hnsize_(e.nsize_), stride_(e.stride_), pad_(e.pad_),
85  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
86  using namespace std;
87  const index_t y = i % height_;
88  i /= height_;
89  const index_t c = i % channel_;
90  const index_t n = i / channel_;
91  const index_t x = j;
92  const index_t cstart = c * stride_ < pad_ ? 0 : c * stride_ - pad_;
93  const index_t cend = min(c * stride_ - pad_ + hnsize_, channel_);
94  DType res; Reducer::SetInitValue(res);
95  for (index_t cc = cstart; cc < cend; ++cc) {
96  Reducer::Reduce(res, src_.Eval((n * src_channel_ + cc) * height_ + y, x));
97  }
98  return res;
99  }
100 
101  private:
103  const index_t channel_, height_, width_, hnsize_, stride_, pad_, src_channel_;
104 };
105 } // namespace expr
106 } // namespace mshadow
107 #endif // MSHADOW_EXTENSION_CHANNEL_POOL_H_
108 
index_t pad_
pad of pooling of each side
Definition: channel_pool.h:32
ChannelPoolingExp< Reducer, SrcExp, DType, ExpInfo< SrcExp >::kDim > chpool(const Exp< SrcExp, DType, etype > &src, index_t nsize)
channel pooling, do reduction over (local nearby) channels, used to implement local response normaliz...
Definition: channel_pool.h:57
PaddingExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > pad(const Exp< SrcExp, DType, etype > &src, index_t pad)
padding expression, pad a image with zeros on boundaries, padding affects shape[0], and shape[1]
Definition: pad.h:53
Definition: expr_engine-inl.h:40
used to help static type check
Definition: expr_engine-inl.h:312
channel pooling expression, do reduction over (local nearby) channels, used to implement local respon...
Definition: channel_pool.h:22
Definition: optional.h:241
index_t stride_
stride of pooling
Definition: channel_pool.h:30
ChannelPoolingExp(const SrcExp &src, index_t nsize, index_t stride, index_t pad)
constructor
Definition: channel_pool.h:35
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: channel_pool.h:85
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:204
index_t src_channel_
Definition: channel_pool.h:33
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:244
int32_t index_t
type that will be used for index
Definition: base.h:291
index_t nsize_
neighbor size
Definition: channel_pool.h:28
Plan(const ChannelPoolingExp< Reducer, SrcExp, DType, srcdim > &e)
Definition: channel_pool.h:80
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
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
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:29
const SrcExp & src_
source operand
Definition: channel_pool.h:26