test_pool_op.cpp 9.8 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
14

Z
ZhenWang 已提交
15
#include <iostream>
W
wangliu 已提交
16
#include "../test_include.h"
H
hjchen2 已提交
17
#include "operators/math/pooling.h"
W
wangliu 已提交
18
#include "operators/pool_op.h"
19

Z
ZhenWang 已提交
20
namespace paddle_mobile {
H
hjchen2 已提交
21 22 23

namespace math = operators::math;

H
hjchen2 已提交
24
template <int PoolType, int Kernel, int Pad, int Stride>
Z
ZhenWang 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
int TestPoolOp(int in_channels, int in_height, int in_width) {
  int kernel_h = Kernel;
  int kernel_w = Kernel;
  int pad_h = Pad;
  int pad_w = Pad;
  int stride_h = Stride;
  int stride_w = Stride;
  std::string pooling_type = (PoolType == 0 ? "max" : "avg");

  int batch_size = 1;
  int input_c = in_channels;
  int input_h = in_height;
  int input_w = in_width;

  framework::DDim input_shape =
      framework::make_ddim({batch_size, input_c, input_h, input_w});

  VariableNameMap inputs;
  VariableNameMap outputs;
  auto scope = std::make_shared<framework::Scope>();
  inputs["X"] = std::vector<std::string>({"input"});
  outputs["Out"] = std::vector<std::string>({"output"});
47

Z
ZhenWang 已提交
48 49
  auto input_var = scope.get()->Var("input");
  auto input = input_var->template GetMutable<framework::LoDTensor>();
H
hjchen2 已提交
50 51 52 53 54
  SetupTensor<float>(input, input_shape, -127, 127);

  //  for (int i = 0; i < input->numel(); ++i) {
  //    DLOG << "input[" << i << "] = " << input->data<float>()[i];
  //  }
55

Z
ZhenWang 已提交
56 57 58 59 60 61
  auto output_var = scope.get()->Var("output");
  framework::AttributeMap attrs;
  attrs["pooling_type"].SetString(pooling_type);
  attrs["ksize"].Set<vector<int>>(std::vector<int>({kernel_h, kernel_w}));
  attrs["strides"].Set<vector<int>>(std::vector<int>({stride_h, stride_w}));
  attrs["paddings"].Set<vector<int>>(std::vector<int>({pad_h, pad_w}));
62 63
  attrs["ceil_mode"].Set<bool>(true);
  //  attrs["ceil_mode"].Set<bool>(false);
Z
ZhenWang 已提交
64 65 66 67 68 69 70 71
  attrs["global_pooling"].Set<bool>(false);

  auto *op = new operators::PoolOp<CPU, float>("pool2d", inputs, outputs, attrs,
                                               scope);
  op->InferShape();
  op->Init();
  op->Run();

H
hjchen2 已提交
72
  auto output = output_var->template Get<framework::LoDTensor>();
Z
ZhenWang 已提交
73
  framework::Tensor output_cmp;
H
hjchen2 已提交
74
  output_cmp.mutable_data<float>(output->dims());
H
hjchen2 已提交
75 76

  if (pooling_type == "avg") {
77
    math::Pooling<AVG>()(*input, std::vector<int>{kernel_h, kernel_w},
H
hjchen2 已提交
78 79
                         std::vector<int>{stride_h, stride_w},
                         std::vector<int>{pad_h, pad_w}, &output_cmp);
Z
ZhenWang 已提交
80
  } else {
81
    math::Pooling<MAX>()(*input, std::vector<int>{kernel_h, kernel_w},
H
hjchen2 已提交
82 83
                         std::vector<int>{stride_h, stride_w},
                         std::vector<int>{pad_h, pad_w}, &output_cmp);
Z
ZhenWang 已提交
84 85 86
  }

  // compare results
H
hjchen2 已提交
87 88
  const float *output_data = output->data<float>();
  float *output_cmp_data = output_cmp.data<float>();
Z
ZhenWang 已提交
89
  for (int i = 0; i < output->numel(); ++i) {
H
hjchen2 已提交
90 91 92 93 94 95 96 97 98 99
    float gap = output_data[i] - output_cmp_data[i];
    //    PADDLE_MOBILE_ENFORCE(output_data[i] == output_cmp_data[i],
    //                          "output[%d] = %d, output_cmp[%d] = %d", i,
    //                          output_data[i], i, output_cmp_data[i]);
    if (gap > 1e-5 && std::abs(gap / (output_data[i] + 1e-5)) > 1e-3) {
      LOG(kLOG_INFO) << "output_data[" << i << "] = " << output_data[i]
                     << ", output_cmp_data[" << i
                     << "] = " << output_cmp_data[i];
      exit(1);
    }
100
  }
Z
ZhenWang 已提交
101
  delete op;
102
  return 0;
103
}
Z
ZhenWang 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}  // namespace paddle_mobile

int main(int argc, char *argv[]) {
  if (argc < 4) {
    LOG(paddle_mobile::kLOG_INFO)
        << "Usage:\n"
        << "  ./test-pool-op in_channels in_height in_width \n"
        << "  params:\n"
        << "   -in_channels: int, input image's channels\n"
        << "   -in_height: int, input image's height\n"
        << "   -in_width: int, input image's width\n";
    return 1;
  }
  int in_channels = atoi(argv[1]);
  int in_height = atoi(argv[2]);
  int in_width = atoi(argv[3]);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=max, kernel=3, pad=0, stride=1";
  paddle_mobile::TestPoolOp<0, 3, 0, 1>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=max, kernel=3, pad=1, stride=1";
  paddle_mobile::TestPoolOp<0, 3, 1, 1>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=max, kernel=3, pad=2, stride=1";
  paddle_mobile::TestPoolOp<0, 3, 2, 1>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=max, kernel=3, pad=5, stride=1";
  paddle_mobile::TestPoolOp<0, 3, 5, 1>(in_channels, in_height, in_width);

  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=avg, kernel=3, pad=0, stride=1";
  paddle_mobile::TestPoolOp<1, 3, 0, 1>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=avg, kernel=3, pad=1, stride=1";
  paddle_mobile::TestPoolOp<1, 3, 1, 1>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=avg, kernel=3, pad=2, stride=1";
  paddle_mobile::TestPoolOp<1, 3, 2, 1>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=avg, kernel=3, pad=5, stride=1";
  paddle_mobile::TestPoolOp<1, 3, 5, 1>(in_channels, in_height, in_width);
H
hjchen2 已提交
145 146 147 148

  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=max, kernel=3, pad=0, stride=2";
  paddle_mobile::TestPoolOp<0, 3, 0, 2>(in_channels, in_height, in_width);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=max, kernel=3, pad=1, stride=2";
  paddle_mobile::TestPoolOp<0, 3, 1, 2>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=max, kernel=3, pad=2, stride=2";
  paddle_mobile::TestPoolOp<0, 3, 2, 2>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=max, kernel=3, pad=5, stride=2";
  paddle_mobile::TestPoolOp<0, 3, 5, 2>(in_channels, in_height, in_width);

  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=avg, kernel=3, pad=0, stride=2";
  paddle_mobile::TestPoolOp<1, 3, 0, 2>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=avg, kernel=3, pad=1, stride=2";
  paddle_mobile::TestPoolOp<1, 3, 1, 2>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=avg, kernel=3, pad=2, stride=2";
  paddle_mobile::TestPoolOp<1, 3, 2, 2>(in_channels, in_height, in_width);
  LOG(paddle_mobile::kLOG_INFO)
      << "float, pooling_type=avg, kernel=3, pad=5, stride=2";
  paddle_mobile::TestPoolOp<1, 3, 5, 2>(in_channels, in_height, in_width);
H
hjchen2 已提交
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=max, kernel=2, pad=0, stride=1";
  //  paddle_mobile::TestPoolOp<0, 2, 0, 1>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=max, kernel=2, pad=1, stride=1";
  //  paddle_mobile::TestPoolOp<0, 2, 1, 1>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=max, kernel=2, pad=2, stride=1";
  //  paddle_mobile::TestPoolOp<0, 2, 2, 1>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=max, kernel=2, pad=5, stride=1";
  //  paddle_mobile::TestPoolOp<0, 2, 5, 1>(in_channels, in_height, in_width);
  //
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=avg, kernel=2, pad=0, stride=1";
  //  paddle_mobile::TestPoolOp<1, 2, 0, 1>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=avg, kernel=2, pad=1, stride=1";
  //  paddle_mobile::TestPoolOp<1, 2, 1, 1>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=avg, kernel=2, pad=2, stride=1";
  //  paddle_mobile::TestPoolOp<1, 2, 2, 1>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=avg, kernel=2, pad=5, stride=1";
  //  paddle_mobile::TestPoolOp<1, 2, 5, 1>(in_channels, in_height, in_width);
  //
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=max, kernel=2, pad=0, stride=2";
  //  paddle_mobile::TestPoolOp<0, 2, 0, 2>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=max, kernel=2, pad=1, stride=2";
  //  paddle_mobile::TestPoolOp<0, 2, 1, 2>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=max, kernel=2, pad=2, stride=2";
  //  paddle_mobile::TestPoolOp<0, 2, 2, 2>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=max, kernel=2, pad=5, stride=2";
  //  paddle_mobile::TestPoolOp<0, 2, 5, 2>(in_channels, in_height, in_width);
  //
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=avg, kernel=2, pad=0, stride=2";
  //  paddle_mobile::TestPoolOp<1, 2, 0, 2>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=avg, kernel=2, pad=1, stride=2";
  //  paddle_mobile::TestPoolOp<1, 2, 1, 2>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=avg, kernel=2, pad=2, stride=2";
  //  paddle_mobile::TestPoolOp<1, 2, 2, 2>(in_channels, in_height, in_width);
  //  LOG(paddle_mobile::kLOG_INFO)
  //      << "float, pooling_type=avg, kernel=2, pad=5, stride=2";
  //  paddle_mobile::TestPoolOp<1, 2, 5, 2>(in_channels, in_height, in_width);
Z
ZhenWang 已提交
223
}