Merge pull request #47135 from PaulKlinger:fix_keras_applications_preprocessing_channels_first

PiperOrigin-RevId: 359044656
Change-Id: Ia7209d0d5680412128aa2d828409aa5a38567d3f
This commit is contained in:
TensorFlower Gardener 2021-02-23 07:46:09 -08:00
commit f669266856
2 changed files with 26 additions and 8 deletions

View File

@ -289,7 +289,10 @@ def _preprocess_symbolic_input(x, data_format, mode):
else:
x = backend.bias_add(x, mean_tensor, data_format)
if std is not None:
x /= std
std_tensor = backend.constant(np.array(std))
if data_format == 'channels_first':
std_tensor = backend.reshape(std_tensor, (-1, 1, 1))
x /= std_tensor
return x

View File

@ -80,18 +80,33 @@ class TestImageNetUtils(keras_parameterized.TestCase):
self.assertAllClose(x, x2[..., ::-1])
self.assertNotEqual(xint.astype('float').max(), xint2.max())
def test_preprocess_input_symbolic(self):
@parameterized.named_parameters([
{
'testcase_name': 'mode_torch',
'mode': 'torch'
},
{
'testcase_name': 'mode_tf',
'mode': 'tf'
},
{
'testcase_name': 'mode_caffe',
'mode': 'caffe'
},
])
def test_preprocess_input_symbolic(self, mode):
# Test image batch
x = np.random.uniform(0, 255, (2, 10, 10, 3))
inputs = keras.layers.Input(shape=x.shape[1:])
outputs = keras.layers.Lambda(
utils.preprocess_input, output_shape=x.shape[1:])(
lambda x: utils.preprocess_input(x, mode=mode),
output_shape=x.shape[1:])(
inputs)
model = keras.Model(inputs, outputs)
self.assertEqual(model.predict(x).shape, x.shape)
outputs1 = keras.layers.Lambda(
lambda x: utils.preprocess_input(x, 'channels_last'),
lambda x: utils.preprocess_input(x, 'channels_last', mode=mode),
output_shape=x.shape[1:])(
inputs)
model1 = keras.Model(inputs, outputs1)
@ -99,7 +114,7 @@ class TestImageNetUtils(keras_parameterized.TestCase):
x2 = np.transpose(x, (0, 3, 1, 2))
inputs2 = keras.layers.Input(shape=x2.shape[1:])
outputs2 = keras.layers.Lambda(
lambda x: utils.preprocess_input(x, 'channels_first'),
lambda x: utils.preprocess_input(x, 'channels_first', mode=mode),
output_shape=x2.shape[1:])(
inputs2)
model2 = keras.Model(inputs2, outputs2)
@ -110,13 +125,13 @@ class TestImageNetUtils(keras_parameterized.TestCase):
x = np.random.uniform(0, 255, (10, 10, 3))
inputs = keras.layers.Input(shape=x.shape)
outputs = keras.layers.Lambda(
utils.preprocess_input, output_shape=x.shape)(
lambda x: utils.preprocess_input(x, mode=mode), output_shape=x.shape)(
inputs)
model = keras.Model(inputs, outputs)
self.assertEqual(model.predict(x[np.newaxis])[0].shape, x.shape)
outputs1 = keras.layers.Lambda(
lambda x: utils.preprocess_input(x, 'channels_last'),
lambda x: utils.preprocess_input(x, 'channels_last', mode=mode),
output_shape=x.shape)(
inputs)
model1 = keras.Model(inputs, outputs1)
@ -124,7 +139,7 @@ class TestImageNetUtils(keras_parameterized.TestCase):
x2 = np.transpose(x, (2, 0, 1))
inputs2 = keras.layers.Input(shape=x2.shape)
outputs2 = keras.layers.Lambda(
lambda x: utils.preprocess_input(x, 'channels_first'),
lambda x: utils.preprocess_input(x, 'channels_first', mode=mode),
output_shape=x2.shape)(
inputs2)
model2 = keras.Model(inputs2, outputs2)