Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
84d47ac2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
84d47ac2
编写于
12月 06, 2016
作者:
D
dangqingqing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
follow comments
上级
9d2f49c6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
22 addition
and
33 deletion
+22
-33
python/paddle/utils/image_multiproc.py
python/paddle/utils/image_multiproc.py
+22
-33
未找到文件。
python/paddle/utils/image_multiproc.py
浏览文件 @
84d47ac2
...
@@ -3,7 +3,8 @@ import numpy as np
...
@@ -3,7 +3,8 @@ import numpy as np
from
PIL
import
Image
from
PIL
import
Image
from
cStringIO
import
StringIO
from
cStringIO
import
StringIO
import
multiprocessing
import
multiprocessing
from
functools
import
partial
import
functools
import
itertools
from
paddle.utils.image_util
import
*
from
paddle.utils.image_util
import
*
from
paddle.trainer.config_parser
import
logger
from
paddle.trainer.config_parser
import
logger
...
@@ -14,10 +15,12 @@ except ImportError:
...
@@ -14,10 +15,12 @@ except ImportError:
logger
.
warning
(
"OpenCV2 is not installed, using PIL to prcoess"
)
logger
.
warning
(
"OpenCV2 is not installed, using PIL to prcoess"
)
cv2
=
None
cv2
=
None
__all__
=
[
"CvTransformer"
,
"PILTransformer"
,
"MultiProcessImageTransformer"
]
class
CvTransfomer
(
ImageTransformer
):
class
CvTransformer
(
ImageTransformer
):
"""
"""
CvTransfomer used python-opencv to process image.
CvTransfo
r
mer used python-opencv to process image.
"""
"""
def
__init__
(
def
__init__
(
...
@@ -97,9 +100,9 @@ class CvTransfomer(ImageTransformer):
...
@@ -97,9 +100,9 @@ class CvTransfomer(ImageTransformer):
return
self
.
transform
(
im
)
return
self
.
transform
(
im
)
class
PILTransfomer
(
ImageTransformer
):
class
PILTransfo
r
mer
(
ImageTransformer
):
"""
"""
PILTransfomer used PIL to process image.
PILTransfo
r
mer used PIL to process image.
"""
"""
def
__init__
(
def
__init__
(
...
@@ -170,8 +173,11 @@ class PILTransfomer(ImageTransformer):
...
@@ -170,8 +173,11 @@ class PILTransfomer(ImageTransformer):
return
self
.
transform
(
im
)
return
self
.
transform
(
im
)
def
warpper
(
cls
,
(
dat
,
label
)):
def
job
(
is_img_string
,
transformer
,
(
data
,
label
)):
return
cls
.
job
(
dat
,
label
)
if
is_img_string
:
return
transformer
.
transform_from_string
(
data
),
label
else
:
return
transformer
.
transform_from_file
(
data
),
label
class
MultiProcessImageTransformer
(
object
):
class
MultiProcessImageTransformer
(
object
):
...
@@ -238,36 +244,19 @@ class MultiProcessImageTransformer(object):
...
@@ -238,36 +244,19 @@ class MultiProcessImageTransformer(object):
:type is_img_string: bool.
:type is_img_string: bool.
"""
"""
self
.
procnum
=
procnum
self
.
pool
=
multiprocessing
.
Pool
(
procnum
)
self
.
pool
=
multiprocessing
.
Pool
(
procnum
)
self
.
is_img_string
=
is_img_string
self
.
is_img_string
=
is_img_string
if
cv2
is
not
None
:
if
cv2
is
not
None
:
self
.
transformer
=
CvTransfomer
(
resize_size
,
crop_size
,
transpose
,
self
.
transformer
=
CvTransformer
(
resize_size
,
crop_size
,
transpose
,
channel_swap
,
mean
,
is_train
,
is_color
)
else
:
self
.
transformer
=
PILTransfomer
(
resize_size
,
crop_size
,
transpose
,
channel_swap
,
mean
,
is_train
,
channel_swap
,
mean
,
is_train
,
is_color
)
is_color
)
def
run
(
self
,
data
,
label
):
try
:
fun
=
partial
(
warpper
,
self
)
return
self
.
pool
.
imap_unordered
(
fun
,
zip
(
data
,
label
),
chunksize
=
5
)
except
KeyboardInterrupt
:
self
.
pool
.
terminate
()
except
Exception
,
e
:
self
.
pool
.
terminate
()
def
job
(
self
,
data
,
label
):
if
self
.
is_img_string
:
return
self
.
transformer
.
transform_from_string
(
data
),
label
else
:
else
:
return
self
.
transformer
.
transform_from_file
(
data
),
label
self
.
transformer
=
PILTransformer
(
resize_size
,
crop_size
,
transpose
,
channel_swap
,
mean
,
is_train
,
def
__getstate__
(
self
):
is_color
)
self_dict
=
self
.
__dict__
.
copy
()
del
self_dict
[
'pool'
]
return
self_dict
def
__setstate__
(
self
,
state
):
def
run
(
self
,
data
,
label
):
self
.
__dict__
.
update
(
state
)
fun
=
functools
.
partial
(
job
,
self
.
is_img_string
,
self
.
transformer
)
return
self
.
pool
.
imap_unordered
(
fun
,
itertools
.
izip
(
data
,
label
),
chunksize
=
100
*
self
.
procnum
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录