-
Notifications
You must be signed in to change notification settings - Fork 19.6k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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) | ||
Comment on lines
+79
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modifying the global state 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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)) | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.