Skip to content

Fix: UpSampling2D bilinear set_image_data_format(channels_first) bug #21456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion keras/src/layers/reshaping/up_sampling2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ def _resize_images(
shape[1] * height_factor,
shape[2] * width_factor,
)
x = ops.image.resize(x, new_shape, interpolation=interpolation)
x = ops.image.resize(
x,
new_shape,
data_format="channels_last",
interpolation=interpolation,
)
if data_format == "channels_first":
x = ops.transpose(x, [0, 3, 1, 2])

Expand Down
11 changes: 10 additions & 1 deletion keras/src/layers/reshaping/up_sampling2d_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from keras.src import backend
from keras.src import layers
from keras.src import testing
from keras.backend import set_image_data_format


class UpSampling2dTest(testing.TestCase):
Expand Down Expand Up @@ -62,15 +63,22 @@ def test_upsampling_2d(self, data_format, length_row, length_col):

@parameterized.product(
data_format=["channels_first", "channels_last"],
use_set_image_data_format=[True, False],
length_row=[2],
length_col=[2, 3],
)
@pytest.mark.requires_trainable_backend
def test_upsampling_2d_bilinear(self, data_format, length_row, length_col):
def test_upsampling_2d_bilinear(
self, data_format, use_set_image_data_format, length_row, length_col
):
num_samples = 2
stack_size = 2
input_num_row = 11
input_num_col = 12

if use_set_image_data_format:
set_image_data_format(data_format)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not set this globally in a unit test, instead pass the argument when you create the UpSampling2D layer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, that's the only way the error happens, as I've stated here
#21401 (comment)

There are no issues if I simply do
keras.layers.UpSampling2D(size=(2, 2), interpolation="bilinear", data_format="channels_first")(x)

I'm okay with just removing that line from the test, but it won't be testing my specific issue. Let me know if that's our best solution.

Comment on lines +79 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Modifying the global state image_data_format can lead to flaky tests. To ensure test isolation, save the original image_data_format before changing it and restore it after the test execution using a try...finally block.

        original_data_format = backend.image_data_format()
        if use_set_image_data_format:
            set_image_data_format(data_format)
        
        try:
            if data_format == "channels_first":
                inputs = np.random.rand(
                    num_samples, stack_size, input_num_row, input_num_col
                )
            else:
                inputs = np.random.rand(
                    num_samples, input_num_row, input_num_col, stack_size
                )

            self.run_layer_test(
                layers.UpSampling2D,
                init_kwargs={
                    "size": (2, 2),
                    "data_format": data_format,
                    "interpolation": "bilinear",
                },
                input_shape=inputs.shape,
            )

            layer = layers.UpSampling2D(
                size=(length_row, length_col),
                data_format=data_format,
                interpolation="bilinear",
            )
            layer.build(inputs.shape)
            np_output = layer(inputs=backend.Variable(inputs))
            if data_format == "channels_first":
                self.assertEqual(np_output.shape[2], length_row * input_num_row)
                self.assertEqual(np_output.shape[3], length_col * input_num_col)
            else:
                self.assertEqual(np_output.shape[1], length_row * input_num_row)
                self.assertEqual(np_output.shape[2], length_col * input_num_col)
        finally:
            if use_set_image_data_format:
                set_image_data_format(original_data_format)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good suggestion


if data_format == "channels_first":
inputs = np.random.rand(
num_samples, stack_size, input_num_row, input_num_col
Expand All @@ -93,6 +101,7 @@ def test_upsampling_2d_bilinear(self, data_format, length_row, length_col):
layer = layers.UpSampling2D(
size=(length_row, length_col),
data_format=data_format,
interpolation="bilinear",
)
layer.build(inputs.shape)
np_output = layer(inputs=backend.Variable(inputs))
Expand Down