Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
c839a87c
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看板
提交
c839a87c
编写于
7月 12, 2022
作者:
D
dongshuilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code format for lite_shitu
上级
fa87707d
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
149 addition
and
168 deletion
+149
-168
deploy/lite_shitu/include/config_parser.h
deploy/lite_shitu/include/config_parser.h
+4
-4
deploy/lite_shitu/include/feature_extractor.h
deploy/lite_shitu/include/feature_extractor.h
+5
-4
deploy/lite_shitu/include/object_detector.h
deploy/lite_shitu/include/object_detector.h
+29
-32
deploy/lite_shitu/include/picodet_postprocess.h
deploy/lite_shitu/include/picodet_postprocess.h
+12
-14
deploy/lite_shitu/include/preprocess_op.h
deploy/lite_shitu/include/preprocess_op.h
+41
-43
deploy/lite_shitu/include/utils.h
deploy/lite_shitu/include/utils.h
+2
-3
deploy/lite_shitu/include/vector_search.h
deploy/lite_shitu/include/vector_search.h
+1
-1
deploy/lite_shitu/src/config_parser.cc
deploy/lite_shitu/src/config_parser.cc
+1
-1
deploy/lite_shitu/src/feature_extractor.cc
deploy/lite_shitu/src/feature_extractor.cc
+11
-12
deploy/lite_shitu/src/main.cc
deploy/lite_shitu/src/main.cc
+5
-5
deploy/lite_shitu/src/object_detector.cc
deploy/lite_shitu/src/object_detector.cc
+30
-39
deploy/lite_shitu/src/picodet_postprocess.cc
deploy/lite_shitu/src/picodet_postprocess.cc
+3
-3
deploy/lite_shitu/src/preprocess_op.cc
deploy/lite_shitu/src/preprocess_op.cc
+3
-3
deploy/lite_shitu/src/utils.cc
deploy/lite_shitu/src/utils.cc
+1
-3
deploy/lite_shitu/src/vector_search.cc
deploy/lite_shitu/src/vector_search.cc
+1
-1
未找到文件。
deploy/lite_shitu/include/config_parser.h
浏览文件 @
c839a87c
...
...
@@ -29,16 +29,16 @@
namespace
PPShiTu
{
void
load_jsonf
(
std
::
string
jsonfile
,
Json
::
Value
&
jsondata
);
void
load_jsonf
(
std
::
string
jsonfile
,
Json
::
Value
&
jsondata
);
// Inference model configuration parser
class
ConfigPaser
{
public:
public:
ConfigPaser
()
{}
~
ConfigPaser
()
{}
bool
load_config
(
const
Json
::
Value
&
config
)
{
bool
load_config
(
const
Json
::
Value
&
config
)
{
// Get model arch : YOLO, SSD, RetinaNet, RCNN, Face
if
(
config
[
"Global"
].
isMember
(
"det_arch"
))
{
...
...
@@ -89,4 +89,4 @@ class ConfigPaser {
std
::
vector
<
int
>
fpn_stride_
;
};
}
// namespace PPShiTu
}
// namespace PPShiTu
deploy/lite_shitu/include/feature_extractor.h
浏览文件 @
c839a87c
...
...
@@ -18,13 +18,13 @@
#include <arm_neon.h>
#include <chrono>
#include <fstream>
#include <include/preprocess_op.h>
#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>
#include <stdlib.h>
#include <sys/time.h>
#include <vector>
#include <include/preprocess_op.h>
using
namespace
paddle
::
lite_api
;
// NOLINT
using
namespace
std
;
...
...
@@ -72,13 +72,14 @@ public:
}
}
void
RunRecModel
(
const
cv
::
Mat
&
img
,
double
&
cost_time
,
std
::
vector
<
float
>
&
feature
);
//void PostProcess(std::vector<float> &feature);
void
RunRecModel
(
const
cv
::
Mat
&
img
,
double
&
cost_time
,
std
::
vector
<
float
>
&
feature
);
// void PostProcess(std::vector<float> &feature);
void
FeatureNorm
(
std
::
vector
<
float
>
&
featuer
);
private:
std
::
shared_ptr
<
PaddlePredictor
>
predictor
;
//std::vector<std::string> label_list;
//
std::vector<std::string> label_list;
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
};
double
scale
=
0.00392157
;
...
...
deploy/lite_shitu/include/object_detector.h
浏览文件 @
c839a87c
...
...
@@ -16,24 +16,24 @@
#include <ctime>
#include <memory>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#include <stdlib.h>
#include "json/json.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "json/json.h"
#include "paddle_api.h"
// NOLINT
#include "paddle_api.h" // NOLINT
#include "include/config_parser.h"
#include "include/picodet_postprocess.h"
#include "include/preprocess_op.h"
#include "include/utils.h"
#include "include/picodet_postprocess.h"
using
namespace
paddle
::
lite_api
;
// NOLINT
using
namespace
paddle
::
lite_api
;
// NOLINT
namespace
PPShiTu
{
...
...
@@ -41,53 +41,51 @@ namespace PPShiTu {
std
::
vector
<
int
>
GenerateColorMap
(
int
num_class
);
// Visualiztion Detection Result
cv
::
Mat
VisualizeResult
(
const
cv
::
Mat
&
img
,
const
std
::
vector
<
PPShiTu
::
ObjectResult
>&
results
,
const
std
::
vector
<
std
::
string
>&
lables
,
const
std
::
vector
<
int
>&
colormap
,
const
bool
is_rbox
);
cv
::
Mat
VisualizeResult
(
const
cv
::
Mat
&
img
,
const
std
::
vector
<
PPShiTu
::
ObjectResult
>
&
results
,
const
std
::
vector
<
std
::
string
>
&
lables
,
const
std
::
vector
<
int
>
&
colormap
,
const
bool
is_rbox
);
class
ObjectDetector
{
public:
explicit
ObjectDetector
(
const
Json
::
Value
&
config
,
const
std
::
string
&
model_dir
,
int
cpu_threads
=
1
,
public:
explicit
ObjectDetector
(
const
Json
::
Value
&
config
,
const
std
::
string
&
model_dir
,
int
cpu_threads
=
1
,
const
int
batch_size
=
1
)
{
config_
.
load_config
(
config
);
printf
(
"config created
\n
"
);
preprocessor_
.
Init
(
config_
.
preprocess_info_
);
printf
(
"before object detector
\n
"
);
if
(
config
[
"Global"
][
"det_model_path"
].
as
<
std
::
string
>
().
empty
())
{
std
::
cout
<<
"Please set [det_model_path] in config file"
<<
std
::
endl
;
exit
(
-
1
);
if
(
config
[
"Global"
][
"det_model_path"
].
as
<
std
::
string
>
().
empty
())
{
std
::
cout
<<
"Please set [det_model_path] in config file"
<<
std
::
endl
;
exit
(
-
1
);
}
LoadModel
(
config
[
"Global"
][
"det_model_path"
].
as
<
std
::
string
>
(),
cpu_threads
);
printf
(
"create object detector
\n
"
);
}
LoadModel
(
config
[
"Global"
][
"det_model_path"
].
as
<
std
::
string
>
(),
cpu_threads
);
printf
(
"create object detector
\n
"
);
}
// Load Paddle inference model
void
LoadModel
(
std
::
string
model_file
,
int
num_theads
);
// Run predictor
void
Predict
(
const
std
::
vector
<
cv
::
Mat
>&
imgs
,
const
int
warmup
=
0
,
void
Predict
(
const
std
::
vector
<
cv
::
Mat
>
&
imgs
,
const
int
warmup
=
0
,
const
int
repeats
=
1
,
std
::
vector
<
PPShiTu
::
ObjectResult
>
*
result
=
nullptr
,
std
::
vector
<
int
>
*
bbox_num
=
nullptr
,
std
::
vector
<
double
>
*
times
=
nullptr
);
std
::
vector
<
PPShiTu
::
ObjectResult
>
*
result
=
nullptr
,
std
::
vector
<
int
>
*
bbox_num
=
nullptr
,
std
::
vector
<
double
>
*
times
=
nullptr
);
// Get Model Label list
const
std
::
vector
<
std
::
string
>
&
GetLabelList
()
const
{
const
std
::
vector
<
std
::
string
>
&
GetLabelList
()
const
{
return
config_
.
label_list_
;
}
private:
private:
// Preprocess image and copy data to input buffer
void
Preprocess
(
const
cv
::
Mat
&
image_mat
);
void
Preprocess
(
const
cv
::
Mat
&
image_mat
);
// Postprocess result
void
Postprocess
(
const
std
::
vector
<
cv
::
Mat
>
mats
,
std
::
vector
<
PPShiTu
::
ObjectResult
>*
result
,
std
::
vector
<
int
>
bbox_num
,
bool
is_rbox
);
std
::
vector
<
PPShiTu
::
ObjectResult
>
*
result
,
std
::
vector
<
int
>
bbox_num
,
bool
is_rbox
);
std
::
shared_ptr
<
PaddlePredictor
>
predictor_
;
Preprocessor
preprocessor_
;
...
...
@@ -96,7 +94,6 @@ class ObjectDetector {
std
::
vector
<
int
>
out_bbox_num_data_
;
float
threshold_
;
ConfigPaser
config_
;
};
}
// namespace PPShiTu
}
// namespace PPShiTu
deploy/lite_shitu/include/picodet_postprocess.h
浏览文件 @
c839a87c
...
...
@@ -14,25 +14,23 @@
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <utility>
#include <ctime>
#include <memory>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
#include "include/utils.h"
namespace
PPShiTu
{
void
PicoDetPostProcess
(
std
::
vector
<
PPShiTu
::
ObjectResult
>*
results
,
std
::
vector
<
const
float
*>
outs
,
std
::
vector
<
int
>
fpn_stride
,
std
::
vector
<
float
>
im_shape
,
std
::
vector
<
float
>
scale_factor
,
float
score_threshold
=
0.3
,
float
nms_threshold
=
0.5
,
int
num_class
=
80
,
int
reg_max
=
7
);
void
PicoDetPostProcess
(
std
::
vector
<
PPShiTu
::
ObjectResult
>
*
results
,
std
::
vector
<
const
float
*>
outs
,
std
::
vector
<
int
>
fpn_stride
,
std
::
vector
<
float
>
im_shape
,
std
::
vector
<
float
>
scale_factor
,
float
score_threshold
=
0.3
,
float
nms_threshold
=
0.5
,
int
num_class
=
80
,
int
reg_max
=
7
);
}
// namespace PPShiTu
}
// namespace PPShiTu
deploy/lite_shitu/include/preprocess_op.h
浏览文件 @
c839a87c
...
...
@@ -21,16 +21,16 @@
#include <utility>
#include <vector>
#include "json/json.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "json/json.h"
namespace
PPShiTu
{
// Object for storing all preprocessed data
class
ImageBlob
{
public:
public:
// image width and height
std
::
vector
<
float
>
im_shape_
;
// Buffer for image data after preprocessing
...
...
@@ -45,20 +45,20 @@ class ImageBlob {
// Abstraction of preprocessing opration class
class
PreprocessOp
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
=
0
;
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
)
=
0
;
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
=
0
;
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
)
=
0
;
};
class
InitInfo
:
public
PreprocessOp
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{}
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{}
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
};
class
NormalizeImage
:
public
PreprocessOp
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{
mean_
.
clear
();
scale_
.
clear
();
for
(
auto
tmp
:
item
[
"mean"
])
{
...
...
@@ -70,11 +70,11 @@ class NormalizeImage : public PreprocessOp {
is_scale_
=
item
[
"is_scale"
].
as
<
bool
>
();
}
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
void
Run_feature
(
cv
::
Mat
*
im
,
const
std
::
vector
<
float
>
&
mean
,
const
std
::
vector
<
float
>
&
std
,
float
scale
);
const
std
::
vector
<
float
>
&
std
,
float
scale
);
private:
private:
// CHW or HWC
std
::
vector
<
float
>
mean_
;
std
::
vector
<
float
>
scale_
;
...
...
@@ -82,15 +82,15 @@ class NormalizeImage : public PreprocessOp {
};
class
Permute
:
public
PreprocessOp
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{}
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{}
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
void
Run_feature
(
const
cv
::
Mat
*
im
,
float
*
data
);
};
class
Resize
:
public
PreprocessOp
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{
interp_
=
item
[
"interp"
].
as
<
int
>
();
// max_size_ = item["target_size"].as<int>();
keep_ratio_
=
item
[
"keep_ratio"
].
as
<
bool
>
();
...
...
@@ -101,12 +101,13 @@ class Resize : public PreprocessOp {
}
// Compute best resize scale for x-dimension, y-dimension
std
::
pair
<
float
,
float
>
GenerateScale
(
const
cv
::
Mat
&
im
);
std
::
pair
<
float
,
float
>
GenerateScale
(
const
cv
::
Mat
&
im
);
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
void
Run_feature
(
const
cv
::
Mat
&
img
,
cv
::
Mat
&
resize_img
,
int
max_size_len
,
int
size
=
0
);
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
void
Run_feature
(
const
cv
::
Mat
&
img
,
cv
::
Mat
&
resize_img
,
int
max_size_len
,
int
size
=
0
);
private:
private:
int
interp_
;
bool
keep_ratio_
;
std
::
vector
<
int
>
target_size_
;
...
...
@@ -115,46 +116,43 @@ class Resize : public PreprocessOp {
// Models with FPN need input shape % stride == 0
class
PadStride
:
public
PreprocessOp
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{
stride_
=
item
[
"stride"
].
as
<
int
>
();
}
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
private:
private:
int
stride_
;
};
class
TopDownEvalAffine
:
public
PreprocessOp
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{
public:
virtual
void
Init
(
const
Json
::
Value
&
item
)
{
trainsize_
.
clear
();
for
(
auto
tmp
:
item
[
"trainsize"
])
{
trainsize_
.
emplace_back
(
tmp
.
as
<
int
>
());
}
}
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
virtual
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
private:
private:
int
interp_
=
1
;
std
::
vector
<
int
>
trainsize_
;
};
void
CropImg
(
cv
::
Mat
&
img
,
cv
::
Mat
&
crop_img
,
std
::
vector
<
int
>&
area
,
std
::
vector
<
float
>&
center
,
std
::
vector
<
float
>&
scale
,
void
CropImg
(
cv
::
Mat
&
img
,
cv
::
Mat
&
crop_img
,
std
::
vector
<
int
>
&
area
,
std
::
vector
<
float
>
&
center
,
std
::
vector
<
float
>
&
scale
,
float
expandratio
=
0.15
);
class
Preprocessor
{
public:
void
Init
(
const
Json
::
Value
&
config_node
)
{
public:
void
Init
(
const
Json
::
Value
&
config_node
)
{
// initialize image info at first
ops_
[
"InitInfo"
]
=
std
::
make_shared
<
InitInfo
>
();
for
(
const
auto
&
item
:
config_node
)
{
for
(
const
auto
&
item
:
config_node
)
{
auto
op_name
=
item
[
"type"
].
as
<
std
::
string
>
();
ops_
[
op_name
]
=
CreateOp
(
op_name
);
...
...
@@ -162,7 +160,7 @@ class Preprocessor {
}
}
std
::
shared_ptr
<
PreprocessOp
>
CreateOp
(
const
std
::
string
&
name
)
{
std
::
shared_ptr
<
PreprocessOp
>
CreateOp
(
const
std
::
string
&
name
)
{
if
(
name
==
"DetResize"
)
{
return
std
::
make_shared
<
Resize
>
();
}
else
if
(
name
==
"DetPermute"
)
{
...
...
@@ -180,13 +178,13 @@ class Preprocessor {
return
nullptr
;
}
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
void
Run
(
cv
::
Mat
*
im
,
ImageBlob
*
data
);
public:
public:
static
const
std
::
vector
<
std
::
string
>
RUN_ORDER
;
private:
private:
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
PreprocessOp
>>
ops_
;
};
}
// namespace PPShiTu
}
// namespace PPShiTu
deploy/lite_shitu/include/utils.h
浏览文件 @
c839a87c
...
...
@@ -43,12 +43,11 @@ void nms(std::vector<ObjectResult> &input_boxes, float nms_threshold,
template
<
typename
T
>
static
inline
bool
SortScorePairDescend
(
const
std
::
pair
<
float
,
T
>
&
pair1
,
const
std
::
pair
<
float
,
T
>
&
pair2
)
{
const
std
::
pair
<
float
,
T
>
&
pair2
)
{
return
pair1
.
first
>
pair2
.
first
;
}
float
RectOverlap
(
const
ObjectResult
&
a
,
const
ObjectResult
&
b
);
float
RectOverlap
(
const
ObjectResult
&
a
,
const
ObjectResult
&
b
);
inline
void
GetMaxScoreIndex
(
const
std
::
vector
<
ObjectResult
>
&
det_result
,
...
...
deploy/lite_shitu/include/vector_search.h
浏览文件 @
c839a87c
...
...
@@ -70,4 +70,4 @@ private:
std
::
vector
<
faiss
::
Index
::
idx_t
>
I
;
SearchResult
sr
;
};
}
}
// namespace PPShiTu
deploy/lite_shitu/src/config_parser.cc
浏览文件 @
c839a87c
...
...
@@ -29,4 +29,4 @@ void load_jsonf(std::string jsonfile, Json::Value &jsondata) {
}
}
}
// namespace PPShiTu
}
// namespace PPShiTu
deploy/lite_shitu/src/feature_extractor.cc
浏览文件 @
c839a87c
...
...
@@ -17,8 +17,7 @@
#include <numeric>
namespace
PPShiTu
{
void
FeatureExtract
::
RunRecModel
(
const
cv
::
Mat
&
img
,
double
&
cost_time
,
void
FeatureExtract
::
RunRecModel
(
const
cv
::
Mat
&
img
,
double
&
cost_time
,
std
::
vector
<
float
>
&
feature
)
{
// Read img
cv
::
Mat
img_fp
;
...
...
@@ -34,7 +33,7 @@ void FeatureExtract::RunRecModel(const cv::Mat &img,
// const float *dimg = reinterpret_cast<const float *>(img_fp.data);
// NeonMeanScale(dimg, data0, img_fp.rows * img_fp.cols);
for
(
int
i
=
0
;
i
<
input
.
size
();
++
i
)
{
for
(
int
i
=
0
;
i
<
input
.
size
();
++
i
)
{
data0
[
i
]
=
input
[
i
];
}
...
...
@@ -44,7 +43,7 @@ void FeatureExtract::RunRecModel(const cv::Mat &img,
// Get output and post process
std
::
unique_ptr
<
const
Tensor
>
output_tensor
(
std
::
move
(
this
->
predictor
->
GetOutput
(
0
)));
//
only one output
std
::
move
(
this
->
predictor
->
GetOutput
(
0
)));
//
only one output
auto
end
=
std
::
chrono
::
system_clock
::
now
();
auto
duration
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
microseconds
>
(
end
-
start
);
...
...
@@ -52,7 +51,7 @@ void FeatureExtract::RunRecModel(const cv::Mat &img,
std
::
chrono
::
microseconds
::
period
::
num
/
std
::
chrono
::
microseconds
::
period
::
den
;
//do postprocess
//
do postprocess
int
output_size
=
1
;
for
(
auto
dim
:
output_tensor
->
shape
())
{
output_size
*=
dim
;
...
...
@@ -60,15 +59,15 @@ void FeatureExtract::RunRecModel(const cv::Mat &img,
feature
.
resize
(
output_size
);
output_tensor
->
CopyToCpu
(
feature
.
data
());
//postprocess include sqrt or binarize.
//
postprocess include sqrt or binarize.
FeatureNorm
(
feature
);
return
;
}
void
FeatureExtract
::
FeatureNorm
(
std
::
vector
<
float
>
&
feature
){
float
feature_sqrt
=
std
::
sqrt
(
std
::
inner_product
(
feature
.
begin
(),
feature
.
end
(),
feature
.
begin
(),
0.0
f
));
for
(
int
i
=
0
;
i
<
feature
.
size
();
++
i
)
feature
[
i
]
/=
feature_sqrt
;
}
void
FeatureExtract
::
FeatureNorm
(
std
::
vector
<
float
>
&
feature
)
{
float
feature_sqrt
=
std
::
sqrt
(
std
::
inner_product
(
feature
.
begin
(),
feature
.
end
(),
feature
.
begin
(),
0.0
f
));
for
(
int
i
=
0
;
i
<
feature
.
size
();
++
i
)
feature
[
i
]
/=
feature_sqrt
;
}
}
// namespace PPShiTu
deploy/lite_shitu/src/main.cc
浏览文件 @
c839a87c
...
...
@@ -27,8 +27,8 @@
#include "include/feature_extractor.h"
#include "include/object_detector.h"
#include "include/preprocess_op.h"
#include "include/vector_search.h"
#include "include/utils.h"
#include "include/vector_search.h"
#include "json/json.h"
Json
::
Value
RT_Config
;
...
...
@@ -216,7 +216,7 @@ int main(int argc, char **argv) {
// add the whole image for recognition to improve recall
PPShiTu
::
ObjectResult
result_whole_img
=
{
{
0
,
0
,
srcimg
.
cols
,
srcimg
.
rows
},
0
,
1.0
};
{
0
,
0
,
srcimg
.
cols
,
srcimg
.
rows
},
0
,
1.0
};
det_result
.
push_back
(
result_whole_img
);
// get rec result
...
...
@@ -233,9 +233,10 @@ int main(int argc, char **argv) {
// do vectore search
search_result
=
searcher
.
Search
(
features
.
data
(),
det_result
.
size
());
for
(
int
i
=
0
;
i
<
det_result
.
size
();
++
i
)
{
det_result
[
i
].
confidence
=
search_result
.
D
[
search_result
.
return_k
*
i
];
det_result
[
i
].
confidence
=
search_result
.
D
[
search_result
.
return_k
*
i
];
}
NMSBoxes
(
det_result
,
searcher
.
GetThreshold
(),
rec_nms_threshold
,
indeices
);
NMSBoxes
(
det_result
,
searcher
.
GetThreshold
(),
rec_nms_threshold
,
indeices
);
PrintResult
(
img_path
,
det_result
,
searcher
,
search_result
);
batch_imgs
.
clear
();
...
...
@@ -243,7 +244,6 @@ int main(int argc, char **argv) {
features
.
clear
();
feature
.
clear
();
indeices
.
clear
();
}
}
return
0
;
...
...
deploy/lite_shitu/src/object_detector.cc
浏览文件 @
c839a87c
...
...
@@ -13,9 +13,9 @@
// limitations under the License.
#include <sstream>
// for setprecision
#include "include/object_detector.h"
#include <chrono>
#include <iomanip>
#include "include/object_detector.h"
namespace
PPShiTu
{
...
...
@@ -30,10 +30,10 @@ void ObjectDetector::LoadModel(std::string model_file, int num_theads) {
}
// Visualiztion MaskDetector results
cv
::
Mat
VisualizeResult
(
const
cv
::
Mat
&
img
,
const
std
::
vector
<
PPShiTu
::
ObjectResult
>
&
results
,
const
std
::
vector
<
std
::
string
>
&
lables
,
const
std
::
vector
<
int
>
&
colormap
,
cv
::
Mat
VisualizeResult
(
const
cv
::
Mat
&
img
,
const
std
::
vector
<
PPShiTu
::
ObjectResult
>
&
results
,
const
std
::
vector
<
std
::
string
>
&
lables
,
const
std
::
vector
<
int
>
&
colormap
,
const
bool
is_rbox
=
false
)
{
cv
::
Mat
vis_img
=
img
.
clone
();
for
(
int
i
=
0
;
i
<
results
.
size
();
++
i
)
{
...
...
@@ -75,24 +75,18 @@ cv::Mat VisualizeResult(const cv::Mat& img,
origin
.
y
=
results
[
i
].
rect
[
1
];
// Configure text background
cv
::
Rect
text_back
=
cv
::
Rect
(
results
[
i
].
rect
[
0
],
results
[
i
].
rect
[
1
]
-
text_size
.
height
,
text_size
.
width
,
text_size
.
height
);
cv
::
Rect
text_back
=
cv
::
Rect
(
results
[
i
].
rect
[
0
],
results
[
i
].
rect
[
1
]
-
text_size
.
height
,
text_size
.
width
,
text_size
.
height
);
// Draw text, and background
cv
::
rectangle
(
vis_img
,
text_back
,
roi_color
,
-
1
);
cv
::
putText
(
vis_img
,
text
,
origin
,
font_face
,
font_scale
,
cv
::
Scalar
(
255
,
255
,
255
),
thickness
);
cv
::
putText
(
vis_img
,
text
,
origin
,
font_face
,
font_scale
,
cv
::
Scalar
(
255
,
255
,
255
),
thickness
);
}
return
vis_img
;
}
void
ObjectDetector
::
Preprocess
(
const
cv
::
Mat
&
ori_im
)
{
void
ObjectDetector
::
Preprocess
(
const
cv
::
Mat
&
ori_im
)
{
// Clone the image : keep the original mat for postprocess
cv
::
Mat
im
=
ori_im
.
clone
();
// cv::cvtColor(im, im, cv::COLOR_BGR2RGB);
...
...
@@ -100,7 +94,7 @@ void ObjectDetector::Preprocess(const cv::Mat& ori_im) {
}
void
ObjectDetector
::
Postprocess
(
const
std
::
vector
<
cv
::
Mat
>
mats
,
std
::
vector
<
PPShiTu
::
ObjectResult
>
*
result
,
std
::
vector
<
PPShiTu
::
ObjectResult
>
*
result
,
std
::
vector
<
int
>
bbox_num
,
bool
is_rbox
=
false
)
{
result
->
clear
();
...
...
@@ -156,12 +150,11 @@ void ObjectDetector::Postprocess(const std::vector<cv::Mat> mats,
}
}
void
ObjectDetector
::
Predict
(
const
std
::
vector
<
cv
::
Mat
>&
imgs
,
const
int
warmup
,
void
ObjectDetector
::
Predict
(
const
std
::
vector
<
cv
::
Mat
>
&
imgs
,
const
int
warmup
,
const
int
repeats
,
std
::
vector
<
PPShiTu
::
ObjectResult
>
*
result
,
std
::
vector
<
int
>
*
bbox_num
,
std
::
vector
<
double
>
*
times
)
{
std
::
vector
<
PPShiTu
::
ObjectResult
>
*
result
,
std
::
vector
<
int
>
*
bbox_num
,
std
::
vector
<
double
>
*
times
)
{
auto
preprocess_start
=
std
::
chrono
::
steady_clock
::
now
();
int
batch_size
=
imgs
.
size
();
...
...
@@ -180,29 +173,29 @@ void ObjectDetector::Predict(const std::vector<cv::Mat>& imgs,
scale_factor_all
[
bs_idx
*
2
+
1
]
=
inputs_
.
scale_factor_
[
1
];
// TODO: reduce cost time
in_data_all
.
insert
(
in_data_all
.
end
(),
inputs_
.
im_data_
.
begin
(),
inputs_
.
im_data_
.
end
());
in_data_all
.
insert
(
in_data_all
.
end
(),
inputs_
.
im_data_
.
begin
(),
inputs_
.
im_data_
.
end
());
}
auto
preprocess_end
=
std
::
chrono
::
steady_clock
::
now
();
std
::
vector
<
const
float
*>
output_data_list_
;
// Prepare input tensor
auto
input_names
=
predictor_
->
GetInputNames
();
for
(
const
auto
&
tensor_name
:
input_names
)
{
for
(
const
auto
&
tensor_name
:
input_names
)
{
auto
in_tensor
=
predictor_
->
GetInputByName
(
tensor_name
);
if
(
tensor_name
==
"image"
)
{
int
rh
=
inputs_
.
in_net_shape_
[
0
];
int
rw
=
inputs_
.
in_net_shape_
[
1
];
in_tensor
->
Resize
({
batch_size
,
3
,
rh
,
rw
});
auto
*
inptr
=
in_tensor
->
mutable_data
<
float
>
();
auto
*
inptr
=
in_tensor
->
mutable_data
<
float
>
();
std
::
copy_n
(
in_data_all
.
data
(),
in_data_all
.
size
(),
inptr
);
}
else
if
(
tensor_name
==
"im_shape"
)
{
in_tensor
->
Resize
({
batch_size
,
2
});
auto
*
inptr
=
in_tensor
->
mutable_data
<
float
>
();
auto
*
inptr
=
in_tensor
->
mutable_data
<
float
>
();
std
::
copy_n
(
im_shape_all
.
data
(),
im_shape_all
.
size
(),
inptr
);
}
else
if
(
tensor_name
==
"scale_factor"
)
{
in_tensor
->
Resize
({
batch_size
,
2
});
auto
*
inptr
=
in_tensor
->
mutable_data
<
float
>
();
auto
*
inptr
=
in_tensor
->
mutable_data
<
float
>
();
std
::
copy_n
(
scale_factor_all
.
data
(),
scale_factor_all
.
size
(),
inptr
);
}
}
...
...
@@ -216,7 +209,7 @@ void ObjectDetector::Predict(const std::vector<cv::Mat>& imgs,
if
(
config_
.
arch_
==
"PicoDet"
)
{
for
(
int
j
=
0
;
j
<
output_names
.
size
();
j
++
)
{
auto
output_tensor
=
predictor_
->
GetTensor
(
output_names
[
j
]);
const
float
*
outptr
=
output_tensor
->
data
<
float
>
();
const
float
*
outptr
=
output_tensor
->
data
<
float
>
();
std
::
vector
<
int64_t
>
output_shape
=
output_tensor
->
shape
();
output_data_list_
.
push_back
(
outptr
);
}
...
...
@@ -242,7 +235,7 @@ void ObjectDetector::Predict(const std::vector<cv::Mat>& imgs,
if
(
config_
.
arch_
==
"PicoDet"
)
{
for
(
int
i
=
0
;
i
<
output_names
.
size
();
i
++
)
{
auto
output_tensor
=
predictor_
->
GetTensor
(
output_names
[
i
]);
const
float
*
outptr
=
output_tensor
->
data
<
float
>
();
const
float
*
outptr
=
output_tensor
->
data
<
float
>
();
std
::
vector
<
int64_t
>
output_shape
=
output_tensor
->
shape
();
if
(
i
==
0
)
{
num_class
=
output_shape
[
2
];
...
...
@@ -268,16 +261,15 @@ void ObjectDetector::Predict(const std::vector<cv::Mat>& imgs,
std
::
cerr
<<
"[WARNING] No object detected."
<<
std
::
endl
;
}
output_data_
.
resize
(
output_size
);
std
::
copy_n
(
output_tensor
->
mutable_data
<
float
>
(),
output_size
,
output_data_
.
data
());
std
::
copy_n
(
output_tensor
->
mutable_data
<
float
>
(),
output_size
,
output_data_
.
data
());
int
out_bbox_num_size
=
1
;
for
(
int
j
=
0
;
j
<
out_bbox_num_shape
.
size
();
++
j
)
{
out_bbox_num_size
*=
out_bbox_num_shape
[
j
];
}
out_bbox_num_data_
.
resize
(
out_bbox_num_size
);
std
::
copy_n
(
out_bbox_num
->
mutable_data
<
int
>
(),
out_bbox_num_size
,
std
::
copy_n
(
out_bbox_num
->
mutable_data
<
int
>
(),
out_bbox_num_size
,
out_bbox_num_data_
.
data
());
}
// Postprocessing result
...
...
@@ -285,9 +277,8 @@ void ObjectDetector::Predict(const std::vector<cv::Mat>& imgs,
result
->
clear
();
if
(
config_
.
arch_
==
"PicoDet"
)
{
PPShiTu
::
PicoDetPostProcess
(
result
,
output_data_list_
,
config_
.
fpn_stride_
,
inputs_
.
im_shape_
,
inputs_
.
scale_factor_
,
config_
.
nms_info_
[
"score_threshold"
].
as
<
float
>
(),
result
,
output_data_list_
,
config_
.
fpn_stride_
,
inputs_
.
im_shape_
,
inputs_
.
scale_factor_
,
config_
.
nms_info_
[
"score_threshold"
].
as
<
float
>
(),
config_
.
nms_info_
[
"nms_threshold"
].
as
<
float
>
(),
num_class
,
reg_max
);
bbox_num
->
push_back
(
result
->
size
());
}
else
{
...
...
@@ -326,4 +317,4 @@ std::vector<int> GenerateColorMap(int num_class) {
return
colormap
;
}
}
// namespace PPShiTu
}
// namespace PPShiTu
deploy/lite_shitu/src/picodet_postprocess.cc
浏览文件 @
c839a87c
...
...
@@ -47,9 +47,9 @@ int activation_function_softmax(const _Tp *src, _Tp *dst, int length) {
}
// PicoDet decode
PPShiTu
::
ObjectResult
disPred2Bbox
(
const
float
*&
dfl_det
,
int
label
,
float
score
,
int
x
,
int
y
,
int
stride
,
std
::
vector
<
float
>
im_shape
,
int
reg_max
)
{
PPShiTu
::
ObjectResult
disPred2Bbox
(
const
float
*&
dfl_det
,
int
label
,
float
score
,
int
x
,
int
y
,
int
stride
,
std
::
vector
<
float
>
im_shape
,
int
reg_max
)
{
float
ct_x
=
(
x
+
0.5
)
*
stride
;
float
ct_y
=
(
y
+
0.5
)
*
stride
;
std
::
vector
<
float
>
dis_pred
;
...
...
deploy/lite_shitu/src/preprocess_op.cc
浏览文件 @
c839a87c
...
...
@@ -47,7 +47,7 @@ void NormalizeImage::Run(cv::Mat *im, ImageBlob *data) {
}
void
NormalizeImage
::
Run_feature
(
cv
::
Mat
*
im
,
const
std
::
vector
<
float
>
&
mean
,
const
std
::
vector
<
float
>
&
std
,
float
scale
)
{
const
std
::
vector
<
float
>
&
std
,
float
scale
)
{
(
*
im
).
convertTo
(
*
im
,
CV_32FC3
,
scale
);
for
(
int
h
=
0
;
h
<
im
->
rows
;
h
++
)
{
for
(
int
w
=
0
;
w
<
im
->
cols
;
w
++
)
{
...
...
@@ -127,8 +127,8 @@ std::pair<float, float> Resize::GenerateScale(const cv::Mat &im) {
return
resize_scale
;
}
void
Resize
::
Run_feature
(
const
cv
::
Mat
&
img
,
cv
::
Mat
&
resize_img
,
int
resize_short_size
,
int
size
)
{
void
Resize
::
Run_feature
(
const
cv
::
Mat
&
img
,
cv
::
Mat
&
resize_img
,
int
resize_short_size
,
int
size
)
{
int
resize_h
=
0
;
int
resize_w
=
0
;
if
(
size
>
0
)
{
...
...
deploy/lite_shitu/src/utils.cc
浏览文件 @
c839a87c
...
...
@@ -54,9 +54,7 @@ void nms(std::vector<ObjectResult> &input_boxes, float nms_threshold,
}
}
float
RectOverlap
(
const
ObjectResult
&
a
,
const
ObjectResult
&
b
)
{
float
RectOverlap
(
const
ObjectResult
&
a
,
const
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
);
...
...
deploy/lite_shitu/src/vector_search.cc
浏览文件 @
c839a87c
...
...
@@ -64,4 +64,4 @@ const SearchResult &VectorSearch::Search(float *feature, int query_number) {
const
std
::
string
&
VectorSearch
::
GetLabel
(
faiss
::
Index
::
idx_t
ind
)
{
return
this
->
id_map
.
at
(
ind
);
}
}
}
// namespace PPShiTu
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录