Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dy2St] transforms.RandomRotation Support static mode #49128

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion python/paddle/tests/test_transforms_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def setUp(self):
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)
Expand Down Expand Up @@ -142,5 +142,21 @@ def set_trans_api(self):
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()
107 changes: 78 additions & 29 deletions python/paddle/vision/transforms/functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import math
import numbers

import numpy as np

import paddle
import paddle.nn.functional as F

Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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))
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion python/paddle/vision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,12 @@ def __init__(
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

Expand Down