mxnet
crop.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_CROP_H_
8 #define MSHADOW_EXTENSION_CROP_H_
9 #include "../extension.h"
10 namespace mshadow {
11 namespace expr {
18 template<typename SrcExp, typename DType, int srcdim>
19 struct CroppingExp:
20  public MakeTensorExp<CroppingExp<SrcExp, DType, srcdim>,
21  SrcExp, srcdim, DType> {
23  const SrcExp &src_;
31  explicit CroppingExp(const SrcExp &src, Shape<2> cshape)
32  : src_(src) {
34  CHECK_GE(this->shape_[srcdim - 2], cshape[0]) << "CroppingExp: height requirement not met";
35  CHECK_GE(this->shape_[srcdim - 1], cshape[1]) << "CroppingExp: width requirement not met";
36  pad_height_ = (this->shape_[srcdim - 2] - cshape[0]) / 2;
37  pad_width_ = (this->shape_[srcdim - 1] - cshape[1]) / 2;
38  src_height_ = this->shape_[srcdim - 2];
39  this->shape_[srcdim - 2] = cshape[0]; // height
40  this->shape_[srcdim - 1] = cshape[1]; // width
41  }
43  explicit CroppingExp(const SrcExp &src, Shape<2> cshape,
44  index_t start_height, index_t start_width)
45  : src_(src), pad_height_(start_height), pad_width_(start_width) {
47  CHECK_GE(this->shape_[srcdim - 2], cshape[0] + start_height)
48  << "CroppingExp: height requirement not met";
49  CHECK_GE(this->shape_[srcdim - 1], cshape[1] + start_width)
50  << "CroppingExp: width requirement not met";
51  src_height_ = this->shape_[srcdim - 2];
52  this->shape_[srcdim - 2] = cshape[0]; // height
53  this->shape_[srcdim - 1] = cshape[1]; // width
54  }
55 }; // struct CroppingExp
66 template<typename SrcExp, typename DType, int etype>
70  ::Error_Expression_Does_Not_Meet_Dimension_Req();
72 }
84 template<typename SrcExp, typename DType, int etype>
87  index_t start_height, index_t start_width) {
89  ::Error_Expression_Does_Not_Meet_Dimension_Req();
91  (src.self(), oshape, start_height, start_width);
92 }
93 //----------------------
94 // Execution plan
95 //----------------------
96 template<typename SrcExp, typename DType, int srcdim>
97 struct Plan<CroppingExp<SrcExp, DType, srcdim>, DType> {
98  public:
100  : src_(MakePlan(e.src_)),
102  new_height_(e.shape_[srcdim - 2]), src_height_(e.src_height_) {}
103  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
104  const index_t x = j;
105  const index_t y = i % new_height_;
106  const index_t c = i / new_height_;
107  const index_t h = y + pad_height_;
108  const index_t w = x + pad_width_;
109  return src_.Eval(c * src_height_ + h, w);
110  }
111  private:
114  const index_t new_height_;
115  const index_t src_height_;
116 };
117 } // namespace expr
118 } // namespace mshadow
119 #endif // MSHADOW_EXTENSION_CROP_H_
CroppingExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > crop(const Exp< SrcExp, DType, etype > &src, Shape< 2 > oshape)
revserse operationg of padding, cut off boundaries, crop output from center of input ...
Definition: crop.h:68
Definition: expr_engine-inl.h:40
used to help static type check
Definition: expr_engine-inl.h:312
CroppingExp(const SrcExp &src, Shape< 2 > cshape, index_t start_height, index_t start_width)
constructor
Definition: crop.h:43
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:204
index_t src_height_
src height
Definition: crop.h:29
index_t pad_width_
pad height
Definition: crop.h:27
int32_t index_t
type that will be used for index
Definition: base.h:291
const SrcExp & src_
source operand
Definition: crop.h:23
CroppingExp(const SrcExp &src, Shape< 2 > cshape)
constructor
Definition: crop.h:31
crop expression, cut off the boundary region, reverse operation of padding
Definition: crop.h:19
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: crop.h:103
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
Plan(const CroppingExp< SrcExp, DType, srcdim > &e)
Definition: crop.h:99
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
index_t pad_height_
pad height
Definition: crop.h:25