Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
1ee341b9
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
1ee341b9
编写于
4月 22, 2022
作者:
W
Wenyu
提交者:
GitHub
4月 22, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
save detection results to file using coco format #5782 (#5787)
* save detection results to file using coco format * update save docs
上级
fa250ff1
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
83 addition
and
3 deletion
+83
-3
deploy/python/README.md
deploy/python/README.md
+2
-0
deploy/python/infer.py
deploy/python/infer.py
+75
-3
deploy/python/utils.py
deploy/python/utils.py
+6
-0
未找到文件。
deploy/python/README.md
浏览文件 @
1ee341b9
...
@@ -91,6 +91,8 @@ python deploy/python/mot_keypoint_unite_infer.py --mot_model_dir=output_inferenc
...
@@ -91,6 +91,8 @@ python deploy/python/mot_keypoint_unite_infer.py --mot_model_dir=output_inferenc
| --enable_mkldnn | Option | CPU预测中是否开启MKLDNN加速,默认为False |
| --enable_mkldnn | Option | CPU预测中是否开启MKLDNN加速,默认为False |
| --cpu_threads | Option| 设置cpu线程数,默认为1 |
| --cpu_threads | Option| 设置cpu线程数,默认为1 |
| --trt_calib_mode | Option| TensorRT是否使用校准功能,默认为False。使用TensorRT的int8功能时,需设置为True,使用PaddleSlim量化后的模型时需要设置为False |
| --trt_calib_mode | Option| TensorRT是否使用校准功能,默认为False。使用TensorRT的int8功能时,需设置为True,使用PaddleSlim量化后的模型时需要设置为False |
| --save_results | Option| 是否在文件夹下将图片的预测结果以JSON的形式保存 |
说明:
说明:
...
...
deploy/python/infer.py
浏览文件 @
1ee341b9
...
@@ -15,6 +15,8 @@
...
@@ -15,6 +15,8 @@
import
os
import
os
import
yaml
import
yaml
import
glob
import
glob
import
json
from
pathlib
import
Path
from
functools
import
reduce
from
functools
import
reduce
import
cv2
import
cv2
...
@@ -233,7 +235,8 @@ class Detector(object):
...
@@ -233,7 +235,8 @@ class Detector(object):
image_list
,
image_list
,
run_benchmark
=
False
,
run_benchmark
=
False
,
repeats
=
1
,
repeats
=
1
,
visual
=
True
):
visual
=
True
,
save_file
=
None
):
batch_loop_cnt
=
math
.
ceil
(
float
(
len
(
image_list
))
/
self
.
batch_size
)
batch_loop_cnt
=
math
.
ceil
(
float
(
len
(
image_list
))
/
self
.
batch_size
)
results
=
[]
results
=
[]
for
i
in
range
(
batch_loop_cnt
):
for
i
in
range
(
batch_loop_cnt
):
...
@@ -293,6 +296,10 @@ class Detector(object):
...
@@ -293,6 +296,10 @@ class Detector(object):
if
visual
:
if
visual
:
print
(
'Test iter {}'
.
format
(
i
))
print
(
'Test iter {}'
.
format
(
i
))
if
save_file
is
not
None
:
Path
(
self
.
output_dir
).
mkdir
(
exist_ok
=
True
)
self
.
format_coco_results
(
image_list
,
results
,
save_file
=
save_file
)
results
=
self
.
merge_batch_result
(
results
)
results
=
self
.
merge_batch_result
(
results
)
return
results
return
results
...
@@ -313,7 +320,7 @@ class Detector(object):
...
@@ -313,7 +320,7 @@ class Detector(object):
if
not
os
.
path
.
exists
(
self
.
output_dir
):
if
not
os
.
path
.
exists
(
self
.
output_dir
):
os
.
makedirs
(
self
.
output_dir
)
os
.
makedirs
(
self
.
output_dir
)
out_path
=
os
.
path
.
join
(
self
.
output_dir
,
video_out_name
)
out_path
=
os
.
path
.
join
(
self
.
output_dir
,
video_out_name
)
fourcc
=
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
)
fourcc
=
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
)
writer
=
cv2
.
VideoWriter
(
out_path
,
fourcc
,
fps
,
(
width
,
height
))
writer
=
cv2
.
VideoWriter
(
out_path
,
fourcc
,
fps
,
(
width
,
height
))
index
=
1
index
=
1
while
(
1
):
while
(
1
):
...
@@ -337,6 +344,68 @@ class Detector(object):
...
@@ -337,6 +344,68 @@ class Detector(object):
break
break
writer
.
release
()
writer
.
release
()
@
staticmethod
def
format_coco_results
(
image_list
,
results
,
save_file
=
None
):
coco_results
=
[]
image_id
=
0
for
result
in
results
:
start_idx
=
0
for
box_num
in
result
[
'boxes_num'
]:
idx_slice
=
slice
(
start_idx
,
start_idx
+
box_num
)
start_idx
+=
box_num
image_file
=
image_list
[
image_id
]
image_id
+=
1
if
'boxes'
in
result
:
boxes
=
result
[
'boxes'
][
idx_slice
,
:]
per_result
=
[
{
'image_file'
:
image_file
,
'bbox'
:
[
box
[
2
],
box
[
3
],
box
[
4
]
-
box
[
2
],
box
[
5
]
-
box
[
3
]],
# xyxy -> xywh
'score'
:
box
[
1
],
'category_id'
:
int
(
box
[
0
]),
}
for
k
,
box
in
enumerate
(
boxes
.
tolist
())
]
elif
'segm'
in
result
:
import
pycocotools.mask
as
mask_util
scores
=
result
[
'score'
][
idx_slice
].
tolist
()
category_ids
=
result
[
'label'
][
idx_slice
].
tolist
()
segms
=
result
[
'segm'
][
idx_slice
,
:]
rles
=
[
mask_util
.
encode
(
np
.
array
(
mask
[:,
:,
np
.
newaxis
],
dtype
=
np
.
uint8
,
order
=
'F'
))[
0
]
for
mask
in
segms
]
for
rle
in
rles
:
rle
[
'counts'
]
=
rle
[
'counts'
].
decode
(
'utf-8'
)
per_result
=
[{
'image_file'
:
image_file
,
'segmentation'
:
rle
,
'score'
:
scores
[
k
],
'category_id'
:
category_ids
[
k
],
}
for
k
,
rle
in
enumerate
(
rles
)]
else
:
raise
RuntimeError
(
''
)
# per_result = [item for item in per_result if item['score'] > threshold]
coco_results
.
extend
(
per_result
)
if
save_file
:
with
open
(
os
.
path
.
join
(
save_file
),
'w'
)
as
f
:
json
.
dump
(
coco_results
,
f
)
return
coco_results
class
DetectorSOLOv2
(
Detector
):
class
DetectorSOLOv2
(
Detector
):
"""
"""
...
@@ -807,7 +876,10 @@ def main():
...
@@ -807,7 +876,10 @@ def main():
if
FLAGS
.
image_dir
is
None
and
FLAGS
.
image_file
is
not
None
:
if
FLAGS
.
image_dir
is
None
and
FLAGS
.
image_file
is
not
None
:
assert
FLAGS
.
batch_size
==
1
,
"batch_size should be 1, when image_file is not None"
assert
FLAGS
.
batch_size
==
1
,
"batch_size should be 1, when image_file is not None"
img_list
=
get_test_images
(
FLAGS
.
image_dir
,
FLAGS
.
image_file
)
img_list
=
get_test_images
(
FLAGS
.
image_dir
,
FLAGS
.
image_file
)
detector
.
predict_image
(
img_list
,
FLAGS
.
run_benchmark
,
repeats
=
100
)
save_file
=
os
.
path
.
join
(
FLAGS
.
output_dir
,
'results.json'
)
if
FLAGS
.
save_results
else
None
detector
.
predict_image
(
img_list
,
FLAGS
.
run_benchmark
,
repeats
=
100
,
save_file
=
save_file
)
if
not
FLAGS
.
run_benchmark
:
if
not
FLAGS
.
run_benchmark
:
detector
.
det_times
.
info
(
average
=
True
)
detector
.
det_times
.
info
(
average
=
True
)
else
:
else
:
...
...
deploy/python/utils.py
浏览文件 @
1ee341b9
...
@@ -156,6 +156,12 @@ def argsparser():
...
@@ -156,6 +156,12 @@ def argsparser():
type
=
ast
.
literal_eval
,
type
=
ast
.
literal_eval
,
default
=
False
,
default
=
False
,
help
=
"Whether do random padding for action recognition."
)
help
=
"Whether do random padding for action recognition."
)
parser
.
add_argument
(
"--save_results"
,
type
=
bool
,
default
=
False
,
help
=
"Whether save detection result to file using coco format"
)
return
parser
return
parser
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录