keras/keras_core/operations/image_test.py

104 lines
3.7 KiB
Python
Raw Normal View History

2023-05-05 22:48:24 +00:00
import numpy as np
import tensorflow as tf
from absl.testing import parameterized
from keras_core import backend
2023-05-05 22:48:24 +00:00
from keras_core import testing
from keras_core.backend.common.keras_tensor import KerasTensor
from keras_core.operations 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))
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))
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"),
2023-05-05 22:48:24 +00:00
]
)
def test_resize(self, method, antialias, data_format):
if backend.backend() == "torch":
if "lanczos" in method:
self.skipTest(
"Resizing with Lanczos interpolation is "
"not supported by the PyTorch backend. "
f"Received: method={method}."
)
if method == "bicubic" and antialias is False:
self.skipTest(
"Resizing with Bicubic interpolation in "
"PyTorch backend produces noise. Please "
"turn on anti-aliasing. "
f"Received: method={method}, "
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
2023-05-05 22:48:24 +00:00
out = kimage.resize(
x,
size=(25, 25),
method=method,
antialias=antialias,
data_format=data_format,
2023-05-05 22:48:24 +00:00
)
if data_format == "channels_first":
x = np.transpose(x, (1, 2, 0))
2023-05-05 22:48:24 +00:00
ref_out = tf.image.resize(
x, size=(25, 25), method=method, antialias=antialias
)
if data_format == "channels_first":
ref_out = np.transpose(ref_out, (2, 0, 1))
2023-05-05 22:48:24 +00:00
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
2023-05-05 22:48:24 +00:00
out = kimage.resize(
x,
size=(25, 25),
method=method,
antialias=antialias,
data_format=data_format,
2023-05-05 22:48:24 +00:00
)
if data_format == "channels_first":
x = np.transpose(x, (0, 2, 3, 1))
2023-05-05 22:48:24 +00:00
ref_out = tf.image.resize(
x, size=(25, 25), method=method, antialias=antialias
)
if data_format == "channels_first":
ref_out = np.transpose(ref_out, (0, 3, 1, 2))
2023-05-05 22:48:24 +00:00
self.assertEqual(tuple(out.shape), tuple(ref_out.shape))
self.assertAllClose(ref_out, out, atol=0.3)