Skip to content

Commit 7ecff4e

Browse files
author
wangsiyuan06
committed
add transform module
1 parent 4257c7c commit 7ecff4e

11 files changed

+548
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# deploy
2-
PaddleX Deploy module
2+
Paddle Deploy module

cpp/include/deploy/common/config.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ class ConfigParser {
1616

1717
template <typename T>
1818
const T& Get(const string &name) const {
19-
return config_.as<T>();
19+
return config_[name].as<T>();
2020
}
2121

22+
YAML::Node Get_transforms();
2223

2324
private:
2425
bool Det_parser(const YAML::Node &det_config);
+189-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <vector>
4+
35
#include <opencv2/core/core.hpp>
46
#include <opencv2/highgui/highgui.hpp>
57
#include <opencv2/imgproc/imgproc.hpp>
@@ -8,10 +10,194 @@
810

911
class Transform {
1012
public:
13+
14+
virtual void Init(const YAML::Node& item) = 0;
1115

12-
virtual void Init(const YAML::Node& item) = 0;
16+
virtual void Shape_infer(ShapeInfo* shape) = 0;
1317

14-
virtual void Shape_infer(ShapeInfo* shape) = 0;
18+
virtual bool Run(cv::Mat* im) = 0;
19+
};
20+
21+
class Normalize : public Transform {
22+
public:
23+
virtual void Init(const YAML::Node& item) {
24+
mean_ = item["mean"].as<std::vector<float>>();
25+
std_ = item["std"].as<std::vector<float>>();
26+
if (item["is_scale"].IsDefined()) {
27+
is_scale_ = item["is_scale"];
28+
}
29+
if (item["min_val"].IsDefined()) {
30+
min_val_ = item["min_val"].as<std::vector<float>>();
31+
} else {
32+
min_val_ = std::vector<float>(mean_.size(), 0.);
33+
}
34+
if (item["max_val"].IsDefined()) {
35+
max_val_ = item["max_val"].as<std::vector<float>>();
36+
} else {
37+
max_val_ = std::vector<float>(mean_.size(), 255.);
38+
}
39+
}
40+
virtual bool Run(cv::Mat* im);
41+
virtual void Shape_infer(ShapeInfo* shape);
1542

16-
virtual bool Run(cv::Mat* im) = 0;
43+
private:
44+
bool is_scale_;
45+
std::vector<float> mean_;
46+
std::vector<float> std_;
47+
std::vector<float> min_val_;
48+
std::vector<float> max_val_;
49+
}
50+
51+
/*interp_: std::vector<int> interpolations = {
52+
cv::INTER_LINEAR,
53+
cv::INTER_NEAREST,
54+
cv::INTER_AREA,
55+
cv::INTER_CUBIC,
56+
cv::INTER_LANCZOS4}*/
57+
class ResizeByShort : public Transform {
58+
public:
59+
virtual void Init(const YAML::Node& item) {
60+
target_size_ = item["target_size"].as<int>();
61+
if (item["interp"].IsDefined()) {
62+
interp_ = item["interp"].as<int>();
63+
}
64+
else {
65+
interp_ = 0;
66+
}
67+
if (item["max_size"].IsDefined()) {
68+
max_size_ = item["max_size"].as<int>();
69+
} else {
70+
max_size_ = -1;
71+
}
72+
}
73+
virtual bool Run(cv::Mat* im, ImageBlob* data);
74+
75+
private:
76+
float GenerateScale(const cv::Mat& im);
77+
int target_size_;
78+
int max_size_;
79+
int interp_;
80+
};
81+
82+
83+
class ResizeByLong : public Transform {
84+
public:
85+
virtual void Init(const YAML::Node& item) {
86+
target_size_ = item["target_size"].as<int>();
87+
if (item["interp"].IsDefined()) {
88+
interp_ = item["interp"].as<int>();
89+
} else {
90+
interp_ = 0;
91+
}
92+
if (item["max_size"].IsDefined()) {
93+
max_size_ = item["max_size"].as<int>();
94+
} else {
95+
max_size_ = -1;
96+
}
97+
}
98+
virtual bool Run(cv::Mat* im);
99+
100+
private:
101+
float GenerateScale(const cv::Mat& im);
102+
int target_size_;
103+
int max_size_;
104+
int interp_;
105+
};
106+
107+
class Resize : public Transform {
108+
public:
109+
virtual void Init(const YAML::Node& item) {
110+
if (item["interp"].IsDefined()) {
111+
interp_ = item["interp"].as<int>();
112+
}
113+
height_ = item["height"].as<int>();
114+
width_ = item["width"].as<int>();
115+
if (height_ <= 0 || width_ <= 0) {
116+
std::cerr << "[Resize] target_size should greater than 0" << std::endl;
117+
exit(-1);
118+
}
119+
}
120+
virtual bool Run(cv::Mat* im);
121+
122+
private:
123+
int height_;
124+
int width_;
125+
int interp_;
126+
};
127+
128+
class BGR2RGB : public Transform {
129+
public:
130+
virtual void Init(const YAML::Node& item) {
131+
}
132+
virtual bool Run(cv::Mat* im);
133+
}
134+
135+
class RGB2BGR : public Transform {
136+
public:
137+
virtual void Init(const YAML::Node& item) {
138+
}
139+
virtual bool Run(cv::Mat* im);
140+
}
141+
142+
class Padding : public Transform {
143+
public:
144+
virtual void Init(const YAML::Node& item) {
145+
if (item["stride"].IsDefined()) {
146+
stride_ = item["stride"].as<int>();
147+
if (coarsest_stride_ < 1) {
148+
std::cerr << "[Padding] coarest_stride should greater than 0"
149+
<< std::endl;
150+
exit(-1);
151+
}
152+
}
153+
if (item["width"].IsDefined() && item["height"].IsDefined()) {
154+
width_ = item["width"].as<int>();
155+
height_ = item["height"].as<int>();
156+
}
157+
if (item["im_padding_value"].IsDefined()) {
158+
im_value_ = item["im_padding_value"].as<std::vector<float>>();
159+
} else {
160+
im_value_ = {0, 0, 0};
161+
}
162+
}
163+
virtual bool Run(cv::Mat* im);
164+
virtual void GeneralPadding(cv::Mat* im,
165+
const std::vector<float> &padding_val,
166+
int padding_w, int padding_h);
167+
virtual void MultichannelPadding(cv::Mat* im,
168+
const std::vector<float> &padding_val,
169+
int padding_w, int padding_h);
170+
private:
171+
int stride_ = -1;
172+
int width_ = 0;
173+
int height_ = 0;
174+
std::vector<float> im_value_;
175+
};
176+
177+
class CenterCrop : public Transform {
178+
public:
179+
virtual void Init(const YAML::Node& item) {
180+
height_ = item["width"].as<int>();
181+
width_ = item["height"].as<int>();
182+
}
183+
virtual bool Run(cv::Mat* im);
184+
185+
private:
186+
int height_;
187+
int width_;
188+
};
189+
190+
191+
class Clip : public Transform {
192+
public:
193+
virtual void Init(const YAML::Node& item) {
194+
min_val_ = item["min_val"].as<std::vector<float>>();
195+
max_val_ = item["max_val"].as<std::vector<float>>();
196+
}
197+
198+
virtual bool Run(cv::Mat* im);
199+
200+
private:
201+
std::vector<float> min_val_;
202+
std::vector<float> max_val_;
17203
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "include/deploy/preprocess/preprocess.h"
4+
#include "include/deploy/common/blob.h"
5+
6+
7+
namespace Deploy {
8+
9+
class DetProecess : pubulic BasePreprocess {
10+
public:
11+
DetProecess() {}
12+
13+
~DetProecess() {}
14+
15+
virtual bool Init(const ConfigParser &parser);
16+
17+
virtual bool DetProecess::Run(const std::vector<cv::Mat> &imgs, std::vector<std::vector<Inblob>> *inputs, std::vector<ShapeInfo> *shape_traces);
18+
19+
}
20+
21+
}//namespace

cpp/include/deploy/preprocess/preprocess.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ class BasePreprocess {
1313

1414
~BasePreprocess() {}
1515

16-
virtual bool Init(const YAML::Node &config) = 0;
16+
virtual bool Init(const ConfigParser &parser) = 0;
1717

1818
virtual bool Run(const std::vector<cv::mat> &imgs, std::vector<std::vector<Inblob>> *inputs, std::vector<ShapeInfo> *shape_traces) = 0;
1919

2020
protected:
2121

2222
bool BuildTransform();
23-
24-
std::shared_ptr<Transform> CreateTransform(const std::string& name);
2523

2624
std::vector<std::shared_ptr<Transform>> transforms;
25+
26+
private:
27+
std::shared_ptr<Transform> CreateTransform(const std::string& name);
2728
}

cpp/include/deploy/preprocess/preprocess_det.h

-14
This file was deleted.

cpp/src/config.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ bool ConfigParser::Load(const std::string &cfg_file, const std::string &pp_type)
1919

2020
}
2121

22+
YAML::Node ConfigParser::Get_transforms() {
23+
return _config["transforms"]
24+
}
25+
2226
bool ConfigParser::Det_parser(const YAML::Node &det_config) {
2327
config_["model_format"] = "Paddle";
2428
//arch support value:YOLO, SSD, RetinaNet, RCNN, Face

cpp/src/preprocess/ppdet_pre_proc.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include "ppdet_pre_proc.h"
4+
5+
6+
namespace Deploy {
7+
8+
bool DetProecess::Init(const ConfigParser &parser) {
9+
BuildTransform(parser);
10+
}
11+
12+
virtual bool DetProecess::Run(const std::vector<cv::Mat> &imgs, std::vector<std::vector<Inblob>> *inputs, std::vector<ShapeInfo> *shape_traces){
13+
inputs.clear();
14+
for (int i=0; i < imgs.size(); i++) {
15+
ShapeInfo im_shape;
16+
cv::Mat im = imgs[i].clone();
17+
//(w,h)
18+
std::vector<int> size = {im.cols, im.rows};
19+
im_shape.transform_order.push_back("Origin")
20+
im_shape.shape["Origin"] = size;
21+
for (int j=0; j < transforms.size(); j++) {
22+
if (!transforms[j]->Run(&im)) {
23+
std::cerr << "Apply transforms to image failed!" << std::endl;
24+
return false;
25+
}
26+
if (!transforms[j]->Shape_infer(&im_shape)) {
27+
std::cerr << "Apply shape inference failed!" << std::endl;
28+
return false;
29+
}
30+
}
31+
32+
}
33+
}
34+
35+
}

cpp/src/preprocess/preprocess.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
22

33
namespace Deploy {
44

5+
bool BasePreprocess::BuildTransform(const ConfigParser &parser) {
6+
transforms.clear();
7+
transforms_node = parser.Get_transforms();
8+
for (const auto& node : transforms_node) {
9+
std::string name = node.begin()->first.as<std::string>();
10+
std::shared_ptr<Transform> transform = CreateTransform(name);
11+
transform->Init(node.begin()->second);
12+
transforms.push_back(transform);
13+
}
14+
}
515

16+
std::shared_ptr<Transform> BasePreprocess::CreateTransform(
17+
const std::string& transform_name) {
18+
if (transform_name == "Normalize") {
19+
return std::make_shared<Normalize>();
20+
} else if (transform_name == "ResizeByShort") {
21+
return std::make_shared<ResizeByShort>();
22+
} else if (transform_name == "ResizeByLong") {
23+
return std::make_shared<ResizeByLong>();
24+
} else if (transform_name == "CenterCrop") {
25+
return std::make_shared<CenterCrop>();
26+
} else if (transform_name == "Permute") {
27+
return std::make_shared<Permute>();
28+
} else if (transform_name == "Resize") {
29+
return std::make_shared<Resize>();
30+
} else if (transform_name == "Padding") {
31+
return std::make_shared<Padding>();
32+
} else if (transform_name == "Clip") {
33+
return std::make_shared<Clip>();
34+
} else if (transform_name == "RGB2BGR") {
35+
return std::make_shared<RGB2BGR>();
36+
} else if (transform_name == "BGR2RGB") {
37+
return std::make_shared<BGR2RGB>();
38+
} else {
39+
std::cerr << "There's unexpected transform(name='" << transform_name
40+
<< "')." << std::endl;
41+
exit(-1);
42+
}
43+
}
644

745
} // namespace name

cpp/src/preprocess/proprocess_det.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)