Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
fe6b9a74
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
1 年多 前同步成功
通知
696
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看板
未验证
提交
fe6b9a74
编写于
9月 19, 2021
作者:
W
Wenyu
提交者:
GitHub
9月 19, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Decode and cache (#4204)
* decode with cache * update * update
上级
08619b4e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
78 addition
and
0 deletion
+78
-0
ppdet/data/transform/operators.py
ppdet/data/transform/operators.py
+78
-0
未找到文件。
ppdet/data/transform/operators.py
浏览文件 @
fe6b9a74
...
...
@@ -36,6 +36,9 @@ import copy
import
logging
import
cv2
from
PIL
import
Image
,
ImageDraw
import
pickle
import
threading
MUTEX
=
threading
.
Lock
()
from
ppdet.core.workspace
import
serializable
from
ppdet.modeling
import
bbox_utils
...
...
@@ -149,6 +152,81 @@ class Decode(BaseOperator):
return
sample
def
_make_dirs
(
dirname
):
try
:
from
pathlib
import
Path
except
ImportError
:
from
pathlib2
import
Path
Path
(
dirname
).
mkdir
(
exist_ok
=
True
)
@
register_op
class
DecodeCache
(
BaseOperator
):
def
__init__
(
self
,
cache_root
=
None
):
'''decode image and caching
'''
super
(
DecodeCache
,
self
).
__init__
()
self
.
use_cache
=
False
if
cache_root
is
None
else
True
self
.
cache_root
=
cache_root
if
cache_root
is
not
None
:
_make_dirs
(
cache_root
)
def
apply
(
self
,
sample
,
context
=
None
):
if
self
.
use_cache
and
os
.
path
.
exists
(
self
.
cache_path
(
self
.
cache_root
,
sample
[
'im_file'
])):
path
=
self
.
cache_path
(
self
.
cache_root
,
sample
[
'im_file'
])
im
=
self
.
load
(
path
)
else
:
if
'image'
not
in
sample
:
with
open
(
sample
[
'im_file'
],
'rb'
)
as
f
:
sample
[
'image'
]
=
f
.
read
()
im
=
sample
[
'image'
]
data
=
np
.
frombuffer
(
im
,
dtype
=
'uint8'
)
im
=
cv2
.
imdecode
(
data
,
1
)
# BGR mode, but need RGB mode
if
'keep_ori_im'
in
sample
and
sample
[
'keep_ori_im'
]:
sample
[
'ori_image'
]
=
im
im
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2RGB
)
if
self
.
use_cache
and
not
os
.
path
.
exists
(
self
.
cache_path
(
self
.
cache_root
,
sample
[
'im_file'
])):
path
=
self
.
cache_path
(
self
.
cache_root
,
sample
[
'im_file'
])
self
.
dump
(
im
,
path
)
sample
[
'image'
]
=
im
sample
[
'h'
]
=
im
.
shape
[
0
]
sample
[
'w'
]
=
im
.
shape
[
1
]
sample
[
'im_shape'
]
=
np
.
array
(
im
.
shape
[:
2
],
dtype
=
np
.
float32
)
sample
[
'scale_factor'
]
=
np
.
array
([
1.
,
1.
],
dtype
=
np
.
float32
)
return
sample
@
staticmethod
def
cache_path
(
dir_oot
,
im_file
):
return
os
.
path
.
join
(
dir_oot
,
os
.
path
.
basename
(
im_file
)
+
'.pkl'
)
@
staticmethod
def
load
(
path
):
with
open
(
path
,
'rb'
)
as
f
:
im
=
pickle
.
load
(
f
)
return
im
@
staticmethod
def
dump
(
obj
,
path
):
MUTEX
.
acquire
()
try
:
with
open
(
path
,
'wb'
)
as
f
:
pickle
.
dump
(
obj
,
f
)
except
Exception
as
e
:
logger
.
warning
(
'dump {} occurs exception {}'
.
format
(
path
,
str
(
e
)))
finally
:
MUTEX
.
release
()
@
register_op
class
Permute
(
BaseOperator
):
def
__init__
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录