-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathimage_test.py
210 lines (196 loc) · 8.04 KB
/
image_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
202
203
204
205
206
207
208
209
210
import numpy as np
import tensorflow as tf
from absl.testing import parameterized
from keras_core import backend
from keras_core import testing
from keras_core.backend.common.keras_tensor import KerasTensor
from keras_core.ops import image as kimage
class ImageOpsDynamicShapeTest(testing.TestCase):
def test_resize(self):
x = KerasTensor([None, 20, 20, 3])
out = kimage.resize(x, size=(15, 15))
self.assertEqual(out.shape, (None, 15, 15, 3))
x = KerasTensor([None, None, 3])
out = kimage.resize(x, size=(15, 15))
self.assertEqual(out.shape, (15, 15, 3))
def test_affine_transform(self):
x = KerasTensor([None, 20, 20, 3])
transform = KerasTensor([None, 8])
out = kimage.affine_transform(x, transform)
self.assertEqual(out.shape, (None, 20, 20, 3))
class ImageOpsStaticShapeTest(testing.TestCase):
def test_resize(self):
x = KerasTensor([20, 20, 3])
out = kimage.resize(x, size=(15, 15))
self.assertEqual(out.shape, (15, 15, 3))
def test_affine_transform(self):
x = KerasTensor([20, 20, 3])
transform = KerasTensor([8])
out = kimage.affine_transform(x, transform)
self.assertEqual(out.shape, (20, 20, 3))
class ImageOpsCorrectnessTest(testing.TestCase, parameterized.TestCase):
@parameterized.parameters(
[
("bilinear", True, "channels_last"),
("nearest", True, "channels_last"),
("lanczos3", True, "channels_last"),
("lanczos5", True, "channels_last"),
("bicubic", True, "channels_last"),
("bilinear", False, "channels_last"),
("nearest", False, "channels_last"),
("lanczos3", False, "channels_last"),
("lanczos5", False, "channels_last"),
("bicubic", False, "channels_last"),
("bilinear", True, "channels_first"),
]
)
def test_resize(self, interpolation, antialias, data_format):
if backend.backend() == "torch":
if "lanczos" in interpolation:
self.skipTest(
"Resizing with Lanczos interpolation is "
"not supported by the PyTorch backend. "
f"Received: interpolation={interpolation}."
)
if interpolation == "bicubic" and antialias is False:
self.skipTest(
"Resizing with Bicubic interpolation in "
"PyTorch backend produces noise. Please "
"turn on anti-aliasing. "
f"Received: interpolation={interpolation}, "
f"antialias={antialias}."
)
# Unbatched case
if data_format == "channels_first":
x = np.random.random((3, 50, 50)) * 255
else:
x = np.random.random((50, 50, 3)) * 255
out = kimage.resize(
x,
size=(25, 25),
interpolation=interpolation,
antialias=antialias,
data_format=data_format,
)
if data_format == "channels_first":
x = np.transpose(x, (1, 2, 0))
ref_out = tf.image.resize(
x, size=(25, 25), method=interpolation, antialias=antialias
)
if data_format == "channels_first":
ref_out = np.transpose(ref_out, (2, 0, 1))
self.assertEqual(tuple(out.shape), tuple(ref_out.shape))
self.assertAllClose(ref_out, out, atol=0.3)
# Batched case
if data_format == "channels_first":
x = np.random.random((2, 3, 50, 50)) * 255
else:
x = np.random.random((2, 50, 50, 3)) * 255
out = kimage.resize(
x,
size=(25, 25),
interpolation=interpolation,
antialias=antialias,
data_format=data_format,
)
if data_format == "channels_first":
x = np.transpose(x, (0, 2, 3, 1))
ref_out = tf.image.resize(
x, size=(25, 25), method=interpolation, antialias=antialias
)
if data_format == "channels_first":
ref_out = np.transpose(ref_out, (0, 3, 1, 2))
self.assertEqual(tuple(out.shape), tuple(ref_out.shape))
self.assertAllClose(ref_out, out, atol=0.3)
@parameterized.parameters(
[
("bilinear", "constant", "channels_last"),
("nearest", "constant", "channels_last"),
("bilinear", "nearest", "channels_last"),
("nearest", "nearest", "channels_last"),
("bilinear", "wrap", "channels_last"),
("nearest", "wrap", "channels_last"),
("bilinear", "reflect", "channels_last"),
("nearest", "reflect", "channels_last"),
("bilinear", "constant", "channels_first"),
]
)
def test_affine_transform(self, interpolation, fill_mode, data_format):
if fill_mode == "wrap" and backend.backend() == "torch":
self.skipTest(
"Applying affine transform with fill_mode=wrap is not support"
" in torch backend"
)
if fill_mode == "wrap" and backend.backend() in ("jax", "numpy"):
self.skipTest(
"The numerical results of applying affine transform with "
"fill_mode=wrap in tensorflow is inconsistent with jax and "
"numpy backends"
)
# Unbatched case
if data_format == "channels_first":
x = np.random.random((3, 50, 50)) * 255
else:
x = np.random.random((50, 50, 3)) * 255
transform = np.random.random(size=(6))
transform = np.pad(transform, (0, 2)) # makes c0, c1 always 0
out = kimage.affine_transform(
x,
transform,
interpolation=interpolation,
fill_mode=fill_mode,
data_format=data_format,
)
if data_format == "channels_first":
x = np.transpose(x, (1, 2, 0))
ref_out = tf.raw_ops.ImageProjectiveTransformV3(
images=tf.expand_dims(x, axis=0),
transforms=tf.cast(tf.expand_dims(transform, axis=0), tf.float32),
output_shape=tf.shape(x)[:-1],
fill_value=0,
interpolation=interpolation.upper(),
fill_mode=fill_mode.upper(),
)
ref_out = ref_out[0]
if data_format == "channels_first":
ref_out = np.transpose(ref_out, (2, 0, 1))
self.assertEqual(tuple(out.shape), tuple(ref_out.shape))
if backend.backend() == "torch":
# TODO: cannot pass with torch backend
with self.assertRaises(AssertionError):
self.assertAllClose(ref_out, out, atol=0.3)
else:
self.assertAllClose(ref_out, out, atol=0.3)
# Batched case
if data_format == "channels_first":
x = np.random.random((2, 3, 50, 50)) * 255
else:
x = np.random.random((2, 50, 50, 3)) * 255
transform = np.random.random(size=(2, 6))
transform = np.pad(transform, [(0, 0), (0, 2)]) # makes c0, c1 always 0
out = kimage.affine_transform(
x,
transform,
interpolation=interpolation,
fill_mode=fill_mode,
data_format=data_format,
)
if data_format == "channels_first":
x = np.transpose(x, (0, 2, 3, 1))
ref_out = tf.raw_ops.ImageProjectiveTransformV3(
images=x,
transforms=tf.cast(transform, tf.float32),
output_shape=tf.shape(x)[1:-1],
fill_value=0,
interpolation=interpolation.upper(),
fill_mode=fill_mode.upper(),
)
if data_format == "channels_first":
ref_out = np.transpose(ref_out, (0, 3, 1, 2))
self.assertEqual(tuple(out.shape), tuple(ref_out.shape))
if backend.backend() == "torch":
# TODO: cannot pass with torch backend
with self.assertRaises(AssertionError):
self.assertAllClose(ref_out, out, atol=0.3)
else:
self.assertAllClose(ref_out, out, atol=0.3)