mxnet
channel_unpool.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_CHANNEL_UNPOOL_H_
8 #define MSHADOW_EXTENSION_CHANNEL_UNPOOL_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<ChannelUnpoolingExp<Reducer, SrcExp, DType, srcdim>,
24  SrcExp, srcdim, DType> {
26  const SrcExp &data_src_;
28  const SrcExp &data_pooled_;
30  const SrcExp &grad_pooled_;
40  ChannelUnpoolingExp(const SrcExp &data_src,
41  const SrcExp &data_pooled,
42  const SrcExp &grad_pooled,
43  index_t nsize, index_t kstride, index_t pad)
44  : data_src_(data_src), data_pooled_(data_pooled),
45  grad_pooled_(grad_pooled),
46  nsize_(nsize), kstride_(kstride), pad_(pad) {
48  typedef ShapeCheck<srcdim, SrcExp> ShapeCheckSrcDimSrcExp;
49  CHECK_EQ(pshape, ShapeCheckSrcDimSrcExp::Check(data_pooled))
50  << "ChannelUnPoolingExp: data and grad shape mismatch";
52  for (int k = 0; k < srcdim; ++k) {
53  if (k == 1) {
54  continue;
55  }
56  CHECK_EQ(pshape[k], sshape[k])
57  << "ChannelUnPoolingExp: pooled tensor and src tensor shape mismatch"
58  << pshape[k]
59  << " vs "
60  << sshape[k];
61  }
62  pchannel_ = pshape[1];
63  this->shape_ = sshape;
64  }
65 };
78 template<typename Reducer, typename SrcExp, typename DType, int etype>
81  const Exp<SrcExp, DType, etype> &data_pooled,
82  const Exp<SrcExp, DType, etype> &grad_pooled,
83  index_t nsize, index_t stride, index_t pad) {
85  ::Error_Expression_Does_Not_Meet_Dimension_Req();
87  (data_src.self(), data_pooled.self(), grad_pooled.self(), nsize, stride, pad);
88 }
89 
90 template<typename Reducer, typename SrcExp, typename DType, int etype>
93  const Exp<SrcExp, DType, etype> &data_pooled,
94  const Exp<SrcExp, DType, etype> &grad_pooled, index_t nsize) {
95  return ch_unpool(data_src, data_pooled, grad_pooled, nsize, 1, nsize / 2);
96 }
97 
98 
99 //----------------------
100 // Execution plan
101 //----------------------
102 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
103 struct Plan<ChannelUnpoolingExp<Reducer, SrcExp, DType, srcdim>, DType> {
104  public:
107  grad_pooled_(e.grad_pooled_), channel_(e.shape_[srcdim - 3]),
108  height_(e.shape_[srcdim - 2]), pchannel_(e.pchannel_),
109  hnsize_(e.nsize_), stride_(e.kstride_), pad_(e.pad_) {}
110  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
111  using namespace std;
112  const DType vsrc = data_src_.Eval(i, j);
113  const index_t y = i % height_;
114  i /= height_;
115  const index_t c = i % channel_;
116  const index_t n = i / channel_;
117  const index_t x = j;
118  const index_t cstart = c < hnsize_ - pad_ ? 0
119  : (c - (hnsize_ - pad_) + stride_) / stride_;
120  const index_t cend = min((c + pad_ + stride_) / stride_, channel_);
121  DType val = static_cast<DType>(0);
122  for (index_t cc = cstart; cc < cend; ++cc) {
123  val += Reducer::PartialGrad(vsrc,
124  data_pooled_.Eval((n * pchannel_ + cc) * height_ + y, x)) *
125  grad_pooled_.Eval((n * pchannel_ + cc) * height_ + y, x);
126  }
127  return val;
128  }
129 
130  private:
132  const index_t channel_, height_, pchannel_, hnsize_, stride_, pad_;
133 };
134 } // namespace expr
135 } // namespace mshadow
136 #endif // MSHADOW_EXTENSION_CHANNEL_UNPOOL_H_
137 
const SrcExp & grad_pooled_
gradient data of pooled part, to be propgate down
Definition: channel_unpool.h:30
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
index_t pad_
pad
Definition: channel_unpool.h:38
Definition: expr_engine-inl.h:40
used to help static type check
Definition: expr_engine-inl.h:312
index_t nsize_
kernel size in height
Definition: channel_unpool.h:34
const SrcExp & data_pooled_
result of pooled data, corresponds to result of pooling
Definition: channel_unpool.h:28
ChannelUnpoolingExp(const SrcExp &data_src, const SrcExp &data_pooled, const SrcExp &grad_pooled, index_t nsize, index_t kstride, index_t pad)
constructor
Definition: channel_unpool.h:40
channel pooling expression, do reduction over (local nearby) channels, used to implement local respon...
Definition: channel_unpool.h:22
Definition: optional.h:241
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
Plan(const ChannelUnpoolingExp< Reducer, SrcExp, DType, srcdim > &e)
Definition: channel_unpool.h:105
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:346
index_t kstride_
kernel size in width
Definition: channel_unpool.h:36
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
index_t pchannel_
channel of pooled expression
Definition: channel_unpool.h:32
const SrcExp & data_src_
source input, corresponds to src in pooling
Definition: channel_unpool.h:26
const SubType & self(void) const
Definition: expression.h:64
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
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: channel_unpool.h:110
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:29
ChannelUnpoolingExp< Reducer, SrcExp, DType, ExpInfo< SrcExp >::kDim > ch_unpool(const Exp< SrcExp, DType, etype > &data_src, const Exp< SrcExp, DType, etype > &data_pooled, const Exp< SrcExp, DType, etype > &grad_pooled, index_t nsize, index_t stride, index_t pad)
channel unpooling, do unroll over (local nearby) channels
Definition: channel_unpool.h:80