提交 6f8f468f 编写于 作者: G gaoyuan

Add priorbox layer gpu unit test.

上级 7dfe3bdf
...@@ -24,14 +24,15 @@ using namespace std; // NOLINT ...@@ -24,14 +24,15 @@ using namespace std; // NOLINT
// Do one forward pass of priorBox layer and check to see if its output // Do one forward pass of priorBox layer and check to see if its output
// matches the given result // matches the given result
void doOnePriorBoxTest(size_t featureMapWidth, void doOnePriorBoxTest(size_t feature_map_width,
size_t featureMapHeight, size_t feature_map_height,
size_t imageWidth, size_t image_width,
size_t imageHeight, size_t image_height,
vector<int> minSize, vector<int> min_size,
vector<int> maxSize, vector<int> max_size,
vector<float> aspectRatio, vector<float> aspect_ratio,
vector<float> variance, vector<float> variance,
bool use_gpu,
MatrixPtr& result) { MatrixPtr& result) {
// Setting up the priorbox layer // Setting up the priorbox layer
TestConfig configt; TestConfig configt;
...@@ -42,28 +43,27 @@ void doOnePriorBoxTest(size_t featureMapWidth, ...@@ -42,28 +43,27 @@ void doOnePriorBoxTest(size_t featureMapWidth,
configt.inputDefs.push_back({INPUT_DATA, "image", 1, 0}); configt.inputDefs.push_back({INPUT_DATA, "image", 1, 0});
configt.layerConfig.add_inputs(); configt.layerConfig.add_inputs();
PriorBoxConfig* pb = input->mutable_priorbox_conf(); 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 < min_size.size(); i++) pb->add_min_size(min_size[i]);
for (size_t i = 0; i < maxSize.size(); i++) pb->add_max_size(maxSize[i]); for (size_t i = 0; i < max_size.size(); i++) pb->add_max_size(max_size[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]); for (size_t i = 0; i < variance.size(); i++) pb->add_variance(variance[i]);
for (size_t i = 0; i < aspect_ratio.size(); i++)
pb->add_aspect_ratio(aspect_ratio[i]);
// data layer initialize // data layer initialize
std::vector<DataLayerPtr> dataLayers; std::vector<DataLayerPtr> dataLayers;
LayerMap layerMap; LayerMap layerMap;
vector<Argument> datas; vector<Argument> datas;
initDataLayer( initDataLayer(
configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, false); configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, use_gpu);
dataLayers[0]->getOutput().setFrameHeight(featureMapHeight); dataLayers[0]->getOutput().setFrameHeight(feature_map_height);
dataLayers[0]->getOutput().setFrameWidth(featureMapWidth); dataLayers[0]->getOutput().setFrameWidth(feature_map_width);
dataLayers[1]->getOutput().setFrameHeight(imageHeight); dataLayers[1]->getOutput().setFrameHeight(image_height);
dataLayers[1]->getOutput().setFrameWidth(imageWidth); dataLayers[1]->getOutput().setFrameWidth(image_width);
// test layer initialize // test layer initialize
std::vector<ParameterPtr> parameters; std::vector<ParameterPtr> parameters;
LayerPtr priorboxLayer; LayerPtr priorboxLayer;
initTestLayer(configt, &layerMap, &parameters, &priorboxLayer); initTestLayer(configt, &layerMap, &parameters, &priorboxLayer);
priorboxLayer->forward(PASS_GC); priorboxLayer->forward(PASS_GC);
checkMatrixEqual(priorboxLayer->getOutputValue(), result); checkMatrixEqual(priorboxLayer->getOutputValue(), result);
} }
...@@ -73,6 +73,7 @@ TEST(Layer, priorBoxLayerFwd) { ...@@ -73,6 +73,7 @@ TEST(Layer, priorBoxLayerFwd) {
vector<int> maxSize; vector<int> maxSize;
vector<float> aspectRatio; vector<float> aspectRatio;
vector<float> variance; vector<float> variance;
bool useGpu = false;
minSize.push_back(276); minSize.push_back(276);
maxSize.push_back(330); maxSize.push_back(330);
...@@ -81,9 +82,8 @@ TEST(Layer, priorBoxLayerFwd) { ...@@ -81,9 +82,8 @@ TEST(Layer, priorBoxLayerFwd) {
variance.push_back(0.2); variance.push_back(0.2);
variance.push_back(0.2); variance.push_back(0.2);
// CPU case 1.
MatrixPtr result; MatrixPtr result;
result = Matrix::create(1, 2 * 8, false, false);
float resultData[] = {0.04, float resultData[] = {0.04,
0.04, 0.04,
0.96, 0.96,
...@@ -100,52 +100,109 @@ TEST(Layer, priorBoxLayerFwd) { ...@@ -100,52 +100,109 @@ TEST(Layer, priorBoxLayerFwd) {
0.1, 0.1,
0.2, 0.2,
0.2}; 0.2};
result = Matrix::create(1, 2 * 8, false, useGpu);
result->setData(resultData); result->setData(resultData);
doOnePriorBoxTest(/* featureMapWidth */ 1, doOnePriorBoxTest(/* feature_map_width */ 1,
/* featureMapHeight */ 1, /* feature_map_height */ 1,
/* imageWidth */ 300, /* image_width */ 300,
/* imageHeight */ 300, /* image_height */ 300,
minSize, minSize,
maxSize, maxSize,
aspectRatio, aspectRatio,
variance, variance,
useGpu,
result); result);
// CPU case 2.
variance[1] = 0.2; variance[1] = 0.2;
variance[3] = 0.1; variance[3] = 0.1;
maxSize.pop_back(); 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, 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.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, 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}; 0.405, 0.405, 1, 1, 0.1, 0.2, 0.2, 0.1};
Matrix::resizeOrCreate(result, 1, 4 * 8, false, useGpu);
result->setData(resultData2); result->setData(resultData2);
doOnePriorBoxTest(/* featureMapWidth */ 2, doOnePriorBoxTest(/* feature_map_width */ 2,
/* featureMapHeight */ 2, /* feature_map_height */ 2,
/* imageWidth */ 400, /* image_width */ 400,
/* imageHeight */ 400, /* image_height */ 400,
minSize, minSize,
maxSize, maxSize,
aspectRatio, aspectRatio,
variance, variance,
useGpu,
result); result);
// CPU case 3.
aspectRatio.push_back(2); 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, 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.2, 0.1, 0, 0.17473088, 1, 0.825269,
0.1, 0.2, 0.2, 0.1, 0.17473088, 0, 0.1, 0.2, 0.2, 0.1, 0.17473088, 0,
0.825269, 1, 0.1, 0.2, 0.2, 0.1}; 0.825269, 1, 0.1, 0.2, 0.2, 0.1};
Matrix::resizeOrCreate(result, 1, 3 * 8, false, useGpu);
result->setData(resultData3); result->setData(resultData3);
doOnePriorBoxTest(/* featureMapWidth */ 1, doOnePriorBoxTest(/* feature_map_width */ 1,
/* featureMapHeight */ 1, /* feature_map_height */ 1,
/* imageWidth */ 300, /* image_width */ 300,
/* imageHeight */ 300, /* image_height */ 300,
minSize, minSize,
maxSize, maxSize,
aspectRatio, aspectRatio,
variance, variance,
useGpu,
result); result);
#ifndef PADDLE_ONLY_CPU
// 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
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册