pool_op.cpp 2.4 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
14

L
liuruilong 已提交
15 16
#ifdef POOL_OP

L
liuruilong 已提交
17
#include "operators/pool_op.h"
L
liuruilong 已提交
18
#include <vector>
19
#include "framework/op_proto_maker.h"
L
liuruilong 已提交
20
#include "framework/op_registry.h"
21 22 23 24 25 26

namespace paddle_mobile {
namespace operators {

int PoolOutputSize(int input_size, int filter_size, int padding, int stride,
                   bool ceil_mode) {
27 28 29 30 31 32 33 34
  int output_size;
  if (!ceil_mode) {
    output_size = (input_size - filter_size + 2 * padding) / stride + 1;
  } else {
    output_size =
        (input_size - filter_size + 2 * padding + stride - 1) / stride + 1;
  }
  return output_size;
35 36 37
}
template <typename DeviceType, typename T>
void PoolOp<DeviceType, T>::InferShape() const {
L
liuruilong 已提交
38 39 40 41 42
  auto in_x_dims = this->param_.Input()->dims();
  std::vector<int> ksize = this->param_.Ksize();
  std::vector<int> paddings = this->param_.Paddings();
  std::vector<int> strides = this->param_.Strides();
  bool ceil_mode = this->param_.isCeilMode();
43

L
liuruilong 已提交
44
  if (this->param_.isGlobalPooling()) {
45
    ksize.resize(static_cast<size_t>(in_x_dims.size()) - 2);
46
    for (size_t i = 0; i < ksize.size(); ++i) {
47 48
      paddings[i] = 0;
      ksize[i] = static_cast<int>(in_x_dims[i + 2]);
49
    }
50 51 52 53 54 55
  }
  std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
  for (size_t i = 0; i < ksize.size(); ++i) {
    output_shape.push_back(PoolOutputSize(in_x_dims[i + 2], ksize[i],
                                          paddings[i], strides[i], ceil_mode));
  }
L
liuruilong 已提交
56
  this->param_.Output()->Resize(framework::make_ddim(output_shape));
57
}
L
liuruilong 已提交
58

朔-望's avatar
朔-望 已提交
59 60
}  // namespace operators
}  // namespace paddle_mobile
E
eclipsess 已提交
61 62

namespace ops = paddle_mobile::operators;
L
for  
liuruilong 已提交
63 64 65 66
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU(pool2d, ops::PoolOp);
#endif
#ifdef PADDLE_MOBILE_MALI_GPU
S
sharper 已提交
67
REGISTER_OPERATOR_MALI_GPU(pool2d, ops::PoolOp);
L
for  
liuruilong 已提交
68 69
#endif
#ifdef PADDLE_MOBILE_FPGA
qnqinan's avatar
qnqinan 已提交
70
REGISTER_OPERATOR_FPGA(pool2d, ops::PoolOp);
L
for  
liuruilong 已提交
71
#endif
Y
yangfei 已提交
72 73 74
#ifdef PADDLE_MOBILE_CL
REGISTER_OPERATOR_CL(pool2d, ops::PoolOp);
#endif
L
liuruilong 已提交
75 76

#endif