Skip to content

Commit 221ea6b

Browse files
authored
Add pad_to_aspect_ratio flag to ImageConverter (#2045)
* Add `pad_to_aspect_ratio` flag to ImageConverter * skip resize test with pad_to_aspect_ratio when backend set to torch * nit
1 parent 96b2fe5 commit 221ea6b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

keras_hub/src/layers/preprocessing/image_converter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def __init__(
9898
scale=None,
9999
offset=None,
100100
crop_to_aspect_ratio=True,
101+
pad_to_aspect_ratio=False,
101102
interpolation="bilinear",
102103
data_format=None,
103104
**kwargs,
@@ -112,12 +113,19 @@ def __init__(
112113

113114
super().__init__(**kwargs)
114115

116+
if crop_to_aspect_ratio and pad_to_aspect_ratio:
117+
raise ValueError(
118+
"Only one of 'crop_to_aspect_ratio' or 'pad_to_aspect_ratio' "
119+
"can be True."
120+
)
121+
115122
# Create the `Resizing` layer here even if it's not being used. That
116123
# allows us to make `image_size` a settable property.
117124
self.resizing = keras.layers.Resizing(
118125
height=image_size[0] if image_size else None,
119126
width=image_size[1] if image_size else None,
120127
crop_to_aspect_ratio=crop_to_aspect_ratio,
128+
pad_to_aspect_ratio=pad_to_aspect_ratio,
121129
interpolation=interpolation,
122130
data_format=data_format,
123131
dtype=self.dtype_policy,
@@ -126,6 +134,7 @@ def __init__(
126134
self.scale = scale
127135
self.offset = offset
128136
self.crop_to_aspect_ratio = crop_to_aspect_ratio
137+
self.pad_to_aspect_ratio = pad_to_aspect_ratio
129138
self.interpolation = interpolation
130139
self.data_format = standardize_data_format(data_format)
131140

@@ -182,6 +191,7 @@ def get_config(self):
182191
"offset": self.offset,
183192
"interpolation": self.interpolation,
184193
"crop_to_aspect_ratio": self.crop_to_aspect_ratio,
194+
"pad_to_aspect_ratio": self.pad_to_aspect_ratio,
185195
}
186196
)
187197
return config

keras_hub/src/layers/preprocessing/image_converter_test.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
import pathlib
33

4+
import keras
45
import numpy as np
56
import pytest
7+
from absl.testing import parameterized
68
from keras import ops
79

810
from keras_hub.src.layers.preprocessing.image_converter import ImageConverter
@@ -33,11 +35,21 @@ def test_unbatched(self):
3335
self.assertAllClose(outputs[:, :, 1], np.ones((4, 4)) * 0.301569)
3436
self.assertAllClose(outputs[:, :, 2], np.ones((4, 4)) * 0.852353)
3537

36-
def test_resize_batch(self):
38+
@parameterized.parameters(
39+
(True, False),
40+
(False, True),
41+
)
42+
@pytest.mark.skipif(
43+
keras.config.backend() == "torch",
44+
reason="disabled until resize is fixed for torch backend",
45+
) # TODO: remove skip after new release with fix of https://github.com/keras-team/keras/pull/20797
46+
def test_resize_batch(self, crop_to_aspect_ratio, pad_to_aspect_ratio):
3747
converter = ImageConverter(
3848
image_size=(4, 4),
3949
scale=(1.0 / 255.0, 0.8 / 255.0, 1.2 / 255.0),
4050
offset=(0.2, -0.1, 0.25),
51+
crop_to_aspect_ratio=crop_to_aspect_ratio,
52+
pad_to_aspect_ratio=pad_to_aspect_ratio,
4153
)
4254
inputs = np.ones((2, 10, 10, 3)) * 128
4355
outputs = converter(inputs)
@@ -46,6 +58,15 @@ def test_resize_batch(self):
4658
self.assertAllClose(outputs[:, :, :, 1], np.ones((2, 4, 4)) * 0.301569)
4759
self.assertAllClose(outputs[:, :, :, 2], np.ones((2, 4, 4)) * 0.852353)
4860

61+
def test_pad_and_crop_to_aspect_ratio(self):
62+
with self.assertRaisesRegex(ValueError, "Only one of"):
63+
_ = ImageConverter(
64+
image_size=(4, 4),
65+
scale=1 / 255.0,
66+
crop_to_aspect_ratio=True,
67+
pad_to_aspect_ratio=True,
68+
)
69+
4970
def test_config(self):
5071
converter = ImageConverter(
5172
image_size=(12, 20),

0 commit comments

Comments
 (0)