pool_arm_func.h 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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. */

#ifdef POOL_OP
#pragma once

#include <string>
#include <vector>
H
hjchen2 已提交
20
#include "common/types.h"
21 22 23 24
#include "operators/math/pooling.h"

namespace paddle_mobile {
namespace operators {
Z
ZhenWang 已提交
25

26
template <typename P>
N
nhzlx 已提交
27
void PoolCompute(const PoolParam<CPU> &param) {
H
hjchen2 已提交
28 29 30
  const framework::Tensor *input = param.Input();
  framework::Tensor *output = param.Output();
  const std::string &pooling_type = param.PoolingType();
31 32 33 34 35 36
  std::vector<int> ksize = param.Ksize();
  std::vector<int> strides = param.Strides();
  std::vector<int> paddings = param.Paddings();
  if (param.isGlobalPooling()) {
    for (size_t i = 0; i < ksize.size(); ++i) {
      paddings[i] = 0;
H
hjchen2 已提交
37
      ksize[i] = static_cast<int>(input->dims()[i + 2]);
38
    }
N
nhzlx 已提交
39
  }
H
hjchen2 已提交
40 41 42
  if (ksize[0] == 3 && ksize[0] == ksize[1]) {
    if (pooling_type == "max" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
43
        math::Pooling3x3<MAX, 1>()(*input, paddings, output);
H
hjchen2 已提交
44
      } else if (strides[0] == 2) {
45
        math::Pooling3x3<MAX, 2>()(*input, paddings, output);
46
      } else {
47
        math::Pooling<MAX>()(*input, ksize, strides, paddings, output);
H
hjchen2 已提交
48 49 50
      }
    } else if (pooling_type == "avg" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
51
        math::Pooling3x3<AVG, 1>()(*input, paddings, output);
H
hjchen2 已提交
52
      } else if (strides[0] == 2) {
53
        math::Pooling3x3<AVG, 2>()(*input, paddings, output);
H
hjchen2 已提交
54
      } else {
55
        math::Pooling<AVG>()(*input, ksize, strides, paddings, output);
56
      }
Z
ZhenWang 已提交
57
    } else {
H
hjchen2 已提交
58
      // Others
59
    }
Z
ZhenWang 已提交
60
  } else {
H
hjchen2 已提交
61
    if (pooling_type == "max") {
62
      math::Pooling<MAX>()(*input, ksize, strides, paddings, output);
H
hjchen2 已提交
63
    } else if (pooling_type == "avg") {
64
      math::Pooling<AVG>()(*input, ksize, strides, paddings, output);
Z
ZhenWang 已提交
65
    } else {
H
hjchen2 已提交
66
      // Others
Z
ZhenWang 已提交
67
    }
68 69 70 71 72 73
  }
}

}  // namespace operators
}  // namespace paddle_mobile
#endif