o
    ci                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlZ	d dlm
Z
 d dlmZ d dlmZ er8d dlmZ edd	G d
d deZdS )    N)Path)TYPE_CHECKINGOptional)keras)FrameworkCheckpoint)	PublicAPI)Preprocessorbeta)	stabilityc                   @   s   e Zd ZdZdZedddejded dd fd	d
Z	eddde
ded dd fddZeddde
ded dd fddZdejjfddZdS )TensorflowCheckpointzKA :py:class:`~ray.train.Checkpoint` with TensorFlow-specific functionality._model_filenameN)preprocessormodelr   r   returnc                C   sL   t  }d}|t||  | |}|r|| || j|i |S )au  Create a :py:class:`~ray.train.Checkpoint` that stores a Keras model.

        The checkpoint created with this method needs to be paired with
        `model` when used.

        Args:
            model: The Keras model, whose weights are stored in the checkpoint.
            preprocessor: A fitted preprocessor to be applied before inference.

        Returns:
            A :py:class:`TensorflowCheckpoint` containing the specified model.

        Examples:

            .. testcode::

                from ray.train.tensorflow import TensorflowCheckpoint
                import tensorflow as tf

                model = tf.keras.applications.resnet.ResNet101()
                checkpoint = TensorflowCheckpoint.from_model(model)

            .. testoutput::
                :options: +MOCK
                :hide:

                ...  # Model may or may not be downloaded

        zmodel.keras)	tempfilemkdtempsaver   as_posixfrom_directoryset_preprocessorupdate_metadataMODEL_FILENAME_KEY)clsr   r   tempdirfilename
checkpoint r   ^/home/ubuntu/.local/lib/python3.10/site-packages/ray/train/tensorflow/tensorflow_checkpoint.py
from_model   s   $

zTensorflowCheckpoint.from_model	file_pathc                C   sx   t j|r|dstdt }t j|}t||	 }t
|| | |}|r2|| || j|i |S )aT  Create a :py:class:`~ray.train.Checkpoint` that stores a Keras
        model from H5 format.

        The checkpoint generated by this method contains all the information needed.
        Thus no `model` is needed to be supplied when using this checkpoint.

        Args:
            file_path: The path to the .h5 file to load model from. This is the
                same path that is used for ``model.save(path)``.
            preprocessor: A fitted preprocessor to be applied before inference.

        Returns:
            A :py:class:`TensorflowCheckpoint` converted from h5 format.

        z.h5zAPlease supply a h5 file path to `TensorflowCheckpoint.from_h5()`.)ospathisfileendswith
ValueErrorr   r   basenamer   r   shutilcopyr   r   r   r   )r   r   r   r   r   new_checkpoint_filer   r   r   r   from_h5E   s   

zTensorflowCheckpoint.from_h5dir_pathc                C   s^   t j|s
tdt }t | t|| | 	|}|r%|
| || jdi |S )ac  Create a :py:class:`~ray.train.Checkpoint` that stores a Keras
        model from SavedModel format.

        The checkpoint generated by this method contains all the information needed.
        Thus no `model` is needed to be supplied when using this checkpoint.

        Args:
            dir_path: The directory containing the saved model. This is the same
                directory as used by ``model.save(dir_path)``.
            preprocessor: A fitted preprocessor to be applied before inference.

        Returns:
            A :py:class:`TensorflowCheckpoint` converted from SavedModel format.

        zDPlease supply a directory to `TensorflowCheckpoint.from_saved_model`.)r    r!   isdirr$   r   r   rmdirr&   copytreer   r   r   r   )r   r*   r   r   r   r   r   r   from_saved_modelg   s   


z%TensorflowCheckpoint.from_saved_modelc                 C   sj   |   }| j|vrtd|| j }|  }t|| }tj|W  d   S 1 s.w   Y  dS )zRetrieve the model stored in this checkpoint.

        Returns:
            The Tensorflow Keras model stored in the checkpoint.
        z`TensorflowCheckpoint` cannot retrieve the model if you override the checkpoint metadata. Please use `Checkpoint.update_metadata` instead.N)	get_metadatar   r$   as_directoryr   r   r   models
load_model)selfmetadatamodel_filenamecheckpoint_dir
model_pathr   r   r   	get_model   s   



$zTensorflowCheckpoint.get_model)__name__
__module____qualname____doc__r   classmethodr   Modelr   r   strr)   r/   tfr9   r   r   r   r   r      sB    -!"r   )r    r&   r   pathlibr   typingr   r   
tensorflowrA   r   (ray.train._internal.framework_checkpointr   ray.util.annotationsr   ray.data.preprocessorr   r   r   r   r   r   <module>   s    