-
Notifications
You must be signed in to change notification settings - Fork 1
/
transforms.py
231 lines (178 loc) · 7.76 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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
def get_random_start_subscript(shape, required_shape):
shape_change = _calculate_crop_change(shape, required_shape)
return np.array([np.random.randint(low=c, high=1) for c in shape_change])
def get_center_start_subscript(shape, required_shape):
shape_change = _calculate_crop_change(shape, required_shape)
return shape_change // 2
def reshape_by_pad_crop(data, required_shape, crop_start_subscript=None):
"""
Padding occurs along a dimension if the data array is smaller than the
required shape. The start and end of a dimension are padded equally, with
the end receiving one extra for odd differences. Cropping occurs if the data
is larger than required. The start subscript depicts the starting point of
the cropped data in the input data.
"""
assert data.ndim == len(required_shape)
out = pad(data, required_shape)
if crop_start_subscript is None:
crop_start_subscript = get_center_start_subscript(data.shape, required_shape)
out = crop(out, required_shape, crop_start_subscript)
return out
def pad(data, required_shape):
change = _calculate_pad_change(data.shape, required_shape)
pre_change = change // 2
post_change = change - pre_change
padding = list(zip(pre_change, post_change)) # post pad only
return np.pad(data, padding)
def crop(data, required_shape, start_subscript):
change = _calculate_crop_change(data.shape, required_shape)
pre_change = np.array(list(start_subscript))
post_change = change - pre_change
start = -pre_change
end = np.array(list(data.shape)) + post_change
sl = tuple(slice(s, e) for s, e in zip(start, end))
return data[sl]
def _calculate_pad_change(shape, required_shape):
return _calculate_shape_change(shape, required_shape, limit_fn=lambda x: max(0, x))
def _calculate_crop_change(shape, required_shape):
return _calculate_shape_change(shape, required_shape, limit_fn=lambda x: min(0, x))
def _calculate_shape_change(shape, required_shape, limit_fn=None):
"""Positive means pad, negative means crop."""
assert len(shape) == len(required_shape)
shape = np.array(list(shape))
req = np.array(list(required_shape))
change = req - shape
change = np.array([limit_fn(c) for c in change])
return change
def random_affine(
img,
min_rot=None,
max_rot=None,
min_shear=None,
max_shear=None,
min_scale=None,
max_scale=None,
):
# Takes and returns torch cuda tensors with channels 1st (1 img)
# rot and shear params are in degrees
# tf matrices need to be float32, returned as tensors
# we don't do translations
# https://github.com/pytorch/pytorch/issues/12362
# https://stackoverflow.com/questions/42489310/matrix-inversion-3-3-python
# -hard-coded-vs-numpy-linalg-inv
# https://github.com/pytorch/vision/blob/master/torchvision/transforms
# /functional.py#L623
# RSS(a, scale, shear) = [cos(a) *scale - sin(a + shear) * scale 0]
# [ sin(a)*scale cos(a + shear)*scale 0]
# [ 0 0 1]
# used by opencv functional _get_affine_matrix and
# skimage.transform.AffineTransform
assert len(img.shape) == 3
a = np.radians(np.random.rand() * (max_rot - min_rot) + min_rot)
shear = np.radians(np.random.rand() * (max_shear - min_shear) + min_shear)
scale = np.random.rand() * (max_scale - min_scale) + min_scale
affine1_to_2 = np.array(
[
[np.cos(a) * scale, -np.sin(a + shear) * scale, 0.0],
[np.sin(a) * scale, np.cos(a + shear) * scale, 0.0],
[0.0, 0.0, 1.0],
],
dtype=np.float32,
) # 3x3
affine2_to_1 = np.linalg.inv(affine1_to_2).astype(np.float32)
affine1_to_2, affine2_to_1 = affine1_to_2[:2, :], affine2_to_1[:2, :] # 2x3
affine1_to_2, affine2_to_1 = (
torch.from_numpy(affine1_to_2).cuda(),
torch.from_numpy(affine2_to_1).cuda(),
)
img = perform_affine_tf(img.unsqueeze(dim=0), affine1_to_2.unsqueeze(dim=0))
img = img.squeeze(dim=0)
return img, affine1_to_2, affine2_to_1
def perform_affine_tf(data, tf_matrices):
# expects 4D tensor, we preserve gradients if there are any
n_i, k, h, w = data.shape
n_i2, r, c = tf_matrices.shape
assert n_i == n_i2
assert r == 2 and c == 3
grid = F.affine_grid(
tf_matrices, data.shape, align_corners=True # type: ignore
) # output should be same size
data_tf = F.grid_sample(
data, grid, padding_mode="zeros", align_corners=True # type: ignore
) # this can ONLY do bilinear
return data_tf
def random_translation_multiple(data, half_side_min, half_side_max):
n, c, h, w = data.shape
# pad last 2, i.e. spatial, dimensions, equally in all directions
data = F.pad(
data,
(half_side_max, half_side_max, half_side_max, half_side_max),
"constant",
0,
)
assert data.shape[2:] == (2 * half_side_max + h, 2 * half_side_max + w)
# random x, y displacement
t = np.random.randint(half_side_min, half_side_max + 1, size=(2,))
polarities = np.random.choice([-1, 1], size=(2,), replace=True)
t *= polarities
# -x, -y in orig img frame is now -x+half_side_max, -y+half_side_max in new
t += half_side_max
data = data[:, :, t[1] : (t[1] + h), t[0] : (t[0] + w)]
assert data.shape[2:] == (h, w)
return data
def random_translation(img, half_side_min, half_side_max):
# expects 3d (cuda) tensor with channels first
c, h, w = img.shape
# pad last 2, i.e. spatial, dimensions, equally in all directions
img = F.pad(
img, (half_side_max, half_side_max, half_side_max, half_side_max), "constant", 0
)
assert img.shape[1:] == (2 * half_side_max + h, 2 * half_side_max + w)
# random x, y displacement
t = np.random.randint(half_side_min, half_side_max + 1, size=(2,))
polarities = np.random.choice([-1, 1], size=(2,), replace=True)
t *= polarities
# -x, -y in orig img frame is now -x+half_side_max, -y+half_side_max in new
t += half_side_max
img = img[:, t[1] : (t[1] + h), t[0] : (t[0] + w)]
assert img.shape[1:] == (h, w)
return img
def sobel_process(image: torch.Tensor):
gray = image[-1, ...].unsqueeze(0)
other = image[:-1, ...]
sobel = make_sobel(gray.unsqueeze(0))
return torch.cat([other, gray, sobel], dim=0)
def make_sobel(image: torch.Tensor):
HORIZONTAL = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
CONV_H = generate_convolution(HORIZONTAL)
dx = CONV_H(image).data
VERTICAL = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
CONV_V = generate_convolution(VERTICAL)
dy = CONV_V(image).data
return torch.cat([dx, dy], dim=1).squeeze()
def generate_convolution(sobel_filter):
filt = torch.from_numpy(sobel_filter)
filt = filt.cuda().float()
filt = filt.unsqueeze(0).unsqueeze(0)
conv = nn.Conv2d(1, 1, kernel_size=3, padding=1, bias=False)
conv.weight = nn.Parameter(filt, requires_grad=False)
return conv
if __name__ == "__main__":
data = np.arange(24).reshape((4, 6))
req = (2, 2)
start = get_center_start_subscript(data.shape, req)
print(reshape_by_pad_crop(data, req, start))
start = get_random_start_subscript(data.shape, req)
print(reshape_by_pad_crop(data, req, start))
start = get_random_start_subscript(data.shape, req)
print(reshape_by_pad_crop(data, req, start))
start = get_random_start_subscript(data.shape, req)
print(reshape_by_pad_crop(data, req, start))
start = get_random_start_subscript(data.shape, req)
print(reshape_by_pad_crop(data, req, start))
start = get_random_start_subscript(data.shape, req)
print(reshape_by_pad_crop(data, req, start))