mxnet
spatial_upsampling_nearest.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_SPATIAL_UPSAMPLING_NEAREST_H_
27 #define MSHADOW_EXTENSION_SPATIAL_UPSAMPLING_NEAREST_H_
28 #include "../extension.h"
29 
30 namespace mshadow {
31 namespace expr {
32 
39 template<typename SrcExp, typename DType, int srcdim>
41  public MakeTensorExp<UpSamplingNearestExp<SrcExp, DType, srcdim>,
42  SrcExp, srcdim, DType> {
44  const SrcExp &src_;
48  UpSamplingNearestExp(const SrcExp &src, index_t scale)
49  : src_(src), scale_(scale) {
51  this->shape_[srcdim - 2] *= scale_;
52  this->shape_[srcdim - 1] *= scale_;
53  }
54 };
55 
56 
57 template<typename SrcExp, typename DType, int etype>
61  ::Error_Expression_Does_Not_Meet_Dimension_Req();
63 }
64 
65 template<typename SrcExp, typename DType, int srcdim>
66 struct Plan<UpSamplingNearestExp<SrcExp, DType, srcdim>, DType> {
67  public:
69  : src_(MakePlan(e.src_)),
70  scale_(e.scale_),
71  new_height_(e.shape_[srcdim - 2]),
72  src_height_(static_cast<index_t>(e.shape_[srcdim - 2] / e.scale_)) {}
73  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
74  const index_t x = j;
75  const index_t y = i % new_height_;
76  const index_t c = i / new_height_;
77  const index_t h = static_cast<index_t>(y / scale_);
78  const index_t w = static_cast<index_t>(x / scale_);
79  return src_.Eval(c * src_height_ + h, w);
80  }
81 
82  private:
84  const index_t scale_;
85  const index_t new_height_;
86  const index_t src_height_;
87 };
88 } // namespace expr
89 } // namespace mshadow
90 #endif // MSHADOW_EXTENSION_SPATIAL_UPSAMPLING_NEAREST_H_
Plan(const UpSamplingNearestExp< SrcExp, DType, srcdim > &e)
Definition: spatial_upsampling_nearest.h:68
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
UpSamplingNearestExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > upsampling_nearest(const Exp< SrcExp, DType, etype > &src, index_t scale)
Definition: spatial_upsampling_nearest.h:59
nearest neighboor upsampling out(x, y) = in(int(x / scale_x), int(y / scale_y))
Definition: spatial_upsampling_nearest.h:40
static Shape< dim > Check(const E &t)
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: spatial_upsampling_nearest.h:73
#define MSHADOW_XINLINE
Definition: base.h:223
index_t scale_
up sampling scale
Definition: spatial_upsampling_nearest.h:46
int32_t index_t
type that will be used for index
Definition: base.h:336
const SrcExp & src_
source oprand
Definition: spatial_upsampling_nearest.h:44
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
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:44
UpSamplingNearestExp(const SrcExp &src, index_t scale)
constructor
Definition: spatial_upsampling_nearest.h:48
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