test_Upsample.cpp 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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. */

#include <gtest/gtest.h>
#include <string>
#include <vector>

#include "LayerGradUtil.h"
X
Xin Pan 已提交
20
#include "paddle/legacy/math/MathUtils.h"
21 22
#include "paddle/testing/TestUtil.h"

A
Abhinav Arora 已提交
23 24
void setPoolConfig(paddle::TestConfig* config,
                   paddle::PoolConfig* pool,
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
                   const string& poolType) {
  (*config).biasSize = 0;
  (*config).layerConfig.set_type("pool");
  (*config).layerConfig.set_num_filters(1);

  int kw = 2, kh = 2;
  int pw = 0, ph = 0;
  int sw = 2, sh = 2;
  pool->set_pool_type(poolType);
  pool->set_channels(2);
  pool->set_size_x(kw);
  pool->set_size_y(kh);
  pool->set_start(0);
  pool->set_padding(pw);
  pool->set_padding_y(ph);
  pool->set_stride(sw);
  pool->set_stride_y(sh);

A
Abhinav Arora 已提交
43 44 45 46
  int ow =
      paddle::outputSize(pool->img_size(), kw, pw, sw, /* caffeMode */ false);
  int oh =
      paddle::outputSize(pool->img_size_y(), kh, ph, sh, /* caffeMode */ false);
47 48 49 50
  pool->set_output_x(ow);
  pool->set_output_y(oh);
}

A
Abhinav Arora 已提交
51 52 53 54
paddle::LayerPtr doOneUpsampleTest(const paddle::MatrixPtr& inputMat,
                                   const string& poolType,
                                   bool use_gpu,
                                   real* tempGradData) {
55
  /* prepare maxPoolWithMaskLayer */
A
Abhinav Arora 已提交
56 57 58 59
  paddle::TestConfig config;
  config.inputDefs.push_back({paddle::INPUT_DATA, "layer_0", 128, 0});
  paddle::LayerInputConfig* input = config.layerConfig.add_inputs();
  paddle::PoolConfig* pool = input->mutable_pool_conf();
60 61 62 63 64 65 66 67 68

  pool->set_img_size(8);
  pool->set_img_size_y(8);
  setPoolConfig(&config, pool, "max-pool-with-mask");
  config.layerConfig.set_size(pool->output_x() * pool->output_y() *
                              pool->channels());

  config.layerConfig.set_name("MaxPoolWithMask");

A
Abhinav Arora 已提交
69 70 71
  std::vector<paddle::DataLayerPtr> dataLayers;
  paddle::LayerMap layerMap;
  vector<paddle::Argument> datas;
72 73 74 75 76 77 78 79 80 81 82 83 84

  initDataLayer(config,
                &dataLayers,
                &datas,
                &layerMap,
                "MaxPoolWithMask",
                1,
                false,
                use_gpu);

  dataLayers[0]->getOutputValue()->copyFrom(*inputMat);

  FLAGS_use_gpu = use_gpu;
A
Abhinav Arora 已提交
85 86
  std::vector<paddle::ParameterPtr> parameters;
  paddle::LayerPtr maxPoolingWithMaskOutputLayer;
87
  initTestLayer(config, &layerMap, &parameters, &maxPoolingWithMaskOutputLayer);
A
Abhinav Arora 已提交
88
  maxPoolingWithMaskOutputLayer->forward(paddle::PASS_GC);
89 90

  /* prepare the upsample layer */
A
Abhinav Arora 已提交
91
  paddle::LayerConfig upsampleLayerConfig;
92
  upsampleLayerConfig.set_type("upsample");
A
Abhinav Arora 已提交
93
  paddle::LayerInputConfig* input1 = upsampleLayerConfig.add_inputs();
94 95
  upsampleLayerConfig.add_inputs();

A
Abhinav Arora 已提交
96
  paddle::UpsampleConfig* upsampleConfig = input1->mutable_upsample_conf();
97
  upsampleConfig->set_scale(2);
A
Abhinav Arora 已提交
98
  paddle::ImageConfig* imageConfig = upsampleConfig->mutable_image_conf();
99 100 101 102 103 104 105
  imageConfig->set_channels(2);
  imageConfig->set_img_size(4);
  imageConfig->set_img_size_y(4);
  upsampleLayerConfig.set_size(2 * 8 * 8);
  upsampleLayerConfig.set_name("upsample");

  for (size_t i = 0; i < 2; i++) {
A
Abhinav Arora 已提交
106 107
    paddle::LayerInputConfig& inputTemp =
        *(upsampleLayerConfig.mutable_inputs(i));
108 109 110
    inputTemp.set_input_layer_name("MaxPoolWithMask");
  }

A
Abhinav Arora 已提交
111 112 113
  paddle::LayerPtr upsampleLayer;
  paddle::ParameterMap parameterMap;
  upsampleLayer = paddle::Layer::create(upsampleLayerConfig);
114 115 116
  layerMap[upsampleLayerConfig.name()] = upsampleLayer;
  upsampleLayer->init(layerMap, parameterMap);
  upsampleLayer->setNeedGradient(true);
A
Abhinav Arora 已提交
117
  upsampleLayer->forward(paddle::PASS_GC);
118 119 120 121 122 123 124 125
  upsampleLayer->getOutputGrad()->copyFrom(tempGradData, 128);
  upsampleLayer->backward();

  return upsampleLayer;
}

TEST(Layer, maxPoolingWithMaskOutputLayerFwd) {
  bool useGpu = false;
A
Abhinav Arora 已提交
126 127 128
  paddle::MatrixPtr inputMat;
  paddle::MatrixPtr inputGPUMat;
  paddle::MatrixPtr tempGradMat;
129

A
Abhinav Arora 已提交
130
  inputMat = paddle::Matrix::create(1, 128, false, useGpu);
131 132
  inputMat->randomizeUniform();

A
Abhinav Arora 已提交
133
  tempGradMat = paddle::Matrix::create(1, 128, false, useGpu);
134 135 136
  tempGradMat->randomizeUniform();
  real* tempGradData = tempGradMat->getData();

A
Abhinav Arora 已提交
137
  paddle::LayerPtr upsampleLayerCPU =
138 139 140 141
      doOneUpsampleTest(inputMat, "max-pool-with-mask", useGpu, tempGradData);

#ifdef PADDLE_WITH_CUDA
  useGpu = true;
A
Abhinav Arora 已提交
142 143
  real* data = inputMat->getData();
  inputGPUMat = paddle::Matrix::create(1, 128, false, useGpu);
144
  inputGPUMat->copyFrom(data, 128);
A
Abhinav Arora 已提交
145
  paddle::LayerPtr upsampleLayerGPU = doOneUpsampleTest(
146
      inputGPUMat, "max-pool-with-mask", useGpu, tempGradData);
A
Abhinav Arora 已提交
147 148
  paddle::checkMatrixEqual(upsampleLayerCPU->getOutput("").value,
                           upsampleLayerGPU->getOutput("").value);
149

A
Abhinav Arora 已提交
150 151
  paddle::checkMatrixEqual(upsampleLayerCPU->getPrev(0)->getOutputGrad(),
                           upsampleLayerGPU->getPrev(0)->getOutputGrad());
152 153
#endif
}