pool_kernel.cpp 2.6 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
}

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

44
  std::vector<int> ksize = param.Ksize();
45

46
  std::vector<int> strides = param.Strides();
47

48 49 50 51 52
  std::vector<int> paddings = param.Paddings();
  if (ksize.size() != 2) {
    LOG(paddle_mobile::LogLevel::kLOG_ERROR)
        << "Pool op only supports 2D and 3D input.";
  }
53

54 55 56 57
  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]);
58
    }
W
wangliu 已提交
59 60 61 62 63 64
  } 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);
    }
65

W
wangliu 已提交
66 67 68 69 70 71 72 73 74 75
  } 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);
  }
76
}
朔-望's avatar
朔-望 已提交
77 78
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
79 80

#endif