diff --git a/keras/layers/preprocessing/random_zoom.py b/keras/layers/preprocessing/random_zoom.py index 454325d941a..db8ab5fcbb3 100644 --- a/keras/layers/preprocessing/random_zoom.py +++ b/keras/layers/preprocessing/random_zoom.py @@ -227,8 +227,12 @@ def _get_zoom_matrix(self, zooms, image_height, image_width): # [0 0 1]] # where the last entry is implicit. # zoom matrices are always float32. - x_offset = ((float(image_width) - 1.0) / 2.0) * (1.0 - zooms[:, 0:1]) - y_offset = ((float(image_height) - 1.0) / 2.0) * (1.0 - zooms[:, 1:]) + x_offset = ((self.backend.cast(image_width, "float32") - 1.0) / 2.0) * ( + 1.0 - zooms[:, 0:1] + ) + y_offset = ( + (self.backend.cast(image_height, "float32") - 1.0) / 2.0 + ) * (1.0 - zooms[:, 1:]) return self.backend.numpy.concatenate( [ zooms[:, 0:1], diff --git a/keras/layers/preprocessing/random_zoom_test.py b/keras/layers/preprocessing/random_zoom_test.py index 970b0c9c032..8781572d780 100644 --- a/keras/layers/preprocessing/random_zoom_test.py +++ b/keras/layers/preprocessing/random_zoom_test.py @@ -4,6 +4,7 @@ from keras import backend from keras import layers +from keras import models from keras import testing @@ -108,3 +109,14 @@ def test_tf_data_compatibility(self): for output in ds.take(1): output = output.numpy() self.assertAllClose(expected_output, output) + + def test_dynamic_shape(self): + inputs = layers.Input((None, None, 3)) + outputs = layers.RandomZoom( + height_factor=(0.5, 0.5), + width_factor=(0.8, 0.8), + interpolation="nearest", + fill_mode="constant", + )(inputs) + model = models.Model(inputs, outputs) + model.predict(np.random.random((1, 6, 6, 3)))