test_PriorBox.cpp 7.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
G
gaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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

G
gaoyuan 已提交
15
#include <gtest/gtest.h>
G
gaoyuan 已提交
16 17 18 19
#include <string>
#include <vector>

#include "LayerGradUtil.h"
20
#include "paddle/testing/TestUtil.h"
G
gaoyuan 已提交
21 22 23 24 25 26

using namespace paddle;  // NOLINT
using namespace std;     // NOLINT

// Do one forward pass of priorBox layer and check to see if its output
// matches the given result
G
gaoyuan 已提交
27 28 29 30 31 32
void doOnePriorBoxTest(size_t feature_map_width,
                       size_t feature_map_height,
                       size_t image_width,
                       size_t image_height,
                       vector<int> min_size,
                       vector<int> max_size,
G
gaoyuan 已提交
33 34
                       vector<real> aspect_ratio,
                       vector<real> variance,
G
gaoyuan 已提交
35
                       bool use_gpu,
G
gaoyuan 已提交
36 37 38 39 40 41 42 43 44 45
                       MatrixPtr& result) {
  // Setting up the priorbox layer
  TestConfig configt;
  configt.layerConfig.set_type("priorbox");

  configt.inputDefs.push_back({INPUT_DATA, "featureMap", 1, 0});
  LayerInputConfig* input = configt.layerConfig.add_inputs();
  configt.inputDefs.push_back({INPUT_DATA, "image", 1, 0});
  configt.layerConfig.add_inputs();
  PriorBoxConfig* pb = input->mutable_priorbox_conf();
G
gaoyuan 已提交
46 47
  for (size_t i = 0; i < min_size.size(); i++) pb->add_min_size(min_size[i]);
  for (size_t i = 0; i < max_size.size(); i++) pb->add_max_size(max_size[i]);
G
gaoyuan 已提交
48
  for (size_t i = 0; i < variance.size(); i++) pb->add_variance(variance[i]);
G
gaoyuan 已提交
49 50
  for (size_t i = 0; i < aspect_ratio.size(); i++)
    pb->add_aspect_ratio(aspect_ratio[i]);
G
gaoyuan 已提交
51 52 53 54 55 56

  // data layer initialize
  std::vector<DataLayerPtr> dataLayers;
  LayerMap layerMap;
  vector<Argument> datas;
  initDataLayer(
G
gaoyuan 已提交
57 58 59 60 61
      configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, use_gpu);
  dataLayers[0]->getOutput().setFrameHeight(feature_map_height);
  dataLayers[0]->getOutput().setFrameWidth(feature_map_width);
  dataLayers[1]->getOutput().setFrameHeight(image_height);
  dataLayers[1]->getOutput().setFrameWidth(image_width);
G
gaoyuan 已提交
62 63 64 65 66 67 68 69 70 71 72 73

  // test layer initialize
  std::vector<ParameterPtr> parameters;
  LayerPtr priorboxLayer;
  initTestLayer(configt, &layerMap, &parameters, &priorboxLayer);
  priorboxLayer->forward(PASS_GC);
  checkMatrixEqual(priorboxLayer->getOutputValue(), result);
}

TEST(Layer, priorBoxLayerFwd) {
  vector<int> minSize;
  vector<int> maxSize;
G
gaoyuan 已提交
74 75
  vector<real> aspectRatio;
  vector<real> variance;
G
gaoyuan 已提交
76
  bool useGpu = false;
G
gaoyuan 已提交
77 78 79 80 81 82 83 84

  minSize.push_back(276);
  maxSize.push_back(330);
  variance.push_back(0.1);
  variance.push_back(0.1);
  variance.push_back(0.2);
  variance.push_back(0.2);

G
gaoyuan 已提交
85
  // CPU case 1.
G
gaoyuan 已提交
86
  MatrixPtr result;
G
gaoyuan 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  real resultData[] = {0.04,
                       0.04,
                       0.96,
                       0.96,
                       0.1,
                       0.1,
                       0.2,
                       0.2,
                       0,
                       0,
                       1,
                       1,
                       0.1,
                       0.1,
                       0.2,
                       0.2};
G
gaoyuan 已提交
103
  result = Matrix::create(1, 2 * 8, false, useGpu);
G
gaoyuan 已提交
104
  result->setData(resultData);
G
gaoyuan 已提交
105 106 107 108
  doOnePriorBoxTest(/* feature_map_width */ 1,
                    /* feature_map_height */ 1,
                    /* image_width */ 300,
                    /* image_height */ 300,
G
gaoyuan 已提交
109 110 111 112
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
G
gaoyuan 已提交
113
                    useGpu,
G
gaoyuan 已提交
114
                    result);
G
gaoyuan 已提交
115
  // CPU case 2.
G
gaoyuan 已提交
116 117 118
  variance[1] = 0.2;
  variance[3] = 0.1;
  maxSize.pop_back();
G
gaoyuan 已提交
119 120 121 122
  real resultData2[] = {0,     0,     0.595, 0.595, 0.1, 0.2, 0.2, 0.1,
                        0.405, 0,     1,     0.595, 0.1, 0.2, 0.2, 0.1,
                        0,     0.405, 0.595, 1,     0.1, 0.2, 0.2, 0.1,
                        0.405, 0.405, 1,     1,     0.1, 0.2, 0.2, 0.1};
G
gaoyuan 已提交
123
  Matrix::resizeOrCreate(result, 1, 4 * 8, false, useGpu);
G
gaoyuan 已提交
124
  result->setData(resultData2);
G
gaoyuan 已提交
125 126 127 128
  doOnePriorBoxTest(/* feature_map_width */ 2,
                    /* feature_map_height */ 2,
                    /* image_width */ 400,
                    /* image_height */ 400,
G
gaoyuan 已提交
129 130 131 132
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
G
gaoyuan 已提交
133
                    useGpu,
G
gaoyuan 已提交
134
                    result);
G
gaoyuan 已提交
135
  // CPU case 3.
G
gaoyuan 已提交
136
  aspectRatio.push_back(2);
G
gaoyuan 已提交
137 138 139 140
  real resultData3[] = {0.04,     0.04, 0.96, 0.96,       0.1,        0.2,
                        0.2,      0.1,  0,    0.17473088, 1,          0.825269,
                        0.1,      0.2,  0.2,  0.1,        0.17473088, 0,
                        0.825269, 1,    0.1,  0.2,        0.2,        0.1};
G
gaoyuan 已提交
141
  Matrix::resizeOrCreate(result, 1, 3 * 8, false, useGpu);
G
gaoyuan 已提交
142
  result->setData(resultData3);
G
gaoyuan 已提交
143 144 145 146
  doOnePriorBoxTest(/* feature_map_width */ 1,
                    /* feature_map_height */ 1,
                    /* image_width */ 300,
                    /* image_height */ 300,
G
gaoyuan 已提交
147 148 149 150
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
G
gaoyuan 已提交
151
                    useGpu,
G
gaoyuan 已提交
152
                    result);
G
gaoyuan 已提交
153

154
#ifdef PADDLE_WITH_CUDA
G
gaoyuan 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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
  // reset the input parameters
  variance[1] = 0.1;
  variance[3] = 0.2;
  maxSize.push_back(330);
  aspectRatio.pop_back();
  MatrixPtr resultGpu;
  useGpu = true;
  // GPU case 1.
  resultGpu = Matrix::create(1, 2 * 8, false, useGpu);
  resultGpu->copyFrom(resultData, 2 * 8);
  doOnePriorBoxTest(/* feature_map_width */ 1,
                    /* feature_map_height */ 1,
                    /* image_width */ 300,
                    /* image_height */ 300,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    resultGpu);
  // GPU case 2.
  variance[1] = 0.2;
  variance[3] = 0.1;
  maxSize.pop_back();
  Matrix::resizeOrCreate(resultGpu, 1, 4 * 8, false, useGpu);
  resultGpu->copyFrom(resultData2, 4 * 8);
  doOnePriorBoxTest(/* feature_map_width */ 2,
                    /* feature_map_height */ 2,
                    /* image_width */ 400,
                    /* image_height */ 400,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    resultGpu);
  // GPU case 3.
  aspectRatio.push_back(2);
  Matrix::resizeOrCreate(resultGpu, 1, 3 * 8, false, useGpu);
  resultGpu->copyFrom(resultData3, 3 * 8);
  doOnePriorBoxTest(/* feature_map_width */ 1,
                    /* feature_map_height */ 1,
                    /* image_width */ 300,
                    /* image_height */ 300,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    resultGpu);
#endif
G
gaoyuan 已提交
206 207 208 209 210 211 212
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  initMain(argc, argv);
  return RUN_ALL_TESTS();
}