pool_arm_func.h 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
H
hjchen2 已提交
16

17 18 19 20
#pragma once

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

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

27
template <typename P>
N
nhzlx 已提交
28
void PoolCompute(const PoolParam<CPU> &param) {
H
hjchen2 已提交
29 30 31
  const framework::Tensor *input = param.Input();
  framework::Tensor *output = param.Output();
  const std::string &pooling_type = param.PoolingType();
32 33 34 35 36 37
  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 已提交
38
      ksize[i] = static_cast<int>(input->dims()[i + 2]);
39
    }
N
nhzlx 已提交
40
  }
H
hjchen2 已提交
41 42 43
  if (ksize[0] == 3 && ksize[0] == ksize[1]) {
    if (pooling_type == "max" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
44
        math::Pooling3x3<MAX, 1>()(*input, paddings, output);
H
hjchen2 已提交
45
      } else if (strides[0] == 2) {
46
        math::Pooling3x3<MAX, 2>()(*input, paddings, output);
47
      } else {
48
        math::Pooling<MAX>()(*input, ksize, strides, paddings, output);
H
hjchen2 已提交
49 50 51
      }
    } else if (pooling_type == "avg" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
52
        math::Pooling3x3<AVG, 1>()(*input, paddings, output);
H
hjchen2 已提交
53
      } else if (strides[0] == 2) {
54
        math::Pooling3x3<AVG, 2>()(*input, paddings, output);
H
hjchen2 已提交
55
      } else {
56
        math::Pooling<AVG>()(*input, ksize, strides, paddings, output);
57
      }
H
hjchen2 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    }
  } else if (ksize[0] == 2 && ksize[0] == ksize[1]) {
    if (pooling_type == "max" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
        math::Pooling2x2<MAX, 1>()(*input, paddings, output);
      } else if (strides[0] == 2) {
        math::Pooling2x2<MAX, 2>()(*input, paddings, output);
      } else {
        math::Pooling<MAX>()(*input, ksize, strides, paddings, output);
      }
    } else if (pooling_type == "avg" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
        math::Pooling2x2<AVG, 1>()(*input, paddings, output);
      } else if (strides[0] == 2) {
        math::Pooling2x2<AVG, 2>()(*input, paddings, output);
      } else {
        math::Pooling<AVG>()(*input, ksize, strides, paddings, output);
      }
76
    }
Z
ZhenWang 已提交
77
  } else {
H
hjchen2 已提交
78
    if (pooling_type == "max") {
79
      math::Pooling<MAX>()(*input, ksize, strides, paddings, output);
H
hjchen2 已提交
80
    } else if (pooling_type == "avg") {
81
      math::Pooling<AVG>()(*input, ksize, strides, paddings, output);
Z
ZhenWang 已提交
82
    } else {
H
hjchen2 已提交
83
      // Others
Z
ZhenWang 已提交
84
    }
85 86 87 88 89 90
  }
}

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