From 9f990d9059dcf1b3536c3060670121a2fe67ce66 Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Fri, 16 Dec 2016 19:15:03 +0800 Subject: [PATCH] Add unittest of the priorbox layer --- paddle/gserver/layers/PriorBox.cpp | 1 + paddle/gserver/tests/CMakeLists.txt | 8 ++ paddle/gserver/tests/test_PriorBox.cpp | 160 +++++++++++++++++++++++++ python/paddle/trainer/config_parser.py | 3 +- 4 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 paddle/gserver/tests/test_PriorBox.cpp diff --git a/paddle/gserver/layers/PriorBox.cpp b/paddle/gserver/layers/PriorBox.cpp index dd52f61c3..ca61dfec5 100644 --- a/paddle/gserver/layers/PriorBox.cpp +++ b/paddle/gserver/layers/PriorBox.cpp @@ -76,6 +76,7 @@ void PriorBoxLayer::forward(PassType passType) { auto image = getInput(1); int imageWidth = image.getFrameWidth(); int imageHeight = image.getFrameHeight(); + float stepW = static_cast(imageWidth) / layerWidth; float stepH = static_cast(imageHeight) / layerHeight; int dim = layerHeight * layerWidth * numPriors_ * 4; diff --git a/paddle/gserver/tests/CMakeLists.txt b/paddle/gserver/tests/CMakeLists.txt index 34dc375f2..c26a2a7f0 100644 --- a/paddle/gserver/tests/CMakeLists.txt +++ b/paddle/gserver/tests/CMakeLists.txt @@ -34,6 +34,14 @@ add_unittest_without_exec(test_ConvTrans add_test(NAME test_ConvTrans COMMAND test_ConvTrans) +################# test_PriorBox ####################### +add_unittest_without_exec(test_PriorBox + test_PriorBox.cpp + LayerGradUtil.cpp + TestUtil.cpp) + +add_test(NAME test_PriorBox + COMMAND test_PriorBox) ################# test_ConvUnify ####################### add_unittest_without_exec(test_ConvUnify test_ConvUnify.cpp diff --git a/paddle/gserver/tests/test_PriorBox.cpp b/paddle/gserver/tests/test_PriorBox.cpp new file mode 100644 index 000000000..fd63be2f8 --- /dev/null +++ b/paddle/gserver/tests/test_PriorBox.cpp @@ -0,0 +1,160 @@ +/* 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 +#include + +#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 minSize, + vector maxSize, + vector aspectRatio, + vector 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 dataLayers; + LayerMap layerMap; + vector 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 parameters; + LayerPtr priorboxLayer; + initTestLayer(configt, &layerMap, ¶meters, &priorboxLayer); + + priorboxLayer->forward(PASS_GC); + checkMatrixEqual(priorboxLayer->getOutputValue(), result); +} + +TEST(Layer, priorBoxLayerFwd) { + vector minSize; + vector maxSize; + vector aspectRatio; + vector 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(); +} diff --git a/python/paddle/trainer/config_parser.py b/python/paddle/trainer/config_parser.py index 83fda9f70..4056ecdf7 100644 --- a/python/paddle/trainer/config_parser.py +++ b/python/paddle/trainer/config_parser.py @@ -1583,7 +1583,7 @@ class PriorBoxLayer(LayerBase): def __init__(self, name, inputs, size, min_size, max_size, aspect_ratio, variance): super(PriorBoxLayer, self).__init__(name, 'priorbox', 0, inputs) - config_assert(len(inputs) == 2, 'PriorBoxLayer must have 2 input') + config_assert(len(inputs) == 2, 'PriorBoxLayer must have 2 inputs') input_layer = self.get_input_layer(1) config_assert( input_layer.type == 'data', @@ -1591,6 +1591,7 @@ class PriorBoxLayer(LayerBase): 'a data layer') config_assert(input_layer.width > 0, 'The data layer must set width') config_assert(input_layer.height > 0, 'The data layer must set height') + config_assert(len(variance) == 4, 'The variance must have 4 inputs') self.config.inputs[0].priorbox_conf.min_size.extend(min_size) self.config.inputs[0].priorbox_conf.max_size.extend(max_size) self.config.inputs[0].priorbox_conf.aspect_ratio.extend(aspect_ratio) -- GitLab