Source code for deepview_tensorflow._tensorflow._tensorflow_loading
### Copyright 2024 BetterWithData## 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.## This file contains code that is part of Apple's DNIKit project, licensed# under the Apache License, Version 2.0:## Copyright 2022 Apple Inc.## 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.#importtypingastimporttensorflowastffromdeepview.baseimportModelimportdeepview.typingasdtfrom._tensorflow_protocolsimportrunning_tf_1ifrunning_tf_1():from._tf1_loadingimportload_tf_1_model_from_memoryastf_memory_loadfrom._tf1_loadingimportload_tf_1_model_from_pathastf_path_loadelse:# Using type ignores here because the signature of these functions changes (input param)from._tf2_loadingimportload_tf_2_model_from_memoryastf_memory_load# type: ignorefrom._tf2_loadingimportload_tf_2_model_from_pathastf_path_load# type: ignore
[docs]defload_tf_model_from_memory(*,session:t.Optional[tf.compat.v1.Session]=None,model:t.Optional[tf.keras.models.Model]=None)->Model:""" Initialize a TensorFlow :class:`Model <deepview.base.Model>` from a model loaded in ``memory``. This function is supported for both TF2 and TF1, but different parameters are required. For TF2, only pass parameter ``model``. For TF1, only pass parameter ``session``. Args: session: Pass only this parameter when running TensorFlow 1. This is the session that contains the graph to execute. model: Pass only this parameter when running TensorFlow 2. This is the TF Keras model. Returns: A TensorFlow :class:`Model <deepview.base.Model>`. """ifrunning_tf_1():error_message='For TF1 (currently installed), please pass param `session`'else:error_message='For TF2 (currently installed), please pass param `model`'# Raise errors for incorrectifsessionisNoneandmodelisNone:raiseValueError(error_message)ifsessionisnotNoneandmodelisnotNone:raiseValueError(error_message+' only.')ifrunning_tf_1()andsessionisNone:raiseValueError(error_message)ifnotrunning_tf_1()andmodelisNone:raiseValueError(error_message)# Load TF1 with "session"ifrunning_tf_1():assertsessionisnotNonereturntf_memory_load(session)# else, load TF2 with "model"assertmodelisnotNonereturntf_memory_load(model)
[docs]defload_tf_model_from_path(path:dt.PathOrStr)->Model:""" Initialize a TensorFlow :class:`Model <deepview.base.Model>` from a model serialized in ``path`` Currently accepted serialized model formats, depending on if TF 1 or TF 2 is running. TF2 Supported formats: - TensorFlow Keras SavedModel - Keras whole models (h5) - Keras models with separate architecture and weights files TF1 Supported formats: - TensorFlow SavedModel - TensorFlow checkpoint (pass checkpoint prefix as ``path`` param) - TensorFlow protobuf - Keras whole models - Keras models with separate architecture and weights files Note: The keras loaders are currently using ``tf.keras`` instead of ``keras`` natively, and so issues might appear when trying to load models saved with native ``keras`` (not tf.keras). In this case, load the model outside of DeepView with ``keras`` and pass it to load with :func:`load_tf_model_from_memory <deepview_tensorflow.load_tf_model_from_memory>`. Args: path: Model path (for single model file) or directory that contains all the model files. Returns: A DeepView TensorFlow :class:`Model <deepview.base.Model>`. """returntf_path_load(path)