|
| 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 <gtest/gtest.h> |
| 16 | +#include <string> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "LayerGradUtil.h" |
| 20 | +#include "TestUtil.h" |
| 21 | + |
| 22 | +using namespace paddle; // NOLINT |
| 23 | +using namespace std; // NOLINT |
| 24 | + |
| 25 | +// Do one forward pass of priorBox layer and check to see if its output |
| 26 | +// matches the given result |
| 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<real> aspect_ratio, |
| 34 | + vector<real> variance, |
| 35 | + bool use_gpu, |
| 36 | + MatrixPtr& result) { |
| 37 | + // Setting up the priorbox layer |
| 38 | + TestConfig configt; |
| 39 | + configt.layerConfig.set_type("priorbox"); |
| 40 | + |
| 41 | + configt.inputDefs.push_back({INPUT_DATA, "featureMap", 1, 0}); |
| 42 | + LayerInputConfig* input = configt.layerConfig.add_inputs(); |
| 43 | + configt.inputDefs.push_back({INPUT_DATA, "image", 1, 0}); |
| 44 | + configt.layerConfig.add_inputs(); |
| 45 | + PriorBoxConfig* pb = input->mutable_priorbox_conf(); |
| 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]); |
| 48 | + 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]); |
| 51 | + |
| 52 | + // data layer initialize |
| 53 | + std::vector<DataLayerPtr> dataLayers; |
| 54 | + LayerMap layerMap; |
| 55 | + vector<Argument> datas; |
| 56 | + initDataLayer( |
| 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); |
| 62 | + |
| 63 | + // test layer initialize |
| 64 | + std::vector<ParameterPtr> parameters; |
| 65 | + LayerPtr priorboxLayer; |
| 66 | + initTestLayer(configt, &layerMap, ¶meters, &priorboxLayer); |
| 67 | + priorboxLayer->forward(PASS_GC); |
| 68 | + checkMatrixEqual(priorboxLayer->getOutputValue(), result); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(Layer, priorBoxLayerFwd) { |
| 72 | + vector<int> minSize; |
| 73 | + vector<int> maxSize; |
| 74 | + vector<real> aspectRatio; |
| 75 | + vector<real> variance; |
| 76 | + bool useGpu = false; |
| 77 | + |
| 78 | + minSize.push_back(276); |
| 79 | + maxSize.push_back(330); |
| 80 | + variance.push_back(0.1); |
| 81 | + variance.push_back(0.1); |
| 82 | + variance.push_back(0.2); |
| 83 | + variance.push_back(0.2); |
| 84 | + |
| 85 | + // CPU case 1. |
| 86 | + MatrixPtr result; |
| 87 | + real resultData[] = {0.04, |
| 88 | + 0.04, |
| 89 | + 0.96, |
| 90 | + 0.96, |
| 91 | + 0.1, |
| 92 | + 0.1, |
| 93 | + 0.2, |
| 94 | + 0.2, |
| 95 | + 0, |
| 96 | + 0, |
| 97 | + 1, |
| 98 | + 1, |
| 99 | + 0.1, |
| 100 | + 0.1, |
| 101 | + 0.2, |
| 102 | + 0.2}; |
| 103 | + result = Matrix::create(1, 2 * 8, false, useGpu); |
| 104 | + result->setData(resultData); |
| 105 | + doOnePriorBoxTest(/* feature_map_width */ 1, |
| 106 | + /* feature_map_height */ 1, |
| 107 | + /* image_width */ 300, |
| 108 | + /* image_height */ 300, |
| 109 | + minSize, |
| 110 | + maxSize, |
| 111 | + aspectRatio, |
| 112 | + variance, |
| 113 | + useGpu, |
| 114 | + result); |
| 115 | + // CPU case 2. |
| 116 | + variance[1] = 0.2; |
| 117 | + variance[3] = 0.1; |
| 118 | + maxSize.pop_back(); |
| 119 | + real resultData2[] = {0, 0, 0.595, 0.595, 0.1, 0.2, 0.2, 0.1, |
| 120 | + 0.405, 0, 1, 0.595, 0.1, 0.2, 0.2, 0.1, |
| 121 | + 0, 0.405, 0.595, 1, 0.1, 0.2, 0.2, 0.1, |
| 122 | + 0.405, 0.405, 1, 1, 0.1, 0.2, 0.2, 0.1}; |
| 123 | + Matrix::resizeOrCreate(result, 1, 4 * 8, false, useGpu); |
| 124 | + result->setData(resultData2); |
| 125 | + doOnePriorBoxTest(/* feature_map_width */ 2, |
| 126 | + /* feature_map_height */ 2, |
| 127 | + /* image_width */ 400, |
| 128 | + /* image_height */ 400, |
| 129 | + minSize, |
| 130 | + maxSize, |
| 131 | + aspectRatio, |
| 132 | + variance, |
| 133 | + useGpu, |
| 134 | + result); |
| 135 | + // CPU case 3. |
| 136 | + aspectRatio.push_back(2); |
| 137 | + real resultData3[] = {0.04, 0.04, 0.96, 0.96, 0.1, 0.2, |
| 138 | + 0.2, 0.1, 0, 0.17473088, 1, 0.825269, |
| 139 | + 0.1, 0.2, 0.2, 0.1, 0.17473088, 0, |
| 140 | + 0.825269, 1, 0.1, 0.2, 0.2, 0.1}; |
| 141 | + Matrix::resizeOrCreate(result, 1, 3 * 8, false, useGpu); |
| 142 | + result->setData(resultData3); |
| 143 | + doOnePriorBoxTest(/* feature_map_width */ 1, |
| 144 | + /* feature_map_height */ 1, |
| 145 | + /* image_width */ 300, |
| 146 | + /* image_height */ 300, |
| 147 | + minSize, |
| 148 | + maxSize, |
| 149 | + aspectRatio, |
| 150 | + variance, |
| 151 | + useGpu, |
| 152 | + 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 |
| 206 | +} |
| 207 | + |
| 208 | +int main(int argc, char** argv) { |
| 209 | + testing::InitGoogleTest(&argc, argv); |
| 210 | + initMain(argc, argv); |
| 211 | + return RUN_ALL_TESTS(); |
| 212 | +} |
0 commit comments