Skip to content

Commit 9f990d9

Browse files
author
gaoyuan
committed
Add unittest of the priorbox layer
1 parent 1048aee commit 9f990d9

File tree

4 files changed

+171
-1
lines changed

4 files changed

+171
-1
lines changed

paddle/gserver/layers/PriorBox.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void PriorBoxLayer::forward(PassType passType) {
7676
auto image = getInput(1);
7777
int imageWidth = image.getFrameWidth();
7878
int imageHeight = image.getFrameHeight();
79+
7980
float stepW = static_cast<float>(imageWidth) / layerWidth;
8081
float stepH = static_cast<float>(imageHeight) / layerHeight;
8182
int dim = layerHeight * layerWidth * numPriors_ * 4;

paddle/gserver/tests/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ add_unittest_without_exec(test_ConvTrans
3434

3535
add_test(NAME test_ConvTrans
3636
COMMAND test_ConvTrans)
37+
################# test_PriorBox #######################
38+
add_unittest_without_exec(test_PriorBox
39+
test_PriorBox.cpp
40+
LayerGradUtil.cpp
41+
TestUtil.cpp)
42+
43+
add_test(NAME test_PriorBox
44+
COMMAND test_PriorBox)
3745
################# test_ConvUnify #######################
3846
add_unittest_without_exec(test_ConvUnify
3947
test_ConvUnify.cpp
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <string>
16+
#include <vector>
17+
18+
#include "LayerGradUtil.h"
19+
#include "TestUtil.h"
20+
21+
using namespace paddle; // NOLINT
22+
using namespace std; // NOLINT
23+
24+
P_DECLARE_bool(use_gpu);
25+
P_DECLARE_int32(gpu_id);
26+
P_DECLARE_bool(thread_local_rand_use_global_seed);
27+
28+
// Do one forward pass of priorBox layer and check to see if its output
29+
// matches the given result
30+
void doOnePriorBoxTest(size_t featureMapWidth,
31+
size_t featureMapHeight,
32+
size_t imageWidth,
33+
size_t imageHeight,
34+
vector<int> minSize,
35+
vector<int> maxSize,
36+
vector<float> aspectRatio,
37+
vector<float> variance,
38+
MatrixPtr& result) {
39+
// Setting up the priorbox layer
40+
TestConfig configt;
41+
configt.layerConfig.set_type("priorbox");
42+
43+
configt.inputDefs.push_back({INPUT_DATA, "featureMap", 1, 0});
44+
LayerInputConfig* input = configt.layerConfig.add_inputs();
45+
configt.inputDefs.push_back({INPUT_DATA, "image", 1, 0});
46+
configt.layerConfig.add_inputs();
47+
PriorBoxConfig* pb = input->mutable_priorbox_conf();
48+
for (size_t i = 0; i < minSize.size(); i++) pb->add_min_size(minSize[i]);
49+
for (size_t i = 0; i < maxSize.size(); i++) pb->add_max_size(maxSize[i]);
50+
for (size_t i = 0; i < aspectRatio.size(); i++)
51+
pb->add_aspect_ratio(aspectRatio[i]);
52+
for (size_t i = 0; i < variance.size(); i++) pb->add_variance(variance[i]);
53+
54+
// data layer initialize
55+
std::vector<DataLayerPtr> dataLayers;
56+
LayerMap layerMap;
57+
vector<Argument> datas;
58+
initDataLayer(
59+
configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, true);
60+
dataLayers[0]->getOutput().setFrameHeight(featureMapHeight);
61+
dataLayers[0]->getOutput().setFrameWidth(featureMapWidth);
62+
dataLayers[1]->getOutput().setFrameHeight(imageHeight);
63+
dataLayers[1]->getOutput().setFrameWidth(imageWidth);
64+
65+
// test layer initialize
66+
std::vector<ParameterPtr> parameters;
67+
LayerPtr priorboxLayer;
68+
initTestLayer(configt, &layerMap, &parameters, &priorboxLayer);
69+
70+
priorboxLayer->forward(PASS_GC);
71+
checkMatrixEqual(priorboxLayer->getOutputValue(), result);
72+
}
73+
74+
TEST(Layer, priorBoxLayerFwd) {
75+
vector<int> minSize;
76+
vector<int> maxSize;
77+
vector<float> aspectRatio;
78+
vector<float> variance;
79+
80+
minSize.push_back(276);
81+
maxSize.push_back(330);
82+
variance.push_back(0.1);
83+
variance.push_back(0.1);
84+
variance.push_back(0.2);
85+
variance.push_back(0.2);
86+
87+
MatrixPtr result;
88+
result = Matrix::create(1, 2 * 8, false, false);
89+
90+
float resultData[] = {0.04,
91+
0.04,
92+
0.96,
93+
0.96,
94+
0.1,
95+
0.1,
96+
0.2,
97+
0.2,
98+
0,
99+
0,
100+
1,
101+
1,
102+
0.1,
103+
0.1,
104+
0.2,
105+
0.2};
106+
result->setData(resultData);
107+
doOnePriorBoxTest(/* featureMapWidth */ 1,
108+
/* featureMapHeight */ 1,
109+
/* imageWidth */ 300,
110+
/* imageHeight */ 300,
111+
minSize,
112+
maxSize,
113+
aspectRatio,
114+
variance,
115+
result);
116+
117+
variance[1] = 0.2;
118+
variance[3] = 0.1;
119+
maxSize.pop_back();
120+
Matrix::resizeOrCreate(result, 1, 4 * 8, false, false);
121+
float resultData2[] = {0, 0, 0.595, 0.595, 0.1, 0.2, 0.2, 0.1,
122+
0.405, 0, 1, 0.595, 0.1, 0.2, 0.2, 0.1,
123+
0, 0.405, 0.595, 1, 0.1, 0.2, 0.2, 0.1,
124+
0.405, 0.405, 1, 1, 0.1, 0.2, 0.2, 0.1};
125+
result->setData(resultData2);
126+
doOnePriorBoxTest(/* featureMapWidth */ 2,
127+
/* featureMapHeight */ 2,
128+
/* imageWidth */ 400,
129+
/* imageHeight */ 400,
130+
minSize,
131+
maxSize,
132+
aspectRatio,
133+
variance,
134+
result);
135+
136+
aspectRatio.push_back(2);
137+
Matrix::resizeOrCreate(result, 1, 3 * 8, false, false);
138+
float resultData3[] = {0.04, 0.04, 0.96, 0.96, 0.1, 0.2,
139+
0.2, 0.1, 0, 0.17473088, 1, 0.825269,
140+
0.1, 0.2, 0.2, 0.1, 0.17473088, 0,
141+
0.825269, 1, 0.1, 0.2, 0.2, 0.1};
142+
result->setData(resultData3);
143+
doOnePriorBoxTest(/* featureMapWidth */ 1,
144+
/* featureMapHeight */ 1,
145+
/* imageWidth */ 300,
146+
/* imageHeight */ 300,
147+
minSize,
148+
maxSize,
149+
aspectRatio,
150+
variance,
151+
result);
152+
}
153+
154+
int main(int argc, char** argv) {
155+
testing::InitGoogleTest(&argc, argv);
156+
initMain(argc, argv);
157+
FLAGS_thread_local_rand_use_global_seed = true;
158+
srand(1);
159+
return RUN_ALL_TESTS();
160+
}

python/paddle/trainer/config_parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,14 +1583,15 @@ class PriorBoxLayer(LayerBase):
15831583
def __init__(self, name, inputs, size, min_size, max_size, aspect_ratio,
15841584
variance):
15851585
super(PriorBoxLayer, self).__init__(name, 'priorbox', 0, inputs)
1586-
config_assert(len(inputs) == 2, 'PriorBoxLayer must have 2 input')
1586+
config_assert(len(inputs) == 2, 'PriorBoxLayer must have 2 inputs')
15871587
input_layer = self.get_input_layer(1)
15881588
config_assert(
15891589
input_layer.type == 'data',
15901590
'Expecting the second input layer of an priorbox layer to be '
15911591
'a data layer')
15921592
config_assert(input_layer.width > 0, 'The data layer must set width')
15931593
config_assert(input_layer.height > 0, 'The data layer must set height')
1594+
config_assert(len(variance) == 4, 'The variance must have 4 inputs')
15941595
self.config.inputs[0].priorbox_conf.min_size.extend(min_size)
15951596
self.config.inputs[0].priorbox_conf.max_size.extend(max_size)
15961597
self.config.inputs[0].priorbox_conf.aspect_ratio.extend(aspect_ratio)

0 commit comments

Comments
 (0)