-
Notifications
You must be signed in to change notification settings - Fork 2
/
transforms.py
234 lines (178 loc) · 7.87 KB
/
transforms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import torch
import kornia
from kornia.geometry import warp_perspective
import numpy as np
from math import pi
class To_Color(object):
def __init__(self):
pass
def __call__(self, tensor):
"""
"""
#tensor_rgb = transforms.functional.to_tensor(tensor).repeat(3, 1, 1)
#tensor_rgb[1:] = 0.0
#return transforms.functional.to_pil_image(tensor_rgb)
tensor_rgb = tensor.repeat(1, 3, 1, 1)
tensor_rgb[:,1:] = 0.0
return tensor_rgb
def __repr__(self):
format_string = self.__class__.__name__
return format_string
class AddRandomTransformationDims(object):
def __init__(self, angle_set, color_set, scale_set):
self.angle_set = angle_set
self.color_set = list(map(lambda x: np.radians(x) - np.pi, color_set))
self.scale_set = scale_set
def __call__(self, tensor, transform_type, step):
x = tensor
b, c, h, w = x.shape
# define the rotation center
center = torch.ones(x.shape[0], 2, device=x.device)
center[..., 0] = w / 2 # x
center[..., 1] = h / 2 # y
scale = self.scale_set[step]
color = self.color_set[step]
angle = self.angle_set[step]
bsz_angles = torch.ones(x.shape[0], device=x.device)
bsz_colors = torch.ones(x.shape[0], device=x.device)
bsz_scales = torch.ones(x.shape[0], 2, device=x.device)
if transform_type == 0:
M = kornia.get_rotation_matrix2d(center, bsz_angles*0.0, bsz_scales * scale).to(x.device)
x_t = kornia.warp_affine(x, M, dsize=(h, w))
elif transform_type == 1:
M = kornia.get_rotation_matrix2d(center, bsz_angles * angle, bsz_scales).to(x.device)
x_t = kornia.warp_affine(x, M, dsize=(h, w))
elif transform_type == 2:
x_t = kornia.enhance.adjust_hue(x, bsz_colors * color)
return x_t
class AddDualTransformationDims(object):
def __init__(self, angle_set, color_set, scale_set):
self.angle_set = angle_set
self.color_set = color_set
self.scale_set = scale_set
def __call__(self, tensor):
x = tensor.unsqueeze(0)
b, c, h, w = x.shape
# define the rotation center
center = torch.ones(x.shape[0], 2, device=x.device)
center[..., 0] = w / 2 # x
center[..., 1] = h / 2 # y
x_expanded = x.new_zeros((x.shape[0], len(self.angle_set), 1, 1, c, h, w))
start_angle = torch.randint(len(self.angle_set), (1,))
start_color = torch.randint(len(self.color_set), (1,))
scale = 1.0
self.angle_set = self.angle_set[start_angle:] + self.angle_set[:start_angle]
self.color_set = self.color_set[start_color:] + self.color_set[:start_color]
for t_i, (angle, color) in enumerate(zip(self.angle_set, self.color_set)):
bsz_angles = torch.ones(x.shape[0]) * angle # * speed
bsz_colors = torch.ones(x.shape[0]) * color
bsz_scales = torch.ones(x.shape[0]) * scale
# compute the transformation matrix
M = kornia.get_rotation_matrix2d(center, bsz_angles, bsz_scales).to(x.device)
# apply the transformation to original image
x_t = kornia.warp_affine(x, M, dsize=(h, w))
if c == 3:
# Apply color rotation
x_t = kornia.enhance.adjust_hue(x_t, bsz_colors)
x_expanded[:, t_i, :, :] = x_t
tensor = x_expanded.squeeze(0)
return tensor
def get_rad(theta, phi, gamma):
return (deg_to_rad(theta),
deg_to_rad(phi),
deg_to_rad(gamma))
def deg_to_rad(deg):
return deg * pi / 180.0
class ImageTransformer(object):
""" Perspective transformation class for image
with shape (c, height, width) """
def __init__(self, shape):
self.bsz = shape[0]
self.num_channels = shape[1]
self.height = shape[2]
self.width = shape[3]
""" Wrapper of Rotating a Image """
def rotate_along_axis(self, image_batch, theta=0, phi=0, gamma=0, dx=0, dy=0, dz=0):
# Get radius of rotation along 3 axes
rtheta, rphi, rgamma = get_rad(theta, phi, gamma)
# Get ideal focal length on z axis
# NOTE: Change this section to other axis if needed
d = np.sqrt(self.height ** 2 + self.width ** 2)
self.focal = d / (2 * np.sin(rgamma) if np.sin(rgamma) != 0 else 1)
dz = self.focal
# Get projection matrix
mat = self.get_M(rtheta, rphi, rgamma, dx, dy, dz)
M = torch.tensor(np.stack([mat] * self.bsz)).to(torch.float32)
return warp_perspective(image_batch, M, (self.height, self.width))
""" Get Perspective Projection Matrix """
def get_M(self, theta, phi, gamma, dx, dy, dz):
w = self.width
h = self.height
f = self.focal
# Projection 2D -> 3D matrix
A1 = np.array([[1, 0, -w / 2],
[0, 1, -h / 2],
[0, 0, 1],
[0, 0, 1]])
# Rotation matrices around the X, Y, and Z axis
RX = np.array([[1, 0, 0, 0],
[0, np.cos(theta), -np.sin(theta), 0],
[0, np.sin(theta), np.cos(theta), 0],
[0, 0, 0, 1]])
RY = np.array([[np.cos(phi), 0, -np.sin(phi), 0],
[0, 1, 0, 0],
[np.sin(phi), 0, np.cos(phi), 0],
[0, 0, 0, 1]])
RZ = np.array([[np.cos(gamma), -np.sin(gamma), 0, 0],
[np.sin(gamma), np.cos(gamma), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
# Composed rotation matrix with (RX, RY, RZ)
R = np.dot(np.dot(RX, RY), RZ)
# Translation matrix
T = np.array([[1, 0, 0, dx],
[0, 1, 0, dy],
[0, 0, 1, dz],
[0, 0, 0, 1]])
# Projection 3D -> 2D matrix
A2 = np.array([[f, 0, w / 2, 0],
[0, f, h / 2, 0],
[0, 0, 1, 0]])
# Final transformation matrix
return np.dot(A2, np.dot(T, np.dot(R, A1)))
class AddPerspectiveTransformationDims(object):
def __init__(self, angle_set, color_set, scale_set, seq_len=18, shape=(1, 3, 28, 28)):
self.seq_len = seq_len
self.it = ImageTransformer(shape)
self.angle_set = angle_set
self.color_set = color_set
self.scale_set = scale_set
def __call__(self, tensor):
x = tensor.unsqueeze(0)
b, c, h, w = x.shape
start_angle = torch.randint(len(self.angle_set), (1,))
x_expanded = x.new_zeros((x.shape[0], len(self.angle_set), 1, 1, c, h, w))
start_angle = torch.randint(len(self.angle_set), (1,))
start_color = torch.randint(len(self.color_set), (1,))
scale = 1.0
self.angle_set = self.angle_set[start_angle:] + self.angle_set[:start_angle]
self.color_set = self.color_set[start_color:] + self.color_set[:start_color]
for t_i, (angle, color) in enumerate(zip(self.angle_set, self.color_set)):
bsz_colors = torch.ones(x.shape[0]) * color
# phi = 60*np.cos(np.radians(angle))
# theta = 60*np.sin(np.radians(angle))
if angle != 0.0:
if angle == 180.0:
x_t = self.it.rotate_along_axis(x, phi=angle + 1, gamma=angle + 1)
else:
x_t = self.it.rotate_along_axis(x, phi=angle, gamma=angle)
# x_t = self.it.rotate_along_axis(x, theta=theta, phi=phi)
# x_t = self.it.rotate_along_axis(x, phi=angle, dx = 5)
else:
x_t = x
if c == 3:
# Apply color rotation
x_t = kornia.color.adjust_hue(x_t, bsz_colors)
x_expanded[:, t_i, :, :] = x_t
tensor = x_expanded.squeeze(0)
return tensor