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 
25 #ifndef MSHADOW_EXTENSION_CHANNEL_POOL_H_
26 #define MSHADOW_EXTENSION_CHANNEL_POOL_H_
27 #include <algorithm>
28 #include "../extension.h"
29 namespace mshadow {
30 namespace expr {
39 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
41  public MakeTensorExp<ChannelPoolingExp<Reducer, SrcExp, DType, srcdim>,
42  SrcExp, srcdim, DType> {
44  const SrcExp &src_;
53  ChannelPoolingExp(const SrcExp &src, index_t nsize, index_t stride, index_t pad)
54  : src_(src), nsize_(nsize), stride_(stride), pad_(pad) {
56  this->src_channel_ = this->shape_[srcdim - 3];
57  CHECK_GE(this->shape_[srcdim - 3], nsize_)
58  << "chpool: local size must be smaller than nchannels";
59  this->shape_[srcdim - 3] = (this->src_channel_ - nsize + pad * 2 + 1) / stride;
60  }
61 };
73 template<typename Reducer, typename SrcExp, typename DType, int etype>
74 inline ChannelPoolingExp<Reducer, SrcExp, DType, ExpInfo<SrcExp>::kDim>
77  ::Error_Expression_Does_Not_Meet_Dimension_Req();
78  CHECK_EQ(nsize % 2, 1U) << "chpool: if no pad is specified, local size must be odd";
79  return ChannelPoolingExp<Reducer, SrcExp,
80  DType, ExpInfo<SrcExp>::kDim>(src.self(), nsize, 1, nsize / 2);
81 }
82 
83 template<typename Reducer, typename SrcExp, typename DType, int etype>
84 inline ChannelPoolingExp<Reducer, SrcExp, DType, ExpInfo<SrcExp>::kDim>
87  ::Error_Expression_Does_Not_Meet_Dimension_Req();
88  return ChannelPoolingExp<Reducer, SrcExp,
89  DType, ExpInfo<SrcExp>::kDim>(src.self(), nsize, stride, pad);
90 }
91 
92 //----------------------
93 // Execution plan
94 //----------------------
95 template<typename Reducer, typename SrcExp, typename DType, int srcdim>
96 struct Plan<ChannelPoolingExp<Reducer, SrcExp, DType, srcdim>, DType> {
97  public:
99  : src_(MakePlan(e.src_)), channel_(e.shape_[srcdim - 3]),
100  height_(e.shape_[srcdim - 2]), width_(e.shape_[srcdim - 1]),
101  hnsize_(e.nsize_), stride_(e.stride_), pad_(e.pad_),
102  src_channel_(e.src_channel_) {}
103  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
104  using namespace std;
105  const index_t y = i % height_;
106  i /= height_;
107  const index_t c = i % channel_;
108  const index_t n = i / channel_;
109  const index_t x = j;
110  const index_t cstart = c * stride_ < pad_ ? 0 : c * stride_ - pad_;
111  const index_t cend = min(c * stride_ - pad_ + hnsize_, channel_);
112  DType res; Reducer::SetInitValue(res);
113  for (index_t cc = cstart; cc < cend; ++cc) {
114  Reducer::Reduce(res, src_.Eval((n * src_channel_ + cc) * height_ + y, x));
115  }
116  return res;
117  }
118 
119  private:
120  Plan<SrcExp, DType> src_;
121  const index_t channel_, height_, width_, hnsize_, stride_, pad_, src_channel_;
122 };
123 } // namespace expr
124 } // namespace mshadow
125 #endif // MSHADOW_EXTENSION_CHANNEL_POOL_H_
126 
mshadow::expr::ChannelPoolingExp::nsize_
index_t nsize_
neighbor size
Definition: channel_pool.h:46
mshadow::expr::ChannelPoolingExp::src_channel_
index_t src_channel_
Definition: channel_pool.h:51
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::ChannelPoolingExp::stride_
index_t stride_
stride of pooling
Definition: channel_pool.h:48
mshadow::expr::TypeCheckPass
used to help static type check
Definition: expr_engine-inl.h:330
mshadow::expr::Plan< ChannelPoolingExp< Reducer, SrcExp, DType, srcdim >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: channel_pool.h:103
mshadow::expr::ChannelPoolingExp::src_
const SrcExp & src_
source operand
Definition: channel_pool.h:44
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::ChannelPoolingExp::pad_
index_t pad_
pad of pooling of each side
Definition: channel_pool.h:50
mshadow::expr::ExpInfo
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:262
mshadow::expr::MakePlan
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:239
mshadow::expr::MakeTensorExp< ChannelPoolingExp< Reducer, SrcExp, DType, srcdim >, SrcExp, srcdim, DType >::shape_
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:47
mshadow::expr::ChannelPoolingExp::ChannelPoolingExp
ChannelPoolingExp(const SrcExp &src, index_t nsize, index_t stride, index_t pad)
constructor
Definition: channel_pool.h:53
mshadow::index_t
int32_t index_t
type that will be used for index
Definition: base.h:328
mshadow::expr::Plan
Definition: expr_engine-inl.h:58
mshadow::expr::ChannelPoolingExp
channel pooling expression, do reduction over (local nearby) channels, used to implement local respon...
Definition: channel_pool.h:40
mshadow::expr::Exp
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
mshadow::expr::pad
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],...
Definition: pad.h:71
mshadow
overloaded + operator between half_t and bf16_t
Definition: base.h:319
mshadow::expr::MakeTensorExp
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:43
std
Definition: optional.h:251
mshadow::expr::chpool
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:75
mshadow::expr::Plan< ChannelPoolingExp< Reducer, SrcExp, DType, srcdim >, DType >::Plan
Plan(const ChannelPoolingExp< Reducer, SrcExp, DType, srcdim > &e)
Definition: channel_pool.h:98