Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2afa7b93
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看板
未验证
提交
2afa7b93
编写于
12月 20, 2022
作者:
R
Ryan
提交者:
GitHub
12月 20, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2St] transforms.RandomRotation Support static mode (#49128)
上级
0edbd7a5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
101 addition
and
31 deletion
+101
-31
python/paddle/tests/test_transforms_static.py
python/paddle/tests/test_transforms_static.py
+17
-1
python/paddle/vision/transforms/functional_tensor.py
python/paddle/vision/transforms/functional_tensor.py
+78
-29
python/paddle/vision/transforms/transforms.py
python/paddle/vision/transforms/transforms.py
+6
-1
未找到文件。
python/paddle/tests/test_transforms_static.py
浏览文件 @
2afa7b93
...
...
@@ -30,7 +30,7 @@ class TestTransformUnitTestBase(unittest.TestCase):
self
.
set_trans_api
()
def
get_shape
(
self
):
return
(
64
,
64
,
3
)
return
(
3
,
64
,
64
)
def
set_trans_api
(
self
):
self
.
api
=
transforms
.
Resize
(
size
=
16
)
...
...
@@ -142,5 +142,21 @@ class TestRandomCrop_same(TestTransformUnitTestBase):
self
.
api
=
transforms
.
RandomCrop
(
self
.
crop_size
)
class
TestRandomRotation
(
TestTransformUnitTestBase
):
def
set_trans_api
(
self
):
degree
=
np
.
random
.
uniform
(
-
180
,
180
)
eps
=
10e-5
degree_tuple
=
(
degree
-
eps
,
degree
+
eps
)
self
.
api
=
transforms
.
RandomRotation
(
degree_tuple
)
class
TestRandomRotation_expand_True
(
TestTransformUnitTestBase
):
def
set_trans_api
(
self
):
degree
=
np
.
random
.
uniform
(
-
180
,
180
)
eps
=
10e-5
degree_tuple
=
(
degree
-
eps
,
degree
+
eps
)
self
.
api
=
transforms
.
RandomRotation
(
degree_tuple
,
expand
=
True
,
fill
=
3
)
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/vision/transforms/functional_tensor.py
浏览文件 @
2afa7b93
...
...
@@ -15,6 +15,8 @@
import
math
import
numbers
import
numpy
as
np
import
paddle
import
paddle.nn.functional
as
F
...
...
@@ -221,12 +223,23 @@ def _affine_grid(theta, w, h, ow, oh):
x_grid
=
paddle
.
linspace
(
-
ow
*
0.5
+
d
,
ow
*
0.5
+
d
-
1
,
ow
)
base_grid
[...,
0
]
=
x_grid
y_grid
=
paddle
.
linspace
(
-
oh
*
0.5
+
d
,
oh
*
0.5
+
d
-
1
,
oh
).
unsqueeze_
(
-
1
)
base_grid
[...,
1
]
=
y_grid
scaled_theta
=
theta
.
transpose
((
0
,
2
,
1
))
/
paddle
.
to_tensor
(
[
0.5
*
w
,
0.5
*
h
]
)
if
paddle
.
in_dynamic_mode
():
y_grid
=
paddle
.
linspace
(
-
oh
*
0.5
+
d
,
oh
*
0.5
+
d
-
1
,
oh
).
unsqueeze_
(
-
1
)
base_grid
[...,
1
]
=
y_grid
tmp
=
paddle
.
to_tensor
([
0.5
*
w
,
0.5
*
h
])
else
:
# To eliminate the warning:
# In static mode, unsqueeze_() is the same as unsqueeze() and does not perform inplace operation.
y_grid
=
paddle
.
linspace
(
-
oh
*
0.5
+
d
,
oh
*
0.5
+
d
-
1
,
oh
).
unsqueeze
(
-
1
)
base_grid
[...,
1
]
=
y_grid
tmp
=
paddle
.
assign
(
np
.
array
([
0.5
*
w
,
0.5
*
h
],
dtype
=
"float32"
))
scaled_theta
=
theta
.
transpose
((
0
,
2
,
1
))
/
tmp
output_grid
=
base_grid
.
reshape
((
1
,
oh
*
ow
,
3
)).
bmm
(
scaled_theta
)
return
output_grid
.
reshape
((
1
,
oh
,
ow
,
2
))
...
...
@@ -252,11 +265,21 @@ def _grid_transform(img, grid, mode, fill):
if
fill
is
not
None
:
mask
=
img
[:,
-
1
:,
:,
:]
# n 1 h w
img
=
img
[:,
:
-
1
,
:,
:]
# n c h w
mask
=
mask
.
expand_as
(
img
)
mask
=
mask
.
tile
([
1
,
img
.
shape
[
1
],
1
,
1
]
)
len_fill
=
len
(
fill
)
if
isinstance
(
fill
,
(
tuple
,
list
))
else
1
fill_img
=
(
paddle
.
to_tensor
(
fill
).
reshape
((
1
,
len_fill
,
1
,
1
)).
expand_as
(
img
)
)
if
paddle
.
in_dynamic_mode
():
fill_img
=
(
paddle
.
to_tensor
(
fill
)
.
reshape
((
1
,
len_fill
,
1
,
1
))
.
astype
(
img
.
dtype
)
.
expand_as
(
img
)
)
else
:
fill
=
np
.
array
(
fill
).
reshape
(
len_fill
).
astype
(
"float32"
)
fill_img
=
paddle
.
ones_like
(
img
)
*
paddle
.
assign
(
fill
).
reshape
(
[
1
,
len_fill
,
1
,
1
]
)
if
mode
==
'nearest'
:
mask
=
paddle
.
cast
(
mask
<
0.5
,
img
.
dtype
)
...
...
@@ -363,15 +386,29 @@ def rotate(
else
:
rotn_center
=
[(
p
-
s
*
0.5
)
for
p
,
s
in
zip
(
center
,
[
w
,
h
])]
angle
=
math
.
radians
(
angle
)
matrix
=
[
math
.
cos
(
angle
),
math
.
sin
(
angle
),
0.0
,
-
math
.
sin
(
angle
),
math
.
cos
(
angle
),
0.0
,
]
if
paddle
.
in_dynamic_mode
():
angle
=
math
.
radians
(
angle
)
matrix
=
[
math
.
cos
(
angle
),
math
.
sin
(
angle
),
0.0
,
-
math
.
sin
(
angle
),
math
.
cos
(
angle
),
0.0
,
]
matrix
=
paddle
.
to_tensor
(
matrix
,
place
=
img
.
place
)
else
:
angle
=
angle
/
180
*
math
.
pi
matrix
=
paddle
.
concat
(
[
paddle
.
cos
(
angle
),
paddle
.
sin
(
angle
),
paddle
.
zeros
([
1
]),
-
paddle
.
sin
(
angle
),
paddle
.
cos
(
angle
),
paddle
.
zeros
([
1
]),
]
)
matrix
[
2
]
+=
matrix
[
0
]
*
(
-
rotn_center
[
0
]
-
post_trans
[
0
])
+
matrix
[
1
]
*
(
-
rotn_center
[
1
]
-
post_trans
[
1
]
...
...
@@ -383,20 +420,29 @@ def rotate(
matrix
[
2
]
+=
rotn_center
[
0
]
matrix
[
5
]
+=
rotn_center
[
1
]
matrix
=
paddle
.
to_tensor
(
matrix
,
place
=
img
.
place
)
matrix
=
matrix
.
reshape
((
1
,
2
,
3
))
if
expand
:
# calculate output size
corners
=
paddle
.
to_tensor
(
[
[
-
0.5
*
w
,
-
0.5
*
h
,
1.0
],
[
-
0.5
*
w
,
0.5
*
h
,
1.0
],
[
0.5
*
w
,
0.5
*
h
,
1.0
],
[
0.5
*
w
,
-
0.5
*
h
,
1.0
],
],
place
=
matrix
.
place
,
).
astype
(
matrix
.
dtype
)
if
paddle
.
in_dynamic_mode
():
corners
=
paddle
.
to_tensor
(
[
[
-
0.5
*
w
,
-
0.5
*
h
,
1.0
],
[
-
0.5
*
w
,
0.5
*
h
,
1.0
],
[
0.5
*
w
,
0.5
*
h
,
1.0
],
[
0.5
*
w
,
-
0.5
*
h
,
1.0
],
],
place
=
matrix
.
place
,
).
astype
(
matrix
.
dtype
)
else
:
corners
=
paddle
.
assign
(
[
[
-
0.5
*
w
,
-
0.5
*
h
,
1.0
],
[
-
0.5
*
w
,
0.5
*
h
,
1.0
],
[
0.5
*
w
,
0.5
*
h
,
1.0
],
[
0.5
*
w
,
-
0.5
*
h
,
1.0
],
],
).
astype
(
matrix
.
dtype
)
_pos
=
(
corners
.
reshape
((
1
,
-
1
,
3
))
...
...
@@ -410,7 +456,10 @@ def rotate(
nw
=
npos
[
0
][
0
]
nh
=
npos
[
0
][
1
]
ow
,
oh
=
int
(
nw
.
numpy
()[
0
]),
int
(
nh
.
numpy
()[
0
])
if
paddle
.
in_dynamic_mode
():
ow
,
oh
=
int
(
nw
.
numpy
()[
0
]),
int
(
nh
.
numpy
()[
0
])
else
:
ow
,
oh
=
nw
.
astype
(
"int32"
),
nh
.
astype
(
"int32"
)
else
:
ow
,
oh
=
w
,
h
...
...
python/paddle/vision/transforms/transforms.py
浏览文件 @
2afa7b93
...
...
@@ -1520,7 +1520,12 @@ class RandomRotation(BaseTransform):
self
.
fill
=
fill
def
_get_param
(
self
,
degrees
):
angle
=
random
.
uniform
(
degrees
[
0
],
degrees
[
1
])
if
paddle
.
in_dynamic_mode
():
angle
=
random
.
uniform
(
degrees
[
0
],
degrees
[
1
])
else
:
angle
=
paddle
.
uniform
(
[
1
],
dtype
=
"float32"
,
min
=
degrees
[
0
],
max
=
degrees
[
1
]
)
return
angle
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录