Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
629e53df
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看板
未验证
提交
629e53df
编写于
6月 05, 2021
作者:
F
Felix
提交者:
GitHub
6月 05, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add vector search to reg infer (#791)
* Update inference_rec.yaml * Update predict.sh
上级
05d08019
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
77 addition
and
8 deletion
+77
-8
deploy/configs/inference_rec.yaml
deploy/configs/inference_rec.yaml
+18
-1
deploy/python/predict_system.py
deploy/python/predict_system.py
+54
-4
deploy/shell/predict.sh
deploy/shell/predict.sh
+2
-2
deploy/vector_search/interface.py
deploy/vector_search/interface.py
+3
-1
未找到文件。
deploy/configs/inference_rec.yaml
浏览文件 @
629e53df
...
@@ -49,4 +49,21 @@ RecPreProcess:
...
@@ -49,4 +49,21 @@ RecPreProcess:
order
:
'
'
order
:
'
'
-
ToCHWImage
:
-
ToCHWImage
:
RecPostProcess
:
null
RecPostProcess
:
null
\ No newline at end of file
# indexing engine config
IndexProcess
:
build
:
enable
:
True
index_path
:
"
./logo_index/"
image_root
:
"
dataset/LogoDet-3K-crop/train"
data_file
:
"
dataset/LogoDet-3K-crop/LogoDet-3K+train.txt"
spacer
:
"
"
dist_type
:
"
IP"
pq_size
:
100
embedding_size
:
1000
infer
:
index_path
:
"
./logo_index/"
search_budget
:
100
return_k
:
10
deploy/python/predict_system.py
浏览文件 @
629e53df
# Copyright (c) 202
0
PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 202
1
PaddlePaddle Authors. All Rights Reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# you may not use this file except in compliance with the License.
...
@@ -23,28 +23,78 @@ import numpy as np
...
@@ -23,28 +23,78 @@ import numpy as np
from
python.predict_rec
import
RecPredictor
from
python.predict_rec
import
RecPredictor
from
python.predict_det
import
DetPredictor
from
python.predict_det
import
DetPredictor
from
vector_search
import
Graph_Index
from
utils
import
logger
from
utils
import
logger
from
utils
import
config
from
utils
import
config
from
utils.get_image_list
import
get_image_list
from
utils.get_image_list
import
get_image_list
def
split_datafile
(
data_file
,
image_root
):
gallery_images
=
[]
gallery_docs
=
[]
with
open
(
datafile
)
as
f
:
lines
=
f
.
readlines
()
for
i
,
line
in
enumerate
(
lines
):
line
=
line
.
strip
().
split
(
"
\t
"
)
if
line
[
0
]
==
'image_id'
:
continue
image_file
=
os
.
path
.
join
(
image_root
,
line
[
3
])
image_doc
=
line
[
1
]
gallery_images
.
append
(
image_file
)
gallery_docs
.
append
(
image_doc
)
return
gallery_images
,
gallery_docs
class
SystemPredictor
(
object
):
class
SystemPredictor
(
object
):
def
__init__
(
self
,
config
):
def
__init__
(
self
,
config
):
self
.
config
=
config
self
.
rec_predictor
=
RecPredictor
(
config
)
self
.
rec_predictor
=
RecPredictor
(
config
)
self
.
det_predictor
=
DetPredictor
(
config
)
self
.
det_predictor
=
DetPredictor
(
config
)
assert
'IndexProcess'
in
config
.
keys
(),
"Index config not found ... "
self
.
indexer
(
config
[
'IndexProcess'
])
self
.
return_k
=
self
.
config
[
'IndexProcess'
][
'infer'
][
'return_k'
]
self
.
search_budget
=
self
.
config
[
'IndexProcess'
][
'infer'
][
'search_budget'
]
def
indexer
(
self
,
config
):
if
'build'
in
config
.
keys
()
and
config
[
'build'
][
'enable'
]:
# build the index from scratch
with
open
(
config
[
'build'
][
'datafile'
])
as
f
:
lines
=
f
.
readlines
()
gallery_images
,
gallery_docs
=
split_datafile
(
config
[
'build'
][
'data_file'
],
config
[
'build'
][
'image_root'
])
# extract gallery features
gallery_features
=
np
.
zeros
([
len
(
gallery_images
),
config
[
'build'
][
'embedding_size'
]],
dtype
=
np
.
float32
)
for
i
,
image_file
in
enumerate
(
gallery_images
):
img
=
cv2
.
imread
(
image_file
)[:,
:,
::
-
1
]
rec_feat
=
self
.
rec_predictor
.
predict
(
img
)
gallery_features
[
i
,:]
=
rec_feat
# train index
self
.
Searcher
=
Graph_Index
(
dist_type
=
config
[
'build'
][
'dist_type'
])
self
.
Searcher
.
build
(
gallery_vectors
=
gallery_features
,
gallery_docs
=
gallery_docs
,
pq_size
=
config
[
'build'
][
'pq_size'
],
index_path
=
config
[
'build'
][
'index_path'
])
else
:
# load local index
self
.
Searcher
=
Graph_Index
(
dist_type
=
config
[
'build'
][
'dist_type'
])
self
.
Searcher
.
load
(
config
[
'infer'
][
'index_path'
])
def
predict
(
self
,
img
):
def
predict
(
self
,
img
):
output
=
[]
output
=
[]
results
=
self
.
det_predictor
.
predict
(
img
)
results
=
self
.
det_predictor
.
predict
(
img
)
for
result
in
results
:
for
result
in
results
:
print
(
result
)
#
print(result)
xmin
,
xmax
,
ymin
,
ymax
=
result
[
"bbox"
].
astype
(
"int"
)
xmin
,
xmax
,
ymin
,
ymax
=
result
[
"bbox"
].
astype
(
"int"
)
crop_img
=
img
[
xmin
:
xmax
,
ymin
:
ymax
,
:].
copy
()
crop_img
=
img
[
xmin
:
xmax
,
ymin
:
ymax
,
:].
copy
()
rec_results
=
self
.
rec_predictor
.
predict
(
crop_img
)
rec_results
=
self
.
rec_predictor
.
predict
(
crop_img
)
result
[
"feature"
]
=
rec_results
result
[
"featrue"
]
=
rec_results
scores
,
docs
=
self
.
Searcher
.
search
(
query
=
rec_results
,
return_k
=
self
.
return_k
,
search_budget
=
self
.
search_budget
)
result
[
"ret_docs"
]
=
docs
result
[
"ret_scores"
]
=
scores
output
.
append
(
result
)
output
.
append
(
result
)
return
output
return
output
def
main
(
config
):
def
main
(
config
):
...
@@ -55,7 +105,7 @@ def main(config):
...
@@ -55,7 +105,7 @@ def main(config):
for
idx
,
image_file
in
enumerate
(
image_list
):
for
idx
,
image_file
in
enumerate
(
image_list
):
img
=
cv2
.
imread
(
image_file
)[:,
:,
::
-
1
]
img
=
cv2
.
imread
(
image_file
)[:,
:,
::
-
1
]
output
=
system_predictor
.
predict
(
img
)
output
=
system_predictor
.
predict
(
img
)
print
(
output
)
#
print(output)
return
return
...
...
deploy/shell/predict.sh
浏览文件 @
629e53df
...
@@ -7,5 +7,5 @@ python3.7 python/predict_cls.py -c configs/inference_cls.yaml
...
@@ -7,5 +7,5 @@ python3.7 python/predict_cls.py -c configs/inference_cls.yaml
# detection
# detection
# python3.7 python/predict_det.py -c configs/inference_rec.yaml
# python3.7 python/predict_det.py -c configs/inference_rec.yaml
# mainbody detection + feature extractor
# mainbody detection + feature extractor + retrieval
# python3.7 python/predict_system.py -c configs/inference_rec.yaml
# python3.7 python/predict_system.py -c configs/inference_rec.yaml
\ No newline at end of file
deploy/vector_search/interface.py
浏览文件 @
629e53df
...
@@ -22,7 +22,9 @@ import json
...
@@ -22,7 +22,9 @@ import json
from
ctypes
import
*
from
ctypes
import
*
from
numpy.ctypeslib
import
ndpointer
from
numpy.ctypeslib
import
ndpointer
lib
=
ctypes
.
cdll
.
LoadLibrary
(
"./index.so"
)
__dir__
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
so_path
=
os
.
path
.
join
(
__dir__
,
"index.so"
)
lib
=
ctypes
.
cdll
.
LoadLibrary
(
so_path
)
class
IndexContext
(
Structure
):
class
IndexContext
(
Structure
):
_fields_
=
[(
"graph"
,
c_void_p
),
_fields_
=
[(
"graph"
,
c_void_p
),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录