Remove the __init__ content for keras/saving.

All the direct dependencies has been changed to use explicit import, rather than rely on the __init__ shortcut.

PiperOrigin-RevId: 274001008
This commit is contained in:
Scott Zhu 2019-10-10 11:25:09 -07:00 committed by TensorFlower Gardener
parent e258e9e31c
commit 402ccb1803
9 changed files with 26 additions and 55 deletions

View File

@ -14,6 +14,9 @@ keras_packages = [
"tensorflow.python", "tensorflow.python",
"tensorflow.python.keras", "tensorflow.python.keras",
"tensorflow.python.keras.activations", "tensorflow.python.keras.activations",
"tensorflow.python.keras.saving.model_config",
"tensorflow.python.keras.saving.save",
"tensorflow.python.keras.saving.saved_model_experimental",
"tensorflow.python.keras.utils.data_utils", "tensorflow.python.keras.utils.data_utils",
"tensorflow.python.keras.utils.generic_utils", "tensorflow.python.keras.utils.generic_utils",
"tensorflow.python.keras.utils.io_utils", "tensorflow.python.keras.utils.io_utils",

View File

@ -39,12 +39,13 @@ from tensorflow.python.framework import func_graph
from tensorflow.python.framework import ops from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape from tensorflow.python.framework import tensor_shape
from tensorflow.python.keras import backend from tensorflow.python.keras import backend
from tensorflow.python.keras import saving
from tensorflow.python.keras.engine import base_layer from tensorflow.python.keras.engine import base_layer
from tensorflow.python.keras.engine import base_layer_utils from tensorflow.python.keras.engine import base_layer_utils
from tensorflow.python.keras.engine import input_layer as input_layer_module from tensorflow.python.keras.engine import input_layer as input_layer_module
from tensorflow.python.keras.engine import node as node_module from tensorflow.python.keras.engine import node as node_module
from tensorflow.python.keras.engine import training_utils from tensorflow.python.keras.engine import training_utils
from tensorflow.python.keras.saving import hdf5_format
from tensorflow.python.keras.saving import save
from tensorflow.python.keras.saving.saved_model import network_serialization from tensorflow.python.keras.saving.saved_model import network_serialization
from tensorflow.python.keras.utils import generic_utils from tensorflow.python.keras.utils import generic_utils
from tensorflow.python.keras.utils import layer_utils from tensorflow.python.keras.utils import layer_utils
@ -982,8 +983,8 @@ class Network(base_layer.Layer):
model = load_model('my_model.h5') model = load_model('my_model.h5')
``` ```
""" """
saving.save_model(self, filepath, overwrite, include_optimizer, save_format, save.save_model(self, filepath, overwrite, include_optimizer, save_format,
signatures, options) signatures, options)
def save_weights(self, filepath, overwrite=True, save_format=None): def save_weights(self, filepath, overwrite=True, save_format=None):
"""Saves all layer weights. """Saves all layer weights.
@ -1082,7 +1083,7 @@ class Network(base_layer.Layer):
return return
if save_format == 'h5': if save_format == 'h5':
with h5py.File(filepath, 'w') as f: with h5py.File(filepath, 'w') as f:
saving.save_weights_to_hdf5_group(f, self.layers) hdf5_format.save_weights_to_hdf5_group(f, self.layers)
else: else:
if context.executing_eagerly(): if context.executing_eagerly():
session = None session = None
@ -1194,10 +1195,10 @@ class Network(base_layer.Layer):
if 'layer_names' not in f.attrs and 'model_weights' in f: if 'layer_names' not in f.attrs and 'model_weights' in f:
f = f['model_weights'] f = f['model_weights']
if by_name: if by_name:
saving.load_weights_from_hdf5_group_by_name( hdf5_format.load_weights_from_hdf5_group_by_name(
f, self.layers, skip_mismatch=skip_mismatch) f, self.layers, skip_mismatch=skip_mismatch)
else: else:
saving.load_weights_from_hdf5_group(f, self.layers) hdf5_format.load_weights_from_hdf5_group(f, self.layers)
def _updated_config(self): def _updated_config(self):
"""Util shared between different serialization methods. """Util shared between different serialization methods.

View File

@ -469,7 +469,7 @@ class CuDNNV1OnlyTest(keras_parameterized.TestCase):
def assert_not_compatible(src, dest, message): def assert_not_compatible(src, dest, message):
with self.assertRaises(ValueError) as ex: with self.assertRaises(ValueError) as ex:
keras.saving.preprocess_weights_for_loading( keras.saving.hdf5_format.preprocess_weights_for_loading(
dest, dest,
get_layer_weights(src)) get_layer_weights(src))
self.assertIn(message, str(ex.exception)) self.assertIn(message, str(ex.exception))

View File

@ -31,9 +31,9 @@ from tensorflow.python import keras
from tensorflow.python.eager import context from tensorflow.python.eager import context
from tensorflow.python.framework import dtypes from tensorflow.python.framework import dtypes
from tensorflow.python.keras import keras_parameterized from tensorflow.python.keras import keras_parameterized
from tensorflow.python.keras import saving
from tensorflow.python.keras import testing_utils from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.layers.preprocessing import preprocessing_test_utils from tensorflow.python.keras.layers.preprocessing import preprocessing_test_utils
from tensorflow.python.keras.saving import saved_model_experimental as saving
from tensorflow.python.keras.utils.generic_utils import CustomObjectScope from tensorflow.python.keras.utils.generic_utils import CustomObjectScope
from tensorflow.python.platform import test from tensorflow.python.platform import test

View File

@ -29,9 +29,9 @@ from tensorflow.python.eager import def_function
from tensorflow.python.framework import constant_op from tensorflow.python.framework import constant_op
from tensorflow.python.framework import ops from tensorflow.python.framework import ops
from tensorflow.python.keras import keras_parameterized from tensorflow.python.keras import keras_parameterized
from tensorflow.python.keras import saving
from tensorflow.python.keras import testing_utils from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.optimizer_v2 import adam from tensorflow.python.keras.optimizer_v2 import adam
from tensorflow.python.keras.saving import model_config
from tensorflow.python.ops import array_ops from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_nn_ops from tensorflow.python.ops import gen_nn_ops
from tensorflow.python.ops import math_ops from tensorflow.python.ops import math_ops
@ -332,7 +332,7 @@ class AutoLambdaTest(keras_parameterized.TestCase):
def test_json_serialization(self): def test_json_serialization(self):
inputs = keras.Input(shape=(4,), dtype='uint8') inputs = keras.Input(shape=(4,), dtype='uint8')
outputs = math_ops.cast(inputs, 'float32') / 4. outputs = math_ops.cast(inputs, 'float32') / 4.
model = saving.model_from_json(keras.Model(inputs, outputs).to_json()) model = model_config.model_from_json(keras.Model(inputs, outputs).to_json())
self.assertAllEqual( self.assertAllEqual(
self.evaluate(model(np.array([0, 64, 128, 192], np.uint8))), self.evaluate(model(np.array([0, 64, 128, 192], np.uint8))),
[0., 16., 32., 48.]) [0., 16., 32., 48.])

View File

@ -37,7 +37,6 @@ from tensorflow.python.keras import layers
from tensorflow.python.keras import models from tensorflow.python.keras import models
from tensorflow.python.keras import optimizers from tensorflow.python.keras import optimizers
from tensorflow.python.keras import regularizers from tensorflow.python.keras import regularizers
from tensorflow.python.keras import saving
from tensorflow.python.keras import testing_utils from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.engine import base_layer from tensorflow.python.keras.engine import base_layer
from tensorflow.python.keras.engine import base_layer_utils from tensorflow.python.keras.engine import base_layer_utils
@ -46,6 +45,7 @@ from tensorflow.python.keras.mixed_precision.experimental import loss_scale_opti
from tensorflow.python.keras.mixed_precision.experimental import policy from tensorflow.python.keras.mixed_precision.experimental import policy
from tensorflow.python.keras.mixed_precision.experimental import test_util as mp_test_util from tensorflow.python.keras.mixed_precision.experimental import test_util as mp_test_util
from tensorflow.python.keras.optimizer_v2 import gradient_descent from tensorflow.python.keras.optimizer_v2 import gradient_descent
from tensorflow.python.keras.saving import save
from tensorflow.python.ops import array_ops from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops from tensorflow.python.ops import math_ops
from tensorflow.python.ops import variables from tensorflow.python.ops import variables
@ -1135,7 +1135,7 @@ class KerasModelTest(keras_parameterized.TestCase):
self.assertEqual(backend.get_value(loss_scale._num_good_steps), 0) self.assertEqual(backend.get_value(loss_scale._num_good_steps), 0)
# Load model weights and ensure loss scale weights are restored. # Load model weights and ensure loss scale weights are restored.
model = saving.load_model(save_path, custom_objects={'AddLayer': AddLayer}) model = save.load_model(save_path, custom_objects={'AddLayer': AddLayer})
loss_scale = model.optimizer.loss_scale loss_scale = model.optimizer.loss_scale
(weight,) = model.trainable_weights (weight,) = model.trainable_weights
loaded_weight = backend.get_value(weight) loaded_weight = backend.get_value(weight)

View File

@ -22,7 +22,6 @@ from __future__ import print_function
from tensorflow.python.keras import backend as K from tensorflow.python.keras import backend as K
from tensorflow.python.keras import metrics as metrics_module from tensorflow.python.keras import metrics as metrics_module
from tensorflow.python.keras import optimizers from tensorflow.python.keras import optimizers
from tensorflow.python.keras import saving
from tensorflow.python.keras.engine import network from tensorflow.python.keras.engine import network
from tensorflow.python.keras.engine import sequential from tensorflow.python.keras.engine import sequential
from tensorflow.python.keras.engine import training from tensorflow.python.keras.engine import training
@ -31,6 +30,8 @@ from tensorflow.python.keras.engine.base_layer import Layer
from tensorflow.python.keras.engine.input_layer import Input from tensorflow.python.keras.engine.input_layer import Input
from tensorflow.python.keras.engine.input_layer import InputLayer from tensorflow.python.keras.engine.input_layer import InputLayer
from tensorflow.python.keras.engine.network import Network from tensorflow.python.keras.engine.network import Network
from tensorflow.python.keras.saving import model_config
from tensorflow.python.keras.saving import save
from tensorflow.python.keras.utils import generic_utils from tensorflow.python.keras.utils import generic_utils
from tensorflow.python.keras.utils.generic_utils import CustomObjectScope from tensorflow.python.keras.utils.generic_utils import CustomObjectScope
from tensorflow.python.platform import tf_logging as logging from tensorflow.python.platform import tf_logging as logging
@ -41,11 +42,11 @@ from tensorflow.python.util.tf_export import keras_export
# API entries importable from `keras.models`: # API entries importable from `keras.models`:
Model = training.Model # pylint: disable=invalid-name Model = training.Model # pylint: disable=invalid-name
Sequential = sequential.Sequential # pylint: disable=invalid-name Sequential = sequential.Sequential # pylint: disable=invalid-name
save_model = saving.save_model save_model = save.save_model
load_model = saving.load_model load_model = save.load_model
model_from_config = saving.model_from_config model_from_config = model_config.model_from_config
model_from_yaml = saving.model_from_yaml model_from_yaml = model_config.model_from_yaml
model_from_json = saving.model_from_json model_from_json = model_config.model_from_json
# Callable used to clone a layer with weights preserved. # Callable used to clone a layer with weights preserved.

View File

@ -1,35 +0,0 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.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.
# ==============================================================================
"""Utils for saving and loading Keras Models."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.python.keras.saving.hdf5_format import load_attributes_from_hdf5_group
from tensorflow.python.keras.saving.hdf5_format import load_model_from_hdf5
from tensorflow.python.keras.saving.hdf5_format import load_weights_from_hdf5_group
from tensorflow.python.keras.saving.hdf5_format import load_weights_from_hdf5_group_by_name
from tensorflow.python.keras.saving.hdf5_format import preprocess_weights_for_loading
from tensorflow.python.keras.saving.hdf5_format import save_attributes_to_hdf5_group
from tensorflow.python.keras.saving.hdf5_format import save_model_to_hdf5
from tensorflow.python.keras.saving.hdf5_format import save_weights_to_hdf5_group
from tensorflow.python.keras.saving.model_config import model_from_config
from tensorflow.python.keras.saving.model_config import model_from_json
from tensorflow.python.keras.saving.model_config import model_from_yaml
from tensorflow.python.keras.saving.save import load_model
from tensorflow.python.keras.saving.save import save_model
from tensorflow.python.keras.saving.saved_model_experimental import export_saved_model
from tensorflow.python.keras.saving.saved_model_experimental import load_from_saved_model
from tensorflow.python.keras.saving.saving_utils import trace_model_call

View File

@ -25,7 +25,7 @@ from tensorflow.python.framework import ops
from tensorflow.python.keras import backend as K from tensorflow.python.keras import backend as K
from tensorflow.python.keras import optimizers from tensorflow.python.keras import optimizers
from tensorflow.python.keras.optimizer_v2 import optimizer_v2 from tensorflow.python.keras.optimizer_v2 import optimizer_v2
from tensorflow.python.keras.saving import model_from_json from tensorflow.python.keras.saving import model_config
from tensorflow.python.keras.saving import saving_utils from tensorflow.python.keras.saving import saving_utils
from tensorflow.python.keras.utils import mode_keys from tensorflow.python.keras.utils import mode_keys
from tensorflow.python.lib.io import file_io from tensorflow.python.lib.io import file_io
@ -417,7 +417,8 @@ def load_from_saved_model(saved_model_path, custom_objects=None):
compat.as_bytes(constants.ASSETS_DIRECTORY), compat.as_bytes(constants.ASSETS_DIRECTORY),
compat.as_bytes(constants.SAVED_MODEL_FILENAME_JSON)) compat.as_bytes(constants.SAVED_MODEL_FILENAME_JSON))
model_json = file_io.read_file_to_string(model_json_filepath) model_json = file_io.read_file_to_string(model_json_filepath)
model = model_from_json(model_json, custom_objects=custom_objects) model = model_config.model_from_json(
model_json, custom_objects=custom_objects)
# restore model weights # restore model weights
checkpoint_prefix = os.path.join( checkpoint_prefix = os.path.join(