mxnet
channel_pool.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_CHANNEL_POOL_H_
27 #define MSHADOW_EXTENSION_CHANNEL_POOL_H_
28 #include <algorithm>
29 #include "../extension.h"
30 namespace mshadow {
31 namespace expr {
40 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
42  public MakeTensorExp<ChannelPoolingExp<Reducer, SrcExp, DType, srcdim>,
43  SrcExp, srcdim, DType> {
45  const SrcExp &src_;
54  ChannelPoolingExp(const SrcExp &src, index_t nsize, index_t stride, index_t pad)
55  : src_(src), nsize_(nsize), stride_(stride), pad_(pad) {
57  this->src_channel_ = this->shape_[srcdim - 3];
58  CHECK_GE(this->shape_[srcdim - 3], nsize_)
59  << "chpool: local size must be smaller than nchannels";
60  this->shape_[srcdim - 3] = (this->src_channel_ - nsize + pad * 2 + 1) / stride;
61  }
62 };
74 template<typename Reducer, typename SrcExp, typename DType, int etype>
78  ::Error_Expression_Does_Not_Meet_Dimension_Req();
79  CHECK_EQ(nsize % 2, 1U) << "chpool: if no pad is specified, local size must be odd";
80  return ChannelPoolingExp<Reducer, SrcExp,
81  DType, ExpInfo<SrcExp>::kDim>(src.self(), nsize, 1, nsize / 2);
82 }
83 
84 template<typename Reducer, typename SrcExp, typename DType, int etype>
88  ::Error_Expression_Does_Not_Meet_Dimension_Req();
89  return ChannelPoolingExp<Reducer, SrcExp,
90  DType, ExpInfo<SrcExp>::kDim>(src.self(), nsize, stride, pad);
91 }
92 
93 //----------------------
94 // Execution plan
95 //----------------------
96 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
97 struct Plan<ChannelPoolingExp<Reducer, SrcExp, DType, srcdim>, DType> {
98  public:
100  : src_(MakePlan(e.src_)), channel_(e.shape_[srcdim - 3]),
101  height_(e.shape_[srcdim - 2]), width_(e.shape_[srcdim - 1]),
102  hnsize_(e.nsize_), stride_(e.stride_), pad_(e.pad_),
104  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
105  using namespace std;
106  const index_t y = i % height_;
107  i /= height_;
108  const index_t c = i % channel_;
109  const index_t n = i / channel_;
110  const index_t x = j;
111  const index_t cstart = c * stride_ < pad_ ? 0 : c * stride_ - pad_;
112  const index_t cend = min(c * stride_ - pad_ + hnsize_, channel_);
113  DType res; Reducer::SetInitValue(res);
114  for (index_t cc = cstart; cc < cend; ++cc) {
115  Reducer::Reduce(res, src_.Eval((n * src_channel_ + cc) * height_ + y, x));
116  }
117  return res;
118  }
119 
120  private:
122  const index_t channel_, height_, width_, hnsize_, stride_, pad_, src_channel_;
123 };
124 } // namespace expr
125 } // namespace mshadow
126 #endif // MSHADOW_EXTENSION_CHANNEL_POOL_H_
127 
index_t pad_
pad of pooling of each side
Definition: channel_pool.h:51
ChannelPoolingExp< Reducer, SrcExp, DType, ExpInfo< SrcExp >::kDim > chpool(const Exp< SrcExp, DType, etype > &src, index_t nsize)
channel pooling, do reduction over (local nearby) channels, used to implement local response normaliz...
Definition: channel_pool.h:76
PaddingExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > pad(const Exp< SrcExp, DType, etype > &src, index_t pad)
padding expression, pad a image with zeros on boundaries, padding affects shape[0], and shape[1]
Definition: pad.h:72
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
channel pooling expression, do reduction over (local nearby) channels, used to implement local respon...
Definition: channel_pool.h:41
Definition: optional.h:241
index_t stride_
stride of pooling
Definition: channel_pool.h:49
ChannelPoolingExp(const SrcExp &src, index_t nsize, index_t stride, index_t pad)
constructor
Definition: channel_pool.h:54
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: channel_pool.h:104
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:223
index_t src_channel_
Definition: channel_pool.h:52
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:263
int32_t index_t
type that will be used for index
Definition: base.h:336
index_t nsize_
neighbor size
Definition: channel_pool.h:47
Plan(const ChannelPoolingExp< Reducer, SrcExp, DType, srcdim > &e)
Definition: channel_pool.h:99
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
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
const SrcExp & src_
source operand
Definition: channel_pool.h:45