From 6f8f468fdbfafafda1661b8002cfda76263cf9af Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Tue, 20 Dec 2016 16:28:41 +0800 Subject: [PATCH] Add priorbox layer gpu unit test. --- paddle/gserver/tests/test_PriorBox.cpp | 127 ++++++++++++++++++------- 1 file changed, 92 insertions(+), 35 deletions(-) diff --git a/paddle/gserver/tests/test_PriorBox.cpp b/paddle/gserver/tests/test_PriorBox.cpp index 1dab21218e4..19dfd0f065d 100644 --- a/paddle/gserver/tests/test_PriorBox.cpp +++ b/paddle/gserver/tests/test_PriorBox.cpp @@ -24,14 +24,15 @@ using namespace std; // NOLINT // 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, +void doOnePriorBoxTest(size_t feature_map_width, + size_t feature_map_height, + size_t image_width, + size_t image_height, + vector min_size, + vector max_size, + vector aspect_ratio, vector variance, + bool use_gpu, MatrixPtr& result) { // Setting up the priorbox layer TestConfig configt; @@ -42,28 +43,27 @@ void doOnePriorBoxTest(size_t featureMapWidth, 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 < min_size.size(); i++) pb->add_min_size(min_size[i]); + for (size_t i = 0; i < max_size.size(); i++) pb->add_max_size(max_size[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 std::vector dataLayers; LayerMap layerMap; vector datas; initDataLayer( - configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, false); - dataLayers[0]->getOutput().setFrameHeight(featureMapHeight); - dataLayers[0]->getOutput().setFrameWidth(featureMapWidth); - dataLayers[1]->getOutput().setFrameHeight(imageHeight); - dataLayers[1]->getOutput().setFrameWidth(imageWidth); + configt, &dataLayers, &datas, &layerMap, "priorbox", 1, false, use_gpu); + dataLayers[0]->getOutput().setFrameHeight(feature_map_height); + dataLayers[0]->getOutput().setFrameWidth(feature_map_width); + dataLayers[1]->getOutput().setFrameHeight(image_height); + dataLayers[1]->getOutput().setFrameWidth(image_width); // test layer initialize std::vector parameters; LayerPtr priorboxLayer; initTestLayer(configt, &layerMap, ¶meters, &priorboxLayer); - priorboxLayer->forward(PASS_GC); checkMatrixEqual(priorboxLayer->getOutputValue(), result); } @@ -73,6 +73,7 @@ TEST(Layer, priorBoxLayerFwd) { vector maxSize; vector aspectRatio; vector variance; + bool useGpu = false; minSize.push_back(276); maxSize.push_back(330); @@ -81,9 +82,8 @@ TEST(Layer, priorBoxLayerFwd) { variance.push_back(0.2); variance.push_back(0.2); + // CPU case 1. MatrixPtr result; - result = Matrix::create(1, 2 * 8, false, false); - float resultData[] = {0.04, 0.04, 0.96, @@ -100,52 +100,109 @@ TEST(Layer, priorBoxLayerFwd) { 0.1, 0.2, 0.2}; + result = Matrix::create(1, 2 * 8, false, useGpu); result->setData(resultData); - doOnePriorBoxTest(/* featureMapWidth */ 1, - /* featureMapHeight */ 1, - /* imageWidth */ 300, - /* imageHeight */ 300, + doOnePriorBoxTest(/* feature_map_width */ 1, + /* feature_map_height */ 1, + /* image_width */ 300, + /* image_height */ 300, minSize, maxSize, aspectRatio, variance, + useGpu, result); - + // CPU case 2. 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}; + Matrix::resizeOrCreate(result, 1, 4 * 8, false, useGpu); result->setData(resultData2); - doOnePriorBoxTest(/* featureMapWidth */ 2, - /* featureMapHeight */ 2, - /* imageWidth */ 400, - /* imageHeight */ 400, + doOnePriorBoxTest(/* feature_map_width */ 2, + /* feature_map_height */ 2, + /* image_width */ 400, + /* image_height */ 400, minSize, maxSize, aspectRatio, variance, + useGpu, result); - + // CPU case 3. 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}; + Matrix::resizeOrCreate(result, 1, 3 * 8, false, useGpu); result->setData(resultData3); - doOnePriorBoxTest(/* featureMapWidth */ 1, - /* featureMapHeight */ 1, - /* imageWidth */ 300, - /* imageHeight */ 300, + doOnePriorBoxTest(/* feature_map_width */ 1, + /* feature_map_height */ 1, + /* image_width */ 300, + /* image_height */ 300, minSize, maxSize, aspectRatio, variance, + useGpu, 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) { -- GitLab