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 43 44 45
  if (ksize[0] == 3 && ksize[0] == ksize[1]) {
    if (pooling_type == "max" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
        math::Pooling3x3<Max, 1>()(*input, paddings, output);
      } else if (strides[0] == 2) {
        math::Pooling3x3<Max, 2>()(*input, paddings, output);
46
      } else {
H
hjchen2 已提交
47 48 49 50 51 52 53 54 55
        math::Pooling<Max>()(*input, ksize, strides, paddings, output);
      }
    } else if (pooling_type == "avg" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
        math::Pooling3x3<Avg, 1>()(*input, paddings, output);
      } else if (strides[0] == 2) {
        math::Pooling3x3<Avg, 2>()(*input, paddings, output);
      } else {
        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 62 63 64
    if (pooling_type == "max") {
      math::Pooling<Max>()(*input, ksize, strides, paddings, output);
    } else if (pooling_type == "avg") {
      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