mxnet
spatial_unpool.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_SPATIAL_UNPOOL_H_
8 #define MSHADOW_EXTENSION_SPATIAL_UNPOOL_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 UnPoolingExp:
22  public MakeTensorExp<UnPoolingExp<Reducer, SrcExp, DType, srcdim>,
23  SrcExp, srcdim, DType> {
25  const SrcExp &data_src_;
27  const SrcExp &data_pooled_;
29  const SrcExp &grad_pooled_;
43  UnPoolingExp(const SrcExp &data_src,
44  const SrcExp &data_pooled,
45  const SrcExp &grad_pooled,
46  index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x)
47  : data_src_(data_src), data_pooled_(data_pooled),
48  grad_pooled_(grad_pooled),
49  ksize_y_(ksize_y), ksize_x_(ksize_x),
50  kstride_y_(kstride_y), kstride_x_(kstride_x) {
52  typedef ShapeCheck<srcdim, SrcExp> ShapeCheckSrcDimSrcExp;
53  CHECK_EQ(pshape, ShapeCheckSrcDimSrcExp::Check(data_pooled))
54  << "UnPoolingExp: pooled shape mismatch";
56  for (int k = 0; k < srcdim - 2; ++k) {
57  CHECK_EQ(pshape[k], sshape[k]) << "UnPoolingExp: pool and src shape mismatch";
58  }
59  pshape_x_ = pshape[srcdim - 1];
60  pshape_y_ = pshape[srcdim - 2];
61  this->shape_ = sshape;
62  }
63 };
80 template<typename Reducer, typename SrcExp, typename DType, int etype>
83  const Exp<SrcExp, DType, etype> &data_pooled,
84  const Exp<SrcExp, DType, etype> &grad_pooled,
85  index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x) {
87  (data_src.self(), data_pooled.self(), grad_pooled.self(),
88  ksize_y, ksize_x, kstride_y, kstride_x);
89 }
90 //----------------------
91 // Execution plan
92 //----------------------
93 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
94 struct Plan<UnPoolingExp<Reducer, SrcExp, DType, srcdim>, DType> {
95  public:
98  grad_pooled_(MakePlan(e.grad_pooled_)), sshape_y_(e.shape_[srcdim - 2]),
102  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
103  using namespace std;
104  const index_t x = j;
105  const index_t y = i % sshape_y_;
106  const index_t c = i / sshape_y_;
107  const DType vsrc = data_src_.Eval(i, j);
108  const index_t py_min =
109  y < ksize_y_ ? 0 : (y - ksize_y_ + kstride_y_) / kstride_y_;
110  const index_t px_min =
111  x < ksize_x_ ? 0 : (x - ksize_x_ + kstride_x_) / kstride_x_;
112  const index_t py_max = min((y + kstride_y_) / kstride_y_, pshape_y_);
113  const index_t px_max = min((x + kstride_x_) / kstride_x_, pshape_x_);
114 
115  DType val = static_cast<DType>(0);
116  for (index_t py = py_min; py < py_max; ++py) {
117  for (index_t px = px_min; px < px_max; ++px) {
118  val += Reducer::PartialGrad(vsrc,
119  data_pooled_.Eval(c * pshape_y_ + py, px)) *
120  grad_pooled_.Eval(c * pshape_y_ + py, px);
121  }
122  }
123 
124  return val;
125  }
126 
127  private:
129  const index_t sshape_y_, pshape_y_, pshape_x_;
130  const index_t ksize_y_, ksize_x_;
132 };
133 } // namespace expr
134 } // namespace mshadow
135 #endif // MSHADOW_EXTENSION_SPATIAL_UNPOOL_H_
const SrcExp & grad_pooled_
gradient data of pooled part, to be propgate down
Definition: spatial_unpool.h:29
Definition: expr_engine-inl.h:40
index_t kstride_x_
kernel stride in x directory
Definition: spatial_unpool.h:41
Definition: optional.h:241
index_t ksize_x_
kernel size in width
Definition: spatial_unpool.h:37
index_t pshape_x_
shape of pooled expression
Definition: spatial_unpool.h:33
const SrcExp & data_pooled_
result of pooled data, corresponds to result of pooling
Definition: spatial_unpool.h:27
unpooling expr reverse operation of pooling, used to pass gradient back
Definition: spatial_unpool.h:21
static Shape< dim > Check(const E &t)
const SrcExp & data_src_
source input, corresponds to src in pooling
Definition: spatial_unpool.h:25
#define MSHADOW_XINLINE
Definition: base.h:204
int32_t index_t
type that will be used for index
Definition: base.h:291
Plan(const UnPoolingExp< Reducer, SrcExp, DType, srcdim > &e)
Definition: spatial_unpool.h:96
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:346
index_t pshape_y_
shape of pooled expression
Definition: spatial_unpool.h:31
UnPoolingExp(const SrcExp &data_src, const SrcExp &data_pooled, const SrcExp &grad_pooled, index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x)
constructor
Definition: spatial_unpool.h:43
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
index_t ksize_y_
kernel size in height
Definition: spatial_unpool.h:35
namespace for mshadow
Definition: base.h:282
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: spatial_unpool.h:102
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:29
UnPoolingExp< Reducer, SrcExp, DType, ExpInfo< SrcExp >::kDim > unpool(const Exp< SrcExp, DType, etype > &data_src, const Exp< SrcExp, DType, etype > &data_pooled, const Exp< SrcExp, DType, etype > &grad_pooled, index_t ksize_y, index_t ksize_x, index_t kstride_y, index_t kstride_x)
unpooling gradient for 4D, backprop gradient value back, revserse operation of pooling, same as unpooling, but allows unequal size of kernel
Definition: spatial_unpool.h:82
index_t kstride_y_
kernel stride in y directory
Definition: spatial_unpool.h:39