Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions keras_hub/src/layers/preprocessing/image_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def __init__(
scale=None,
offset=None,
crop_to_aspect_ratio=True,
pad_to_aspect_ratio=False,
interpolation="bilinear",
data_format=None,
**kwargs,
Expand All @@ -112,12 +113,19 @@ def __init__(

super().__init__(**kwargs)

if crop_to_aspect_ratio and pad_to_aspect_ratio:
raise ValueError(
"Only one of 'crop_to_aspect_ratio' or 'pad_to_aspect_ratio' "
"can be True."
)

# Create the `Resizing` layer here even if it's not being used. That
# allows us to make `image_size` a settable property.
self.resizing = keras.layers.Resizing(
height=image_size[0] if image_size else None,
width=image_size[1] if image_size else None,
crop_to_aspect_ratio=crop_to_aspect_ratio,
pad_to_aspect_ratio=pad_to_aspect_ratio,
interpolation=interpolation,
data_format=data_format,
dtype=self.dtype_policy,
Expand All @@ -126,6 +134,7 @@ def __init__(
self.scale = scale
self.offset = offset
self.crop_to_aspect_ratio = crop_to_aspect_ratio
self.pad_to_aspect_ratio = pad_to_aspect_ratio
self.interpolation = interpolation
self.data_format = standardize_data_format(data_format)

Expand Down Expand Up @@ -182,6 +191,7 @@ def get_config(self):
"offset": self.offset,
"interpolation": self.interpolation,
"crop_to_aspect_ratio": self.crop_to_aspect_ratio,
"pad_to_aspect_ratio": self.pad_to_aspect_ratio,
}
)
return config
Expand Down
23 changes: 22 additions & 1 deletion keras_hub/src/layers/preprocessing/image_converter_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import pathlib

import keras
import numpy as np
import pytest
from absl.testing import parameterized
from keras import ops

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

def test_resize_batch(self):
@parameterized.parameters(
(True, False),
(False, True),
)
@pytest.mark.skipif(
keras.config.backend() == "torch",
reason="disabled until resize is fixed for torch backend",
) # TODO: remove skip after new release with fix of https://github.com/keras-team/keras/pull/20797
def test_resize_batch(self, crop_to_aspect_ratio, pad_to_aspect_ratio):
converter = ImageConverter(
image_size=(4, 4),
scale=(1.0 / 255.0, 0.8 / 255.0, 1.2 / 255.0),
offset=(0.2, -0.1, 0.25),
crop_to_aspect_ratio=crop_to_aspect_ratio,
pad_to_aspect_ratio=pad_to_aspect_ratio,
)
inputs = np.ones((2, 10, 10, 3)) * 128
outputs = converter(inputs)
Expand All @@ -46,6 +58,15 @@ def test_resize_batch(self):
self.assertAllClose(outputs[:, :, :, 1], np.ones((2, 4, 4)) * 0.301569)
self.assertAllClose(outputs[:, :, :, 2], np.ones((2, 4, 4)) * 0.852353)

def test_pad_and_crop_to_aspect_ratio(self):
with self.assertRaisesRegex(ValueError, "Only one of"):
_ = ImageConverter(
image_size=(4, 4),
scale=1 / 255.0,
crop_to_aspect_ratio=True,
pad_to_aspect_ratio=True,
)

def test_config(self):
converter = ImageConverter(
image_size=(12, 20),
Expand Down
Loading