conf_parser.h 11.3 KB
Newer Older
1 2 3 4 5 6
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8 9 10 11 12 13 14 15
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
16
#include <yaml-cpp/yaml.h>
17 18 19 20
#include <iostream>
#include <vector>
#include <string>
#include <map>
B
Bin Long 已提交
21
#include <paddle_inference_api.h>
22 23 24

namespace PaddleSolution {

25 26
class PaddleModelConfigPaser {
    std::map<std::string, int> _scaling_map;
27

28 29 30 31 32 33
 public:
    PaddleModelConfigPaser()
        :_class_num(0),
        _channels(0),
        _use_gpu(0),
        _batch_size(1),
B
Bin Long 已提交
34
        _enable_trt(false),
35 36 37 38 39 40 41
        _target_short_size(0),
        _model_file_name("__model__"),
        _param_file_name("__params__"),
        _scaling_map{{"UNPADDING", 0},
                     {"RANGE_SCALING", 1}},
        _feeds_size(1),
    _coarsest_stride(1) {}
42

43
    ~PaddleModelConfigPaser() {}
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    void reset() {
        _crop_size.clear();
        _resize.clear();
        _mean.clear();
        _std.clear();
        _img_type.clear();
        _class_num = 0;
        _channels = 0;
        _use_gpu = 0;
        _target_short_size = 0;
        _batch_size = 1;
        _model_file_name = "__model__";
        _model_path = "./";
        _param_file_name = "__params__";
        _resize_type = 0;
        _resize_max_size = 0;
        _feeds_size = 1;
         _coarsest_stride = 1;
B
Bin Long 已提交
63
         _enable_trt = false;
64 65 66 67 68 69 70 71 72 73
    }

    std::string process_parenthesis(const std::string& str) {
        if (str.size() < 2) {
            return str;
        }
        std::string nstr(str);
        if (str[0] == '(' && str.back() == ')') {
            nstr[0] = '[';
            nstr[str.size() - 1] = ']';
74
        }
75 76
        return nstr;
    }
77

78 79 80 81 82 83 84 85 86
    template <typename T>
    std::vector<T> parse_str_to_vec(const std::string& str) {
        std::vector<T> data;
        auto node = YAML::Load(str);
        for (const auto& item : node) {
            data.push_back(item.as<T>());
        }
        return data;
    }
87

88 89 90 91 92 93 94 95 96 97
    bool load_config(const std::string& conf_file) {
        reset();
        YAML::Node config;
        try {
            config = YAML::LoadFile(conf_file);
        } catch(...) {
            return false;
        }
        // 1. get resize
        if (config["DEPLOY"]["EVAL_CROP_SIZE"].IsDefined()) {
98 99
            auto str = config["DEPLOY"]["EVAL_CROP_SIZE"].as<std::string>();
            _resize = parse_str_to_vec<int>(process_parenthesis(str));
100 101 102 103 104 105 106 107 108 109 110
        } else {
            std::cerr << "Please set EVAL_CROP_SIZE: (xx, xx)" << std::endl;
            return false;
        }
        // 0. get crop_size
        if (config["DEPLOY"]["CROP_SIZE"].IsDefined()) {
            auto crop_str = config["DEPLOY"]["CROP_SIZE"].as<std::string>();
             _crop_size = parse_str_to_vec<int>(process_parenthesis(crop_str));
        } else {
            _crop_size = _resize;
        }
111

112 113
        // 2. get mean
        if (config["DEPLOY"]["MEAN"].IsDefined()) {
114 115 116
            for (const auto& item : config["DEPLOY"]["MEAN"]) {
                _mean.push_back(item.as<float>());
            }
117 118 119 120 121 122
        } else {
            std::cerr << "Please set MEAN: [xx, xx, xx]" << std::endl;
            return false;
        }
        // 3. get std
        if(config["DEPLOY"]["STD"].IsDefined()) {
123 124 125
            for (const auto& item : config["DEPLOY"]["STD"]) {
                _std.push_back(item.as<float>());
            }
126 127 128 129 130 131
        } else {
            std::cerr << "Please set STD: [xx, xx, xx]" << std::endl;
            return false;
        }
        // 4. get image type
        if (config["DEPLOY"]["IMAGE_TYPE"].IsDefined()) {
132
            _img_type = config["DEPLOY"]["IMAGE_TYPE"].as<std::string>();
133 134 135 136 137 138
        } else {
            std::cerr << "Please set IMAGE_TYPE: \"rgb\" or \"rgba\"" << std::endl;
            return false;
        }
        // 5. get class number
        if (config["DEPLOY"]["NUM_CLASSES"].IsDefined()) {
139
            _class_num = config["DEPLOY"]["NUM_CLASSES"].as<int>();
140 141 142 143 144 145
        } else {
            std::cerr << "Please set NUM_CLASSES: x" << std::endl;
            return false;
        }
        // 7. set model path
        if (config["DEPLOY"]["MODEL_PATH"].IsDefined()) {
146
            _model_path = config["DEPLOY"]["MODEL_PATH"].as<std::string>();
147 148 149 150 151 152
        } else {
            std::cerr << "Please set MODEL_PATH: \"/path/to/model_dir\"" << std::endl;
            return false;
        }
        // 8. get model file_name
        if (config["DEPLOY"]["MODEL_FILENAME"].IsDefined()) {
153
            _model_file_name = config["DEPLOY"]["MODEL_FILENAME"].as<std::string>();
154 155 156 157 158 159 160 161 162 163 164 165
        } else {
            _model_file_name = "__model__";
        }
        // 9. get model param file name
        if (config["DEPLOY"]["PARAMS_FILENAME"].IsDefined()) {
            _param_file_name
                = config["DEPLOY"]["PARAMS_FILENAME"].as<std::string>();
        } else {
            _param_file_name = "__params__";
        }
        // 10. get pre_processor
        if (config["DEPLOY"]["PRE_PROCESSOR"].IsDefined()) {
166
            _pre_processor = config["DEPLOY"]["PRE_PROCESSOR"].as<std::string>();
167 168 169 170 171 172
        } else {
            std::cerr << "Please set PRE_PROCESSOR: \"DetectionPreProcessor\"" << std::endl;
            return false;
        }
        // 11. use_gpu
        if (config["DEPLOY"]["USE_GPU"].IsDefined()) { 
173
            _use_gpu = config["DEPLOY"]["USE_GPU"].as<int>();
174 175 176 177 178
        } else {
            _use_gpu = 0;
        }
        // 12. predictor_mode
        if (config["DEPLOY"]["PREDICTOR_MODE"].IsDefined()) {
179
            _predictor_mode = config["DEPLOY"]["PREDICTOR_MODE"].as<std::string>();
180 181 182 183 184 185
        } else {
            std::cerr << "Please set PREDICTOR_MODE: \"NATIVE\" or \"ANALYSIS\""  << std::endl;
            return false;
        }
        // 13. batch_size
        if (config["DEPLOY"]["BATCH_SIZE"].IsDefined()) {
186
            _batch_size = config["DEPLOY"]["BATCH_SIZE"].as<int>();
187 188 189 190 191
        } else {
            _batch_size = 1;
        }
        // 14. channels
        if (config["DEPLOY"]["CHANNELS"].IsDefined()) {
192
            _channels = config["DEPLOY"]["CHANNELS"].as<int>();
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        } else {
            std::cerr << "Please set CHANNELS: x"  << std::endl;
            return false;
        }
        // 15. target_short_size
        if (config["DEPLOY"]["TARGET_SHORT_SIZE"].IsDefined()) {
           _target_short_size = config["DEPLOY"]["TARGET_SHORT_SIZE"].as<int>();
        }
        // 16.resize_type
        if (config["DEPLOY"]["RESIZE_TYPE"].IsDefined() &&
            _scaling_map.find(config["DEPLOY"]["RESIZE_TYPE"].as<std::string>()) != _scaling_map.end()) {
            _resize_type = _scaling_map[config["DEPLOY"]["RESIZE_TYPE"].as<std::string>()];
        } else {
            _resize_type = 0;
        }
        // 17.resize_max_size
        if (config["DEPLOY"]["RESIZE_MAX_SIZE"].IsDefined()) {
            _resize_max_size = config["DEPLOY"]["RESIZE_MAX_SIZE"].as<int>();
211
        }
212 213 214 215 216 217 218 219
        // 18.feeds_size
        if (config["DEPLOY"]["FEEDS_SIZE"].IsDefined()) {
            _feeds_size = config["DEPLOY"]["FEEDS_SIZE"].as<int>();
        }
        // 19. coarsest_stride
        if (config["DEPLOY"]["COARSEST_STRIDE"].IsDefined()) {
            _coarsest_stride = config["DEPLOY"]["COARSEST_STRIDE"].as<int>();
        }
B
Bin Long 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        // 20. enable_trt
        if (config["DEPLOY"]["USE_TRT"].IsDefined()) {
            _enable_trt = config["DEPLOY"]["USE_TRT"].as<int>();
            _enable_trt &= _use_gpu;
        } else {
            _enable_trt = false;
        }
        if (_enable_trt) {
            std::string trt_mode = "";
            if (config["DEPLOY"]["TRT_MODE"].IsDefined()) {
                trt_mode = config["DEPLOY"]["TRT_MODE"].as<std::string>();
            } else {
                trt_mode = "FP32";
            }

            if (trt_mode == "FP16") {
                _trt_precision = paddle::AnalysisConfig::Precision::kHalf;
            } else if (trt_mode == "FP32") {
                _trt_precision = paddle::AnalysisConfig::Precision::kFloat32;
            } else if (trt_mode == "INT8") {
                _trt_precision = paddle::AnalysisConfig::Precision::kInt8;
            } else {
                _enable_trt = false;
            }
        }
        if (_predictor_mode == "NATIVE") {
            _enable_trt = false;
        }
248 249
        return true;
    }
250

251 252 253
    void debug() const {
        std::cout << "SCALE_RESIZE: (" << _resize[0] << ", "
                  << _resize[1] << ")" << std::endl;
254

255 256 257 258 259 260
        std::cout << "MEAN: [";
        for (int i = 0; i < _mean.size(); ++i) {
            if (i != _mean.size() - 1) {
                std::cout << _mean[i] << ", ";
            } else {
                std::cout << _mean[i];
261
            }
262 263
        }
        std::cout << "]" << std::endl;
264

265 266 267 268 269 270
        std::cout << "STD: [";
        for (int i = 0; i < _std.size(); ++i) {
            if (i != _std.size() - 1) {
                std::cout << _std[i] << ", ";
            } else {
                std::cout << _std[i];
271
            }
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        }
        std::cout << "]" << std::endl;
        std::cout << "DEPLOY.TARGET_SHORT_SIZE: " << _target_short_size
                  << std::endl;
        std::cout << "DEPLOY.IMAGE_TYPE: " << _img_type << std::endl;
        std::cout << "DEPLOY.NUM_CLASSES: " << _class_num << std::endl;
        std::cout << "DEPLOY.CHANNELS: " << _channels << std::endl;
        std::cout << "DEPLOY.MODEL_PATH: " << _model_path << std::endl;
        std::cout << "DEPLOY.MODEL_FILENAME: " << _model_file_name
                  << std::endl;
        std::cout << "DEPLOY.PARAMS_FILENAME: " << _param_file_name
                  << std::endl;
        std::cout << "DEPLOY.PRE_PROCESSOR: " << _pre_processor << std::endl;
        std::cout << "DEPLOY.USE_GPU: " << _use_gpu << std::endl;
        std::cout << "DEPLOY.PREDICTOR_MODE: " << _predictor_mode << std::endl;
        std::cout << "DEPLOY.BATCH_SIZE: " << _batch_size << std::endl;
    }
    // DEPLOY.COARSEST_STRIDE
    int _coarsest_stride;
    // DEPLOY.FEEDS_SIZE
    int _feeds_size;
    // DEPLOY.RESIZE_TYPE  0:unpadding 1:rangescaling  Default:0
    int _resize_type;
    // DEPLOY.RESIZE_MAX_SIZE
    int _resize_max_size;
    // DEPLOY.CROP_SIZE
    std::vector<int> _crop_size;
    // DEPLOY.SCALE_RESIZE
    std::vector<int> _resize;
    // DEPLOY.MEAN
    std::vector<float> _mean;
    // DEPLOY.STD
    std::vector<float> _std;
    // DEPLOY.IMAGE_TYPE
    std::string _img_type;
    // DEPLOY.TARGET_SHORT_SIZE
    int _target_short_size;
    // DEPLOY.NUM_CLASSES
    int _class_num;
    // DEPLOY.CHANNELS
    int _channels;
    // DEPLOY.MODEL_PATH
    std::string _model_path;
    // DEPLOY.MODEL_FILENAME
    std::string _model_file_name;
    // DEPLOY.PARAMS_FILENAME
    std::string _param_file_name;
    // DEPLOY.PRE_PROCESSOR
    std::string _pre_processor;
    // DEPLOY.USE_GPU
    int _use_gpu;
    // DEPLOY.PREDICTOR_MODE
    std::string _predictor_mode;
    // DEPLOY.BATCH_SIZE
    int _batch_size;
B
Bin Long 已提交
327 328 329 330
    // bool enable_trt
    bool _enable_trt;
    // TRT Precision
    paddle::AnalysisConfig::Precision _trt_precision;
331 332
};
}  // namespace PaddleSolution