This repository was archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathaffine_transformations_test.py
201 lines (165 loc) · 7.02 KB
/
affine_transformations_test.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
import numpy as np
import pytest
from keras_preprocessing.image import affine_transformations
def test_random_transforms():
x = np.random.random((2, 28, 28))
assert affine_transformations.random_rotation(x, 45).shape == (2, 28, 28)
assert affine_transformations.random_shift(x, 1, 1).shape == (2, 28, 28)
assert affine_transformations.random_shear(x, 20).shape == (2, 28, 28)
assert affine_transformations.random_channel_shift(x, 20).shape == (2, 28, 28)
def test_deterministic_transform():
x = np.ones((3, 3, 3))
x_rotated = np.array([[[0., 0., 0.],
[1., 1., 1.],
[0., 0., 0.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[0., 0., 0.],
[1., 1., 1.],
[0., 0., 0.]]])
assert np.allclose(
affine_transformations.apply_affine_transform(x,
theta=45,
row_axis=0,
col_axis=1,
channel_axis=2,
fill_mode='constant'),
x_rotated)
def test_matrix_center():
x = np.expand_dims(np.array([
[0, 1],
[0, 0],
]), -1)
x_rotated90 = np.expand_dims(np.array([
[1, 0],
[0, 0],
]), -1)
assert np.allclose(
affine_transformations.apply_affine_transform(x,
theta=90,
row_axis=0,
col_axis=1,
channel_axis=2),
x_rotated90)
def test_translation():
x = np.array([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
])
x_up = np.array([
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
])
x_dn = np.array([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
])
x_left = np.array([
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 0],
])
x_right = np.array([
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
])
# Channels first
x_test = np.expand_dims(x, 0)
# Horizontal translation
assert np.alltrue(x_left == np.squeeze(
affine_transformations.apply_affine_transform(x_test, tx=1)))
assert np.alltrue(x_right == np.squeeze(
affine_transformations.apply_affine_transform(x_test, tx=-1)))
# change axes: x<->y
assert np.alltrue(x_left == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, ty=1, row_axis=2, col_axis=1)))
assert np.alltrue(x_right == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, ty=-1, row_axis=2, col_axis=1)))
# Vertical translation
assert np.alltrue(x_up == np.squeeze(
affine_transformations.apply_affine_transform(x_test, ty=1)))
assert np.alltrue(x_dn == np.squeeze(
affine_transformations.apply_affine_transform(x_test, ty=-1)))
# change axes: x<->y
assert np.alltrue(x_up == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, tx=1, row_axis=2, col_axis=1)))
assert np.alltrue(x_dn == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, tx=-1, row_axis=2, col_axis=1)))
# Channels last
x_test = np.expand_dims(x, -1)
# Horizontal translation
assert np.alltrue(x_left == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, tx=1, row_axis=0, col_axis=1, channel_axis=2)))
assert np.alltrue(x_right == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, tx=-1, row_axis=0, col_axis=1, channel_axis=2)))
# change axes: x<->y
assert np.alltrue(x_left == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, ty=1, row_axis=1, col_axis=0, channel_axis=2)))
assert np.alltrue(x_right == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, ty=-1, row_axis=1, col_axis=0, channel_axis=2)))
# Vertical translation
assert np.alltrue(x_up == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, ty=1, row_axis=0, col_axis=1, channel_axis=2)))
assert np.alltrue(x_dn == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, ty=-1, row_axis=0, col_axis=1, channel_axis=2)))
# change axes: x<->y
assert np.alltrue(x_up == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, tx=1, row_axis=1, col_axis=0, channel_axis=2)))
assert np.alltrue(x_dn == np.squeeze(
affine_transformations.apply_affine_transform(
x_test, tx=-1, row_axis=1, col_axis=0, channel_axis=2)))
def test_random_zoom():
x = np.random.random((2, 28, 28))
assert affine_transformations.random_zoom(x, (5, 5)).shape == (2, 28, 28)
assert np.allclose(x, affine_transformations.random_zoom(x, (1, 1)))
def test_random_zoom_error():
with pytest.raises(ValueError):
affine_transformations.random_zoom(0, zoom_range=[0])
def test_apply_brightness_shift_error(monkeypatch):
monkeypatch.setattr(affine_transformations, 'ImageEnhance', None)
with pytest.raises(ImportError):
affine_transformations.apply_brightness_shift(0, [0])
def test_random_brightness(monkeypatch):
monkeypatch.setattr(affine_transformations,
'apply_brightness_shift', lambda x, y, z: (x, y))
assert (0, 3.) == affine_transformations.random_brightness(0, (3, 3))
def test_random_brightness_error():
with pytest.raises(ValueError):
affine_transformations.random_brightness(0, [0])
def test_random_brightness_scale():
img = np.ones((1, 1, 3)) * 128
zeros = np.zeros((1, 1, 3))
must_be_128 = affine_transformations.random_brightness(img, [1, 1], False)
assert np.array_equal(img, must_be_128)
must_be_0 = affine_transformations.random_brightness(img, [1, 1], True)
assert np.array_equal(zeros, must_be_0)
def test_random_brightness_scale_outside_range_positive():
img = np.ones((1, 1, 3)) * 1024
zeros = np.zeros((1, 1, 3))
must_be_1024 = affine_transformations.random_brightness(img, [1, 1], False)
assert np.array_equal(img, must_be_1024)
must_be_0 = affine_transformations.random_brightness(img, [1, 1], True)
assert np.array_equal(zeros, must_be_0)
def test_random_brightness_scale_outside_range_negative():
img = np.ones((1, 1, 3)) * -1024
zeros = np.zeros((1, 1, 3))
must_be_neg_1024 = affine_transformations.random_brightness(img, [1, 1], False)
assert np.array_equal(img, must_be_neg_1024)
must_be_0 = affine_transformations.random_brightness(img, [1, 1], True)
assert np.array_equal(zeros, must_be_0)