Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
5ef89df9
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
接近 2 年 前同步成功
通知
116
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看板
提交
5ef89df9
编写于
11月 09, 2021
作者:
D
dongshuilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update for vector search
上级
a89d6ae7
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
65 addition
and
58 deletion
+65
-58
deploy/cpp_shitu/include/vector_search.h
deploy/cpp_shitu/include/vector_search.h
+19
-24
deploy/cpp_shitu/src/main.cpp
deploy/cpp_shitu/src/main.cpp
+21
-8
deploy/cpp_shitu/src/vector_search.cpp
deploy/cpp_shitu/src/vector_search.cpp
+23
-25
deploy/cpp_shitu/tools/build.sh
deploy/cpp_shitu/tools/build.sh
+2
-1
未找到文件。
deploy/cpp_shitu/include/vector_search.h
浏览文件 @
5ef89df9
...
...
@@ -19,51 +19,46 @@
#define OS_PATH_SEP "/"
#endif
#include "yaml-cpp/yaml.h"
#include <cstring>
#include <faiss/Index.h>
#include <faiss/index_io.h>
#include <cstring>
#include <map>
#include "yaml-cpp/yaml.h"
struct
SearchResult
{
faiss
::
Index
::
idx_t
*
I
;
float
*
D
;
int
query_number
;
struct
SearchResult
{
std
::
vector
<
faiss
::
Index
::
idx_t
>
I
;
std
::
vector
<
float
>
D
;
int
return_k
;
};
class
VectorSearch
{
class
VectorSearch
{
public:
explicit
VectorSearch
(
const
YAML
::
Node
&
config_file
){
explicit
VectorSearch
(
const
YAML
::
Node
&
config_file
)
{
// IndexProcess
this
->
index_dir
=
config_file
[
"IndexProcess"
][
"index_dir"
].
as
<
std
::
string
>
();
this
->
index_dir
=
config_file
[
"IndexProcess"
][
"index_dir"
].
as
<
std
::
string
>
();
this
->
return_k
=
config_file
[
"IndexProcess"
][
"return_k"
].
as
<
int
>
();
this
->
score_thres
=
config_file
[
"IndexProcess"
][
"score_thres"
].
as
<
float
>
();
this
->
max_query_number
=
config_file
[
"Global"
][
"max_det_results"
].
as
<
int
>
()
+
1
;
this
->
max_query_number
=
config_file
[
"Global"
][
"max_det_results"
].
as
<
int
>
()
+
1
;
LoadIdMap
();
LoadIndexFile
();
this
->
I
=
new
faiss
::
Index
::
idx_t
[
this
->
return_k
*
this
->
max_query_number
];
this
->
D
=
new
float
[
this
->
return_k
*
this
->
max_query_number
];
}
~
VectorSearch
(){
delete
[]
I
;
delete
[]
D
;
this
->
I
.
resize
(
this
->
return_k
*
this
->
max_query_number
);
this
->
D
.
resize
(
this
->
return_k
*
this
->
max_query_number
);
};
void
LoadIdMap
();
void
LoadIndexFile
();
void
Search
(
float
*
feature
,
int
query_number
);
const
SearchResult
&
GetSearchResult
();
const
std
::
string
&
GetLabel
(
faiss
::
Index
::
idx_t
ind
);
const
SearchResult
&
Search
(
float
*
feature
,
int
query_number
);
const
std
::
string
&
GetLabel
(
faiss
::
Index
::
idx_t
ind
);
private:
std
::
string
index_dir
;
int
return_k
=
5
;
float
score_thres
=
0.5
;
std
::
map
<
long
int
,
std
::
string
>
id_map
;
faiss
::
Index
*
index
;
std
::
map
<
long
int
,
std
::
string
>
id_map
;
faiss
::
Index
*
index
;
int
max_query_number
=
6
;
int
real_query_number
=
0
;
float
*
D
=
NULL
;
faiss
::
Index
::
idx_t
*
I
=
NULL
;
std
::
vector
<
float
>
D
;
std
::
vector
<
faiss
::
Index
::
idx_t
>
I
;
SearchResult
sr
;
};
deploy/cpp_shitu/src/main.cpp
浏览文件 @
5ef89df9
...
...
@@ -29,8 +29,8 @@
#include <auto_log/autolog.h>
#include <include/cls.h>
#include <include/object_detector.h>
#include <include/yaml_config.h>
#include <include/vector_search.h>
#include <include/yaml_config.h>
using
namespace
std
;
using
namespace
cv
;
...
...
@@ -137,6 +137,11 @@ int main(int argc, char **argv) {
YamlConfig
config
(
argv
[
1
]);
config
.
PrintConfigInfo
();
// initialize detector, rec_Model, vector_search
PaddleClas
::
Classifier
classifier
(
config
.
config_file
);
PaddleDetection
::
ObjectDetector
detector
(
config
.
config_file
);
VectorSearch
searcher
(
config
.
config_file
);
// config
const
int
batch_size
=
config
.
config_file
[
"Global"
][
"batch_size"
].
as
<
int
>
();
bool
visual_det
=
false
;
...
...
@@ -152,6 +157,7 @@ int main(int argc, char **argv) {
max_det_results
=
config
.
config_file
[
"Global"
][
"max_det_results"
].
as
<
int
>
();
}
// load image_file_path
std
::
string
path
=
config
.
config_file
[
"Global"
][
"infer_imgs"
].
as
<
std
::
string
>
();
std
::
vector
<
std
::
string
>
img_files_list
;
...
...
@@ -164,12 +170,8 @@ int main(int argc, char **argv) {
}
else
{
img_files_list
.
push_back
(
path
);
}
std
::
cout
<<
"img_file_list length: "
<<
img_files_list
.
size
()
<<
std
::
endl
;
PaddleClas
::
Classifier
classifier
(
config
.
config_file
);
PaddleDetection
::
ObjectDetector
detector
(
config
.
config_file
);
double
elapsed_time
=
0.0
;
std
::
vector
<
double
>
cls_times
=
{
0
,
0
,
0
};
std
::
vector
<
double
>
det_times
=
{
0
,
0
,
0
};
...
...
@@ -177,6 +179,8 @@ int main(int argc, char **argv) {
std
::
vector
<
std
::
string
>
img_paths
;
std
::
vector
<
PaddleDetection
::
ObjectResult
>
det_result
;
std
::
vector
<
int
>
det_bbox_num
;
std
::
vector
<
float
>
features
;
std
::
vector
<
float
>
feature
;
int
warmup_iter
=
img_files_list
.
size
()
>
5
?
5
:
0
;
for
(
int
idx
=
0
;
idx
<
img_files_list
.
size
();
++
idx
)
{
...
...
@@ -206,20 +210,29 @@ int main(int argc, char **argv) {
det_result
.
push_back
(
result_whole_img
);
det_bbox_num
[
0
]
=
det_result
.
size
()
+
1
;
// step3: recognition process, use score_thres to ensure accuracy
// step3: extract feature for all boxes in an inmage
SearchResult
search_result
;
for
(
int
j
=
0
;
j
<
det_result
.
size
();
++
j
)
{
int
w
=
det_result
[
j
].
rect
[
2
]
-
det_result
[
j
].
rect
[
0
];
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
);
std
::
vector
<
float
>
feature
;
classifier
.
Run
(
crop_img
,
feature
,
cls_times
);
features
.
insert
(
features
.
end
(),
feature
.
begin
(),
feature
.
end
());
}
// double run_time = classifier.Run(srcimg, cls_times);
// step4: get search result
search_result
=
searcher
.
Search
(
features
.
data
(),
det_result
.
size
());
// nms for search result
// for postprocess
batch_imgs
.
clear
();
img_paths
.
clear
();
det_bbox_num
.
clear
();
det_result
.
clear
();
feature
.
clear
();
features
.
clear
();
}
std
::
string
presion
=
"fp32"
;
...
...
deploy/cpp_shitu/src/vector_search.cpp
浏览文件 @
5ef89df9
...
...
@@ -11,54 +11,52 @@
// 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.
#include "include/vector_search.h"
#include <cstdio>
#include <faiss/index_factory.h>
#include <faiss/index_io.h>
#include <fstream>
#include <regex>
#include <iostream>
#include <cstdio>
#include "include/vector_search.h"
#include <regex>
void
VectorSearch
::
LoadIndexFile
(){
std
::
string
file_path
=
this
->
index_dir
+
OS_PATH_SEP
+
"vector.index"
;
const
char
*
fname
=
file_path
.
c_str
();
void
VectorSearch
::
LoadIndexFile
()
{
std
::
string
file_path
=
this
->
index_dir
+
OS_PATH_SEP
+
"vector.index"
;
const
char
*
fname
=
file_path
.
c_str
();
this
->
index
=
faiss
::
read_index
(
fname
,
0
);
}
void
VectorSearch
::
LoadIdMap
(){
void
VectorSearch
::
LoadIdMap
()
{
std
::
string
file_path
=
this
->
index_dir
+
OS_PATH_SEP
+
"id_map.txt"
;
std
::
ifstream
in
(
file_path
);
std
::
string
line
;
std
::
vector
<
std
::
string
>
m_vec
;
if
(
in
){
while
(
getline
(
in
,
line
)){
if
(
in
)
{
while
(
getline
(
in
,
line
))
{
std
::
regex
ws_re
(
"
\\
s+"
);
std
::
vector
<
std
::
string
>
v
(
std
::
sregex_token_iterator
(
line
.
begin
(),
line
.
end
(),
ws_re
,
-
1
),
std
::
sregex_token_iterator
());
if
(
v
.
size
()
!=
2
){
std
::
cout
<<
"The number of element for each line in : "
<<
file_path
<<
"must be 2, exit the program..."
<<
std
::
endl
;
exit
(
1
);
}
else
this
->
id_map
.
insert
(
std
::
pair
<
long
int
,
std
::
string
>
(
std
::
stol
(
v
[
0
],
nullptr
,
10
),
v
[
1
]));
std
::
sregex_token_iterator
(
line
.
begin
(),
line
.
end
(),
ws_re
,
-
1
),
std
::
sregex_token_iterator
());
if
(
v
.
size
()
!=
2
)
{
std
::
cout
<<
"The number of element for each line in : "
<<
file_path
<<
"must be 2, exit the program..."
<<
std
::
endl
;
exit
(
1
);
}
else
this
->
id_map
.
insert
(
std
::
pair
<
long
int
,
std
::
string
>
(
std
::
stol
(
v
[
0
],
nullptr
,
10
),
v
[
1
]));
}
}
}
void
VectorSearch
::
Search
(
float
*
feature
,
int
query_number
){
this
->
index
->
search
(
query_number
,
feature
,
return_k
,
D
,
I
);
this
->
real_query_number
=
query_number
;
}
const
SearchResult
&
VectorSearch
::
GetSearchResult
(){
this
->
sr
.
query_number
=
this
->
real_query_number
;
const
SearchResult
&
VectorSearch
::
Search
(
float
*
feature
,
int
query_number
)
{
this
->
D
.
resize
(
this
->
return_k
*
query_number
);
this
->
I
.
resize
(
this
->
return_k
*
query_number
);
this
->
index
->
search
(
query_number
,
feature
,
return_k
,
D
.
data
(),
I
.
data
());
this
->
sr
.
return_k
=
this
->
return_k
;
this
->
sr
.
D
=
this
->
D
;
this
->
sr
.
I
=
this
->
I
;
return
this
->
sr
;
}
const
std
::
string
&
VectorSearch
::
GetLabel
(
faiss
::
Index
::
idx_t
ind
)
{
const
std
::
string
&
VectorSearch
::
GetLabel
(
faiss
::
Index
::
idx_t
ind
)
{
return
this
->
id_map
.
at
(
ind
);
}
deploy/cpp_shitu/tools/build.sh
浏览文件 @
5ef89df9
...
...
@@ -3,6 +3,7 @@ LIB_DIR=/work/project/project/cpp_infer/paddle_inference/
CUDA_LIB_DIR
=
/usr/local/cuda/lib64
CUDNN_LIB_DIR
=
/usr/lib/x86_64-linux-gnu/
FAISS_DIR
=
/work/project/project/cpp_infer/faiss/faiss_install
FAISS_WITH_MKL
=
OFF
BUILD_DIR
=
build
rm
-rf
${
BUILD_DIR
}
...
...
@@ -18,6 +19,6 @@ cmake .. \
-DCUDNN_LIB
=
${
CUDNN_LIB_DIR
}
\
-DCUDA_LIB
=
${
CUDA_LIB_DIR
}
\
-DFAISS_DIR
=
${
FAISS_DIR
}
\
-DFAISS_WITH_MKL
=
OFF
-DFAISS_WITH_MKL
=
${
FAISS_WITH_MKL
}
make
-j
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录