test_PriorBox.cpp 5.7 KB
Newer Older
G
gaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

G
gaoyuan 已提交
15
#include <gtest/gtest.h>
G
gaoyuan 已提交
16 17
#include <string>
#include <vector>
G
gaoyuan 已提交
18 19 20 21 22
#include "ModelConfig.pb.h"
#include "paddle/gserver/layers/DataLayer.h"
#include "paddle/math/MathUtils.h"
#include "paddle/trainer/Trainer.h"
#include "paddle/utils/GlobalConstants.h"
G
gaoyuan 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

#include "LayerGradUtil.h"
#include "TestUtil.h"

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

P_DECLARE_bool(use_gpu);
P_DECLARE_int32(gpu_id);
P_DECLARE_bool(thread_local_rand_use_global_seed);

// Do one forward pass of priorBox layer and check to see if its output
// matches the given result
void doOnePriorBoxTest(size_t featureMapWidth,
                       size_t featureMapHeight,
                       size_t imageWidth,
                       size_t imageHeight,
                       vector<int> minSize,
                       vector<int> maxSize,
                       vector<float> aspectRatio,
                       vector<float> variance,
                       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();
  for (size_t i = 0; i < minSize.size(); i++) pb->add_min_size(minSize[i]);
  for (size_t i = 0; i < maxSize.size(); i++) pb->add_max_size(maxSize[i]);
  for (size_t i = 0; i < aspectRatio.size(); i++)
    pb->add_aspect_ratio(aspectRatio[i]);
  for (size_t i = 0; i < variance.size(); i++) pb->add_variance(variance[i]);

  // data layer initialize
  std::vector<DataLayerPtr> dataLayers;
  LayerMap layerMap;
  vector<Argument> datas;
  initDataLayer(
      configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, true);
  dataLayers[0]->getOutput().setFrameHeight(featureMapHeight);
  dataLayers[0]->getOutput().setFrameWidth(featureMapWidth);
  dataLayers[1]->getOutput().setFrameHeight(imageHeight);
  dataLayers[1]->getOutput().setFrameWidth(imageWidth);

  // 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;
  vector<float> aspectRatio;
  vector<float> variance;

  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);

  MatrixPtr result;
  result = Matrix::create(1, 2 * 8, false, false);

  float 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};
  result->setData(resultData);
  doOnePriorBoxTest(/* featureMapWidth */ 1,
                    /* featureMapHeight */ 1,
                    /* imageWidth */ 300,
                    /* imageHeight */ 300,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    result);

  variance[1] = 0.2;
  variance[3] = 0.1;
  maxSize.pop_back();
  Matrix::resizeOrCreate(result, 1, 4 * 8, false, false);
  float 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};
  result->setData(resultData2);
  doOnePriorBoxTest(/* featureMapWidth */ 2,
                    /* featureMapHeight */ 2,
                    /* imageWidth */ 400,
                    /* imageHeight */ 400,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    result);

  aspectRatio.push_back(2);
  Matrix::resizeOrCreate(result, 1, 3 * 8, false, false);
  float 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};
  result->setData(resultData3);
  doOnePriorBoxTest(/* featureMapWidth */ 1,
                    /* featureMapHeight */ 1,
                    /* imageWidth */ 300,
                    /* imageHeight */ 300,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    result);
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  initMain(argc, argv);
  FLAGS_thread_local_rand_use_global_seed = true;
  srand(1);
  return RUN_ALL_TESTS();
}