Skip to content

Commit 6f8f468

Browse files
author
gaoyuan
committed
Add priorbox layer gpu unit test.
1 parent 7dfe3bd commit 6f8f468

File tree

1 file changed

+92
-35
lines changed

1 file changed

+92
-35
lines changed

paddle/gserver/tests/test_PriorBox.cpp

Lines changed: 92 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ using namespace std; // NOLINT
2424

2525
// Do one forward pass of priorBox layer and check to see if its output
2626
// matches the given result
27-
void doOnePriorBoxTest(size_t featureMapWidth,
28-
size_t featureMapHeight,
29-
size_t imageWidth,
30-
size_t imageHeight,
31-
vector<int> minSize,
32-
vector<int> maxSize,
33-
vector<float> aspectRatio,
27+
void doOnePriorBoxTest(size_t feature_map_width,
28+
size_t feature_map_height,
29+
size_t image_width,
30+
size_t image_height,
31+
vector<int> min_size,
32+
vector<int> max_size,
33+
vector<float> aspect_ratio,
3434
vector<float> variance,
35+
bool use_gpu,
3536
MatrixPtr& result) {
3637
// Setting up the priorbox layer
3738
TestConfig configt;
@@ -42,28 +43,27 @@ void doOnePriorBoxTest(size_t featureMapWidth,
4243
configt.inputDefs.push_back({INPUT_DATA, "image", 1, 0});
4344
configt.layerConfig.add_inputs();
4445
PriorBoxConfig* pb = input->mutable_priorbox_conf();
45-
for (size_t i = 0; i < minSize.size(); i++) pb->add_min_size(minSize[i]);
46-
for (size_t i = 0; i < maxSize.size(); i++) pb->add_max_size(maxSize[i]);
47-
for (size_t i = 0; i < aspectRatio.size(); i++)
48-
pb->add_aspect_ratio(aspectRatio[i]);
46+
for (size_t i = 0; i < min_size.size(); i++) pb->add_min_size(min_size[i]);
47+
for (size_t i = 0; i < max_size.size(); i++) pb->add_max_size(max_size[i]);
4948
for (size_t i = 0; i < variance.size(); i++) pb->add_variance(variance[i]);
49+
for (size_t i = 0; i < aspect_ratio.size(); i++)
50+
pb->add_aspect_ratio(aspect_ratio[i]);
5051

5152
// data layer initialize
5253
std::vector<DataLayerPtr> dataLayers;
5354
LayerMap layerMap;
5455
vector<Argument> datas;
5556
initDataLayer(
56-
configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, false);
57-
dataLayers[0]->getOutput().setFrameHeight(featureMapHeight);
58-
dataLayers[0]->getOutput().setFrameWidth(featureMapWidth);
59-
dataLayers[1]->getOutput().setFrameHeight(imageHeight);
60-
dataLayers[1]->getOutput().setFrameWidth(imageWidth);
57+
configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, use_gpu);
58+
dataLayers[0]->getOutput().setFrameHeight(feature_map_height);
59+
dataLayers[0]->getOutput().setFrameWidth(feature_map_width);
60+
dataLayers[1]->getOutput().setFrameHeight(image_height);
61+
dataLayers[1]->getOutput().setFrameWidth(image_width);
6162

6263
// test layer initialize
6364
std::vector<ParameterPtr> parameters;
6465
LayerPtr priorboxLayer;
6566
initTestLayer(configt, &layerMap, &parameters, &priorboxLayer);
66-
6767
priorboxLayer->forward(PASS_GC);
6868
checkMatrixEqual(priorboxLayer->getOutputValue(), result);
6969
}
@@ -73,6 +73,7 @@ TEST(Layer, priorBoxLayerFwd) {
7373
vector<int> maxSize;
7474
vector<float> aspectRatio;
7575
vector<float> variance;
76+
bool useGpu = false;
7677

7778
minSize.push_back(276);
7879
maxSize.push_back(330);
@@ -81,9 +82,8 @@ TEST(Layer, priorBoxLayerFwd) {
8182
variance.push_back(0.2);
8283
variance.push_back(0.2);
8384

85+
// CPU case 1.
8486
MatrixPtr result;
85-
result = Matrix::create(1, 2 * 8, false, false);
86-
8787
float resultData[] = {0.04,
8888
0.04,
8989
0.96,
@@ -100,52 +100,109 @@ TEST(Layer, priorBoxLayerFwd) {
100100
0.1,
101101
0.2,
102102
0.2};
103+
result = Matrix::create(1, 2 * 8, false, useGpu);
103104
result->setData(resultData);
104-
doOnePriorBoxTest(/* featureMapWidth */ 1,
105-
/* featureMapHeight */ 1,
106-
/* imageWidth */ 300,
107-
/* imageHeight */ 300,
105+
doOnePriorBoxTest(/* feature_map_width */ 1,
106+
/* feature_map_height */ 1,
107+
/* image_width */ 300,
108+
/* image_height */ 300,
108109
minSize,
109110
maxSize,
110111
aspectRatio,
111112
variance,
113+
useGpu,
112114
result);
113-
115+
// CPU case 2.
114116
variance[1] = 0.2;
115117
variance[3] = 0.1;
116118
maxSize.pop_back();
117-
Matrix::resizeOrCreate(result, 1, 4 * 8, false, false);
118119
float resultData2[] = {0, 0, 0.595, 0.595, 0.1, 0.2, 0.2, 0.1,
119120
0.405, 0, 1, 0.595, 0.1, 0.2, 0.2, 0.1,
120121
0, 0.405, 0.595, 1, 0.1, 0.2, 0.2, 0.1,
121122
0.405, 0.405, 1, 1, 0.1, 0.2, 0.2, 0.1};
123+
Matrix::resizeOrCreate(result, 1, 4 * 8, false, useGpu);
122124
result->setData(resultData2);
123-
doOnePriorBoxTest(/* featureMapWidth */ 2,
124-
/* featureMapHeight */ 2,
125-
/* imageWidth */ 400,
126-
/* imageHeight */ 400,
125+
doOnePriorBoxTest(/* feature_map_width */ 2,
126+
/* feature_map_height */ 2,
127+
/* image_width */ 400,
128+
/* image_height */ 400,
127129
minSize,
128130
maxSize,
129131
aspectRatio,
130132
variance,
133+
useGpu,
131134
result);
132-
135+
// CPU case 3.
133136
aspectRatio.push_back(2);
134-
Matrix::resizeOrCreate(result, 1, 3 * 8, false, false);
135137
float resultData3[] = {0.04, 0.04, 0.96, 0.96, 0.1, 0.2,
136138
0.2, 0.1, 0, 0.17473088, 1, 0.825269,
137139
0.1, 0.2, 0.2, 0.1, 0.17473088, 0,
138140
0.825269, 1, 0.1, 0.2, 0.2, 0.1};
141+
Matrix::resizeOrCreate(result, 1, 3 * 8, false, useGpu);
139142
result->setData(resultData3);
140-
doOnePriorBoxTest(/* featureMapWidth */ 1,
141-
/* featureMapHeight */ 1,
142-
/* imageWidth */ 300,
143-
/* imageHeight */ 300,
143+
doOnePriorBoxTest(/* feature_map_width */ 1,
144+
/* feature_map_height */ 1,
145+
/* image_width */ 300,
146+
/* image_height */ 300,
144147
minSize,
145148
maxSize,
146149
aspectRatio,
147150
variance,
151+
useGpu,
148152
result);
153+
154+
#ifndef PADDLE_ONLY_CPU
155+
// reset the input parameters
156+
variance[1] = 0.1;
157+
variance[3] = 0.2;
158+
maxSize.push_back(330);
159+
aspectRatio.pop_back();
160+
MatrixPtr resultGpu;
161+
useGpu = true;
162+
// GPU case 1.
163+
resultGpu = Matrix::create(1, 2 * 8, false, useGpu);
164+
resultGpu->copyFrom(resultData, 2 * 8);
165+
doOnePriorBoxTest(/* feature_map_width */ 1,
166+
/* feature_map_height */ 1,
167+
/* image_width */ 300,
168+
/* image_height */ 300,
169+
minSize,
170+
maxSize,
171+
aspectRatio,
172+
variance,
173+
useGpu,
174+
resultGpu);
175+
// GPU case 2.
176+
variance[1] = 0.2;
177+
variance[3] = 0.1;
178+
maxSize.pop_back();
179+
Matrix::resizeOrCreate(resultGpu, 1, 4 * 8, false, useGpu);
180+
resultGpu->copyFrom(resultData2, 4 * 8);
181+
doOnePriorBoxTest(/* feature_map_width */ 2,
182+
/* feature_map_height */ 2,
183+
/* image_width */ 400,
184+
/* image_height */ 400,
185+
minSize,
186+
maxSize,
187+
aspectRatio,
188+
variance,
189+
useGpu,
190+
resultGpu);
191+
// GPU case 3.
192+
aspectRatio.push_back(2);
193+
Matrix::resizeOrCreate(resultGpu, 1, 3 * 8, false, useGpu);
194+
resultGpu->copyFrom(resultData3, 3 * 8);
195+
doOnePriorBoxTest(/* feature_map_width */ 1,
196+
/* feature_map_height */ 1,
197+
/* image_width */ 300,
198+
/* image_height */ 300,
199+
minSize,
200+
maxSize,
201+
aspectRatio,
202+
variance,
203+
useGpu,
204+
resultGpu);
205+
#endif
149206
}
150207

151208
int main(int argc, char** argv) {

0 commit comments

Comments
 (0)