From f5b293d0927506c2a979a091bf0d07ecc78fa181 Mon Sep 17 00:00:00 2001 From: Dmitrii Agibov Date: Thu, 8 Sep 2022 14:24:39 +0100 Subject: MLIA-386 Simplify typing in the source code - Enable deferred annotations evaluation - Use builtin types for type hints whenever possible - Use | syntax for union types - Rename mlia.core._typing into mlia.core.typing Change-Id: I3f6ffc02fa069c589bdd9e8bddbccd504285427a --- src/mlia/nn/tensorflow/utils.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/mlia/nn/tensorflow/utils.py') diff --git a/src/mlia/nn/tensorflow/utils.py b/src/mlia/nn/tensorflow/utils.py index b1034d9..6250f56 100644 --- a/src/mlia/nn/tensorflow/utils.py +++ b/src/mlia/nn/tensorflow/utils.py @@ -2,11 +2,12 @@ # SPDX-FileCopyrightText: Copyright The TensorFlow Authors. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """Collection of useful functions for optimizations.""" +from __future__ import annotations + import logging from pathlib import Path from typing import Callable from typing import Iterable -from typing import Union import numpy as np import tensorflow as tf @@ -101,21 +102,19 @@ def convert_tf_to_tflite(model: str, quantized: bool = False) -> Interpreter: return tflite_model -def save_keras_model(model: tf.keras.Model, save_path: Union[str, Path]) -> None: +def save_keras_model(model: tf.keras.Model, save_path: str | Path) -> None: """Save Keras model at provided path.""" # Checkpoint: saving the optimizer is necessary. model.save(save_path, include_optimizer=True) -def save_tflite_model( - model: tf.lite.TFLiteConverter, save_path: Union[str, Path] -) -> None: +def save_tflite_model(model: tf.lite.TFLiteConverter, save_path: str | Path) -> None: """Save TFLite model at provided path.""" with open(save_path, "wb") as file: file.write(model) -def is_tflite_model(model: Union[Path, str]) -> bool: +def is_tflite_model(model: str | Path) -> bool: """Check if model type is supported by TFLite API. TFLite model is indicated by the model file extension .tflite @@ -124,7 +123,7 @@ def is_tflite_model(model: Union[Path, str]) -> bool: return model_path.suffix == ".tflite" -def is_keras_model(model: Union[Path, str]) -> bool: +def is_keras_model(model: str | Path) -> bool: """Check if model type is supported by Keras API. Keras model is indicated by: @@ -139,7 +138,7 @@ def is_keras_model(model: Union[Path, str]) -> bool: return model_path.suffix in (".h5", ".hdf5") -def is_tf_model(model: Union[Path, str]) -> bool: +def is_tf_model(model: str | Path) -> bool: """Check if model type is supported by TensorFlow API. TensorFlow model is indicated if its directory (meaning saved model) -- cgit v1.2.1