mxnet
unpack_patch2col.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_UNPACK_PATCH2COL_H_
27 #define MSHADOW_EXTENSION_UNPACK_PATCH2COL_H_
28 #include "../extension.h"
29 namespace mshadow {
30 namespace expr {
39 template<typename SrcExp, typename DType, int srcdim>
41  public MakeTensorExp<UnpackPatchToColXExp<SrcExp, DType, srcdim>,
42  SrcExp, 2, DType>{
44  const SrcExp &img_;
62  UnpackPatchToColXExp(const SrcExp &img,
63  index_t psize_y,
64  index_t psize_x,
65  index_t pstride_y,
66  index_t pstride_x,
67  index_t pdilate_y,
68  index_t pdilate_x)
69  : img_(img), psize_y_(psize_y), psize_x_(psize_x),
70  pstride_y_(pstride_y), pstride_x_(pstride_x),
71  pdilate_y_(pdilate_y), pdilate_x_(pdilate_x){
73  CHECK(imshape[srcdim - 1] >= psize_x && imshape[srcdim - 2] >= psize_y)
74  << "UnpackPatchToCol:image shape smaller than patch size";
75  this->i_channel_ = imshape[srcdim - 3];
76  this->i_height_ = imshape[srcdim - 2];
77  this->i_width_ = imshape[srcdim - 1];
78  // calculate number of batches
79  const index_t num = imshape.ProdShape(0, srcdim - 3);
80  const index_t o_height = (i_height_ -
81  (pdilate_y * (psize_y - 1) + 1)) / pstride_y + 1;
82  const index_t o_width = (i_width_ -
83  (pdilate_x * (psize_x - 1) + 1)) / pstride_x + 1;
84  this->shape_[1] = o_height * o_width * num;
85  this->shape_[0] = psize_y * psize_x * i_channel_;
86  }
87 };
88 
108 template<typename SrcExp, typename DType, int etype>
111  index_t psize_y, index_t psize_x, index_t pstride, index_t pdilate) {
113  ::Error_Expression_Does_Not_Meet_Dimension_Req();
115  (img.self(), psize_y, psize_x, pstride, pstride, pdilate, pdilate);
116 }
117 
121 template<typename SrcExp, typename DType, int etype>
124  index_t psize_y, index_t psize_x, index_t pstride_y_, index_t pstride_x_,
127  ::Error_Expression_Does_Not_Meet_Dimension_Req();
129  (img.self(), psize_y, psize_x, pstride_y_, pstride_x_, pdilate_y_, pdilate_x_);
130 }
131 //----------------------
132 // Execution plan
133 //----------------------
134 template<typename SrcExp, typename DType, int srcdim>
135 struct Plan<UnpackPatchToColXExp<SrcExp, DType, srcdim>, DType> {
136  public:
138  :src_(MakePlan(e.img_)),
143  o_height_((i_height_ - (pdilate_y_ * (psize_y_ - 1) + 1)) / pstride_y_ + 1),
144  o_width_((i_width_ - (pdilate_x_ * (psize_x_ - 1) + 1)) / pstride_x_ + 1) {}
145  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
146  const index_t x_offset = i % psize_x_ * pdilate_x_;
147  const index_t idivp = i / psize_x_;
148  const index_t y_offset = idivp % psize_y_ * pdilate_y_;
149  const index_t c = idivp / psize_y_;
150  const index_t x = (j % o_width_) * pstride_x_ + x_offset;
151  const index_t jdivw = j / o_width_;
152  const index_t y = (jdivw % o_height_) * pstride_y_ + y_offset;
153  const index_t n = jdivw / o_height_;
154 
155  if (x < i_width_ && y < i_height_) {
156  return src_.Eval((n * i_channel_ + c) * i_height_ + y, x);
157  } else {
158  return DType(0.0f);
159  }
160  }
161 
162  private:
163  Plan<SrcExp, DType> src_;
166  const index_t i_height_, i_width_, o_height_, o_width_;
167 };
168 } // namespace expr
169 } // namespace mshadow
170 #endif // MSHADOW_EXTENSION_UNPACK_PATCH2COL_H_
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
UnpackPatchToColXExp(const SrcExp &img, index_t psize_y, index_t psize_x, index_t pstride_y, index_t pstride_x, index_t pdilate_y, index_t pdilate_x)
constructor
Definition: unpack_patch2col.h:62
index_t psize_x_
patch width
Definition: unpack_patch2col.h:48
unpack local (overlap) patches of image to column of mat, can be used to implement convolution...
Definition: unpack_patch2col.h:40
index_t pstride_y_
patch stride
Definition: unpack_patch2col.h:50
UnpackPatchToColXExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > unpack_patch2col(const Exp< SrcExp, DType, etype > &img, index_t psize_y, index_t psize_x, index_t pstride, index_t pdilate)
unpack local (overlap) patches of image to column of mat, can be used to implement convolution after ...
Definition: unpack_patch2col.h:110
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:223
int32_t index_t
type that will be used for index
Definition: base.h:336
index_t i_height_
height of img
Definition: unpack_patch2col.h:58
index_t i_width_
width of img
Definition: unpack_patch2col.h:60
MSHADOW_XINLINE index_t ProdShape(int dimstart, int dimend) const
Definition: tensor.h:158
index_t pdilate_x_
Definition: unpack_patch2col.h:54
const SrcExp & img_
source operand
Definition: unpack_patch2col.h:44
index_t i_channel_
number of input channel
Definition: unpack_patch2col.h:56
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: unpack_patch2col.h:145
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 UnpackPatchToColXExp< SrcExp, DType, srcdim > &e)
Definition: unpack_patch2col.h:137
index_t pdilate_y_
patch dilate
Definition: unpack_patch2col.h:53
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 pstride_x_
Definition: unpack_patch2col.h:51
index_t psize_y_
patch height
Definition: unpack_patch2col.h:46