Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
f6139c05
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f6139c05
编写于
5月 14, 2021
作者:
G
Guanghua Yu
提交者:
GitHub
5月 14, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix static trt_int8 inference (#3001)
上级
1c582c26
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
40 addition
and
19 deletion
+40
-19
ppdet/engine/callbacks.py
ppdet/engine/callbacks.py
+3
-1
static/deploy/cpp/include/object_detector.h
static/deploy/cpp/include/object_detector.h
+5
-3
static/deploy/cpp/src/main.cc
static/deploy/cpp/src/main.cc
+2
-1
static/deploy/cpp/src/object_detector.cc
static/deploy/cpp/src/object_detector.cc
+3
-4
static/deploy/python/infer.py
static/deploy/python/infer.py
+27
-10
未找到文件。
ppdet/engine/callbacks.py
浏览文件 @
f6139c05
...
...
@@ -158,7 +158,9 @@ class Checkpointer(Callback):
if
dist
.
get_world_size
()
<
2
or
dist
.
get_rank
()
==
0
:
if
mode
==
'train'
:
end_epoch
=
self
.
model
.
cfg
.
epoch
if
epoch_id
%
self
.
model
.
cfg
.
snapshot_epoch
==
0
or
epoch_id
==
end_epoch
-
1
:
if
(
epoch_id
+
1
)
%
self
.
model
.
cfg
.
snapshot_epoch
==
0
or
epoch_id
==
end_epoch
-
1
:
save_name
=
str
(
epoch_id
)
if
epoch_id
!=
end_epoch
-
1
else
"model_final"
weight
=
self
.
weight
...
...
static/deploy/cpp/include/object_detector.h
浏览文件 @
f6139c05
...
...
@@ -58,11 +58,12 @@ class ObjectDetector {
explicit
ObjectDetector
(
const
std
::
string
&
model_dir
,
bool
use_gpu
=
false
,
const
std
::
string
&
run_mode
=
"fluid"
,
const
int
gpu_id
=
0
)
{
const
int
gpu_id
=
0
,
bool
trt_calib_mode
=
false
)
{
config_
.
load_config
(
model_dir
);
threshold_
=
config_
.
draw_threshold_
;
preprocessor_
.
Init
(
config_
.
preprocess_info_
,
config_
.
arch_
);
LoadModel
(
model_dir
,
use_gpu
,
config_
.
min_subgraph_size_
,
1
,
run_mode
,
gpu_id
);
LoadModel
(
model_dir
,
use_gpu
,
config_
.
min_subgraph_size_
,
1
,
run_mode
,
gpu_id
,
trt_calib_mode
);
}
// Load Paddle inference model
...
...
@@ -72,7 +73,8 @@ class ObjectDetector {
const
int
min_subgraph_size
,
const
int
batch_size
=
1
,
const
std
::
string
&
run_mode
=
"fluid"
,
const
int
gpu_id
=
0
);
const
int
gpu_id
=
0
,
bool
trt_calib_mode
=
false
);
// Run predictor
void
Predict
(
const
cv
::
Mat
&
im
,
...
...
static/deploy/cpp/src/main.cc
浏览文件 @
f6139c05
...
...
@@ -43,6 +43,7 @@ DEFINE_int32(camera_id, -1, "Device id of camera to predict");
DEFINE_bool
(
run_benchmark
,
false
,
"Whether to predict a image_file repeatedly for benchmark"
);
DEFINE_double
(
threshold
,
0.5
,
"Threshold of score."
);
DEFINE_string
(
output_dir
,
"output"
,
"Directory of output visualization files."
);
DEFINE_bool
(
trt_calib_mode
,
false
,
"If the model is produced by TRT offline quantitative calibration, trt_calib_mode need to set True"
);
static
std
::
string
DirName
(
const
std
::
string
&
filepath
)
{
auto
pos
=
filepath
.
rfind
(
OS_PATH_SEP
);
...
...
@@ -206,7 +207,7 @@ int main(int argc, char** argv) {
// Load model and create a object detector
PaddleDetection
::
ObjectDetector
det
(
FLAGS_model_dir
,
FLAGS_use_gpu
,
FLAGS_run_mode
,
FLAGS_gpu_id
);
FLAGS_run_mode
,
FLAGS_gpu_id
,
FLAGS_trt_calib_mode
);
// Do inference on input video or image
if
(
!
FLAGS_video_path
.
empty
()
||
FLAGS_use_camera
)
{
PredictVideo
(
FLAGS_video_path
,
&
det
);
...
...
static/deploy/cpp/src/object_detector.cc
浏览文件 @
f6139c05
...
...
@@ -25,7 +25,8 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
const
int
min_subgraph_size
,
const
int
batch_size
,
const
std
::
string
&
run_mode
,
const
int
gpu_id
)
{
const
int
gpu_id
,
bool
trt_calib_mode
)
{
paddle
::
AnalysisConfig
config
;
std
::
string
prog_file
=
model_dir
+
OS_PATH_SEP
+
"__model__"
;
std
::
string
params_file
=
model_dir
+
OS_PATH_SEP
+
"__params__"
;
...
...
@@ -33,14 +34,12 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
if
(
use_gpu
)
{
config
.
EnableUseGpu
(
100
,
gpu_id
);
config
.
SwitchIrOptim
(
true
);
bool
use_calib_mode
=
false
;
if
(
run_mode
!=
"fluid"
)
{
auto
precision
=
paddle
::
AnalysisConfig
::
Precision
::
kFloat32
;
if
(
run_mode
==
"trt_fp16"
)
{
precision
=
paddle
::
AnalysisConfig
::
Precision
::
kHalf
;
}
else
if
(
run_mode
==
"trt_int8"
)
{
precision
=
paddle
::
AnalysisConfig
::
Precision
::
kInt8
;
use_calib_mode
=
true
;
}
else
{
printf
(
"run_mode should be 'fluid', 'trt_fp32', 'trt_fp16' or 'trt_int8'"
);
}
...
...
@@ -50,7 +49,7 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
min_subgraph_size
,
precision
,
false
,
use
_calib_mode
);
trt
_calib_mode
);
}
}
else
{
config
.
DisableGpu
();
...
...
static/deploy/python/infer.py
浏览文件 @
f6139c05
...
...
@@ -65,7 +65,8 @@ class Detector(object):
model_dir
,
use_gpu
=
False
,
run_mode
=
'fluid'
,
threshold
=
0.5
):
threshold
=
0.5
,
trt_calib_mode
=
False
):
self
.
config
=
config
if
self
.
config
.
use_python_inference
:
self
.
executor
,
self
.
program
,
self
.
fecth_targets
=
load_executor
(
...
...
@@ -75,7 +76,8 @@ class Detector(object):
model_dir
,
run_mode
=
run_mode
,
min_subgraph_size
=
self
.
config
.
min_subgraph_size
,
use_gpu
=
use_gpu
)
use_gpu
=
use_gpu
,
trt_calib_mode
=
trt_calib_mode
)
def
preprocess
(
self
,
im
):
preprocess_ops
=
[]
...
...
@@ -221,13 +223,15 @@ class DetectorSOLOv2(Detector):
model_dir
,
use_gpu
=
False
,
run_mode
=
'fluid'
,
threshold
=
0.5
):
threshold
=
0.5
,
trt_calib_mode
=
False
):
super
(
DetectorSOLOv2
,
self
).
__init__
(
config
=
config
,
model_dir
=
model_dir
,
use_gpu
=
use_gpu
,
run_mode
=
run_mode
,
threshold
=
threshold
)
threshold
=
threshold
,
trt_calib_mode
=
trt_calib_mode
)
def
predict
(
self
,
image
,
...
...
@@ -379,11 +383,14 @@ def load_predictor(model_dir,
run_mode
=
'fluid'
,
batch_size
=
1
,
use_gpu
=
False
,
min_subgraph_size
=
3
):
min_subgraph_size
=
3
,
trt_calib_mode
=
False
):
"""set AnalysisConfig, generate AnalysisPredictor
Args:
model_dir (str): root path of __model__ and __params__
use_gpu (bool): whether use gpu
trt_calib_mode (bool): If the model is produced by TRT offline quantitative
calibration, trt_calib_mode need to set True
Returns:
predictor (PaddlePredictor): AnalysisPredictor
Raises:
...
...
@@ -393,7 +400,6 @@ def load_predictor(model_dir,
raise
ValueError
(
"Predict by TensorRT mode: {}, expect use_gpu==True, but use_gpu == {}"
.
format
(
run_mode
,
use_gpu
))
use_calib_mode
=
True
if
run_mode
==
'trt_int8'
else
False
precision_map
=
{
'trt_int8'
:
fluid
.
core
.
AnalysisConfig
.
Precision
.
Int8
,
'trt_fp32'
:
fluid
.
core
.
AnalysisConfig
.
Precision
.
Float32
,
...
...
@@ -417,7 +423,7 @@ def load_predictor(model_dir,
min_subgraph_size
=
min_subgraph_size
,
precision_mode
=
precision_map
[
run_mode
],
use_static
=
False
,
use_calib_mode
=
use
_calib_mode
)
use_calib_mode
=
trt
_calib_mode
)
# disable print log when predict
config
.
disable_glog_info
()
...
...
@@ -500,7 +506,7 @@ def predict_video(detector, camera_id):
fps
=
30
width
=
int
(
capture
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
))
height
=
int
(
capture
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
))
fourcc
=
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
)
fourcc
=
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
)
if
not
os
.
path
.
exists
(
FLAGS
.
output_dir
):
os
.
makedirs
(
FLAGS
.
output_dir
)
out_path
=
os
.
path
.
join
(
FLAGS
.
output_dir
,
video_name
)
...
...
@@ -531,13 +537,18 @@ def predict_video(detector, camera_id):
def
main
():
config
=
Config
(
FLAGS
.
model_dir
)
detector
=
Detector
(
config
,
FLAGS
.
model_dir
,
use_gpu
=
FLAGS
.
use_gpu
,
run_mode
=
FLAGS
.
run_mode
)
config
,
FLAGS
.
model_dir
,
use_gpu
=
FLAGS
.
use_gpu
,
run_mode
=
FLAGS
.
run_mode
,
trt_calib_mode
=
FLAGS
.
trt_calib_mode
)
if
config
.
arch
==
'SOLOv2'
:
detector
=
DetectorSOLOv2
(
config
,
FLAGS
.
model_dir
,
use_gpu
=
FLAGS
.
use_gpu
,
run_mode
=
FLAGS
.
run_mode
)
run_mode
=
FLAGS
.
run_mode
,
trt_calib_mode
=
FLAGS
.
trt_calib_mode
)
# predict from image
if
FLAGS
.
image_file
!=
''
:
predict_image
(
detector
)
...
...
@@ -590,6 +601,12 @@ if __name__ == '__main__':
type
=
str
,
default
=
"output"
,
help
=
"Directory of output visualization files."
)
parser
.
add_argument
(
"--trt_calib_mode"
,
type
=
bool
,
default
=
False
,
help
=
"If the model is produced by TRT offline quantitative "
"calibration, trt_calib_mode need to set True."
)
FLAGS
=
parser
.
parse_args
()
print_arguments
(
FLAGS
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录