Beispiel #1
0
from tensorflow.python.platform import tf_logging as logging


# pylint: disable=g-import-not-at-top
try:
  import h5py
  HDF5_OBJECT_HEADER_LIMIT = 64512
except ImportError:
  h5py = None
# pylint: enable=g-import-not-at-top

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
sequential_lib = LazyLoader(
    "sequential_lib", globals(),
    "keras.engine.sequential")
# pylint:enable=g-inconsistent-quotes


def save_model_to_hdf5(model, filepath, overwrite=True, include_optimizer=True):
  """Saves a model to a HDF5 file.

  The saved model contains:
      - the model's configuration (topology)
      - the model's weights
      - the model's optimizer's state (if any)

  Thus the saved model can be reinstantiated in
  the exact same state, without any of the code
  used for model definition or training.
# limitations under the License.
# ==============================================================================
"""Helper classes that list&validate all attributes to serialize to SavedModel.
"""

from keras.saving.saved_model import constants
from keras.saving.saved_model import order_preserving_set as ops
from keras.saving.saved_model import save_impl
from keras.utils.generic_utils import LazyLoader
import tensorflow.compat.v2 as tf

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
base_layer = LazyLoader(
    "base_layer", globals(),
    "keras.engine.base_layer")
training_lib = LazyLoader(
    "training_lib", globals(),
    "keras.engine.training")
metrics = LazyLoader("metrics", globals(),
                     "keras.metrics")
recurrent = LazyLoader(
    "recurrent", globals(),
    "keras.layers.recurrent")
# pylint:enable=g-inconsistent-quotes


class SerializedAttributes:
  """Class that tracks and validates all serialization attributes.
Beispiel #3
0
from keras.utils import tf_inspect
from keras.utils import tf_utils
from keras.utils import version_utils
from keras.utils.generic_utils import LazyLoader
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.training.tracking import base as trackable


# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
base_layer = LazyLoader(
    "base_layer", globals(),
    "keras.engine.base_layer")
metrics = LazyLoader("metrics", globals(),
                     "keras.metrics")
input_layer = LazyLoader(
    "input_layer", globals(),
    "keras.engine.input_layer")
training_lib = LazyLoader(
    "training_lib", globals(),
    "keras.engine.training")
sequential_lib = LazyLoader(
    "sequential_lib", globals(),
    "keras.engine.sequential")
# pylint:enable=g-inconsistent-quotes

Beispiel #4
0
from tensorflow.core.framework import versions_pb2
from keras import backend as K
from keras.protobuf import saved_metadata_pb2
from keras.saving import saving_utils
from keras.saving.saved_model import constants
from keras.saving.saved_model import save_impl
from keras.saving.saved_model import utils
from keras.utils.generic_utils import LazyLoader
from keras.utils.io_utils import ask_to_proceed_with_overwrite
from tensorflow.python.saved_model import save as save_lib

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

base_layer = LazyLoader("base_layer", globals(), "keras.engine.base_layer")
training_lib = LazyLoader("training_lib", globals(), "keras.engine.training")


def save(model,
         filepath,
         overwrite,
         include_optimizer,
         signatures=None,
         options=None,
         save_traces=True):
    """Saves a model as a SavedModel to the filepath.

  Args:
    model: Keras model instance to be saved.
    filepath: String path to save the model.
Beispiel #5
0
from keras.utils.generic_utils import LazyLoader
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import constants
from tensorflow.python.saved_model import model_utils
from tensorflow.python.saved_model import utils_impl as saved_model_utils
from tensorflow.python.training.tracking import graph_view
from tensorflow.python.util.tf_export import keras_export

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
metrics_lib = LazyLoader("metrics_lib", globals(), "keras.metrics")
models_lib = LazyLoader("models_lib", globals(), "keras.models")
sequential = LazyLoader("sequential", globals(), "keras.engine.sequential")
# pylint:enable=g-inconsistent-quotes


@keras_export(v1=['keras.experimental.export_saved_model'])
def export_saved_model(model,
                       saved_model_path,
                       custom_objects=None,
                       as_text=False,
                       input_signature=None,
                       serving_only=False):
    """Exports a `tf.keras.Model` as a Tensorflow SavedModel.

  Note that at this time, subclassed models can only be saved using
Beispiel #6
0
from keras.utils import generic_utils
from keras.utils import metrics_utils
from keras.utils.generic_utils import LazyLoader
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.saved_model import loader_impl
from tensorflow.python.saved_model import nested_structure_coder
from tensorflow.python.saved_model import revived_types
from tensorflow.python.training.tracking import base as trackable

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
models_lib = LazyLoader("models_lib", globals(),
                        "keras.models")
base_layer = LazyLoader(
    "base_layer", globals(),
    "keras.engine.base_layer")
layers_module = LazyLoader(
    "layers_module", globals(),
    "keras.layers")
input_layer = LazyLoader(
    "input_layer", globals(),
    "keras.engine.input_layer")
functional_lib = LazyLoader(
    "functional_lib", globals(),
    "keras.engine.functional")
training_lib = LazyLoader(
    "training_lib", globals(),
    "keras.engine.training")
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Helper classes that list&validate all attributes to serialize to SavedModel.
"""

from keras.saving.saved_model import constants
from keras.saving.saved_model import order_preserving_set as ops
from keras.saving.saved_model import save_impl
from keras.utils.generic_utils import LazyLoader
import tensorflow.compat.v2 as tf

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
base_layer = LazyLoader("base_layer", globals(), "keras.engine.base_layer")
training_lib = LazyLoader("training_lib", globals(), "keras.engine.training")
metrics = LazyLoader("metrics", globals(), "keras.metrics")
base_rnn = LazyLoader("base_rnn", globals(), "keras.layers.rnn.base_rnn")
# pylint:enable=g-inconsistent-quotes


class SerializedAttributes:
    """Class that tracks and validates all serialization attributes.

    Keras models contain many Python-defined components. For example, the
    trainable_variable property lists the model's trainable variables by
    recursively retrieving the trainable variables from each of the child layers.
    Another example is model.call, a python function that calls child layers and
    adds ops to the backend graph.
Beispiel #8
0
import inspect as _inspect
import itertools
import threading
import types

from keras import backend
from keras.engine import base_layer_utils
from keras.utils import control_flow_util
from keras.utils import tf_contextlib
from keras.utils import tf_inspect
from keras.utils.generic_utils import LazyLoader

import tensorflow.compat.v2 as tf

# pylint:disable=g-inconsistent-quotes
training_lib = LazyLoader("training_lib", globals(), "keras.engine.training")
# pylint:enable=g-inconsistent-quotes


def use_wrapped_call(layer,
                     call_fn,
                     default_training_value=None,
                     return_method=False):
    """Creates fn that adds the losses returned by call_fn & returns the outputs.

  Args:
    layer: A Keras layer object
    call_fn: tf.function that takes layer inputs (and possibly a training arg),
      and returns a tuple of (outputs, list of losses).
    default_training_value: Default value of the training kwarg. If `None`, the
      default is `tf.keras.backend.learning_phase()`.
Beispiel #9
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# pylint: disable=protected-access
"""Utilities for Keras classes with v1 and v2 versions."""

import tensorflow.compat.v2 as tf
from keras.utils.generic_utils import LazyLoader

# TODO(b/134426265): Switch back to single-quotes once the issue
# with copybara is fixed.
# pylint: disable=g-inconsistent-quotes
training = LazyLoader("training", globals(), "keras.engine.training")
training_v1 = LazyLoader("training_v1", globals(), "keras.engine.training_v1")
base_layer = LazyLoader("base_layer", globals(), "keras.engine.base_layer")
base_layer_v1 = LazyLoader("base_layer_v1", globals(),
                           "keras.engine.base_layer_v1")
callbacks = LazyLoader("callbacks", globals(), "keras.callbacks")
callbacks_v1 = LazyLoader("callbacks_v1", globals(), "keras.callbacks_v1")

# pylint: enable=g-inconsistent-quotes


class ModelVersionSelector:
    """Chooses between Keras v1 and v2 Model class."""
    def __new__(cls, *args, **kwargs):  # pylint: disable=unused-argument
        use_v2 = should_use_v2()
        cls = swap_class(cls, training.Model, training_v1.Model, use_v2)  # pylint: disable=self-cls-assignment
Beispiel #10
0
from keras.saving.saved_model import utils
from keras.utils import layer_utils
from keras.utils import tf_contextlib
from keras.utils import tf_utils
from keras.utils import version_utils
from keras.utils.generic_utils import LazyLoader
import tensorflow.compat.v1.logging as logging
import tensorflow.compat.v2 as tf

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
base_layer = LazyLoader('base_layer', globals(), 'keras.engine.base_layer')
metrics = LazyLoader('metrics', globals(), 'keras.metrics')
input_layer = LazyLoader('input_layer', globals(), 'keras.engine.input_layer')
training_lib = LazyLoader('training_lib', globals(), 'keras.engine.training')
sequential_lib = LazyLoader('sequential_lib', globals(),
                            'keras.engine.sequential')
# pylint:enable=g-inconsistent-quotes


def should_skip_serialization(layer):
    """Skip serializing extra objects and functions if layer inputs aren't set."""
    saved_model_input_spec_set = (
        isinstance(layer, training_lib.Model)
        and layer._saved_model_inputs_spec is not None)  # pylint: disable=protected-access
    if not layer.built and not saved_model_input_spec_set:
        logging.warning(