Skip to content

Commit 33030fa

Browse files
author
wangsiyuan06
committed
coding paser
1 parent 373a485 commit 33030fa

18 files changed

+258
-71
lines changed

commit-prepare.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
path=$(cd `dirname $0`; pwd)
2+
cd $path
3+
4+
pip install pre-commit
5+
pip install yapf
6+
pre-commit install
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <glog/logging.h>
2+
3+
#include "yaml-cpp/yaml.h"
4+
#include "include/deploy/config.h"
5+
#include "include/deploy/blob.h"
6+
7+
#include <opencv2/core/core.hpp>
8+
#include <opencv2/highgui/highgui.hpp>
9+
#include <opencv2/imgproc/imgproc.hpp>
10+
11+
12+
DEFINE_string(model_dir, "", "Path of inference model");
13+
DEFINE_string(cfg_file, "", "Path of yaml file");
14+
DEFINE_string(pp_type, "", "Type of Paddle toolkit");
15+
DEFINE_string(image, "", "Path of test image file");
16+
17+
int main(int argc, char** argv) {
18+
// Parsing command-line
19+
google::ParseCommandLineFlags(&argc, &argv, true);
20+
//paser yaml file
21+
Deploy::ConfigPaser paser;
22+
paser.Load(FLAGS_cfg_file);
23+
24+
// data preprocess
25+
// preprocess init
26+
Deploy::DetPreprocess detpreprocess;
27+
detpreprocess.Init(config)
28+
if (FLAGS_image_list != "") {
29+
//img_list
30+
}
31+
else {
32+
//read image
33+
std::vector<cv::mat> imgs
34+
cv::mat img;
35+
img = cv::imread(FLAGS_cfg_image);
36+
imgs.push_back(img)
37+
//create inpus and shape_traces
38+
std::vector<Deploy::ShapeInfo> shape_traces;
39+
std::vector<std::vector<Deploy::DataBlob>> inputs;
40+
//preprocess
41+
detpreprocess.Run(imgs, &inputs, &shape_traces);
42+
//infer
43+
std::vector<std::vector<Deploy::Datablob>> outputs;
44+
Deploy::PpInferenceEngine ppi_engine;
45+
Deploy::PpiConfig ppi_config;
46+
ppi_engine.Init(FLAGS_model_dir, ppi_config);
47+
ppi_engine.Infer(inputs, &outputs);
48+
49+
//postprocess
50+
Deploy::DetPreprocess detpostprocess;
51+
detpostprocess.Init(config)
52+
std::vector<Deploy::DetResult> detresults;
53+
detpostprocess.Run(detresults, shape_traces)
54+
}
55+
}

cpp/include/config.h

-11
This file was deleted.

cpp/include/blob.h cpp/include/deploy/common/blob.h

+8-19
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
include <vector>
44
include <map>
55

6-
class InBlob{
6+
namespace Deploy {
7+
8+
class DataBlob{
79
public:
810
// data
9-
template <typename T>
10-
std::vector<T> data;
11-
11+
char *data;
12+
1213
// data name
1314
std::string name;
1415

@@ -20,21 +21,7 @@ class InBlob{
2021

2122
}
2223

23-
24-
class OutBlob{
25-
public:
26-
// output data
27-
char *output_data;
28-
29-
// output name
30-
std::string name;
31-
32-
// output shape
33-
std::vector<int> shape;
34-
}
35-
36-
37-
class ShapeBlob{
24+
class ShapeInfo{
3825
public:
3926

4027
// shape trace
@@ -44,3 +31,5 @@ class ShapeBlob{
4431
std::vector<std::string> transform_order;
4532

4633
}
34+
35+
}//namespace

cpp/include/deploy/common/config.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include "yaml-cpp/yaml.h"
6+
7+
namespace Deploy {
8+
9+
class ConfigPaser {
10+
public:
11+
ConfigPaser() {}
12+
13+
~ConfigPaser() {}
14+
15+
bool Load(const std::string &cfg_file, const std::string &pp_type);
16+
17+
18+
private:
19+
bool Det_paser(const YAML::Node &det_config);
20+
bool Det_paser_transforms(const YAML::Node &preprocess_op);
21+
22+
23+
YAML::Node config_;
24+
}
25+
26+
}//namespace
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <opencv2/core/core.hpp>
4+
#include <opencv2/highgui/highgui.hpp>
5+
#include <opencv2/imgproc/imgproc.hpp>
6+
7+
#include "blob.h"
8+
9+
class Transform {
10+
public:
11+
12+
virtual void Init(const YAML::Node& item) = 0;
13+
14+
virtual void Shape_infer(ShapeBlob* shape) = 0;
15+
16+
virtual bool Run(cv::Mat* im) = 0;
17+
};

cpp/include/engine_config.h cpp/include/deploy/engine_config.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#pragma once
22

33

4-
class Engine_config{
4+
class EngineConfig{
55

66
}
77

8-
class PPI_onfig:Engine_cofig{
8+
class PpiConfig:EngineConfig{
99
public:
1010
bool use_trt;
1111
bool use_gpu;
File renamed without changes.

cpp/include/ppinference_engine.h cpp/include/deploy/ppinference_engine.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
#include "paddle_inference_api.h"
66

7-
class PPInference_Engine{
7+
class PpInferenceEngine{
88
public:
99
void Init(std::string model_dir, PPI_config &config)
1010

11-
void Infer(std::vector<std::map<std::string,InBlob>> &inputs, std::vector<OutBlob> *outputs)
11+
void Infer(std::vector<std::vector<std::DateBlob,DateBlob>> &inputs, std::vector<std::vector<DateBlob>> *outputs)
1212
private:
1313
std::unique_ptr<paddle::PaddlePredictor> predictor;
1414
}
File renamed without changes.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <vector>
4+
5+
6+
#include "deploy/blob.h"
7+
#include "transform.h"
8+
#include "config.h"
9+
10+
class BasePreprocess {
11+
public:
12+
BasePreprocess() {}
13+
14+
~BasePreprocess() {}
15+
16+
virtual bool Init(const YAML::Node &config);
17+
18+
virtual bool Run(const std::vector<cv::mat> &imgs, std::vector<std::vector<Inblob>> *inputs, std::vector<ShapeInfo> *shape_traces);
19+
20+
bool BuildTransform();
21+
private:
22+
23+
std::shared_ptr<Transform> CreateTransform(const std::string& name);
24+
25+
std::vector<std::shared_ptr<Transform>> transforms_;
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include "preprocess.h"
4+
5+
6+
namespace Deploy {
7+
8+
class DetProecess:BaseProcess {
9+
private:
10+
11+
}
12+
}
File renamed without changes.

cpp/include/preprocess.h

-24
This file was deleted.

cpp/include/transforms.h

-13
This file was deleted.

cpp/src/config.cpp

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <string>
2+
3+
#include "yaml-cpp/yaml.h"
4+
5+
#include "include/deploy/config/ppdet_cfg.h"
6+
7+
namespace Deploy {
8+
9+
bool ConfigPaser::Load(const std::string &cfg_file, const std::string &pp_type) {
10+
//Load config as a YAML::Node
11+
det_config = YAML::LoadFile(cfg_file);
12+
//Paser yaml file
13+
if (pp_type == 'det') {
14+
if(!Det_paser(det_config)) {
15+
std::cerr << "Fail to paser PaddleDection yaml file" << std::endl;
16+
return false
17+
}
18+
}
19+
20+
}
21+
22+
bool ConfigPaser::Det_paser(const YAML::Node &det_config) {
23+
config_["model_format"] = "Paddle";
24+
//arch support value:YOLO, SSD, RetinaNet, RCNN, Face
25+
if(det_config["arch"].IsDefined()) {
26+
config_["model_name"] = det_config["arch"];
27+
}
28+
else {
29+
std::cerr << "Fail to find arch in PaddleDection yaml file" << std::endl;
30+
return false
31+
}
32+
config_["toolkit"] = "PaddleDetection";
33+
config_["toolkit_version"] = "Unknown";
34+
35+
if(det_config["label_list"].IsDefined()) {
36+
int i = 0;
37+
for (const auto& label : det_config["label_list"]) {
38+
config_["lables"][i] = label.as<std::string>();
39+
i++;
40+
}
41+
}
42+
else {
43+
std::cerr << "Fail to find label_list in PaddleDection yaml file" << std::endl;
44+
return false
45+
}
46+
//Preprocess support Normalize, Permute, Resize, PadStride
47+
if(det_config["Preprocess"].IsDefined()) {
48+
YAML::Node preprocess_info = det_config["Preprocess"];
49+
for (const auto& preprocess_op : preprocess_info) {
50+
if(!Det_paser_transforms(preprocess_op)) {
51+
std::cerr << "Fail to parser PaddleDetection transforms" << std::endl;
52+
return false
53+
}
54+
}
55+
}
56+
else {
57+
std::cerr << "Fail to find Preprocess in PaddleDection yaml file" << std::endl;
58+
return false
59+
}
60+
61+
}
62+
63+
bool Det_build_transforms(const YAML::Node &preprocess_op) {
64+
if (preprocess_op["type"] == "Normalize") {
65+
std::vector<float> mean = preprocess_op.as<std::vector<float>>();
66+
std::vector<float> scale = preprocess_op.as<std::vector<float>>();
67+
config_["transforms"]["Normalize"]["is_scale"] = preprocess_op["is_scale"].as<bool>()
68+
for (int i = 0; i < mean.size(); i++) {
69+
config_["transforms"]["Normalize"]["mean"].push_back(mean[i]);
70+
config_["transforms"]["Normalize"]["std"].push_back(std[i]);
71+
config_["transforms"]["Normalize"]["min_val"].push_back(0);
72+
config_["transforms"]["Normalize"]["max_val"].push_back(255);
73+
}
74+
}
75+
else if (preprocess_op["type"] == "Permute") {
76+
config_["transforms"]["Permute"] = True;
77+
if (preprocess_op["to_bgr"]) {
78+
config_["transforms"]["RGB2BRG"] = True;
79+
}
80+
}
81+
else if (preprocess_op["type"] == "Resize") {
82+
int max_size = preprocess_op["max_size"].as<int>()
83+
if (max_size !=0 && (config_["model_name"] == "RCNN" || config_["model_name"] == "RetinaNet")) {
84+
config_["transforms"]["ResizeByShort"]["target_size"] = preprocess_op["target_size"].as<int>();
85+
config_["transforms"]["ResizeByShort"]["max_size"] = max_size;
86+
config_["transforms"]["ResizeByShort"]["interp"] = preprocess_op["interp"].as<int>();
87+
}
88+
else {
89+
config_["transforms"]["Resize"]["width"] = preprocess_op["target_size"].as<int>();
90+
config_["transforms"]["Resize"]["height"] = preprocess_op["target_size"].as<int>();
91+
config_["transforms"]["Resize"]["max_size"] = max_size;
92+
config_["transforms"]["Resize"]["interp"] = preprocess_op["interp"].as<int>();
93+
}
94+
}
95+
}
96+
97+
98+
}//namespace
99+

cpp/src/preprocess/preprocess.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "preprocess.h"
2+
3+
namespace Deploy {
4+
5+
} // namespace name

cpp/src/preprocess/proprocess_det.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)