pool_kernel.cpp 2.7 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

L
liuruilong 已提交
15 16
#ifdef POOL_OP

17
#include <operators/kernel/pool_kernel.h>
朔-望's avatar
朔-望 已提交
18
#include "common/log.h"
19 20 21 22 23 24 25

namespace paddle_mobile {
namespace operators {

inline void PoolBasic(std::string pooling_type, std::vector<int> ksize,
                      std::vector<int> strides, std::vector<int> paddings,
                      const Tensor *in_x, Tensor *out) {
26 27 28 29
  if (pooling_type == "max") {
    math::PoolFunctor<CPU, math::MaxPool<float>, float> pool2d_forward;
    math::MaxPool<float> pool_process;
    pool2d_forward(*in_x, ksize, strides, paddings, pool_process, out);
30

31 32 33 34 35
  } else if (pooling_type == "avg") {
    math::PoolFunctor<CPU, math::AvgPool<float>, float> pool2d_forward;
    math::AvgPool<float> pool_process;
    pool2d_forward(*in_x, ksize, strides, paddings, pool_process, out);
  }
36 37
}

L
liuruilong 已提交
38
template <>
E
eclipsess 已提交
39
bool PoolKernel<CPU, float>::Init(PoolParam *param) const {
L
liuruilong 已提交
40 41 42
  return true;
}

朔-望's avatar
朔-望 已提交
43 44
template <>
void PoolKernel<CPU, float>::Compute(const PoolParam &param) const {
45 46 47
  const Tensor *in_x = param.Input();
  Tensor *out = param.Output();
  std::string pooling_type = param.PoolingType();
48

49
  std::vector<int> ksize = param.Ksize();
50

51
  std::vector<int> strides = param.Strides();
52

53 54 55 56 57
  std::vector<int> paddings = param.Paddings();
  if (ksize.size() != 2) {
    LOG(paddle_mobile::LogLevel::kLOG_ERROR)
        << "Pool op only supports 2D and 3D input.";
  }
58

59 60 61 62
  if (param.isGlobalPooling()) {
    for (size_t i = 0; i < ksize.size(); ++i) {
      paddings[i] = 0;
      ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
63
    }
W
wangliu 已提交
64 65 66 67 68 69
  } else if (ksize[0] == 3 && ksize[0] == ksize[1]) {
    if (pooling_type == "max") {
      math::Pool3x3Max(strides, paddings, in_x, out);
    } else if (pooling_type == "avg") {
      math::Pool3x3Avg(strides, paddings, in_x, out);
    }
70

W
wangliu 已提交
71 72 73 74 75 76 77 78 79 80
  } else if (ksize[0] == 2 && ksize[0] == ksize[1]) {
    if (pooling_type == "max") {
      math::Pool2x2Max(strides, paddings, in_x, out);
    } else if (pooling_type == "avg") {
      math::Pool2x2Avg(strides, paddings, in_x, out);
    }

  } else {
    PoolBasic(pooling_type, ksize, strides, paddings, in_x, out);
  }
81
}
朔-望's avatar
朔-望 已提交
82 83
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
84 85

#endif