Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
44f66a45
M
models
项目概览
PaddlePaddle
/
models
1 年多 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
44f66a45
编写于
8月 27, 2018
作者:
M
minqiyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix the random difference between python2 and python3
上级
597bae24
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
44 addition
and
43 deletion
+44
-43
fluid/image_classification/reader.py
fluid/image_classification/reader.py
+14
-13
fluid/image_classification/train.py
fluid/image_classification/train.py
+1
-0
fluid/object_detection/image_util.py
fluid/object_detection/image_util.py
+25
-25
fluid/object_detection/reader.py
fluid/object_detection/reader.py
+4
-5
未找到文件。
fluid/image_classification/reader.py
浏览文件 @
44f66a45
...
...
@@ -7,6 +7,7 @@ import paddle
from
PIL
import
Image
,
ImageEnhance
random
.
seed
(
0
)
np
.
random
.
seed
(
0
)
DATA_DIM
=
224
...
...
@@ -36,8 +37,8 @@ def crop_image(img, target_size, center):
w_start
=
(
width
-
size
)
/
2
h_start
=
(
height
-
size
)
/
2
else
:
w_start
=
random
.
randint
(
0
,
width
-
size
)
h_start
=
random
.
randint
(
0
,
height
-
size
)
w_start
=
np
.
random
.
randint
(
0
,
width
-
size
)
h_start
=
np
.
random
.
randint
(
0
,
height
-
size
)
w_end
=
w_start
+
size
h_end
=
h_start
+
size
img
=
img
.
crop
((
w_start
,
h_start
,
w_end
,
h_end
))
...
...
@@ -45,7 +46,7 @@ def crop_image(img, target_size, center):
def
random_crop
(
img
,
size
,
scale
=
[
0.08
,
1.0
],
ratio
=
[
3.
/
4.
,
4.
/
3.
]):
aspect_ratio
=
math
.
sqrt
(
random
.
uniform
(
*
ratio
))
aspect_ratio
=
math
.
sqrt
(
np
.
random
.
uniform
(
*
ratio
))
w
=
1.
*
aspect_ratio
h
=
1.
/
aspect_ratio
...
...
@@ -54,14 +55,14 @@ def random_crop(img, size, scale=[0.08, 1.0], ratio=[3. / 4., 4. / 3.]):
scale_max
=
min
(
scale
[
1
],
bound
)
scale_min
=
min
(
scale
[
0
],
bound
)
target_area
=
img
.
size
[
0
]
*
img
.
size
[
1
]
*
random
.
uniform
(
scale_min
,
target_area
=
img
.
size
[
0
]
*
img
.
size
[
1
]
*
np
.
random
.
uniform
(
scale_min
,
scale_max
)
target_size
=
math
.
sqrt
(
target_area
)
w
=
int
(
target_size
*
w
)
h
=
int
(
target_size
*
h
)
i
=
random
.
randint
(
0
,
img
.
size
[
0
]
-
w
)
j
=
random
.
randint
(
0
,
img
.
size
[
1
]
-
h
)
i
=
np
.
random
.
randint
(
0
,
img
.
size
[
0
]
-
w
)
j
=
np
.
random
.
randint
(
0
,
img
.
size
[
1
]
-
h
)
img
=
img
.
crop
((
i
,
j
,
i
+
w
,
j
+
h
))
img
=
img
.
resize
((
size
,
size
),
Image
.
LANCZOS
)
...
...
@@ -69,26 +70,26 @@ def random_crop(img, size, scale=[0.08, 1.0], ratio=[3. / 4., 4. / 3.]):
def
rotate_image
(
img
):
angle
=
random
.
randint
(
-
10
,
10
)
angle
=
np
.
random
.
randint
(
-
10
,
10
)
img
=
img
.
rotate
(
angle
)
return
img
def
distort_color
(
img
):
def
random_brightness
(
img
,
lower
=
0.5
,
upper
=
1.5
):
e
=
random
.
uniform
(
lower
,
upper
)
e
=
np
.
random
.
uniform
(
lower
,
upper
)
return
ImageEnhance
.
Brightness
(
img
).
enhance
(
e
)
def
random_contrast
(
img
,
lower
=
0.5
,
upper
=
1.5
):
e
=
random
.
uniform
(
lower
,
upper
)
e
=
np
.
random
.
uniform
(
lower
,
upper
)
return
ImageEnhance
.
Contrast
(
img
).
enhance
(
e
)
def
random_color
(
img
,
lower
=
0.5
,
upper
=
1.5
):
e
=
random
.
uniform
(
lower
,
upper
)
e
=
np
.
random
.
uniform
(
lower
,
upper
)
return
ImageEnhance
.
Color
(
img
).
enhance
(
e
)
ops
=
[
random_brightness
,
random_contrast
,
random_color
]
random
.
shuffle
(
ops
)
np
.
random
.
shuffle
(
ops
)
img
=
ops
[
0
](
img
)
img
=
ops
[
1
](
img
)
...
...
@@ -110,7 +111,7 @@ def process_image(sample, mode, color_jitter, rotate):
if
mode
==
'train'
:
if
color_jitter
:
img
=
distort_color
(
img
)
if
random
.
randint
(
0
,
1
)
==
1
:
if
np
.
random
.
randint
(
0
,
1
)
==
1
:
img
=
img
.
transpose
(
Image
.
FLIP_LEFT_RIGHT
)
if
img
.
mode
!=
'RGB'
:
...
...
@@ -135,7 +136,7 @@ def _reader_creator(file_list,
with
open
(
file_list
)
as
flist
:
lines
=
[
line
.
strip
()
for
line
in
flist
]
if
shuffle
:
random
.
shuffle
(
lines
)
np
.
random
.
shuffle
(
lines
)
for
line
in
lines
:
if
mode
==
'train'
or
mode
==
'val'
:
img_path
,
label
=
line
.
split
()
...
...
fluid/image_classification/train.py
浏览文件 @
44f66a45
...
...
@@ -172,6 +172,7 @@ def train(args):
# but it is time consuming. For faster speed, need another dataset.
import
random
random
.
seed
(
0
)
np
.
random
.
seed
(
0
)
train_reader
=
paddle
.
batch
(
flowers
.
train
(
use_xmap
=
False
),
batch_size
=
train_batch_size
)
test_reader
=
paddle
.
batch
(
...
...
fluid/object_detection/image_util.py
浏览文件 @
44f66a45
...
...
@@ -36,8 +36,8 @@ def bbox_area(src_bbox):
def
generate_sample
(
sampler
):
scale
=
random
.
uniform
(
sampler
.
min_scale
,
sampler
.
max_scale
)
aspect_ratio
=
random
.
uniform
(
sampler
.
min_aspect_ratio
,
scale
=
np
.
random
.
uniform
(
sampler
.
min_scale
,
sampler
.
max_scale
)
aspect_ratio
=
np
.
random
.
uniform
(
sampler
.
min_aspect_ratio
,
sampler
.
max_aspect_ratio
)
aspect_ratio
=
max
(
aspect_ratio
,
(
scale
**
2.0
))
aspect_ratio
=
min
(
aspect_ratio
,
1
/
(
scale
**
2.0
))
...
...
@@ -46,8 +46,8 @@ def generate_sample(sampler):
bbox_height
=
scale
/
(
aspect_ratio
**
0.5
)
xmin_bound
=
1
-
bbox_width
ymin_bound
=
1
-
bbox_height
xmin
=
random
.
uniform
(
0
,
xmin_bound
)
ymin
=
random
.
uniform
(
0
,
ymin_bound
)
xmin
=
np
.
random
.
uniform
(
0
,
xmin_bound
)
ymin
=
np
.
random
.
uniform
(
0
,
ymin_bound
)
xmax
=
xmin
+
bbox_width
ymax
=
ymin
+
bbox_height
sampled_bbox
=
bbox
(
xmin
,
ymin
,
xmax
,
ymax
)
...
...
@@ -167,36 +167,36 @@ def crop_image(img, bbox_labels, sample_bbox, image_width, image_height):
def
random_brightness
(
img
,
settings
):
prob
=
random
.
uniform
(
0
,
1
)
prob
=
np
.
random
.
uniform
(
0
,
1
)
if
prob
<
settings
.
_brightness_prob
:
delta
=
random
.
uniform
(
-
settings
.
_brightness_delta
,
delta
=
np
.
random
.
uniform
(
-
settings
.
_brightness_delta
,
settings
.
_brightness_delta
)
+
1
img
=
ImageEnhance
.
Brightness
(
img
).
enhance
(
delta
)
return
img
def
random_contrast
(
img
,
settings
):
prob
=
random
.
uniform
(
0
,
1
)
prob
=
np
.
random
.
uniform
(
0
,
1
)
if
prob
<
settings
.
_contrast_prob
:
delta
=
random
.
uniform
(
-
settings
.
_contrast_delta
,
delta
=
np
.
random
.
uniform
(
-
settings
.
_contrast_delta
,
settings
.
_contrast_delta
)
+
1
img
=
ImageEnhance
.
Contrast
(
img
).
enhance
(
delta
)
return
img
def
random_saturation
(
img
,
settings
):
prob
=
random
.
uniform
(
0
,
1
)
prob
=
np
.
random
.
uniform
(
0
,
1
)
if
prob
<
settings
.
_saturation_prob
:
delta
=
random
.
uniform
(
-
settings
.
_saturation_delta
,
delta
=
np
.
random
.
uniform
(
-
settings
.
_saturation_delta
,
settings
.
_saturation_delta
)
+
1
img
=
ImageEnhance
.
Color
(
img
).
enhance
(
delta
)
return
img
def
random_hue
(
img
,
settings
):
prob
=
random
.
uniform
(
0
,
1
)
prob
=
np
.
random
.
uniform
(
0
,
1
)
if
prob
<
settings
.
_hue_prob
:
delta
=
random
.
uniform
(
-
settings
.
_hue_delta
,
settings
.
_hue_delta
)
delta
=
np
.
random
.
uniform
(
-
settings
.
_hue_delta
,
settings
.
_hue_delta
)
img_hsv
=
np
.
array
(
img
.
convert
(
'HSV'
))
img_hsv
[:,
:,
0
]
=
img_hsv
[:,
:,
0
]
+
delta
img
=
Image
.
fromarray
(
img_hsv
,
mode
=
'HSV'
).
convert
(
'RGB'
)
...
...
@@ -204,30 +204,30 @@ def random_hue(img, settings):
def
distort_image
(
img
,
settings
):
prob
=
random
.
uniform
(
0
,
1
)
prob
=
np
.
random
.
uniform
(
0
,
1
)
# Apply different distort order
if
prob
>
0.5
:
img
=
random_brightness
(
img
,
settings
)
img
=
random_contrast
(
img
,
settings
)
img
=
random_saturation
(
img
,
settings
)
img
=
random_hue
(
img
,
settings
)
img
=
np
.
random_brightness
(
img
,
settings
)
img
=
np
.
random_contrast
(
img
,
settings
)
img
=
np
.
random_saturation
(
img
,
settings
)
img
=
np
.
random_hue
(
img
,
settings
)
else
:
img
=
random_brightness
(
img
,
settings
)
img
=
random_saturation
(
img
,
settings
)
img
=
random_hue
(
img
,
settings
)
img
=
random_contrast
(
img
,
settings
)
img
=
np
.
random_brightness
(
img
,
settings
)
img
=
np
.
random_saturation
(
img
,
settings
)
img
=
np
.
random_hue
(
img
,
settings
)
img
=
np
.
random_contrast
(
img
,
settings
)
return
img
def
expand_image
(
img
,
bbox_labels
,
img_width
,
img_height
,
settings
):
prob
=
random
.
uniform
(
0
,
1
)
prob
=
np
.
random
.
uniform
(
0
,
1
)
if
prob
<
settings
.
_expand_prob
:
if
settings
.
_expand_max_ratio
-
1
>=
0.01
:
expand_ratio
=
random
.
uniform
(
1
,
settings
.
_expand_max_ratio
)
expand_ratio
=
np
.
random
.
uniform
(
1
,
settings
.
_expand_max_ratio
)
height
=
int
(
img_height
*
expand_ratio
)
width
=
int
(
img_width
*
expand_ratio
)
h_off
=
math
.
floor
(
random
.
uniform
(
0
,
height
-
img_height
))
w_off
=
math
.
floor
(
random
.
uniform
(
0
,
width
-
img_width
))
h_off
=
math
.
floor
(
np
.
random
.
uniform
(
0
,
height
-
img_height
))
w_off
=
math
.
floor
(
np
.
random
.
uniform
(
0
,
width
-
img_width
))
expand_bbox
=
bbox
(
-
w_off
/
img_width
,
-
h_off
/
img_height
,
(
width
-
w_off
)
/
img_width
,
(
height
-
h_off
)
/
img_height
)
...
...
fluid/object_detection/reader.py
浏览文件 @
44f66a45
...
...
@@ -14,7 +14,6 @@
import
image_util
from
paddle.utils.image_util
import
*
import
random
from
PIL
import
Image
from
PIL
import
ImageDraw
import
numpy
as
np
...
...
@@ -140,7 +139,7 @@ def preprocess(img, bbox_labels, mode, settings):
img
=
np
.
array
(
img
)
if
len
(
sampled_bbox
)
>
0
:
idx
=
int
(
random
.
uniform
(
0
,
len
(
sampled_bbox
)))
idx
=
int
(
np
.
random
.
uniform
(
0
,
len
(
sampled_bbox
)))
img
,
sampled_labels
=
image_util
.
crop_image
(
img
,
bbox_labels
,
sampled_bbox
[
idx
],
img_width
,
img_height
)
...
...
@@ -149,7 +148,7 @@ def preprocess(img, bbox_labels, mode, settings):
img
=
np
.
array
(
img
)
if
mode
==
'train'
:
mirror
=
int
(
random
.
uniform
(
0
,
2
))
mirror
=
int
(
np
.
random
.
uniform
(
0
,
2
))
if
mirror
==
1
:
img
=
img
[:,
::
-
1
,
:]
for
i
in
six
.
moves
.
xrange
(
len
(
sampled_labels
)):
...
...
@@ -185,7 +184,7 @@ def coco(settings, file_list, mode, shuffle):
def
reader
():
if
mode
==
'train'
and
shuffle
:
random
.
shuffle
(
images
)
np
.
random
.
shuffle
(
images
)
for
image
in
images
:
image_name
=
image
[
'file_name'
]
image_path
=
os
.
path
.
join
(
settings
.
data_dir
,
image_name
)
...
...
@@ -240,7 +239,7 @@ def pascalvoc(settings, file_list, mode, shuffle):
def
reader
():
if
mode
==
'train'
and
shuffle
:
random
.
shuffle
(
images
)
np
.
random
.
shuffle
(
images
)
for
image
in
images
:
image_path
,
label_path
=
image
.
split
()
image_path
=
os
.
path
.
join
(
settings
.
data_dir
,
image_path
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录