Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
e906eb5b
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
e906eb5b
编写于
5月 12, 2022
作者:
J
JYChen
提交者:
GitHub
5月 12, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add batch tensor support for some vision transforms functions (#42701)
上级
43d70bcc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
147 addition
and
7 deletion
+147
-7
python/paddle/tests/test_transforms.py
python/paddle/tests/test_transforms.py
+125
-0
python/paddle/vision/transforms/functional_tensor.py
python/paddle/vision/transforms/functional_tensor.py
+14
-6
python/paddle/vision/transforms/transforms.py
python/paddle/vision/transforms/transforms.py
+8
-1
未找到文件。
python/paddle/tests/test_transforms.py
浏览文件 @
e906eb5b
...
...
@@ -458,6 +458,20 @@ class TestTransformsTensor(TestTransformsCV2):
trans
=
transforms
.
Compose
([
transforms
.
ColorJitter
(
1.1
,
2.2
,
0.8
,
0.1
)])
self
.
do_transform
(
trans
)
color_jitter_trans
=
transforms
.
ColorJitter
(
1.2
,
0.2
,
0.5
,
0.2
)
batch_input
=
paddle
.
rand
((
2
,
3
,
4
,
4
),
dtype
=
paddle
.
float32
)
result
=
color_jitter_trans
(
batch_input
)
def
test_perspective
(
self
):
trans
=
transforms
.
RandomPerspective
(
prob
=
1.0
,
distortion_scale
=
0.7
)
batch_input
=
paddle
.
rand
((
2
,
3
,
4
,
4
),
dtype
=
paddle
.
float32
)
result
=
trans
(
batch_input
)
def
test_affine
(
self
):
trans
=
transforms
.
RandomAffine
(
15
,
translate
=
[
0.1
,
0.1
])
batch_input
=
paddle
.
rand
((
2
,
3
,
4
,
4
),
dtype
=
paddle
.
float32
)
result
=
trans
(
batch_input
)
def
test_pad
(
self
):
trans
=
transforms
.
Compose
([
transforms
.
Pad
(
2
)])
self
.
do_transform
(
trans
)
...
...
@@ -508,6 +522,10 @@ class TestTransformsTensor(TestTransformsCV2):
])
self
.
do_transform
(
trans
)
erase_trans
=
transforms
.
RandomErasing
(
value
=
(
0.5
,
0.2
,
0.01
))
batch_input
=
paddle
.
rand
((
2
,
3
,
4
,
4
),
dtype
=
paddle
.
float32
)
result
=
erase_trans
(
batch_input
)
def
test_exception
(
self
):
trans
=
transforms
.
Compose
([
transforms
.
Resize
(
-
1
)])
...
...
@@ -1003,6 +1021,113 @@ class TestFunctional(unittest.TestCase):
# Tolerance : less than 6% of different pixels
assert
ratio_diff_pixels
<
0.06
def
test_batch_input
(
self
):
paddle
.
seed
(
777
)
batch_tensor
=
paddle
.
rand
((
2
,
3
,
8
,
8
),
dtype
=
paddle
.
float32
)
def
test_erase
(
batch_tensor
):
input1
,
input2
=
paddle
.
unbind
(
batch_tensor
,
axis
=
0
)
target_result
=
paddle
.
stack
([
F
.
erase
(
input1
,
1
,
1
,
2
,
2
,
0.5
),
F
.
erase
(
input2
,
1
,
1
,
2
,
2
,
0.5
)
])
batch_result
=
F
.
erase
(
batch_tensor
,
1
,
1
,
2
,
2
,
0.5
)
return
paddle
.
allclose
(
batch_result
,
target_result
)
self
.
assertTrue
(
test_erase
(
batch_tensor
))
def
test_affine
(
batch_tensor
):
input1
,
input2
=
paddle
.
unbind
(
batch_tensor
,
axis
=
0
)
target_result
=
paddle
.
stack
([
F
.
affine
(
input1
,
45
,
translate
=
[
0.2
,
0.2
],
scale
=
0.5
,
shear
=
[
-
10
,
10
]),
F
.
affine
(
input2
,
45
,
translate
=
[
0.2
,
0.2
],
scale
=
0.5
,
shear
=
[
-
10
,
10
])
])
batch_result
=
F
.
affine
(
batch_tensor
,
45
,
translate
=
[
0.2
,
0.2
],
scale
=
0.5
,
shear
=
[
-
10
,
10
])
return
paddle
.
allclose
(
batch_result
,
target_result
)
self
.
assertTrue
(
test_affine
(
batch_tensor
))
def
test_perspective
(
batch_tensor
):
input1
,
input2
=
paddle
.
unbind
(
batch_tensor
,
axis
=
0
)
startpoints
=
[[
0
,
0
],
[
3
,
0
],
[
4
,
5
],
[
6
,
7
]]
endpoints
=
[[
0
,
1
],
[
3
,
1
],
[
4
,
4
],
[
5
,
7
]]
target_result
=
paddle
.
stack
([
F
.
perspective
(
input1
,
startpoints
,
endpoints
),
F
.
perspective
(
input2
,
startpoints
,
endpoints
)
])
batch_result
=
F
.
perspective
(
batch_tensor
,
startpoints
,
endpoints
)
return
paddle
.
allclose
(
batch_result
,
target_result
)
self
.
assertTrue
(
test_perspective
(
batch_tensor
))
def
test_adjust_brightness
(
batch_tensor
):
input1
,
input2
=
paddle
.
unbind
(
batch_tensor
,
axis
=
0
)
target_result
=
paddle
.
stack
([
F
.
adjust_brightness
(
input1
,
2.1
),
F
.
adjust_brightness
(
input2
,
2.1
)
])
batch_result
=
F
.
adjust_brightness
(
batch_tensor
,
2.1
)
return
paddle
.
allclose
(
batch_result
,
target_result
)
self
.
assertTrue
(
test_adjust_brightness
(
batch_tensor
))
def
test_adjust_contrast
(
batch_tensor
):
input1
,
input2
=
paddle
.
unbind
(
batch_tensor
,
axis
=
0
)
target_result
=
paddle
.
stack
([
F
.
adjust_contrast
(
input1
,
0.3
),
F
.
adjust_contrast
(
input2
,
0.3
)
])
batch_result
=
F
.
adjust_contrast
(
batch_tensor
,
0.3
)
return
paddle
.
allclose
(
batch_result
,
target_result
)
self
.
assertTrue
(
test_adjust_contrast
(
batch_tensor
))
def
test_adjust_saturation
(
batch_tensor
):
input1
,
input2
=
paddle
.
unbind
(
batch_tensor
,
axis
=
0
)
target_result
=
paddle
.
stack
([
F
.
adjust_saturation
(
input1
,
1.1
),
F
.
adjust_saturation
(
input2
,
1.1
)
])
batch_result
=
F
.
adjust_saturation
(
batch_tensor
,
1.1
)
return
paddle
.
allclose
(
batch_result
,
target_result
)
self
.
assertTrue
(
test_adjust_saturation
(
batch_tensor
))
def
test_adjust_hue
(
batch_tensor
):
input1
,
input2
=
paddle
.
unbind
(
batch_tensor
,
axis
=
0
)
target_result
=
paddle
.
stack
(
[
F
.
adjust_hue
(
input1
,
-
0.2
),
F
.
adjust_hue
(
input2
,
-
0.2
)])
batch_result
=
F
.
adjust_hue
(
batch_tensor
,
-
0.2
)
return
paddle
.
allclose
(
batch_result
,
target_result
)
self
.
assertTrue
(
test_adjust_hue
(
batch_tensor
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/vision/transforms/functional_tensor.py
浏览文件 @
e906eb5b
...
...
@@ -28,8 +28,9 @@ __all__ = []
def
_assert_image_tensor
(
img
,
data_format
):
if
not
isinstance
(
img
,
paddle
.
Tensor
)
or
img
.
ndim
!=
3
or
not
data_format
.
lower
()
in
(
'chw'
,
'hwc'
):
img
,
paddle
.
Tensor
)
or
img
.
ndim
<
3
or
img
.
ndim
>
4
or
not
data_format
.
lower
()
in
(
'chw'
,
'hwc'
):
raise
RuntimeError
(
'not support [type={}, ndim={}, data_format={}] paddle image'
.
format
(
type
(
img
),
img
.
ndim
,
data_format
))
...
...
@@ -276,7 +277,10 @@ def affine(img, matrix, interpolation="nearest", fill=None, data_format='CHW'):
paddle.Tensor: Affined image.
"""
img
=
img
.
unsqueeze
(
0
)
ndim
=
len
(
img
.
shape
)
if
ndim
==
3
:
img
=
img
.
unsqueeze
(
0
)
img
=
img
if
data_format
.
lower
()
==
'chw'
else
img
.
transpose
((
0
,
3
,
1
,
2
))
matrix
=
paddle
.
to_tensor
(
matrix
,
place
=
img
.
place
)
...
...
@@ -292,8 +296,9 @@ def affine(img, matrix, interpolation="nearest", fill=None, data_format='CHW'):
out
=
_grid_transform
(
img
,
grid
,
mode
=
interpolation
,
fill
=
fill
)
out
=
out
if
data_format
.
lower
()
==
'chw'
else
out
.
transpose
((
0
,
2
,
3
,
1
))
out
=
out
.
squeeze
(
0
)
if
ndim
==
3
else
out
return
out
.
squeeze
(
0
)
return
out
def
rotate
(
img
,
...
...
@@ -443,7 +448,9 @@ def perspective(img,
"""
img
=
img
.
unsqueeze
(
0
)
ndim
=
len
(
img
.
shape
)
if
ndim
==
3
:
img
=
img
.
unsqueeze
(
0
)
img
=
img
if
data_format
.
lower
()
==
'chw'
else
img
.
transpose
((
0
,
3
,
1
,
2
))
ow
,
oh
=
img
.
shape
[
-
1
],
img
.
shape
[
-
2
]
...
...
@@ -454,8 +461,9 @@ def perspective(img,
out
=
_grid_transform
(
img
,
grid
,
mode
=
interpolation
,
fill
=
fill
)
out
=
out
if
data_format
.
lower
()
==
'chw'
else
out
.
transpose
((
0
,
2
,
3
,
1
))
out
=
out
.
squeeze
(
0
)
if
ndim
==
3
else
out
return
out
.
squeeze
(
0
)
return
out
def
vflip
(
img
,
data_format
=
'CHW'
):
...
...
python/paddle/vision/transforms/transforms.py
浏览文件 @
e906eb5b
...
...
@@ -45,7 +45,14 @@ def _get_image_size(img):
elif
F
.
_is_numpy_image
(
img
):
return
img
.
shape
[:
2
][::
-
1
]
elif
F
.
_is_tensor_image
(
img
):
return
img
.
shape
[
1
:][::
-
1
]
# chw
if
len
(
img
.
shape
)
==
3
:
return
img
.
shape
[
1
:][::
-
1
]
# chw -> wh
elif
len
(
img
.
shape
)
==
4
:
return
img
.
shape
[
2
:][::
-
1
]
# nchw -> wh
else
:
raise
ValueError
(
"The dim for input Tensor should be 3-D or 4-D, but received {}"
.
format
(
len
(
img
.
shape
)))
else
:
raise
TypeError
(
"Unexpected type {}"
.
format
(
type
(
img
)))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录