mxnet
crop.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef MSHADOW_EXTENSION_CROP_H_
27 #define MSHADOW_EXTENSION_CROP_H_
28 #include "../extension.h"
29 namespace mshadow {
30 namespace expr {
37 template<typename SrcExp, typename DType, int srcdim>
38 struct CroppingExp:
39  public MakeTensorExp<CroppingExp<SrcExp, DType, srcdim>,
40  SrcExp, srcdim, DType> {
42  const SrcExp &src_;
50  explicit CroppingExp(const SrcExp &src, Shape<2> cshape)
51  : src_(src) {
53  CHECK_GE(this->shape_[srcdim - 2], cshape[0]) << "CroppingExp: height requirement not met";
54  CHECK_GE(this->shape_[srcdim - 1], cshape[1]) << "CroppingExp: width requirement not met";
55  pad_height_ = (this->shape_[srcdim - 2] - cshape[0]) / 2;
56  pad_width_ = (this->shape_[srcdim - 1] - cshape[1]) / 2;
57  src_height_ = this->shape_[srcdim - 2];
58  this->shape_[srcdim - 2] = cshape[0]; // height
59  this->shape_[srcdim - 1] = cshape[1]; // width
60  }
62  explicit CroppingExp(const SrcExp &src, Shape<2> cshape,
63  index_t start_height, index_t start_width)
64  : src_(src), pad_height_(start_height), pad_width_(start_width) {
66  CHECK_GE(this->shape_[srcdim - 2], cshape[0] + start_height)
67  << "CroppingExp: height requirement not met";
68  CHECK_GE(this->shape_[srcdim - 1], cshape[1] + start_width)
69  << "CroppingExp: width requirement not met";
70  src_height_ = this->shape_[srcdim - 2];
71  this->shape_[srcdim - 2] = cshape[0]; // height
72  this->shape_[srcdim - 1] = cshape[1]; // width
73  }
74 }; // struct CroppingExp
85 template<typename SrcExp, typename DType, int etype>
89  ::Error_Expression_Does_Not_Meet_Dimension_Req();
91 }
103 template<typename SrcExp, typename DType, int etype>
106  index_t start_height, index_t start_width) {
108  ::Error_Expression_Does_Not_Meet_Dimension_Req();
110  (src.self(), oshape, start_height, start_width);
111 }
112 //----------------------
113 // Execution plan
114 //----------------------
115 template<typename SrcExp, typename DType, int srcdim>
116 struct Plan<CroppingExp<SrcExp, DType, srcdim>, DType> {
117  public:
119  : src_(MakePlan(e.src_)),
121  new_height_(e.shape_[srcdim - 2]), src_height_(e.src_height_) {}
122  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
123  const index_t x = j;
124  const index_t y = i % new_height_;
125  const index_t c = i / new_height_;
126  const index_t h = y + pad_height_;
127  const index_t w = x + pad_width_;
128  return src_.Eval(c * src_height_ + h, w);
129  }
130  private:
133  const index_t new_height_;
134  const index_t src_height_;
135 };
136 } // namespace expr
137 } // namespace mshadow
138 #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:87
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
CroppingExp(const SrcExp &src, Shape< 2 > cshape, index_t start_height, index_t start_width)
constructor
Definition: crop.h:62
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:223
index_t src_height_
src height
Definition: crop.h:48
index_t pad_width_
pad height
Definition: crop.h:46
int32_t index_t
type that will be used for index
Definition: base.h:336
const SrcExp & src_
source operand
Definition: crop.h:42
CroppingExp(const SrcExp &src, Shape< 2 > cshape)
constructor
Definition: crop.h:50
crop expression, cut off the boundary region, reverse operation of padding
Definition: crop.h:38
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: crop.h:122
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:80
const SubType & self(void) const
Definition: expression.h:83
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:240
Plan(const CroppingExp< SrcExp, DType, srcdim > &e)
Definition: crop.h:118
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:44
overloaded + operator between half_t and bf16_t
Definition: base.h:327
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:48
index_t pad_height_
pad height
Definition: crop.h:44