Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
cb09f5f4
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 1 年 前同步成功
通知
282
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
cb09f5f4
编写于
3月 13, 2019
作者:
W
wuzewu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add augmentation func
上级
86f6df41
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
202 addition
and
0 deletion
+202
-0
paddle_hub/data/augmentation.py
paddle_hub/data/augmentation.py
+202
-0
未找到文件。
paddle_hub/data/augmentation.py
浏览文件 @
cb09f5f4
...
...
@@ -11,3 +11,205 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
from
PIL
import
Image
,
ImageEnhance
from
paddle_hub.tools
import
utils
import
numpy
as
np
def
_check_range_0_1
(
value
):
value
=
value
if
value
<=
1
else
1
value
=
value
if
value
>=
0
else
0
return
value
def
_check_bound
(
low
,
high
):
low
=
_check_range_0_1
(
low
)
high
=
_check_range_0_1
(
high
)
high
=
high
if
high
>=
low
else
low
return
low
,
high
def
_check_img
(
img
):
if
isinstance
(
img
,
str
):
utils
.
check_path
(
img
)
img
=
Image
.
open
(
img
)
return
img
def
_check_img_and_size
(
img
,
width
,
height
):
img
=
_check_img
(
img
)
img_width
,
img_height
=
img
.
size
height
=
height
if
img_height
>
height
else
img_height
height
=
img_height
if
height
<=
0
else
height
width
=
width
if
img_width
>
width
else
img_width
width
=
img_width
if
width
<=
0
else
width
return
img
,
width
,
height
def
image_crop_from_position
(
img
,
width
,
height
,
w_start
,
h_start
):
img
,
width
,
height
=
_check_img_and_size
(
img
,
width
,
height
)
w_end
=
w_start
+
width
h_end
=
h_start
+
height
return
img
.
crop
((
w_start
,
h_start
,
w_end
,
h_end
))
def
image_crop_from_TL
(
img
,
width
,
height
):
w_start
=
h_start
=
0
return
image_crop_from_position
(
img
,
width
,
height
,
w_start
,
h_start
)
def
image_crop_from_TR
(
img
,
width
,
height
):
img
,
width
,
height
=
_check_img_and_size
(
img
,
width
,
height
)
w_start
=
img
.
size
[
0
]
-
width
h_start
=
0
return
image_crop_from_position
(
img
,
width
,
height
,
w_start
,
h_start
)
def
image_crop_from_BL
(
img
,
width
,
height
):
img
,
width
,
height
=
_check_img_and_size
(
img
,
width
,
height
)
w_start
=
0
h_start
=
img
.
size
[
1
]
-
height
return
image_crop_from_position
(
img
,
width
,
height
,
w_start
,
h_start
)
def
image_crop_from_BR
(
img
,
width
,
height
):
img
,
width
,
height
=
_check_img_and_size
(
img
,
width
,
height
)
w_start
=
img
.
size
[
0
]
-
width
h_start
=
img
.
size
[
1
]
-
height
return
image_crop_from_position
(
img
,
width
,
height
,
w_start
,
h_start
)
def
image_crop_from_centor
(
img
,
width
,
height
):
img
=
_check_img
(
img
)
w_start
=
(
img
.
size
[
0
]
-
width
)
/
2
h_start
=
(
img
.
size
[
1
]
-
height
)
/
2
return
image_crop_from_position
(
img
,
width
,
height
,
w_start
,
h_start
)
def
image_crop_random
(
img
,
width
=
0
,
height
=
0
):
img
=
_check_img
(
img
)
width
=
width
if
width
else
np
.
random
.
randint
(
int
(
img
.
size
[
0
]
/
10
),
img
.
size
[
0
])
height
=
height
if
height
else
np
.
random
.
randint
(
int
(
img
.
size
[
1
]
/
10
),
img
.
size
[
1
])
w_start
=
np
.
random
.
randint
(
0
,
img
.
size
[
0
]
-
width
)
h_start
=
np
.
random
.
randint
(
0
,
img
.
size
[
1
]
-
height
)
return
image_crop_from_position
(
img
,
width
,
height
,
w_start
,
h_start
)
def
image_resize
(
img
,
width
,
height
,
interpolation_method
=
Image
.
LANCZOS
):
img
=
_check_img
(
img
)
return
img
.
resize
((
width
,
height
),
interpolation_method
)
def
image_resize_random
(
img
,
width
=
0
,
height
=
0
,
interpolation_method
=
Image
.
LANCZOS
):
img
=
_check_img
(
img
)
width
=
width
if
width
else
np
.
random
.
randint
(
int
(
img
.
size
[
0
]
/
10
),
img
.
size
[
0
])
height
=
height
if
height
else
np
.
random
.
randint
(
int
(
img
.
size
[
1
]
/
10
),
img
.
size
[
1
])
return
image_resize
(
img
,
width
,
height
,
interpolation_method
)
def
image_rotate
(
img
,
angle
,
expand
=
False
):
img
=
_check_img
(
img
)
return
img
.
rotate
(
angle
,
expand
=
expand
)
def
image_rotate_random
(
img
,
low
=
0
,
high
=
360
,
expand
=
False
):
angle
=
np
.
random
.
randint
(
low
,
high
)
return
image_rotate
(
img
,
angle
,
expand
)
def
image_brightness_adjust
(
img
,
delta
):
delta
=
_check_range_0_1
(
delta
)
img
=
_check_img
(
img
)
return
ImageEnhance
.
Brightness
(
img
).
enhance
(
delta
)
def
image_brightness_adjust_random
(
img
,
low
=
0
,
high
=
1
):
low
,
high
=
_check_bound
(
low
,
high
)
delta
=
np
.
random
.
uniform
(
low
,
high
)
return
image_brightness_adjust
(
img
,
delta
)
def
image_contrast_adjust
(
img
,
delta
):
delta
=
_check_range_0_1
(
delta
)
img
=
_check_img
(
img
)
return
ImageEnhance
.
Contrast
(
img
).
enhance
(
delta
)
def
image_contrast_adjust_random
(
img
,
low
=
0
,
high
=
1
):
low
,
high
=
_check_bound
(
low
,
high
)
delta
=
np
.
random
.
uniform
(
low
,
high
)
return
image_contrast_adjust
(
img
,
delta
)
def
image_saturation_adjust
(
img
,
delta
):
delta
=
_check_range_0_1
(
delta
)
img
=
_check_img
(
img
)
return
ImageEnhance
.
Color
(
img
).
enhance
(
delta
)
def
image_saturation_adjust_random
(
img
,
low
=
0
,
high
=
1
):
low
,
high
=
_check_bound
(
low
,
high
)
delta
=
np
.
random
.
uniform
(
low
,
high
)
return
image_saturation_adjust
(
img
,
delta
)
def
image_flip_top_bottom
(
img
):
img
=
_check_img
(
img
)
return
img
.
transpose
(
Image
.
FLIP_TOP_BOTTOM
)
def
image_flip_left_right
(
img
):
img
=
_check_img
(
img
)
return
img
.
transpose
(
Image
.
FLIP_LEFT_RIGHT
)
def
image_flip_random
(
img
):
img
=
_check_img
(
img
)
flag
=
np
.
random
.
randint
(
0
,
1
)
if
flag
:
return
image_flip_top_bottom
(
img
)
else
:
return
image_flip_left_right
(
img
)
def
image_random_process
(
img
,
enable_resize
=
True
,
enable_crop
=
True
,
enable_rotate
=
True
,
enable_brightness_adjust
=
True
,
enable_contrast_adjust
=
True
,
enable_saturation_adjust
=
True
,
enable_flip
=
True
):
operator_list
=
[]
if
enable_resize
:
operator_list
.
append
(
image_resize_random
)
if
enable_crop
:
operator_list
.
append
(
image_crop_random
)
if
enable_rotate
:
operator_list
.
append
(
image_rotate_random
)
if
enable_brightness_adjust
:
operator_list
.
append
(
image_brightness_adjust_random
)
if
enable_contrast_adjust
:
operator_list
.
append
(
image_contrast_adjust_random
)
if
enable_saturation_adjust
:
operator_list
.
append
(
image_saturation_adjust_random
)
if
enable_flip
:
operator_list
.
append
(
image_flip_random
)
if
not
operator_list
:
return
img
random_op_index
=
np
.
random
.
randint
(
0
,
len
(
operator_list
)
-
1
)
random_op
=
operator_list
[
random_op_index
]
return
random_op
(
img
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录