pool_arm_func.h 3.2 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
  std::vector<int> ksize = param.Ksize();
  std::vector<int> strides = param.Strides();
  std::vector<int> paddings = param.Paddings();
35
  const bool exclusive = param.isExclusive();
36 37 38
  if (param.isGlobalPooling()) {
    for (size_t i = 0; i < ksize.size(); ++i) {
      paddings[i] = 0;
H
hjchen2 已提交
39
      ksize[i] = static_cast<int>(input->dims()[i + 2]);
40
    }
N
nhzlx 已提交
41
  }
H
hjchen2 已提交
42 43 44
  if (ksize[0] == 3 && ksize[0] == ksize[1]) {
    if (pooling_type == "max" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
45
        math::Pooling3x3<MAX, 1>()(*input, paddings, exclusive, output);
H
hjchen2 已提交
46
      } else if (strides[0] == 2) {
47
        math::Pooling3x3<MAX, 2>()(*input, paddings, exclusive, output);
48
      } else {
49
        math::Pooling<MAX>()(*input, ksize, strides, paddings, output);
H
hjchen2 已提交
50 51 52
      }
    } else if (pooling_type == "avg" && strides[0] == strides[1]) {
      if (strides[0] == 1) {
53
        math::Pooling3x3<AVG, 1>()(*input, paddings, exclusive, output);
H
hjchen2 已提交
54
      } else if (strides[0] == 2) {
55
        math::Pooling3x3<AVG, 2>()(*input, paddings, exclusive, output);
H
hjchen2 已提交
56
      } else {
57
        math::Pooling<AVG>()(*input, ksize, strides, paddings, output);
58
      }
H
hjchen2 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    }
  } 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);
      }
77
    }
Z
ZhenWang 已提交
78
  } else {
H
hjchen2 已提交
79
    if (pooling_type == "max") {
80
      math::Pooling<MAX>()(*input, ksize, strides, paddings, output);
H
hjchen2 已提交
81
    } else if (pooling_type == "avg") {
82
      math::Pooling<AVG>()(*input, ksize, strides, paddings, output);
Z
ZhenWang 已提交
83
    } else {
H
hjchen2 已提交
84
      // Others
Z
ZhenWang 已提交
85
    }
86 87 88 89 90 91
  }
}

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