Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
74da43ad
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
大约 1 年 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
74da43ad
编写于
11月 12, 2021
作者:
D
dongshuilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add l2norm for rec feature
上级
ba65fa9a
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
58 addition
and
47 deletion
+58
-47
deploy/cpp_shitu/include/feature_extracter.h
deploy/cpp_shitu/include/feature_extracter.h
+9
-4
deploy/cpp_shitu/include/nms.h
deploy/cpp_shitu/include/nms.h
+4
-4
deploy/cpp_shitu/include/object_detector.h
deploy/cpp_shitu/include/object_detector.h
+2
-2
deploy/cpp_shitu/include/preprocess_op.h
deploy/cpp_shitu/include/preprocess_op.h
+2
-2
deploy/cpp_shitu/include/preprocess_op_det.h
deploy/cpp_shitu/include/preprocess_op_det.h
+2
-2
deploy/cpp_shitu/src/feature_extracter.cpp
deploy/cpp_shitu/src/feature_extracter.cpp
+19
-13
deploy/cpp_shitu/src/main.cpp
deploy/cpp_shitu/src/main.cpp
+14
-14
deploy/cpp_shitu/src/object_detector.cpp
deploy/cpp_shitu/src/object_detector.cpp
+2
-2
deploy/cpp_shitu/src/preprocess_op.cpp
deploy/cpp_shitu/src/preprocess_op.cpp
+2
-2
deploy/cpp_shitu/src/preprocess_op_det.cpp
deploy/cpp_shitu/src/preprocess_op_det.cpp
+2
-2
未找到文件。
deploy/cpp_shitu/include/
cls
.h
→
deploy/cpp_shitu/include/
feature_extracter
.h
浏览文件 @
74da43ad
...
...
@@ -33,11 +33,11 @@
using
namespace
paddle_infer
;
namespace
PaddleClas
{
namespace
Feature
{
class
Classifi
er
{
class
FeatureExtract
er
{
public:
explicit
Classifi
er
(
const
YAML
::
Node
&
config_file
)
{
explicit
FeatureExtract
er
(
const
YAML
::
Node
&
config_file
)
{
this
->
use_gpu_
=
config_file
[
"Global"
][
"use_gpu"
].
as
<
bool
>
();
if
(
config_file
[
"Global"
][
"gpu_id"
].
IsDefined
())
this
->
gpu_id_
=
config_file
[
"Global"
][
"gpu_id"
].
as
<
int
>
();
...
...
@@ -68,6 +68,9 @@ public:
this
->
std_
=
config_file
[
"RecPreProcess"
][
"transform_ops"
][
1
]
[
"NormalizeImage"
][
"std"
]
.
as
<
std
::
vector
<
float
>>
();
if
(
config_file
[
"Global"
][
"rec_feature_normlize"
].
IsDefined
())
this
->
feature_norm
=
config_file
[
"Global"
][
"rec_feature_normlize"
].
as
<
bool
>
();
LoadModel
(
cls_model_path_
,
cls_params_path_
);
}
...
...
@@ -78,6 +81,7 @@ public:
// Run predictor
void
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
float
>
&
out_data
,
std
::
vector
<
double
>
&
times
);
void
FeatureNorm
(
std
::
vector
<
float
>
&
feature
);
std
::
shared_ptr
<
Predictor
>
predictor_
;
...
...
@@ -88,6 +92,7 @@ private:
int
cpu_math_library_num_threads_
=
4
;
bool
use_mkldnn_
=
false
;
bool
use_tensorrt_
=
false
;
bool
feature_norm
=
true
;
bool
use_fp16_
=
false
;
std
::
vector
<
float
>
mean_
=
{
0.485
f
,
0.456
f
,
0.406
f
};
std
::
vector
<
float
>
std_
=
{
0.229
f
,
0.224
f
,
0.225
f
};
...
...
@@ -103,4 +108,4 @@ private:
Permute
permute_op_
;
};
}
// namespace
PaddleClas
}
// namespace
Feature
deploy/cpp_shitu/include/nms.h
浏览文件 @
74da43ad
...
...
@@ -23,8 +23,8 @@ static inline bool SortScorePairDescend(const std::pair<float, T> &pair1,
return
pair1
.
first
>
pair2
.
first
;
}
float
RectOverlap
(
const
Paddle
Detection
::
ObjectResult
&
a
,
const
Paddle
Detection
::
ObjectResult
&
b
)
{
float
RectOverlap
(
const
Detection
::
ObjectResult
&
a
,
const
Detection
::
ObjectResult
&
b
)
{
float
Aa
=
(
a
.
rect
[
2
]
-
a
.
rect
[
0
]
+
1
)
*
(
a
.
rect
[
3
]
-
a
.
rect
[
1
]
+
1
);
float
Ab
=
(
b
.
rect
[
2
]
-
b
.
rect
[
0
]
+
1
)
*
(
b
.
rect
[
3
]
-
b
.
rect
[
1
]
+
1
);
...
...
@@ -40,7 +40,7 @@ float RectOverlap(const PaddleDetection::ObjectResult &a,
// top_k: if -1, keep all; otherwise, keep at most top_k.
// score_index_vec: store the sorted (score, index) pair.
inline
void
GetMaxScoreIndex
(
const
std
::
vector
<
Paddle
Detection
::
ObjectResult
>
&
det_result
,
GetMaxScoreIndex
(
const
std
::
vector
<
Detection
::
ObjectResult
>
&
det_result
,
const
float
threshold
,
std
::
vector
<
std
::
pair
<
float
,
int
>>
&
score_index_vec
)
{
// Generate index score pairs.
...
...
@@ -61,7 +61,7 @@ GetMaxScoreIndex(const std::vector<PaddleDetection::ObjectResult> &det_result,
// }
}
void
NMSBoxes
(
const
std
::
vector
<
Paddle
Detection
::
ObjectResult
>
det_result
,
void
NMSBoxes
(
const
std
::
vector
<
Detection
::
ObjectResult
>
det_result
,
const
float
score_threshold
,
const
float
nms_threshold
,
std
::
vector
<
int
>
&
indices
)
{
int
a
=
1
;
...
...
deploy/cpp_shitu/include/object_detector.h
浏览文件 @
74da43ad
...
...
@@ -31,7 +31,7 @@
using
namespace
paddle_infer
;
namespace
Paddle
Detection
{
namespace
Detection
{
// Object Detection Result
struct
ObjectResult
{
// Rectangle coordinates of detected object: left, right, top, down
...
...
@@ -132,4 +132,4 @@ private:
std
::
vector
<
int
>
out_bbox_num_data_
;
};
}
// namespace
Paddle
Detection
}
// namespace Detection
deploy/cpp_shitu/include/preprocess_op.h
浏览文件 @
74da43ad
...
...
@@ -29,7 +29,7 @@
using
namespace
std
;
namespace
PaddleClas
{
namespace
Feature
{
class
Normalize
{
public:
...
...
@@ -54,4 +54,4 @@ public:
int
size
=
0
);
};
}
// namespace
PaddleClas
}
// namespace
Feature
deploy/cpp_shitu/include/preprocess_op_det.h
浏览文件 @
74da43ad
...
...
@@ -28,7 +28,7 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
namespace
Paddle
Detection
{
namespace
Detection
{
// Object for storing all preprocessed data
class
ImageBlob
{
...
...
@@ -152,4 +152,4 @@ private:
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
PreprocessOp
>>
ops_
;
};
}
// namespace
Paddle
Detection
}
// namespace Detection
deploy/cpp_shitu/src/
cls
.cpp
→
deploy/cpp_shitu/src/
feature_extracter
.cpp
浏览文件 @
74da43ad
...
...
@@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <include/cls.h>
#include <cmath>
#include <include/feature_extracter.h>
#include <numeric>
namespace
PaddleClas
{
namespace
Feature
{
void
Classifi
er
::
LoadModel
(
const
std
::
string
&
model_path
,
const
std
::
string
&
params_path
)
{
void
FeatureExtract
er
::
LoadModel
(
const
std
::
string
&
model_path
,
const
std
::
string
&
params_path
)
{
paddle_infer
::
Config
config
;
config
.
SetModel
(
model_path
,
params_path
);
...
...
@@ -52,11 +54,9 @@ void Classifier::LoadModel(const std::string &model_path,
this
->
predictor_
=
CreatePredictor
(
config
);
}
void
Classifier
::
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
float
>
&
out_data
,
std
::
vector
<
double
>
&
times
)
{
cv
::
Mat
srcimg
;
void
FeatureExtracter
::
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
float
>
&
out_data
,
std
::
vector
<
double
>
&
times
)
{
cv
::
Mat
resize_img
;
img
.
copyTo
(
srcimg
);
std
::
vector
<
double
>
time
;
auto
preprocess_start
=
std
::
chrono
::
system_clock
::
now
();
...
...
@@ -86,10 +86,10 @@ void Classifier::Run(cv::Mat &img, std::vector<float> &out_data,
output_t
->
CopyToCpu
(
out_data
.
data
());
auto
infer_end
=
std
::
chrono
::
system_clock
::
now
();
//
auto postprocess_start = std::chrono::system_clock::now();
// int maxPosition =
// max_element(out_data.begin(), out_data.end()) - out_data.begin(
);
//
auto postprocess_end = std::chrono::system_clock::now();
auto
postprocess_start
=
std
::
chrono
::
system_clock
::
now
();
if
(
this
->
feature_norm
)
FeatureNorm
(
out_data
);
auto
postprocess_end
=
std
::
chrono
::
system_clock
::
now
();
std
::
chrono
::
duration
<
float
>
preprocess_diff
=
preprocess_end
-
preprocess_start
;
...
...
@@ -110,4 +110,10 @@ void Classifier::Run(cv::Mat &img, std::vector<float> &out_data,
times
[
2
]
+=
time
[
2
];
}
}
// namespace PaddleClas
void
FeatureExtracter
::
FeatureNorm
(
std
::
vector
<
float
>
&
featuer
)
{
float
featuer_sqrt
=
std
::
sqrt
(
std
::
inner_product
(
featuer
.
begin
(),
featuer
.
end
(),
featuer
.
begin
(),
0.0
f
));
for
(
int
i
=
0
;
i
<
featuer
.
size
();
++
i
)
featuer
[
i
]
/=
featuer_sqrt
;
}
}
// namespace Feature
deploy/cpp_shitu/src/main.cpp
浏览文件 @
74da43ad
...
...
@@ -28,7 +28,7 @@
#include <auto_log/autolog.h>
#include <gflags/gflags.h>
#include <include/
cls
.h>
#include <include/
feature_extracter
.h>
#include <include/nms.h>
#include <include/object_detector.h>
#include <include/vector_search.h>
...
...
@@ -42,8 +42,8 @@ DEFINE_string(c, "", "Path of yaml file");
void
DetPredictImage
(
const
std
::
vector
<
cv
::
Mat
>
&
batch_imgs
,
const
std
::
vector
<
std
::
string
>
&
all_img_paths
,
const
int
batch_size
,
Paddle
Detection
::
ObjectDetector
*
det
,
std
::
vector
<
Paddle
Detection
::
ObjectResult
>
&
im_result
,
const
int
batch_size
,
Detection
::
ObjectDetector
*
det
,
std
::
vector
<
Detection
::
ObjectResult
>
&
im_result
,
std
::
vector
<
int
>
&
im_bbox_num
,
std
::
vector
<
double
>
&
det_t
,
const
bool
visual_det
=
false
,
const
bool
run_benchmark
=
false
,
...
...
@@ -63,7 +63,7 @@ void DetPredictImage(const std::vector<cv::Mat> &batch_imgs,
// }
// Store all detected result
std
::
vector
<
Paddle
Detection
::
ObjectResult
>
result
;
std
::
vector
<
Detection
::
ObjectResult
>
result
;
std
::
vector
<
int
>
bbox_num
;
std
::
vector
<
double
>
det_times
;
bool
is_rbox
=
false
;
...
...
@@ -73,7 +73,7 @@ void DetPredictImage(const std::vector<cv::Mat> &batch_imgs,
det
->
Predict
(
batch_imgs
,
0
,
1
,
&
result
,
&
bbox_num
,
&
det_times
);
// get labels and colormap
auto
labels
=
det
->
GetLabelList
();
auto
colormap
=
Paddle
Detection
::
GenerateColorMap
(
labels
.
size
());
auto
colormap
=
Detection
::
GenerateColorMap
(
labels
.
size
());
int
item_start_idx
=
0
;
for
(
int
i
=
0
;
i
<
left_image_cnt
;
i
++
)
{
...
...
@@ -81,7 +81,7 @@ void DetPredictImage(const std::vector<cv::Mat> &batch_imgs,
int
detect_num
=
0
;
for
(
int
j
=
0
;
j
<
bbox_num
[
i
];
j
++
)
{
Paddle
Detection
::
ObjectResult
item
=
result
[
item_start_idx
+
j
];
Detection
::
ObjectResult
item
=
result
[
item_start_idx
+
j
];
if
(
item
.
confidence
<
det
->
GetThreshold
()
||
item
.
class_id
==
-
1
)
{
continue
;
}
...
...
@@ -110,8 +110,8 @@ void DetPredictImage(const std::vector<cv::Mat> &batch_imgs,
std
::
cout
<<
all_img_paths
.
at
(
idx
*
batch_size
+
i
)
<<
" The number of detected box: "
<<
detect_num
<<
std
::
endl
;
cv
::
Mat
vis_img
=
PaddleDetection
::
VisualizeResult
(
im
,
im_result
,
labels
,
colormap
,
is_rbox
);
cv
::
Mat
vis_img
=
Detection
::
VisualizeResult
(
im
,
im_result
,
labels
,
colormap
,
is_rbox
);
std
::
vector
<
int
>
compression_params
;
compression_params
.
push_back
(
CV_IMWRITE_JPEG_QUALITY
);
compression_params
.
push_back
(
95
);
...
...
@@ -134,7 +134,7 @@ void DetPredictImage(const std::vector<cv::Mat> &batch_imgs,
}
void
PrintResult
(
std
::
string
&
img_path
,
std
::
vector
<
Paddle
Detection
::
ObjectResult
>
&
det_result
,
std
::
vector
<
Detection
::
ObjectResult
>
&
det_result
,
std
::
vector
<
int
>
&
indeices
,
VectorSearch
&
vector_search
,
SearchResult
&
search_result
)
{
printf
(
"%s:
\n
"
,
img_path
.
c_str
());
...
...
@@ -167,8 +167,8 @@ int main(int argc, char **argv) {
config
.
PrintConfigInfo
();
// initialize detector, rec_Model, vector_search
PaddleClas
::
Classifier
classifi
er
(
config
.
config_file
);
Paddle
Detection
::
ObjectDetector
detector
(
config
.
config_file
);
Feature
::
FeatureExtracter
feature_extract
er
(
config
.
config_file
);
Detection
::
ObjectDetector
detector
(
config
.
config_file
);
VectorSearch
searcher
(
config
.
config_file
);
// config
...
...
@@ -212,7 +212,7 @@ int main(int argc, char **argv) {
std
::
vector
<
cv
::
Mat
>
batch_imgs
;
std
::
vector
<
std
::
string
>
img_paths
;
// for detection
std
::
vector
<
Paddle
Detection
::
ObjectResult
>
det_result
;
std
::
vector
<
Detection
::
ObjectResult
>
det_result
;
std
::
vector
<
int
>
det_bbox_num
;
// for vector search
std
::
vector
<
float
>
features
;
...
...
@@ -243,7 +243,7 @@ int main(int argc, char **argv) {
det_result
.
resize
(
max_det_results
);
}
// step2: add the whole image for recognition to improve recall
Paddle
Detection
::
ObjectResult
result_whole_img
=
{
Detection
::
ObjectResult
result_whole_img
=
{
{
0
,
0
,
srcimg
.
cols
-
1
,
srcimg
.
rows
-
1
},
0
,
1.0
};
det_result
.
push_back
(
result_whole_img
);
det_bbox_num
[
0
]
=
det_result
.
size
()
+
1
;
...
...
@@ -255,7 +255,7 @@ int main(int argc, char **argv) {
int
h
=
det_result
[
j
].
rect
[
3
]
-
det_result
[
j
].
rect
[
1
];
cv
::
Rect
rect
(
det_result
[
j
].
rect
[
0
],
det_result
[
j
].
rect
[
1
],
w
,
h
);
cv
::
Mat
crop_img
=
srcimg
(
rect
);
classifi
er
.
Run
(
crop_img
,
feature
,
cls_times
);
feature_extract
er
.
Run
(
crop_img
,
feature
,
cls_times
);
features
.
insert
(
features
.
end
(),
feature
.
begin
(),
feature
.
end
());
}
...
...
deploy/cpp_shitu/src/object_detector.cpp
浏览文件 @
74da43ad
...
...
@@ -19,7 +19,7 @@
using
namespace
paddle_infer
;
namespace
Paddle
Detection
{
namespace
Detection
{
// Load Model and create model predictor
void
ObjectDetector
::
LoadModel
(
const
std
::
string
&
model_dir
,
...
...
@@ -362,4 +362,4 @@ std::vector<int> GenerateColorMap(int num_class) {
return
colormap
;
}
}
// namespace
Paddle
Detection
}
// namespace Detection
deploy/cpp_shitu/src/preprocess_op.cpp
浏览文件 @
74da43ad
...
...
@@ -30,7 +30,7 @@
#include <include/preprocess_op.h>
namespace
PaddleClas
{
namespace
Feature
{
void
Permute
::
Run
(
const
cv
::
Mat
*
im
,
float
*
data
)
{
int
rh
=
im
->
rows
;
...
...
@@ -88,4 +88,4 @@ void ResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img,
cv
::
resize
(
img
,
resize_img
,
cv
::
Size
(
resize_w
,
resize_h
));
}
}
// namespace
PaddleClas
}
// namespace
Feature
deploy/cpp_shitu/src/preprocess_op_det.cpp
浏览文件 @
74da43ad
...
...
@@ -17,7 +17,7 @@
#include "include/preprocess_op_det.h"
namespace
Paddle
Detection
{
namespace
Detection
{
void
InitInfo
::
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
)
{
data
->
im_shape_
=
{
static_cast
<
float
>
(
im
->
rows
),
...
...
@@ -127,4 +127,4 @@ void Preprocessor::Run(cv::Mat *im, ImageBlob *data) {
}
}
}
// namespace
Paddle
Detection
}
// namespace Detection
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录