Skip to content

Commit

Permalink
Disable legacy tests that relied on tf.keras
Browse files Browse the repository at this point in the history
  • Loading branch information
fchollet committed Sep 22, 2023
1 parent 6383d8a commit f62f659
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
100 changes: 51 additions & 49 deletions keras_core/legacy/saving/legacy_h5_format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np
import pytest
import tensorflow as tf

import keras_core
from keras_core import layers
Expand All @@ -17,6 +16,9 @@
# on exact weight ordering for each layer, so we need
# to test across all types of layers.

# TODO: reenable tests after tf_keras is available.
tf_keras = None


def get_sequential_model(keras):
return keras.Sequential(
Expand Down Expand Up @@ -75,21 +77,21 @@ def _check_reloading_weights(self, ref_input, model, tf_keras_model):
output = model(ref_input)
self.assertAllClose(ref_output, output, atol=1e-5)

def test_sequential_model_weights(self):
def DISABLED_test_sequential_model_weights(self):
model = get_sequential_model(keras_core)
tf_keras_model = get_sequential_model(tf.keras)
tf_keras_model = get_sequential_model(tf_keras)
ref_input = np.random.random((2, 3))
self._check_reloading_weights(ref_input, model, tf_keras_model)

def test_functional_model_weights(self):
def DISABLED_test_functional_model_weights(self):
model = get_functional_model(keras_core)
tf_keras_model = get_functional_model(tf.keras)
tf_keras_model = get_functional_model(tf_keras)
ref_input = np.random.random((2, 3))
self._check_reloading_weights(ref_input, model, tf_keras_model)

def test_subclassed_model_weights(self):
def DISABLED_test_subclassed_model_weights(self):
model = get_subclassed_model(keras_core)
tf_keras_model = get_subclassed_model(tf.keras)
tf_keras_model = get_subclassed_model(tf_keras)
ref_input = np.random.random((2, 3))
self._check_reloading_weights(ref_input, model, tf_keras_model)

Expand All @@ -105,17 +107,17 @@ def _check_reloading_model(self, ref_input, model):
output = loaded(ref_input)
self.assertAllClose(ref_output, output, atol=1e-5)

def test_sequential_model(self):
def DISABLED_test_sequential_model(self):
model = get_sequential_model(keras_core)
ref_input = np.random.random((2, 3))
self._check_reloading_model(ref_input, model)

def test_functional_model(self):
def DISABLED_test_functional_model(self):
model = get_functional_model(keras_core)
ref_input = np.random.random((2, 3))
self._check_reloading_model(ref_input, model)

def test_compiled_model_with_various_layers(self):
def DISABLED_test_compiled_model_with_various_layers(self):
model = models.Sequential()
model.add(layers.Dense(2, input_shape=(3,)))
model.add(layers.RepeatVector(3))
Expand All @@ -125,7 +127,7 @@ def test_compiled_model_with_various_layers(self):
ref_input = np.random.random((1, 3))
self._check_reloading_model(ref_input, model)

def test_saving_lambda(self):
def DISABLED_test_saving_lambda(self):
mean = ops.random.uniform((4, 2, 3))
std = ops.abs(ops.random.uniform((4, 2, 3))) + 1e-5
inputs = layers.Input(shape=(4, 2, 3))
Expand All @@ -143,7 +145,7 @@ def test_saving_lambda(self):
self.assertAllClose(mean, loaded.layers[1].arguments["mu"])
self.assertAllClose(std, loaded.layers[1].arguments["std"])

def test_saving_include_optimizer_false(self):
def DISABLED_test_saving_include_optimizer_false(self):
model = models.Sequential()
model.add(layers.Dense(1))
model.compile("adam", loss="mse")
Expand All @@ -165,7 +167,7 @@ def test_saving_include_optimizer_false(self):
# Compare output
self.assertAllClose(ref_output, output, atol=1e-5)

def test_custom_sequential_registered_no_scope(self):
def DISABLED_test_custom_sequential_registered_no_scope(self):
@object_registration.register_keras_serializable(package="my_package")
class MyDense(layers.Dense):
def __init__(self, units, **kwargs):
Expand All @@ -178,7 +180,7 @@ def __init__(self, units, **kwargs):
ref_input = np.array([5])
self._check_reloading_model(ref_input, model)

def test_custom_functional_registered_no_scope(self):
def DISABLED_test_custom_functional_registered_no_scope(self):
@object_registration.register_keras_serializable(package="my_package")
class MyDense(layers.Dense):
def __init__(self, units, **kwargs):
Expand All @@ -191,7 +193,7 @@ def __init__(self, units, **kwargs):
ref_input = np.array([5])
self._check_reloading_model(ref_input, model)

def test_nested_layers(self):
def DISABLED_test_nested_layers(self):
class MyLayer(layers.Layer):
def __init__(self, sublayers, **kwargs):
super().__init__(**kwargs)
Expand Down Expand Up @@ -271,46 +273,46 @@ def _check_reloading_model(self, ref_input, model, tf_keras_model):
output = loaded(ref_input)
self.assertAllClose(ref_output, output, atol=1e-5)

def test_sequential_model(self):
def DISABLED_test_sequential_model(self):
model = get_sequential_model(keras_core)
tf_keras_model = get_sequential_model(tf.keras)
tf_keras_model = get_sequential_model(tf_keras)
ref_input = np.random.random((2, 3))
self._check_reloading_model(ref_input, model, tf_keras_model)

def test_functional_model(self):
tf_keras_model = get_functional_model(tf.keras)
def DISABLED_test_functional_model(self):
tf_keras_model = get_functional_model(tf_keras)
model = get_functional_model(keras_core)
ref_input = np.random.random((2, 3))
self._check_reloading_model(ref_input, model, tf_keras_model)

def test_compiled_model_with_various_layers(self):
def DISABLED_test_compiled_model_with_various_layers(self):
model = models.Sequential()
model.add(layers.Dense(2, input_shape=(3,)))
model.add(layers.RepeatVector(3))
model.add(layers.TimeDistributed(layers.Dense(3)))
model.compile(optimizer="rmsprop", loss="mse")

tf_keras_model = tf.keras.Sequential()
tf_keras_model.add(tf.keras.layers.Dense(2, input_shape=(3,)))
tf_keras_model.add(tf.keras.layers.RepeatVector(3))
tf_keras_model = tf_keras.Sequential()
tf_keras_model.add(tf_keras.layers.Dense(2, input_shape=(3,)))
tf_keras_model.add(tf_keras.layers.RepeatVector(3))
tf_keras_model.add(
tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(3))
tf_keras.layers.TimeDistributed(tf_keras.layers.Dense(3))
)
tf_keras_model.compile(optimizer="rmsprop", loss="mse")

ref_input = np.random.random((1, 3))
self._check_reloading_model(ref_input, model, tf_keras_model)

def test_saving_lambda(self):
def DISABLED_test_saving_lambda(self):
mean = np.random.random((4, 2, 3))
std = np.abs(np.random.random((4, 2, 3))) + 1e-5
inputs = tf.keras.layers.Input(shape=(4, 2, 3))
output = tf.keras.layers.Lambda(
inputs = tf_keras.layers.Input(shape=(4, 2, 3))
output = tf_keras.layers.Lambda(
lambda image, mu, std: (image - mu) / std,
arguments={"mu": mean, "std": std},
output_shape=inputs.shape,
)(inputs)
tf_keras_model = tf.keras.Model(inputs, output)
tf_keras_model = tf_keras.Model(inputs, output)
tf_keras_model.compile(loss="mse", optimizer="sgd", metrics=["acc"])

temp_filepath = os.path.join(self.get_temp_dir(), "lambda_model.h5")
Expand All @@ -320,9 +322,9 @@ def test_saving_lambda(self):
self.assertAllClose(mean, loaded.layers[1].arguments["mu"])
self.assertAllClose(std, loaded.layers[1].arguments["std"])

def test_saving_include_optimizer_false(self):
tf_keras_model = tf.keras.Sequential()
tf_keras_model.add(tf.keras.layers.Dense(1))
def DISABLED_test_saving_include_optimizer_false(self):
tf_keras_model = tf_keras.Sequential()
tf_keras_model.add(tf_keras.layers.Dense(1))
tf_keras_model.compile("adam", loss="mse")
x, y = np.ones((10, 10)), np.ones((10, 1))
tf_keras_model.fit(x, y)
Expand All @@ -340,15 +342,15 @@ def test_saving_include_optimizer_false(self):
# Compare output
self.assertAllClose(ref_output, output, atol=1e-5)

def test_custom_sequential_registered_no_scope(self):
@tf.keras.saving.register_keras_serializable(package="my_package")
class MyDense(tf.keras.layers.Dense):
def DISABLED_test_custom_sequential_registered_no_scope(self):
@tf_keras.saving.register_keras_serializable(package="my_package")
class MyDense(tf_keras.layers.Dense):
def __init__(self, units, **kwargs):
super().__init__(units, **kwargs)

inputs = tf.keras.layers.Input(shape=[1])
inputs = tf_keras.layers.Input(shape=[1])
custom_layer = MyDense(1)
tf_keras_model = tf.keras.Sequential(layers=[inputs, custom_layer])
tf_keras_model = tf_keras.Sequential(layers=[inputs, custom_layer])

# Re-implement and re-register in Keras Core
@object_registration.register_keras_serializable(package="my_package")
Expand All @@ -363,15 +365,15 @@ def __init__(self, units, **kwargs):
ref_input = np.array([5])
self._check_reloading_model(ref_input, model, tf_keras_model)

def test_custom_functional_registered_no_scope(self):
@tf.keras.saving.register_keras_serializable(package="my_package")
class MyDense(tf.keras.layers.Dense):
def DISABLED_test_custom_functional_registered_no_scope(self):
@tf_keras.saving.register_keras_serializable(package="my_package")
class MyDense(tf_keras.layers.Dense):
def __init__(self, units, **kwargs):
super().__init__(units, **kwargs)

inputs = tf.keras.layers.Input(shape=[1])
inputs = tf_keras.layers.Input(shape=[1])
outputs = MyDense(1)(inputs)
tf_keras_model = tf.keras.Model(inputs, outputs)
tf_keras_model = tf_keras.Model(inputs, outputs)

# Re-implement and re-register in Keras Core
@object_registration.register_keras_serializable(package="my_package")
Expand All @@ -386,8 +388,8 @@ def __init__(self, units, **kwargs):
ref_input = np.array([5])
self._check_reloading_model(ref_input, model, tf_keras_model)

def test_nested_layers(self):
class MyLayer(tf.keras.layers.Layer):
def DISABLED_test_nested_layers(self):
class MyLayer(tf_keras.layers.Layer):
def __init__(self, sublayers, **kwargs):
super().__init__(**kwargs)
self.sublayers = sublayers
Expand All @@ -400,30 +402,30 @@ def call(self, x):

def get_config(self):
config = super().get_config()
config["sublayers"] = tf.keras.saving.serialize_keras_object(
config["sublayers"] = tf_keras.saving.serialize_keras_object(
self.sublayers
)
return config

@classmethod
def from_config(cls, config):
config["sublayers"] = tf.keras.saving.deserialize_keras_object(
config["sublayers"] = tf_keras.saving.deserialize_keras_object(
config["sublayers"]
)
return cls(**config)

@tf.keras.saving.register_keras_serializable(package="Foo")
@tf_keras.saving.register_keras_serializable(package="Foo")
class RegisteredSubLayer(layers.Layer):
def call(self, x):
return x

layer = MyLayer(
[
tf.keras.layers.Dense(2, name="MyDense"),
tf_keras.layers.Dense(2, name="MyDense"),
RegisteredSubLayer(name="MySubLayer"),
]
)
tf_keras_model = tf.keras.Sequential([layer])
tf_keras_model = tf_keras.Sequential([layer])

x = np.random.random((4, 2))
ref_output = tf_keras_model(x)
Expand Down Expand Up @@ -485,7 +487,7 @@ def call(self, x):

@pytest.mark.requires_trainable_backend
class DirectoryCreationTest(testing.TestCase):
def test_directory_creation_on_save(self):
def DISABLED_test_directory_creation_on_save(self):
"""Test if directory is created on model save."""
model = get_sequential_model(keras_core)
nested_dirpath = os.path.join(
Expand Down
2 changes: 1 addition & 1 deletion requirements-common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ tensorboard-plugin-profile
rich
build
dm-tree
pytest-cov
pytest-cov

0 comments on commit f62f659

Please sign in to comment.