Skip to content

Commit

Permalink
Preparation for switch in Keras serialization format (#1851)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkovela1 authored Feb 9, 2023
1 parent 49f93db commit 94f3750
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
5 changes: 3 additions & 2 deletions autokeras/blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@
from autokeras.blocks.wrapper import StructuredDataBlock
from autokeras.blocks.wrapper import TextBlock
from autokeras.blocks.wrapper import TimeseriesBlock
from autokeras.utils import utils


def serialize(obj):
return keras.utils.serialize_keras_object(obj)
return utils.serialize_keras_object(obj)


def deserialize(config, custom_objects=None):
return keras.utils.deserialize_keras_object(
return utils.deserialize_keras_object(
config,
module_objects=globals(),
custom_objects=custom_objects,
Expand Down
6 changes: 3 additions & 3 deletions autokeras/hyper_preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
# 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.
from tensorflow import keras

from autokeras import preprocessors
from autokeras.engine import hyper_preprocessor
from autokeras.utils import utils


def serialize(encoder):
return keras.utils.serialize_keras_object(encoder)
return utils.serialize_keras_object(encoder)


def deserialize(config, custom_objects=None):
return keras.utils.deserialize_keras_object(
return utils.deserialize_keras_object(
config,
module_objects=globals(),
custom_objects=custom_objects,
Expand Down
5 changes: 3 additions & 2 deletions autokeras/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
from autokeras import preprocessors
from autokeras.engine import io_hypermodel
from autokeras.engine import node as node_module
from autokeras.utils import utils


def serialize(obj):
return keras.utils.serialize_keras_object(obj)
return utils.serialize_keras_object(obj)


def deserialize(config, custom_objects=None):
return keras.utils.deserialize_keras_object(
return utils.deserialize_keras_object(
config,
module_objects=globals(),
custom_objects=custom_objects,
Expand Down
5 changes: 3 additions & 2 deletions autokeras/preprocessors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
from autokeras.preprocessors.encoders import OneHotEncoder
from autokeras.preprocessors.postprocessors import SigmoidPostprocessor
from autokeras.preprocessors.postprocessors import SoftmaxPostprocessor
from autokeras.utils import utils


def serialize(preprocessor):
return keras.utils.serialize_keras_object(preprocessor)
return utils.serialize_keras_object(preprocessor)


def deserialize(config, custom_objects=None):
return keras.utils.deserialize_keras_object(
return utils.deserialize_keras_object(
config,
module_objects=globals(),
custom_objects=custom_objects,
Expand Down
20 changes: 20 additions & 0 deletions autokeras/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,23 @@ def add_to_hp(hp, hps, name=None):
class_name = hp.__class__.__name__
func = getattr(hps, class_name)
return func(name=name, **kwargs)


def serialize_keras_object(obj):
if hasattr(tf.keras.utils, "legacy"):
return tf.keras.utils.legacy.serialize_keras_object(obj) # pragma: no cover
else:
return tf.keras.utils.serialize_keras_object(obj)


def deserialize_keras_object(
config, module_objects=None, custom_objects=None, printable_module_name=None
):
if hasattr(tf.keras.utils, "legacy"):
return tf.keras.utils.legacy.deserialize_keras_object( # pragma: no cover
config, custom_objects, module_objects, printable_module_name
)
else:
return tf.keras.utils.deserialize_keras_object(
config, custom_objects, module_objects, printable_module_name
)

0 comments on commit 94f3750

Please sign in to comment.